1###########################################################################
2#
3#   Syslog.pm
4#
5#   Copyright (C) 1999 Raphael Manfredi.
6#   Copyright (C) 2002-2017 Mark Rogaski, mrogaski@cpan.org;
7#   all rights reserved.
8#
9#   See the README file included with the
10#   distribution for license information.
11#
12##########################################################################
13
14use strict;
15
16########################################################################
17package Log::Agent::Driver::Syslog;
18
19require Log::Agent::Driver;
20use vars qw(@ISA);
21@ISA = qw(Log::Agent::Driver);
22
23require Log::Agent::Channel::Syslog;
24
25#
26# ->make			-- defined
27#
28# Creation routine.
29#
30# All switches are passed to Log::Agent::Channel::Syslog.
31#
32# prefix		the application name
33# facility		the syslog facility name to use ("auth", "daemon", etc...)
34# showpid		whether to show pid
35# socktype		socket type ('unix' or 'inet')
36# logopt		list of openlog() options: 'ndelay', 'cons' or 'nowait'
37#
38sub make {
39	my $self = bless {}, shift;
40	my (%args) = @_;
41	my $prefix;
42
43	my %set = (
44		-prefix		=> \$prefix,				# Handled by parent via _init
45	);
46
47	while (my ($arg, $val) = each %args) {
48		my $vset = $set{lc($arg)};
49		next unless ref $vset;
50		$$vset = $val;
51	}
52
53	$self->{channel} = Log::Agent::Channel::Syslog->make(@_);
54	$self->_init($prefix, 0);					# 0 is the skip Carp penalty
55	return $self;
56}
57
58sub channel		{ $_[0]->{channel} }
59
60#
61# ->prefix_msg		-- defined
62#
63# NOP -- syslog will handle this
64#
65sub prefix_msg {
66	my $self = shift;
67	return $_[0];
68}
69
70#
71# ->channel_eq		-- defined
72#
73# Always true.
74#
75sub channel_eq {
76	return 1;
77}
78
79my %syslog_pri = (
80	'em' => 'emerg',
81	'al' => 'alert',
82	'cr' => 'crit',
83	'er' => 'err',
84	'wa' => 'warning',
85	'no' => 'notice',
86	'in' => 'info',
87	'de' => 'debug'
88);
89
90#
91# ->map_pri			-- redefined
92#
93# Levels ignored, only priorities matter.
94#
95sub map_pri {
96	my $self = shift;
97	my ($priority, $level) = @_;
98	return $syslog_pri{lc(substr($priority, 0, 2))} || 'debug';
99}
100
101#
102# ->write			-- defined
103#
104# $channel is ignored
105#
106sub write {
107	my $self = shift;
108	my ($channel, $priority, $logstring) = @_;
109	$self->channel->write($priority, $logstring);
110}
111
1121;	# for require
113__END__
114
115=head1 NAME
116
117Log::Agent::Driver::Syslog - syslog logging driver for Log::Agent
118
119=head1 SYNOPSIS
120
121 use Log::Agent;
122 require Log::Agent::Driver::Syslog;
123
124 my $driver = Log::Agent::Driver::Syslog->make(
125     -prefix     => prefix,
126     -facility   => "user",
127     -showpid    => 1,
128     -socktype   => { port => 514, proto => "udp" },
129     -logopt     => "ndelay",
130 );
131 logconfig(-driver => $driver);
132
133=head1 DESCRIPTION
134
135The syslog logging driver delegates logxxx() operations to syslog() via
136the Sys::Syslog(3) interface.
137
138The creation routine make() takes the following switches:
139
140=over 4
141
142=item C<-facility> => I<facility>
143
144Tell syslog() which facility to use (e.g. "user", "auth", "daemon").
145Unlike the Sys::Syslog(3) interface, the facility is set once and for all:
146every logging message will use the same facility.
147
148If you wish to log something to "auth" for instance, then do so via
149Sys::Syslog directly: there is no guarantee that the application will configure
150its Log::Agent to use syslog anyway!
151
152=item C<-logopt> => I<syslog options>
153
154Specifies logging options, under the form of a string containing zero or
155more of the words I<ndelay>, I<cons> or I<nowait>.
156
157=item C<-prefix> => I<prefix>
158
159The I<prefix> here is syslog's identification string.
160
161=item C<-showpid> => I<flag>
162
163Set to true to have the PID of the process logged. It is false by default.
164
165=item C<-socktype> => I<options>
166
167Specifies the logging socket to use (protocol, destination, etc.).
168The value given is not interpreted and passed as-is to the C<setlogsock()>
169routine in Sys::Syslog(3).
170
171Please refer to Log::Agent::Channel::Syslog(3) for more information.
172
173=back
174
175=head1 CHANNELS
176
177All the channels go to syslog(), of course.
178
179=head1 AUTHOR
180
181Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
182
183=head1 SEE ALSO
184
185Log::Agent::Driver(3), Log::Agent::Channel::Syslog(3), Sys::Syslog(3).
186
187=cut
188