File: syslog

package info (click to toggle)
qpsmtpd 0.32-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 860 kB
  • ctags: 237
  • sloc: perl: 4,219; sh: 592; makefile: 54
file content (166 lines) | stat: -rw-r--r-- 3,468 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl
# $Id$

=head1 NAME

syslog - Syslog logging plugin for qpsmtpd

=head1 DESCRIPTION

The syslog plugin for qpsmtpd passes qpsmtpd log messages into the standard
UNIX syslog facility, mapping qpsmtpd priorities to syslog priorities.

=head1 CONFIGURATION

To enable the logging plugin, add a line of this form to the qpsmtpd plugins
configuration file:

=over

logging/syslog [loglevel l] [priority p] [ident str] [facility f]

For example:

logging/syslog loglevel LOGINFO priority LOG_NOTICE

=back

The following optional configuration settings can be supplied:

=over

=item B<loglevel>

The internal log level below which messages will be logged.  Priorities count
downward as follows:

=over

=item B<LOGDEBUG>

=item B<LOGINFO>

=item B<LOGNOTICE>

=item B<LOGWARN>

=item B<LOGERROR>

=item B<LOGCRIT>

=item B<LOGALERT>

=item B<LOGEMERG>

=back


=item B<priority>

Normally, log messages will be mapped from the above log levels into the
syslog(3) log levels of their corresponding names.  This will cause various
messages to appear or not in syslog outputs according to your syslogd
configuration (typically /etc/syslog.conf).  However, if the B<priority>
setting is used, all messages will be logged at that priority regardless of 
what the original priority might have been.

=item B<ident>

The ident string that will be attached to messages logged via this plugin.
The default is 'qpsmtpd'.

=item B<facility>

The syslog facility to which logged mesages will be directed.  See syslog(3)
for details.  The default is LOG_MAIL.

=back

=head1 AUTHOR

Devin Carraway <qpsmtpd@devin.com>

=head1 LICENSE

Copyright (c) 2005, Devin Carraway.

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

=cut

use strict;
use warnings;

use Sys::Syslog;

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

    if (@args % 2 == 0) {
        %args = @args;
    } else {
        warn "Malformed arguments to syslog plugin";
        return;
    }

    my $ident = 'qpsmtpd';
    my $logopt = 'pid';
    my $facility = 'LOG_MAIL';

    $self->{_loglevel} = LOGWARN;

    if ($args{loglevel}) {
        if ($args{loglevel} =~ /^(\d+)$/) {
            $self->{_loglevel} = $1;
        }
        elsif ($args{loglevel} =~ /^(LOG\w+)$/) {
            $self->{_loglevel} = log_level($1) || LOGWARN;
        }
    }

    if ($args{priority}) {
        if ($args{priority} =~ /^(\d+|LOG\w+)$/) {
            $self->{_priority} = $1;
        }
    }
    
    if ($args{ident} && $args{ident} =~ /^([\w\-.]+)$/) {
        $ident = $1;
    }
    if ($args{facility} && $args{facility} =~ /^(\w+)$/) {
        $facility = $1;
    }

    unless (openlog $ident, $logopt, $facility) {
        warn "Error opening syslog output";
        return;
    }
}

my %priorities_ = (
    0 => 'LOG_EMERG',
    1 => 'LOG_ALERT',
    2 => 'LOG_CRIT',
    3 => 'LOG_ERR',
    4 => 'LOG_WARNING',
    5 => 'LOG_NOTICE',
    6 => 'LOG_INFO',
    7 => 'LOG_DEBUG',
);

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

    return DECLINED if $trace > $self->{_loglevel};
    return DECLINED if defined $plugin and $plugin eq $self->plugin_name;

    my $priority = $self->{_priority} ?
                   $self->{_priority} : $priorities_{$trace};

    syslog $priority, '%s', join(' ', @log);
    return DECLINED;
}

# vi: tabstop=4 shiftwidth=4 expandtab