Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
P is for Practical
 
PerlMonks  

Re^2: Subclassing Log::Dispatch::Email

by dorko (Parson)
on Oct 12, 2009 at 14:44 UTC ( #800727=note: print w/ replies, xml ) Need Help??


in reply to Re: Subclassing Log::Dispatch::Email
in thread Subclassing Log::Dispatch::Email

Thanks for the advise. It's a project I'm working on for a small charity in my spare time, so I didn't feel too bad about asking if anyone wanted to share their existing code. In the spirit in which I asked the question, here's what I wrote:

package Log::Dispatch::Email::Emailstuff; use strict; use warnings; use Log::Dispatch::Email; use base qw( Log::Dispatch::Email ); use Email::Stuff; sub send_email { my $self = shift; my %p = @_; Email::Stuff->to (join ',', @{$self->{to}}) ->from ($self->{from}) ->subject ($self->{subject}) ->text_body ($p{message}) # ->attach_file ('choochoo.gif') ->using ('SMTP', Host => 'smtp.server.goes.her +e.com') ->send; } 1; __END__ =head1 NAME Log::Dispatch::Email::Emailstuff - Subclass of Log::Dispatch::Email th +at uses the Email::Stuff module =head1 SYNOPSIS use Log::Dispatch::Email::Emailstuff; my $email = Log::Dispatch::Email::Emailstuff->new ( name => 'email', min_level => 'emerg', to => [ qw( foo@bar.com bar@baz.org ) ], subject => 'Oh no!!!!!!!!!!!', ); $email->log( message => 'Something bad is happening', level => 'emer +g' ); =head1 DESCRIPTION This is a subclass of Log::Dispatch::Email that implements the send_email method using the Email::Stuff module. =head1 METHODS =over 4 =item * new This method takes a hash of parameters. The following options are valid: =over 8 =item * name ($) The name of the object (not the filename!). Required. =item * min_level ($) The minimum logging level this object will accept. See the Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels" +> for more information. Required. =item * max_level ($) The maximum logging level this obejct will accept. See the Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels" +> for more information. This is not required. By default the maximum is the highest possible level (which means functionally that the object has no maximum). =item * subject ($) The subject of the email messages which are sent. Defaults to "$0: log email" =item * to ($ or \@) Either a string or a list reference of strings containing email addresses. Required. =item * from ($) A string containing an email address. This is optional and may not work with all mail sending methods. This option works with Email::Stuff. NOTE: The Mail::Sendmail module requires an address be passed to it to set this in the mail it sends. We pass in 'LogDispatch@foo.bar' as the default. =item * using Specifies the Email::Send driver that you want to use to send the emai +l, and any options that need to be passed to the driver at the time that we send the mail. Alternatively, you can pass a complete mailer object (which must be an Email::Send object) and it will be used as is. =item * buffered (0 or 1) This determines whether the object sends one email per message it is given or whether it stores them up and sends them all at once. The default is to buffer messages. =item * callbacks( \& or [ \&, \&, ... ] ) This parameter may be a single subroutine reference or an array reference of subroutine references. These callbacks will be called in the order they are given and passed a hash containing the following ke +ys: ( message => $log_message, level => $log_level ) The callbacks are expected to modify the message and then return a single scalar containing that modified message. These callbacks will be called when either the C<log> or C<log_to> methods are called and will only be applied to a given message once. =back =item * log_message( level => $, message => $ ) Sends a message if the level is greater than or equal to the object's minimum level. =back =head1 AUTHOR Dave Rolsky, <autarch@urth.org> wrote Log::Dispatch::Email and Brent Shaw, <brent.shaw.ucf@gmail.com> made it work with Email::Stuff =cut

Cheers,

Brent

-- Yeah, I'm a Delt.


Comment on Re^2: Subclassing Log::Dispatch::Email
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://800727]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-04-16 11:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (424 votes), past polls