File Coverage

blib/lib/Log/Fine/Handle/Syslog.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 8 75.0
condition 7 12 58.3
subroutine 10 10 100.0
pod 1 1 100.0
total 63 70 90.0


line stmt bran cond sub pod time code
1              
2             =head1 NAME
3              
4             Log::Fine::Handle::Syslog - Output log messages to syslog
5              
6             =head1 SYNOPSIS
7              
8             Provides logging to syslog()
9              
10             use Log::Fine;
11             use Log::Fine::Handle::Syslog;
12             use Sys::Syslog;
13              
14             # Get a new logger
15             my $log = Log::Fine->logger("foo");
16              
17             # Create a new syslog handle
18             my $handle = Log::Fine::Handle::Syslog
19             ->new( name => 'syslog0',
20             mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
21             ident => $0,
22             logopts => 'pid',
23             facility => LOG_LEVEL0 );
24              
25             # Register the handle
26             $log->registerHandle($handle);
27              
28             # Log something
29             $log->(INFO, "Opened new log handle");
30              
31             =head1 DESCRIPTION
32              
33             Log::Fine::Handle::Syslog provides logging via the standard UNIX
34             syslog facility. For more information, it is I recommended
35             that you read the L documentation.
36              
37             =cut
38              
39 3     3   44747 use strict;
  3         8  
  3         302  
40 3     3   18 use warnings;
  3         6  
  3         220  
41              
42             package Log::Fine::Handle::Syslog;
43              
44 3     3   25 use base qw( Log::Fine::Handle );
  3         185  
  3         2673  
45              
46 3     3   20 use File::Basename;
  3         10  
  3         339  
47 3     3   56 use Log::Fine;
  3         7  
  3         94  
48 3     3   5777 use Sys::Syslog 0.13 qw( :standard :macros );
  3         18278  
  3         1815  
49              
50             our $VERSION = $Log::Fine::Handle::VERSION;
51              
52             # Constant: LOG_MAPPING
53             #
54             # Maps Log::Fine LOG_LEVELS to Sys::Syslog equivalents
55              
56 3         1516 use constant LOG_MAPPING => {
57             0 => LOG_EMERG,
58             1 => LOG_ALERT,
59             2 => LOG_CRIT,
60             3 => LOG_ERR,
61             4 => LOG_WARNING,
62             5 => LOG_NOTICE,
63             6 => LOG_INFO,
64             7 => LOG_DEBUG,
65 3     3   34 };
  3         8  
66              
67             # Private Methods
68             # --------------------------------------------------------------------
69              
70             {
71             my $flag = 0;
72              
73             # Getter/Setter for flag
74             sub _flag
75             {
76 6 50 66 6   48 $flag = 1 if (defined $_[0] and $_[0] =~ /\d/ and $_[0] > 0);
      66        
77 6         52 return $flag;
78             }
79             }
80              
81             # --------------------------------------------------------------------
82              
83             =head1 METHODS
84              
85             =head2 msgWrite
86              
87             See L
88              
89             Note that this method B make use of a formatter as this is
90             handled by the syslog facility.
91              
92             =cut
93              
94             sub msgWrite
95             {
96              
97 2     2 1 6 my $self = shift;
98 2         6 my $lvl = shift;
99 2         7 my $msg = shift;
100 2         5 my $skip = shift; # NOT USED
101 2         4 my $map = LOG_MAPPING;
102              
103             # Write to syslog
104 2         21 syslog($map->{$lvl}, $msg);
105              
106 2         978 return $self;
107              
108             } # msgWrite()
109              
110             # --------------------------------------------------------------------
111              
112             ##
113             # Initializes our object
114              
115             sub _init
116             {
117              
118 4     4   9 my $self = shift;
119              
120             # Perform any necessary upper class initializations
121 4         33 $self->SUPER::_init();
122              
123             # Make sure we have one and only one syslog object defined
124 4 100       15 $self->_fatal(
125             sprintf("One and _only_ one %s object may be defined",
126             ref $self)) if _flag();
127              
128             # Set ident
129 2         147 $self->{ident} = basename $0;
130              
131             # Set the default logopts (to be passed to Sys::Syslog::openlog()
132 2 50 33     31 $self->{logopts} = "pid"
133             unless (defined $self->{logopts} and $self->{logopts} =~ /\w+/);
134              
135             # Set the default facility
136 2 100 66     18 $self->{facility} = LOG_LOCAL0
137             unless (defined $self->{facility}
138             and $self->{facility} =~ /\w+/);
139              
140             # Open the syslog connection and set flag
141 2         13 openlog($self->{ident}, $self->{logopts}, $self->{facility});
142 2         69 _flag(1);
143              
144 2         7 return $self;
145              
146             } # _init()
147              
148             =head1 BUGS
149              
150             Please report any bugs or feature requests to
151             C, or through the web interface at
152             L.
153             I will be notified, and then you'll automatically be notified of progress on
154             your bug as I make changes.
155              
156             =head1 SUPPORT
157              
158             You can find documentation for this module with the perldoc command.
159              
160             perldoc Log::Fine
161              
162             You can also look for information at:
163              
164             =over 4
165              
166             =item * AnnoCPAN: Annotated CPAN documentation
167              
168             L
169              
170             =item * CPAN Ratings
171              
172             L
173              
174             =item * RT: CPAN's request tracker
175              
176             L
177              
178             =item * Search CPAN
179              
180             L
181              
182             =back
183              
184             =head1 AUTHOR
185              
186             Christopher M. Fuhrman, C<< >>
187              
188             =head1 SEE ALSO
189              
190             L, L, L
191              
192             =head1 COPYRIGHT & LICENSE
193              
194             Copyright (c) 2008, 2010, 2013 Christopher M. Fuhrman,
195             All rights reserved.
196              
197             This program is free software licensed under the...
198              
199             The BSD License
200              
201             The full text of the license can be found in the
202             LICENSE file included with this module.
203              
204             =cut
205              
206             1; # End of Log::Fine::Handle::Syslog