home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Dispatch.pm < prev    next >
Encoding:
Perl POD Document  |  2004-02-11  |  10.9 KB  |  414 lines

  1. package Log::Dispatch;
  2.  
  3. require 5.005;
  4.  
  5. use strict;
  6. use vars qw[ $VERSION %LEVELS ];
  7.  
  8. use base qw( Log::Dispatch::Base );
  9.  
  10. use Carp ();
  11.  
  12. $VERSION = '2.10';
  13.  
  14. 1;
  15.  
  16. BEGIN
  17. {
  18.     no strict 'refs';
  19.     foreach my $l ( qw( debug info notice warning err error crit critical alert emerg emergency ) )
  20.     {
  21.     *{$l} = sub { my $self = shift;
  22.               $self->log( level => $l, message => "@_" ); };
  23.     $LEVELS{$l} = 1;
  24.     }
  25. }
  26.  
  27. sub new
  28. {
  29.     my $proto = shift;
  30.     my $class = ref $proto || $proto;
  31.     my %p = @_;
  32.  
  33.     my $self = bless {}, $class;
  34.  
  35.     my @cb = $self->_get_callbacks(%p);
  36.     $self->{callbacks} = \@cb if @cb;
  37.  
  38.     return $self;
  39. }
  40.  
  41. sub add
  42. {
  43.     my $self = shift;
  44.     my $object = shift;
  45.  
  46.     # Once 5.6 is more established start using the warnings module.
  47.     if (exists $self->{outputs}{$object->name} && $^W)
  48.     {
  49.     Carp::carp("Log::Dispatch::* object ", $object->name, " already exists.");
  50.     }
  51.  
  52.     $self->{outputs}{$object->name} = $object;
  53. }
  54.  
  55. sub remove
  56. {
  57.     my $self = shift;
  58.     my $name = shift;
  59.  
  60.     return delete $self->{outputs}{$name};
  61. }
  62.  
  63. sub log
  64. {
  65.     my $self = shift;
  66.     my %p = @_;
  67.  
  68.     $p{message} = $self->_apply_callbacks(%p)
  69.     if $self->{callbacks};
  70.  
  71.     foreach (keys %{ $self->{outputs} })
  72.     {
  73.     $p{name} = $_;
  74.     $self->_log_to(%p);
  75.     }
  76. }
  77.  
  78. sub log_to
  79. {
  80.     my $self = shift;
  81.     my %p = @_;
  82.  
  83.     $p{message} = $self->_apply_callbacks(%p)
  84.     if $self->{callbacks};
  85.  
  86.     $self->_log_to(%p);
  87. }
  88.  
  89. sub _log_to
  90. {
  91.     my $self = shift;
  92.     my %p = @_;
  93.     my $name = $p{name};
  94.  
  95.     if (exists $self->{outputs}{$name})
  96.     {
  97.     $self->{outputs}{$name}->log(@_);
  98.     }
  99.     elsif ($^W)
  100.     {
  101.     Carp::carp("Log::Dispatch::* object named '$name' not in dispatcher\n");
  102.     }
  103. }
  104.  
  105. sub output
  106. {
  107.     my $self = shift;
  108.     my $name = shift;
  109.  
  110.     return unless exists $self->{outputs}{$name};
  111.  
  112.     return $self->{outputs}{$name};
  113. }
  114.  
  115. sub level_is_valid
  116. {
  117.     shift;
  118.     return $LEVELS{ shift() };
  119. }
  120.  
  121. sub would_log
  122. {
  123.     my $self = shift;
  124.     my $level = shift;
  125.  
  126.     return 0 unless $self->level_is_valid($level);
  127.  
  128.     foreach ( values %{ $self->{outputs} } )
  129.     {
  130.     return 1 if $_->_should_log($level);
  131.     }
  132.  
  133.     return 0;
  134. }
  135.  
  136. __END__
  137.  
  138. =head1 NAME
  139.  
  140. Log::Dispatch - Dispatches messages to one or more outputs
  141.  
  142. =head1 SYNOPSIS
  143.  
  144.   use Log::Dispatch;
  145.  
  146.   my $dispatcher = Log::Dispatch->new;
  147.  
  148.   $dispatcher->add( Log::Dispatch::File->new( name => 'file1',
  149.                                               min_level => 'debug',
  150.                                               filename => 'logfile' ) );
  151.  
  152.   $dispatcher->log( level => 'info',
  153.                     message => 'Blah, blah' );
  154.  
  155.   my $sub = sub { my %p = @_;  return reverse $p{message}; };
  156.   my $reversing_dispatcher = Log::Dispatch->new( callbacks => $sub );
  157.  
  158. =head1 DESCRIPTION
  159.  
  160. This module manages a set of Log::Dispatch::* objects, allowing you to
  161. add and remove output objects as desired.
  162.  
  163. =head1 METHODS
  164.  
  165. =over 4
  166.  
  167. =item * new
  168.  
  169. Returns a new Log::Dispatch object.  This method takes one optional
  170. parameter:
  171.  
  172. =over 8
  173.  
  174. =item * callbacks( \& or [ \&, \&, ... ] )
  175.  
  176. This parameter may be a single subroutine reference or an array
  177. reference of subroutine references.  These callbacks will be called in
  178. the order they are given and passed a hash containing the following keys:
  179.  
  180.  ( message => $log_message, level => $log_level )
  181.  
  182. In addition, any key/value pairs passed to a logging method will be
  183. passed onto your callback.
  184.  
  185. The callbacks are expected to modify the message and then return a
  186. single scalar containing that modified message.  These callbacks will
  187. be called when either the C<log> or C<log_to> methods are called and
  188. will only be applied to a given message once.  If they do not return
  189. the message then you will get no output.  Make sure to return the
  190. message!
  191.  
  192. =back
  193.  
  194. =item * add( Log::Dispatch::* OBJECT )
  195.  
  196. Adds a new a Log::Dispatch::* object to the dispatcher.  If an object
  197. of the same name already exists, then that object is replaced.  A
  198. warning will be issued if the C<$^W> is true.
  199.  
  200. NOTE: This method can really take any object that has methods called
  201. 'name' and 'log'.
  202.  
  203. =item * remove($)
  204.  
  205. Removes the object that matches the name given to the remove method.
  206. The return value is the object being removed or undef if no object
  207. matched this.
  208.  
  209. =item * log( level => $, message => $ )
  210.  
  211. Sends the message (at the appropriate level) to all the
  212. Log::Dispatch::* objects that the dispatcher contains (by calling the
  213. C<log_to> method repeatedly).
  214.  
  215. =item * log_to( name => $, level => $, message => $ )
  216.  
  217. Sends the message only to the named object.
  218.  
  219. =item * level_is_valid( $string )
  220.  
  221. Returns true or false to indicate whether or not the given string is a
  222. valid log level.  Can be called as either a class or object method.
  223.  
  224. =item * would_log( $string )
  225.  
  226. Given a log level, returns true or false to indicate whether or not
  227. anything would be logged for that log level.
  228.  
  229. =item * output( $name )
  230.  
  231. Returns an output of the given name.  Returns undef or an empty list,
  232. depending on context, if the given output does not exist.
  233.  
  234. =back
  235.  
  236. =head1 CONVENIENCE METHODS
  237.  
  238. Version 1.6 of Log::Dispatch adds a number of convenience methods for
  239. logging.  You may now call any valid log level (including valid
  240. abbreviations) as a method on the Log::Dispatch object with a single
  241. argument that is the message to be logged.  This is converted into a
  242. call to the C<log> method with the appropriate level.
  243.  
  244. For example:
  245.  
  246.  $dispatcher->alert('Strange data in incoming request');
  247.  
  248. translates to:
  249.  
  250.  $dispatcher->log( level => 'alert', message => 'Strange data in incoming request' );
  251.  
  252. These methods act like Perl's C<print> built-in when given a list of
  253. arguments.  Thus, the following calls are equivalent:
  254.  
  255.  my @array = ('Something', 'bad', 'is', here');
  256.  $dispatcher->alert(@array);
  257.  
  258.  my $scalar = "@array";
  259.  $dispatcher->alert($scalar);
  260.  
  261. One important caveat about these methods is that its not that forwards
  262. compatible.  If I were to add more parameters to the C<log> call, it
  263. is unlikely that these could be integrated into these methods without
  264. breaking existing uses.  This probably means that any future
  265. parameters to the C<log> method will never be integrated into these
  266. convenience methods.  OTOH, I don't see any immediate need to expand
  267. the parameters given to the C<log> method.
  268.  
  269. =head2 Log Levels
  270.  
  271. The log levels that Log::Dispatch uses are taken directly from the
  272. syslog man pages (except that I expanded them to full words).  Valid
  273. levels are:
  274.  
  275.  debug
  276.  info
  277.  notice
  278.  warning
  279.  error
  280.  critical
  281.  alert
  282.  emergency
  283.  
  284. Alternately, the numbers 0 through 7 may be used (debug is 0 and
  285. emergency is 7).  The syslog standard of 'err', 'crit', and 'emerg'
  286. is also acceptable.
  287.  
  288. =head1 USAGE
  289.  
  290. This module is designed to be used as a one-stop logging system.  In
  291. particular, it was designed to be easy to subclass so that if you want
  292. to handle messaging in a way not implemented in this package, you
  293. should be able to add this with minimal effort.
  294.  
  295. The basic idea behind Log::Dispatch is that you create a Log::Dispatch
  296. object and then add various logging objects to it (such as a file
  297. logger or screen logger).  Then you call the C<log> method of the
  298. dispatch object, which passes the message to each of the objects,
  299. which in turn decide whether or not to accept the message and what to
  300. do with it.
  301.  
  302. This makes it possible to call single method and send a message to a
  303. log file, via email, to the screen, and anywhere else, all with very
  304. little code needed on your part, once the dispatching object has been
  305. created.
  306.  
  307. The logging levels that Log::Dispatch uses are borrowed from the
  308. standard UNIX syslog levels, except that where syslog uses partial
  309. words ("err") Log::Dispatch also allows the use of the full word as
  310. well ("error").
  311.  
  312. =head2 Making your own logging objects
  313.  
  314. Making your own logging object is generally as simple as subclassing
  315. Log::Dispatch::Output and overriding the C<new> and C<log> methods.
  316. See the L<Log::Dispatch::Output> docs for more details.
  317.  
  318. If you would like to create your own subclass for sending email then
  319. it is even simpler.  Simply subclass L<Log::Dispatch::Email> and
  320. override the C<send_email> method.  See the L<Log::Dispatch::Email>
  321. docs for more details.
  322.  
  323. =head2 Why doesn't Log::Dispatch add a newline to the message?
  324.  
  325. A few people have written email to me asking me to add something that
  326. would tack a newline onto the end of all messages that don't have one.
  327. This will never happen.  There are several reasons for this.  First of
  328. all, Log::Dispatch was designed as a simple system to broadcast a
  329. message to multiple outputs.  It does not attempt to understand the
  330. message in any way at all.  Adding a newline implies an attempt to
  331. understand something about the message and I don't want to go there.
  332. Secondly, this is not very cross-platform and I don't want to go down
  333. the road of testing Config values to figure out what to tack onto
  334. messages based on OS.
  335.  
  336. I think people's desire to do this is because they are too focused on
  337. just the logging to files aspect of this module.  In this case
  338. newlines make sense.  However, imagine someone is using this module to
  339. log to a remote server and the interactions between the client and
  340. server use newlines as part of the control flow.  Casually adding a
  341. newline could cause serious problems.
  342.  
  343. However, the 1.2 release adds the callbacks parameter for the
  344. Log::Dispatch object which you can easily use to add newlines to
  345. messages if you so desire.
  346.  
  347. =head1 RELATED MODULES
  348.  
  349. =head2 Log::Dispatch::DBI
  350.  
  351. Written by Tatsuhiko Miyagawa.  Log output to a database table.
  352.  
  353. =head2 Log::Dispatch::FileRotate
  354.  
  355. Written by Mark Pfeiffer.  Rotates log files periodically as part of
  356. its usage.
  357.  
  358. =head2 Log::Dispatch::File::Stamped
  359.  
  360. Written by Eric Cholet.  Stamps log files with date and time
  361. information.
  362.  
  363. =head2 Log::Dispatch::Jabber
  364.  
  365. Written by Aaron Straup Cope.  Logs messages via Jabber.
  366.  
  367. =head2 Log::Dispatch::Tk
  368.  
  369. Written by Dominique Dumont.  Logs messages to a Tk window.
  370.  
  371. =head2 Log::Dispatch::Win32EventLog
  372.  
  373. Written by Arthur Bergman.  Logs messages to the Windows event log.
  374.  
  375. =head2 Log::Log4perl
  376.  
  377. An implementation of Java's log4j API in Perl, using Log::Dispatch to
  378. do the actual logging.  Created by Mike Schilli and Kevin Goess.
  379.  
  380. =head2 Log::Dispatch::Config
  381.  
  382. Written by Tatsuhiko Miyagawa.  Allows configuration of logging via a
  383. text file similar (or so I'm told) to how it is done with log4j.
  384. Simpler than Log::Log4perl.
  385.  
  386. =head2 Log::Agent
  387.  
  388. A very different API for doing many of the same things that
  389. Log::Dispatch does.  Originally written by Raphael Manfredi.
  390.  
  391. =head1 COPYRIGHT
  392.  
  393. Copyright (c) 1999-2003 David Rolsky.  All rights reserved.  This
  394. program is free software; you can redistribute it and/or modify it
  395. under the same terms as Perl itself.
  396.  
  397. The full text of the license can be found in the LICENSE file included
  398. with this module.
  399.  
  400. =head1 AUTHOR
  401.  
  402. Dave Rolsky, <autarch@urth.org>
  403.  
  404. =head1 SEE ALSO
  405.  
  406. Log::Dispatch::ApacheLog, Log::Dispatch::Email,
  407. Log::Dispatch::Email::MailSend, Log::Dispatch::Email::MailSender,
  408. Log::Dispatch::Email::MailSendmail, Log::Dispatch::Email::MIMELite,
  409. Log::Dispatch::File, Log::Dispatch::File::Locked,
  410. Log::Dispatch::Handle, Log::Dispatch::Output, Log::Dispatch::Screen,
  411. Log::Dispatch::Syslog
  412.  
  413. =cut
  414.