1package Log::Dispatch::Syslog;
2
3use strict;
4use warnings;
5
6use Log::Dispatch::Output;
7
8use base qw( Log::Dispatch::Output );
9
10use Params::Validate qw(validate SCALAR);
11Params::Validate::validation_options( allow_extra => 1 );
12
13use Sys::Syslog ();
14
15our $VERSION = '1.18';
16
17sub new
18{
19    my $proto = shift;
20    my $class = ref $proto || $proto;
21
22    my %p = @_;
23
24    my $self = bless {}, $class;
25
26    $self->_basic_init(%p);
27    $self->_init(%p);
28
29    return $self;
30}
31
32sub _init
33{
34    my $self = shift;
35
36    my %p = validate( @_, { ident    => { type => SCALAR,
37                                          default => $0 },
38                            logopt   => { type => SCALAR,
39                                          default => '' },
40                            facility => { type => SCALAR,
41                                          default => 'user' },
42                            socket   => { type => SCALAR,
43                                          default => 'unix' },
44                          } );
45
46    $self->{ident}    = $p{ident};
47    $self->{logopt}   = $p{logopt};
48    $self->{facility} = $p{facility};
49    $self->{socket}   = $p{socket};
50
51    $self->{priorities} = [ 'DEBUG',
52                            'INFO',
53                            'NOTICE',
54                            'WARNING',
55                            'ERR',
56                            'CRIT',
57                            'ALERT',
58                            'EMERG' ];
59
60    Sys::Syslog::setlogsock $self->{socket};
61}
62
63sub log_message
64{
65    my $self = shift;
66    my %p = @_;
67
68    my $pri = $self->_level_as_number($p{level});
69
70    Sys::Syslog::openlog($self->{ident}, $self->{logopt}, $self->{facility});
71    Sys::Syslog::syslog($self->{priorities}[$pri], '%s', $p{message});
72    Sys::Syslog::closelog;
73}
74
75
761;
77
78__END__
79
80=head1 NAME
81
82Log::Dispatch::Syslog - Object for logging to system log.
83
84=head1 SYNOPSIS
85
86  use Log::Dispatch::Syslog;
87
88  my $file = Log::Dispatch::Syslog->new( name      => 'file1',
89                                         min_level => 'info',
90                                         ident     => 'Yadda yadda' );
91
92  $file->log( level => 'emerg', message => "Time to die." );
93
94=head1 DESCRIPTION
95
96This module provides a simple object for sending messages to the
97system log (via UNIX syslog calls).
98
99=head1 METHODS
100
101=over 4
102
103=item * new(%p)
104
105This method takes a hash of parameters.  The following options are
106valid:
107
108=over 8
109
110=item * name ($)
111
112The name of the object.  Required.
113
114=item * min_level ($)
115
116The minimum logging level this object will accept.  See the
117Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels"> for more information.  Required.
118
119=item * max_level ($)
120
121The maximum logging level this obejct will accept.  See the
122Log::Dispatch documentation on L<Log Levels|Log::Dispatch/"Log Levels"> for more information.  This is not
123required.  By default the maximum is the highest possible level (which
124means functionally that the object has no maximum).
125
126=item * ident ($)
127
128This string will be prepended to all messages in the system log.
129Defaults to $0.
130
131=item * logopt ($)
132
133A string containing the log options (separated by any separator you
134like).  Valid options are 'cons', 'pid', 'ndelay', and 'nowait'.  See
135the openlog(3) and Sys::Syslog docs for more details.  I would suggest
136not using 'cons' but instead using Log::Dispatch::Screen.  Defaults to
137''.
138
139=item * facility ($)
140
141Specifies what type of program is doing the logging to the system log.
142Valid options are 'auth', 'authpriv', 'cron', 'daemon', 'kern',
143'local0' through 'local7', 'mail, 'news', 'syslog', 'user',
144'uucp'.  Defaults to 'user'
145
146=item * socket ($)
147
148Tells what type of socket to use for sending syslog messages.  Valid
149options are 'unix' or 'inet'.  Defaults to 'unix'.
150
151=item * callbacks( \& or [ \&, \&, ... ] )
152
153This parameter may be a single subroutine reference or an array
154reference of subroutine references.  These callbacks will be called in
155the order they are given and passed a hash containing the following keys:
156
157 ( message => $log_message, level => $log_level )
158
159The callbacks are expected to modify the message and then return a
160single scalar containing that modified message.  These callbacks will
161be called when either the C<log> or C<log_to> methods are called and
162will only be applied to a given message once.
163
164=back
165
166=item * log_message( message => $ )
167
168Sends a message to the appropriate output.  Generally this shouldn't
169be called directly but should be called through the C<log()> method
170(in Log::Dispatch::Output).
171
172=back
173
174=head1 AUTHOR
175
176Dave Rolsky, <autarch@urth.org>
177
178=cut
179