
=head1 NAME

logging/apache - logging plugin for qpsmtpd which logs to the apache error log

=cut

# more POD at the end

use strict;
use warnings FATAL => 'all';
use Apache2::Log;
use Apache2::RequestUtil ();

use Qpsmtpd::Constants;

sub register {
    my ($self, $qp) = @_;

    die "Not running under Apache::Qpsmtpd"
      unless ($qp->{conn} && $qp->{conn}->isa('Apache2::Connection'));

    my $rr = Apache2::RequestRec->new($self->qp->{conn});
    $self->{_log} = $rr->log
      if $rr;

    $self->log(LOGINFO, 'Initializing logging::apache plugin');
}

sub hook_logging {
    my ($self, $transaction, $trace, $hook, $plugin, @log) = @_;

    # Don't log your own log entries!  If this is the only logging plugin
    # then these lines will not be logged at all.  You can safely comment
    # out this line and it will not cause an infinite loop.
    return DECLINED if defined $plugin and $plugin eq $self->plugin_name;

    unless ($self->{_log}) {
        my $rr = Apache2::RequestRec->new($self->qp->{conn});
        unless ($rr) {
            warn "no Apache2::RequestRec?... logmsg was: ", join(" ", @log);
            return DECLINED;
        }
        $self->{_log} = $rr->log;
    }

    # luckily apache uses the same log levels as qpsmtpd...
    ($trace = lc Qpsmtpd::Constants::log_level($trace)) =~ s/^log//;
    $trace = 'emerg'    # ... well, nearly...
      if $trace eq 'radar';

    my $log = $self->{_log};
    unless ($log->can($trace)) {    # ... but you never know if it changes
        $log->emerg("Can't log with level '$trace', logmsg was: ",
                    join(" ", @log));
        return DECLINED;
    }

    $log->$trace(
                 join(
                      " ",
                      $$
                        . (
                             defined $plugin ? " $plugin plugin:"
                           : defined $hook   ? " running plugin ($hook):"
                           : ""
                          ),
                      @log
                     )
                );    # no \n at the end!

    return DECLINED;
}

=cut

=head1 DESCRIPTION

The logging/apache plugin uses the apache logging mechanism to write its
messages to the apache error log.

=head1 INSTALL AND CONFIG

Place this plugin in the plugin/logging directory beneath the standard
qpsmtpd installation. Edit the config/logging file and add a line like
this:

  logging/apache

To change what is shown in the logs, change the I<LogLevel> directive in
the virtual host config for Qpsmtpd and maybe change the I<ErrorLog> log
file:

         <VirtualHost _default_:25>
          PerlSetVar QpsmtpdDir /path/to/qpsmtpd
          PerlModule Apache::Qpsmtpd
          PerlProcessConnectionHandler Apache::Qpsmtpd
          LogLevel debug
          ErrorLog /var/log/apache2/qpsmtpd.log
         </VirtualHost>

=head1 AUTHOR

Hanno Hecker <vetinari@ankh-morp.org>

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2007 Hanno Hecker

This plugin is licensed under the same terms as the qpsmtpd package itself.
Please see the LICENSE file included with qpsmtpd for details.

=cut

# vim: ts=4 sw=4 expandtab syn=perl
