Log-Log4perl-Appender-SMTP

 view release on metacpan or  search on metacpan

lib/Log/Log4perl/Appender/SMTP.pm  view on Meta::CPAN

package Log::Log4perl::Appender::SMTP;

our @ISA = qw(Log::Log4perl::Appender);

use strict;
use warnings;
use Carp;
use Net::Domain 'hostfqdn';
use Net::SMTP;

our $VERSION = '0.04';

sub new {
	my($class, @options) = @_;

	my $hname = hostfqdn() || '<unknown>';
	my $user  = getlogin || getpwuid($<) || 'log4perl';

	return bless {
		from    => $user.'@'.$hname,
		to      => 'postmaster',
		subject => "Subject: Log4perl from $hname\n",
		Host    => 'localhost',
		@options
	}, $class;
}

sub log {
	my ($self, %params) = @_;

	my $smtp = Net::SMTP->new(%$self) or return
		carp "log4perl: could not connect to the SMTP server";

	$smtp->mail($self->{from}) or return
		carp "log4perl: sender rejected: $self->{from}";

	$smtp->to(split /,/, $self->{to}) or return
		carp "log4perl: recipient(s) rejected: $self->{to}";

	$smtp->data;
	$smtp->datasend("From: ".$self->{from}."\n");
	$smtp->datasend("To: ".$self->{to}."\n");
	$smtp->datasend("Subject: ".$self->{subject}."\n");
	$smtp->datasend("\n" . $params{message} . "\n");
	$smtp->dataend or carp "log4perl: message could not be sent by smtp";
	$smtp->quit;
}

1;

__END__

=encoding utf8

=head1 NAME

Log::Log4perl::Appender::SMTP - Send logs by email

=head1 SYNOPSIS

  use Log::Log4perl::Appender::SMTP;

  my $app = Log::Log4perl::Appender::SMTP->new(
    Host    => "localhost",
    Hello   => "localhost.localdomain",
    Timeout => 2,
    Debug   => 0,
    from    => "app@company.com",
    to      => "bugs@company.com"
  );

  $app->log(message => "You need to come to the office now!");

=head1 DESCRIPTION

This appender is a very thin layer over the Net::SMTP module. It allows
you to easily send important log messages by email, to one or several



( run in 3.301 seconds using v1.01-cache-2.11-cpan-5837b0d9d2c )