File Coverage

blib/lib/HeliosX/Logger/Syslog.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package HeliosX::Logger::Syslog;
2              
3 1     1   45241 use 5.008;
  1         4  
  1         42  
4 1     1   5 use strict;
  1         2  
  1         32  
5 1     1   5 use warnings;
  1         5  
  1         41  
6 1     1   5 use base qw(Helios::Logger);
  1         2  
  1         907  
7              
8             use Sys::Syslog;
9              
10             use Helios::LogEntry::Levels qw(:all);
11             use Helios::Error::LoggingError;
12              
13             our $VERSION = '1.00';
14              
15             =head1 NAME
16              
17             HeliosX::Logger::Syslog - Helios::Logger subclass implementing logging to syslogd for Helios
18              
19             =head1 SYNOPSIS
20              
21             # in helios.ini
22             loggers=HeliosX::Logger::Syslog
23            
24             # (optional) specific a syslog facility (defaults to 'user')
25             syslog_facility=local1
26              
27             # (optional) you can set other syslog options as necessary
28             syslog_options=nofatal,pid
29              
30             # (optional) you can set the logmask by using mask values
31             # 127 will filter out LOG_DEBUG msgs but log everything else
32             syslog_logmask=127
33              
34             # (optional) you can also set a threshold (doesn't pass the message to syslog)
35             # 3 will log errors and worse, but filter out warnings, notices, etc
36             syslog_priority_threshold=3
37              
38             =head1 DESCRIPTION
39              
40             This class implments a Helios::Logger subclass to provide Helios applications
41             the ability to log messages to syslogd.
42              
43             =head1 CONFIGURATION
44              
45             Config options:
46              
47             =over 4
48              
49             =item syslog_facility
50              
51             The syslogd facility to which to log messages. If not specified, it will
52             default to 'user'.
53              
54             =item syslog_options
55              
56             A comma-delimited list of syslog options. This will be passed as the second
57             parameter of openlog(). These options include (from the Sys::Syslog manpage):
58              
59             =over 4
60              
61             =item nofatal
62              
63             When set to true, "openlog()" and "syslog()" will only emit warnings
64             instead of dying if the connection to the syslog can't be established.
65              
66             =item nowait
67              
68             Don't wait for child processes that may have been created while logging
69             the message. (The GNU C library does not create a child process, so this option
70             has no effect on Linux.)
71              
72             =item perror
73              
74             Write the message to standard error output as well to the system log.
75              
76             =item pid
77              
78             Include PID with each message.
79              
80             =back
81              
82             See the L manpage for more details.
83              
84             =item syslog_logmask
85              
86             Allows you choose which log priorities you want to syslogd to actually log.
87             This is like the Helios internal log_priority_threshold, but more capable as
88             you can pick and choose which priorities you want, rather than just a range.
89              
90             Syslogd defines the mask values for priorities as:
91              
92             1 = LOG_EMERG
93             2 = LOG_ALERT
94             4 = LOG_CRIT
95             8 = LOG_ERR
96             16 = LOG_WARNING
97             32 = LOG_NOTICE
98             64 = LOG_INFO
99             128 = LOG_DEBUG
100              
101             So, for example, if you wanted to log everything except LOG_DEBUG messages,
102             putting:
103              
104             syslog_logmask=127
105              
106             in your helios.ini or Ctrl Panel will cause syslogd to filter out messages of
107             LOG_DEBUG priority. In addition, to only log LOG_ERR and LOG_WARNING messages:
108              
109             syslog_logmask=24
110              
111             will filter out any messages not of LOG_ERR or LOG_WARNING priority
112             (8 + 16 = 24).
113              
114             =item syslog_priority_threshold
115              
116             Just like log_priority_threshold, but for syslogd. If you just want to log
117             messages of a certain priority or higher, you can set a numeric value for
118             syslog_priority_threshold and any log messages of a higher value (lower
119             priority) will be discarded. The priority levels are defined in
120             Helios::LogEntry::Levels (which happen to match syslogd's):
121              
122             Helios::LogEntry::Levels numeric values
123             LOG_EMERG 0
124             LOG_ALERT 1
125             LOG_CRIT 2
126             LOG_ERR 3
127             LOG_WARNING 4
128             LOG_NOTICE 5
129             LOG_INFO 6
130             LOG_DEBUG 7
131              
132             So if you want to discard LOG_DEBUG-level messages but log everything else,
133             adding a line like
134              
135             syslog_priority_threshold=6
136              
137             to your helios.ini or Ctrl Panel will discard LOG_DEBUG-level messages but log
138             everything else.
139              
140             The syslog_priority_threshold configuration option is implemented at the Perl
141             level, so if your syslogd-based system is experiencing high load, you can use
142             it instead of syslog_logmask to reduce demand on your logging system.
143              
144             It should be noted that although log_priority_threshold and
145             Blog_priority_threshold work in exactly the same way, they are in fact
146             completely independent. The log_priority_threshold config option only
147             affects the internal Helios logging system (Helios::Logger::Internal), while
148             syslog_priority_threshold only affects HeliosX::Logger::Syslog.
149              
150              
151             =back
152              
153             =head1 IMPLEMENTED METHODS
154              
155             =head2 init()
156              
157             The init() method is empty.
158              
159             =cut
160              
161             sub init { }
162              
163              
164             =head2 logMsg($job, $priority_level, $message)
165              
166             The logMsg() method logs the given message to the configured syslog_facility with the configured
167             syslog_options and the given $priority_level.
168              
169             =cut
170              
171             sub logMsg {
172             my $self = shift;
173             my $job = shift;
174             my $priority = shift;
175             my $msg = shift;
176             my $config = $self->getConfig();
177             my $facility;
178             my $options;
179              
180             # if syslog_priority_threshold is set & this priority
181             # isn't as bad as that, don't bother doing any syslog stuff
182             if ( defined($config->{syslog_priority_threshold}) &&
183             $priority > $config->{syslog_priority_threshold} )
184             {
185             return;
186             }
187              
188             # default to facility 'user'
189             if ( !defined($config->{syslog_facility}) ) {
190             $facility = 'user';
191             } else {
192             $facility = $config->{syslog_facility};
193             }
194             # use options if specified
195             if ( defined($config->{syslog_options}) ) {
196             $options = $config->{syslog_options};
197             }
198              
199             openlog($self->getJobType(), $options, $facility);
200             if ( defined($config->{syslog_logmask}) ) {
201             setlogmask($config->{syslog_logmask});
202             }
203             syslog($priority, $self->assembleMsg($job, $priority, $msg));
204             closelog();
205             }
206              
207              
208             =head2 assembleMsg($job, $priority_level, $msg)
209              
210             Given the information passed to logMsg(), assembleMsg() returns the actual text
211             string to be logged to syslogd. Separating this step into its own method
212             allows you to easily override the default message format if you so
213             choose. Simply subclass HeliosX::Logger::Syslog and override assembleMsg()
214             with your own message formatting method.
215              
216             =cut
217              
218             sub assembleMsg {
219             my ($self, $job, $priority, $msg) = @_;
220             if ( defined($job) ) {
221             return 'Job '.$job->getJobid().': '.$msg;
222             } else {
223             return $msg;
224             }
225             }
226              
227              
228             1;
229             __END__