Courier::Filter::Logger::IOHandle - I/O handle logger for the Courier::Filter


Courier-Filter documentation Contained in the Courier-Filter distribution.

Index


Code Index:

NAME

Top

Courier::Filter::Logger::IOHandle - I/O handle logger for the Courier::Filter framework

SYNOPSIS

Top

    use Courier::Filter::Logger::IOHandle;

    my $logger = Courier::Filter::Logger::IOHandle->new(
        handle => $handle
    );

    # For use in an individual filter module:
    my $module = Courier::Filter::Module::My->new(
        ...
        logger => $logger,
        ...
    );

    # For use as a global Courier::Filter logger object:
    my $filter = Courier::Filter->new(
        ...
        logger => $logger,
        ...
    );

DESCRIPTION

Top

This class is an I/O handle logger class for use with Courier::Filter and its filter modules.

Constructor

The following constructor is provided:

new(%options): returns Courier::Filter::Logger::IOHandle

Creates a new logger that logs messages as lines to an I/O handle.

%options is a list of key/value pairs representing any of the following options:

handle

Required. The I/O handle or IO::Handle object to which log messages should be written.

timestamp

A boolean value controlling whether every log message line should be prefixed with a timestamp (in local time, in ISO format). Defaults to false.

Instance methods

The following instance methods are provided:

log_error($text): throws Perl exceptions

Logs the error message given as $text (a string which may contain newlines). Prefixes each line with a timestamp if the timestamp option has been set through the constructor.

log_rejected_message($message, $reason): throws Perl exceptions

Logs the Courier::Message given as $message as having been rejected due to $reason (a string which may contain newlines).

SEE ALSO

Top

Courier::Filter::Logger, Courier::Filter::Overview.

For AVAILABILITY, SUPPORT, and LICENSE information, see Courier::Filter::Overview.

AUTHOR

Top

Julian Mehnle <julian@mehnle.net>


Courier-Filter documentation Contained in the Courier-Filter distribution.
#
# Courier::Filter::Logger::IOHandle class
#
# (C) 2004-2008 Julian Mehnle <julian@mehnle.net>
# $Id: IOHandle.pm 210 2008-03-21 19:30:31Z julian $
#
###############################################################################

package Courier::Filter::Logger::IOHandle;

use warnings;
use strict;

use base 'Courier::Filter::Logger';

use IO::Handle;

use constant TRUE   => (0 == 0);
use constant FALSE  => not TRUE;

# Implementation:
###############################################################################

sub new {
    my ($class, %options) = @_;
    
    my $self = $class->SUPER::new(%options);
    
    $self->{autoflush} = TRUE
        if not defined($self->{autoflush});
    $self->{handle}->autoflush($self->{autoflush});
    
    return $self;
}

sub log {
    my ($self, $text) = @_;
    
    my $timestamp = '';
    if ($self->{timestamp}) {
        my ($y, $m, $d, $h, $n, $s) = (localtime)[5,4,3,2,1,0];
        $timestamp = sprintf(
            "%04d-%02d-%02d %02d:%02d:%02d ",
            $y+1900, $m+1, $d, $h, $n, $s
        );
    }
    
    my @lines = split(/\n/, $text);
    $self->{handle}->print("$timestamp$_\n")
        foreach @lines;
    
    return;
}

sub log_error {
    my ($self, $text) = @_;
    return $self->log($text);
}

sub log_rejected_message {
    my ($self, $message, $reason) = @_;
    
    $reason =~ s/^/Reason: /gm;
    
    my $text = sprintf(
        "Rejected message sent from %s to %s through %s\n%s\n",
        '<' . $message->sender . '>',
        join(', ', map("<$_>", $message->recipients)),
        $message->remote_host . (
            $message->remote_host_name ?
                ' (' . $message->remote_host_name . ')'
            :   ''
        ),
        $reason
    );
    return $self->log($text);
}

TRUE;