| 1 | # | ||
| 2 | # Courier::Filter::Logger::File class | ||
| 3 | # | ||
| 4 | # (C) 2003-2008 Julian Mehnle <julian@mehnle.net> | ||
| 5 | # $Id: File.pm 210 2008-03-21 19:30:31Z julian $ | ||
| 6 | # | ||
| 7 | ############################################################################### | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Courier::Filter::Logger::File - File logger for the Courier::Filter framework | ||
| 12 | |||
| 13 | =cut | ||
| 14 | |||
| 15 | package Courier::Filter::Logger::File; | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Courier::Filter::Logger::File; | ||
| 20 | |||
| 21 | my $logger = Courier::Filter::Logger::File->new( | ||
| 22 | file_name => $file_name | ||
| 23 | ); | ||
| 24 | |||
| 25 | # For use in an individual filter module: | ||
| 26 | my $module = Courier::Filter::Module::My->new( | ||
| 27 | ... | ||
| 28 | logger => $logger, | ||
| 29 | ... | ||
| 30 | ); | ||
| 31 | |||
| 32 | # For use as a global Courier::Filter logger object: | ||
| 33 | my $filter = Courier::Filter->new( | ||
| 34 | ... | ||
| 35 | logger => $logger, | ||
| 36 | ... | ||
| 37 | ); | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | use warnings; | ||
| 42 | use strict; | ||
| 43 | |||
| 44 | use base 'Courier::Filter::Logger::IOHandle'; | ||
| 45 | |||
| 46 | use Error ':try'; | ||
| 47 | |||
| 48 | use Courier::Error; | ||
| 49 | |||
| 50 | use IO::File; | ||
| 51 | |||
| 52 | use constant TRUE => (0 == 0); | ||
| 53 | use constant FALSE => not TRUE; | ||
| 54 | |||
| 55 | =head1 DESCRIPTION | ||
| 56 | |||
| 57 | This class is a file logger class for use with Courier::Filter and its filter | ||
| 58 | modules. It is derived from B<Courier::Filter::Logger::IOHandle>. | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | # Implementation: | ||
| 63 | ############################################################################### | ||
| 64 | |||
| 65 | =head2 Constructor | ||
| 66 | |||
| 67 | The following constructor is provided: | ||
| 68 | |||
| 69 | =over | ||
| 70 | |||
| 71 | =item B<new(%options)>: returns I<Courier::Filter::Logger::File>; throws | ||
| 72 | I<Courier::Error> | ||
| 73 | |||
| 74 | Creates a new logger that logs messages as lines to a file. Opens the file for | ||
| 75 | writing, creating it if necessary. | ||
| 76 | |||
| 77 | %options is a list of key/value pairs representing any of the following | ||
| 78 | options: | ||
| 79 | |||
| 80 | =over | ||
| 81 | |||
| 82 | =item B<file_name> | ||
| 83 | |||
| 84 | I<Required>. The name of the file to which log messages should be written. | ||
| 85 | |||
| 86 | =item B<timestamp> | ||
| 87 | |||
| 88 | A boolean value controlling whether every log message line should be prefixed | ||
| 89 | with a timestamp (in local time, in ISO format). Defaults to B<false>. | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub new { | ||
| 96 | my ($class, %options) = @_; | ||
| 97 | |||
| 98 | my $handle = IO::File->new($options{file_name}, '>>') | ||
| 99 | or throw Courier::Error("Unable to open log file '$options{file_name}' for writing"); | ||
| 100 | |||
| 101 | return $class->SUPER::new( | ||
| 102 | %options, | ||
| 103 | handle => $handle | ||
| 104 | ); | ||
| 105 | } | ||
| 106 | |||
| 107 | =back | ||
| 108 | |||
| 109 | =head2 Instance methods | ||
| 110 | |||
| 111 | The following instance methods are provided, as inherited from | ||
| 112 | B<Courier::Filter::Logger::IOHandle>: | ||
| 113 | |||
| 114 | =over | ||
| 115 | |||
| 116 | =item B<log_error($text)>: throws Perl exceptions | ||
| 117 | |||
| 118 | Logs the error message given as C<$text> (a string which may contain newlines). | ||
| 119 | Prefixes each line with a timestamp if the C<timestamp> option has been set | ||
| 120 | through the constructor. | ||
| 121 | |||
| 122 | =item B<log_rejected_message($message, $reason)>: throws Perl exceptions | ||
| 123 | |||
| 124 | Logs the B<Courier::Message> given as C<$message> as having been rejected due | ||
| 125 | to C<$reason> (a string which may contain newlines). | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head1 SEE ALSO | ||
| 130 | |||
| 131 | L<Courier::Filter::Logger::IOHandle>, L<Courier::Filter::Logger>, | ||
| 132 | L<Courier::Filter::Overview>. | ||
| 133 | |||
| 134 | For AVAILABILITY, SUPPORT, and LICENSE information, see | ||
| 135 | L<Courier::Filter::Overview>. | ||
| 136 | |||
| 137 | =head1 AUTHOR | ||
| 138 | |||
| 139 | Julian Mehnle <julian@mehnle.net> | ||
| 140 | |||
| 141 | =cut | ||
| 142 | |||
| 143 | TRUE; |