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 / Email.pm < prev    next >
Encoding:
Perl POD Document  |  2003-09-27  |  4.5 KB  |  212 lines

  1. package Log::Dispatch::Email;
  2.  
  3. use strict;
  4.  
  5. use Log::Dispatch::Output;
  6.  
  7. use base qw( Log::Dispatch::Output );
  8.  
  9. use Params::Validate qw(validate SCALAR ARRAYREF BOOLEAN);
  10. Params::Validate::validation_options( allow_extra => 1 );
  11.  
  12. use vars qw[ $VERSION ];
  13.  
  14. $VERSION = sprintf "%d.%02d", q$Revision: 1.19 $ =~ /: (\d+)\.(\d+)/;
  15.  
  16. 1;
  17.  
  18. sub new
  19. {
  20.     my $proto = shift;
  21.     my $class = ref $proto || $proto;
  22.  
  23.     my %p = validate( @_, { subject  => { type => SCALAR,
  24.                       default => "$0: log email" },
  25.                 to       => { type => SCALAR | ARRAYREF },
  26.                 from     => { type => SCALAR,
  27.                       optional => 1 },
  28.                 buffered => { type => BOOLEAN,
  29.                       default => 1 },
  30.               } );
  31.  
  32.     my $self = bless {}, $class;
  33.  
  34.     $self->_basic_init(%p);
  35.  
  36.     $self->{subject} = $p{subject} || "$0: log email";
  37.     $self->{to} = ref $p{to} ? $p{to} : [$p{to}];
  38.     $self->{from} = $p{from};
  39.  
  40.     # Default to buffered for obvious reasons!
  41.     $self->{buffered} = $p{buffered};
  42.  
  43.     $self->{buffer} = [] if $self->{buffered};
  44.  
  45.     return $self;
  46. }
  47.  
  48. sub log_message
  49. {
  50.     my $self = shift;
  51.     my %p = @_;
  52.  
  53.     if ($self->{buffered})
  54.     {
  55.     push @{ $self->{buffer} }, $p{message};
  56.     }
  57.     else
  58.     {
  59.     $self->send_email(@_);
  60.     }
  61. }
  62.  
  63. sub send_email
  64. {
  65.     my $self = shift;
  66.     my $class = ref $self;
  67.  
  68.     die "The send_email method must be overridden in the $class subclass";
  69. }
  70.  
  71. sub flush
  72. {
  73.     my $self = shift;
  74.  
  75.     if ($self->{buffered} && @{ $self->{buffer} })
  76.     {
  77.     my $message = join '', @{ $self->{buffer} };
  78.  
  79.     $self->send_email( message => $message );
  80.         $self->{buffer} = [];
  81.     }
  82. }
  83.  
  84. sub DESTROY
  85. {
  86.     my $self = shift;
  87.  
  88.     $self->flush;
  89. }
  90.  
  91. __END__
  92.  
  93. =head1 NAME
  94.  
  95. Log::Dispatch::Email - Base class for objects that send log messages
  96. via email
  97.  
  98. =head1 SYNOPSIS
  99.  
  100.   package Log::Dispatch::Email::MySender
  101.  
  102.   use Log::Dispatch::Email;
  103.   use base qw( Log::Dispatch::Email );
  104.  
  105.   sub send_email
  106.   {
  107.       my $self = shift;
  108.       my %p = @_;
  109.  
  110.       # Send email somehow.  Message is in $p{message}
  111.   }
  112.  
  113. =head1 DESCRIPTION
  114.  
  115. This module should be used as a base class to implement
  116. Log::Dispatch::* objects that send their log messages via email.
  117. Implementing a subclass simply requires the code shown in the
  118. L<SYNOPSIS> with a real implementation of the C<send_email()> method.
  119.  
  120. =head1 METHODS
  121.  
  122. =over 4
  123.  
  124. =item * new(%p)
  125.  
  126. This method takes a hash of parameters.  The following options are
  127. valid:
  128.  
  129. =over 8
  130.  
  131. =item * name ($)
  132.  
  133. The name of the object (not the filename!).  Required.
  134.  
  135. =item * min_level ($)
  136.  
  137. The minimum logging level this object will accept.  See the
  138. Log::Dispatch documentation for more information.  Required.
  139.  
  140. =item * max_level ($)
  141.  
  142. The maximum logging level this obejct will accept.  See the
  143. Log::Dispatch documentation for more information.  This is not
  144. required.  By default the maximum is the highest possible level (which
  145. means functionally that the object has no maximum).
  146.  
  147. =item * subject ($)
  148.  
  149. The subject of the email messages which are sent.  Defaults to "$0:
  150. log email"
  151.  
  152. =item * to ($ or \@)
  153.  
  154. Either a string or a list reference of strings containing email
  155. addresses.  Required.
  156.  
  157. =item * from ($)
  158.  
  159. A string containing an email address.  This is optional and may not
  160. work with all mail sending methods.
  161.  
  162. =item * buffered (0 or 1)
  163.  
  164. This determines whether the object sends one email per message it is
  165. given or whether it stores them up and sends them all at once.  The
  166. default is to buffer messages.
  167.  
  168. =item * callbacks( \& or [ \&, \&, ... ] )
  169.  
  170. This parameter may be a single subroutine reference or an array
  171. reference of subroutine references.  These callbacks will be called in
  172. the order they are given and passed a hash containing the following keys:
  173.  
  174.  ( message => $log_message, level => $log_level )
  175.  
  176. The callbacks are expected to modify the message and then return a
  177. single scalar containing that modified message.  These callbacks will
  178. be called when either the C<log> or C<log_to> methods are called and
  179. will only be applied to a given message once.
  180.  
  181. =back
  182.  
  183. =item * log_message( message => $ )
  184.  
  185. Sends a message to the appropriate output.  Generally this shouldn't
  186. be called directly but should be called through the C<log()> method
  187. (in Log::Dispatch::Output).
  188.  
  189. =item * send_email(%p)
  190.  
  191. This is the method that must be subclassed.  For now the only
  192. parameter in the hash is 'message'.
  193.  
  194. =item * flush
  195.  
  196. If the object is buffered, then this method will call the
  197. C<send_email()> method to send the contents of the buffer and then
  198. clear the buffer.
  199.  
  200. =item * DESTROY
  201.  
  202. On destruction, the object will call C<flush()> to send any pending
  203. email.
  204.  
  205. =back
  206.  
  207. =head1 AUTHOR
  208.  
  209. Dave Rolsky, <autarch@urth.org>
  210.  
  211. =cut
  212.