1package Log::Dispatch::Syslog;
2{
3  $Log::Dispatch::Syslog::VERSION = '2.34';
4}
5
6use strict;
7use warnings;
8
9use Log::Dispatch::Output;
10
11use base qw( Log::Dispatch::Output );
12
13use Params::Validate qw(validate ARRAYREF SCALAR);
14Params::Validate::validation_options( allow_extra => 1 );
15
16use Sys::Syslog 0.25 ();
17
18sub new {
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
32my ($Ident) = $0 =~ /(.+)/;
33
34sub _init {
35    my $self = shift;
36
37    my %p = validate(
38        @_, {
39            ident => {
40                type    => SCALAR,
41                default => $Ident
42            },
43            logopt => {
44                type    => SCALAR,
45                default => ''
46            },
47            facility => {
48                type    => SCALAR,
49                default => 'user'
50            },
51            socket => {
52                type    => SCALAR | ARRAYREF,
53                default => undef
54            },
55        }
56    );
57
58    $self->{ident}    = $p{ident};
59    $self->{logopt}   = $p{logopt};
60    $self->{facility} = $p{facility};
61    $self->{socket}   = $p{socket};
62
63    $self->{priorities} = [
64        'DEBUG',
65        'INFO',
66        'NOTICE',
67        'WARNING',
68        'ERR',
69        'CRIT',
70        'ALERT',
71        'EMERG'
72    ];
73
74    Sys::Syslog::setlogsock(
75        ref $self->{socket} ? @{ $self->{socket} } : $self->{socket} )
76        if defined $self->{socket};
77}
78
79sub log_message {
80    my $self = shift;
81    my %p    = @_;
82
83    my $pri = $self->_level_as_number( $p{level} );
84
85    eval {
86        Sys::Syslog::openlog(
87            $self->{ident}, $self->{logopt},
88            $self->{facility}
89        );
90        Sys::Syslog::syslog( $self->{priorities}[$pri], $p{message} );
91        Sys::Syslog::closelog;
92    };
93
94    warn $@ if $@ and $^W;
95}
96
971;
98
99# ABSTRACT: Object for logging to system log.
100
101__END__
102
103=pod
104
105=head1 NAME
106
107Log::Dispatch::Syslog - Object for logging to system log.
108
109=head1 VERSION
110
111version 2.34
112
113=head1 SYNOPSIS
114
115  use Log::Dispatch;
116
117  my $log = Log::Dispatch->new(
118      outputs => [
119          [
120              'Syslog',
121              min_level => 'info',
122              ident     => 'Yadda yadda'
123          ]
124      ]
125  );
126
127  $log->emerg("Time to die.");
128
129=head1 DESCRIPTION
130
131This module provides a simple object for sending messages to the
132system log (via UNIX syslog calls).
133
134Note that logging may fail if you try to pass UTF-8 characters in the
135log message. If logging fails and warnings are enabled, the error
136message will be output using Perl's C<warn>.
137
138=head1 CONSTRUCTOR
139
140The constructor takes the following parameters in addition to the standard
141parameters documented in L<Log::Dispatch::Output>:
142
143=over 4
144
145=item * ident ($)
146
147This string will be prepended to all messages in the system log.
148Defaults to $0.
149
150=item * logopt ($)
151
152A string containing the log options (separated by any separator you
153like).  See the openlog(3) and Sys::Syslog docs for more details.
154Defaults to ''.
155
156=item * facility ($)
157
158Specifies what type of program is doing the logging to the system log.
159Valid options are 'auth', 'authpriv', 'cron', 'daemon', 'kern',
160'local0' through 'local7', 'mail, 'news', 'syslog', 'user',
161'uucp'.  Defaults to 'user'
162
163=item * socket ($ or \@)
164
165Tells what type of socket to use for sending syslog messages.  Valid
166options are listed in C<Sys::Syslog>.
167
168If you don't provide this, then we let C<Sys::Syslog> simply pick one
169that works, which is the preferred option, as it makes your code more
170portable.
171
172If you pass an array reference, it is dereferenced and passed to
173C<Sys::Syslog::setlogsock()>.
174
175=back
176
177=head1 AUTHOR
178
179Dave Rolsky <autarch@urth.org>
180
181=head1 COPYRIGHT AND LICENSE
182
183This software is Copyright (c) 2011 by Dave Rolsky.
184
185This is free software, licensed under:
186
187  The Artistic License 2.0 (GPL Compatible)
188
189=cut
190