File Coverage

blib/lib/Log/Any/Adapter/Syslog.pm
Criterion Covered Total %
statement 31 32 96.8
branch 2 2 100.0
condition 10 14 71.4
subroutine 9 10 90.0
pod 0 1 0.0
total 52 59 88.1


line stmt bran cond sub pod time code
1             package Log::Any::Adapter::Syslog;
2 6     6   7621 use strict;
  6         12  
  6         238  
3 6     6   31 use warnings;
  6         9  
  6         361  
4              
5             # ABSTRACT: Send Log::Any logs to syslog
6             our $VERSION = '1.5'; # VERSION
7              
8 6     6   31 use Log::Any::Adapter::Util qw{make_method};
  6         12  
  6         397  
9 6     6   33 use base qw{Log::Any::Adapter::Base};
  6         12  
  6         5558  
10              
11 6     6   17001 use Unix::Syslog qw{:macros :subs};
  6         13  
  6         2256  
12 6     6   38 use File::Basename ();
  6         12  
  6         2547  
13              
14             my $log_params;
15              
16             # When initialized we connect to syslog.
17             sub init {
18 10     10 0 6563 my ($self) = @_;
19              
20 10   50     486 $self->{name} ||= File::Basename::basename($0) || 'perl';
      66        
21 10   66     66 $self->{options} ||= LOG_PID;
22 10   66     56 $self->{facility} ||= LOG_LOCAL7;
23              
24             # We want to avoid re-opening the syslog unnecessarily, so only do it if
25             # the parameters have changed.
26 10         25 my $new_params = $self->_log_params;
27 10 100 100     49 if ((not defined $log_params) or ($log_params ne $new_params)) {
28              
29 8         10 $log_params = $new_params;
30 8         37 openlog($self->{name}, $self->{options}, $self->{facility});
31             }
32              
33 10         49 return $self;
34             }
35              
36             sub _log_params {
37 10     10   17 my ($self) = @_;
38 10         58 return sprintf('%d,%d,%s',
39             $self->{options}, $self->{facility}, $self->{name});
40             }
41              
42             # Create logging methods: debug, info, etc.
43             foreach my $method (Log::Any->logging_methods()) {
44             my $priority = {
45             trace => LOG_DEBUG,
46             debug => LOG_DEBUG,
47             info => LOG_INFO,
48             inform => LOG_INFO,
49             notice => LOG_NOTICE,
50             warning => LOG_WARNING,
51             warn => LOG_WARNING,
52             error => LOG_ERR,
53             err => LOG_ERR,
54             critical => LOG_CRIT,
55             crit => LOG_CRIT,
56             fatal => LOG_CRIT,
57             alert => LOG_ALERT,
58             emergency => LOG_EMERG,
59             }->{$method};
60             defined($priority) or $priority = LOG_ERR; # unknown, take a guess.
61              
62 9     9   7064 make_method($method, sub { shift; syslog($priority, '%s', join('', @_)) });
  9         26  
63             }
64              
65             # Create detection methods: is_debug, is_info, etc.
66 0     0     my $always_on = sub { 1; };
67             foreach my $method (Log::Any->detection_methods()) {
68             make_method($method, $always_on);
69             }
70              
71              
72             1;
73              
74              
75              
76             =pod
77              
78             =head1 NAME
79              
80             Log::Any::Adapter::Syslog - Send Log::Any logs to syslog
81              
82             =head1 VERSION
83              
84             version 1.5
85              
86             =head1 SYNOPSIS
87              
88             use Log::Any::Adapter;
89             Log::Any::Adapter->set('Syslog');
90              
91             # You can override defaults:
92             use Unix::Syslog qw{:macros};
93             Log::Any::Adapter->set(
94             'Syslog',
95             # name defaults to basename($0)
96             name => 'my-name',
97             # options default to LOG_PID
98             options => LOG_PID|LOG_PERROR,
99             # facility defaults to LOG_LOCAL7
100             facility => LOG_LOCAL7
101             );
102              
103             =head1 DESCRIPTION
104              
105             L is a generic adapter for writing logging into Perl modules; this
106             adapter use the L module to direct that output into the standard
107             Unix syslog system.
108              
109             =head1 CONFIGURATION
110              
111             C is designed to work out of the box with no
112             configuration required; the defaults should be reasonably sensible.
113              
114             You can override the default configuration by passing extra arguments to the
115             C method:
116              
117             =over
118              
119             =item name
120              
121             The I argument defaults to the basename of C<$0> if not supplied, and is
122             inserted into each line sent to syslog to identify the source.
123              
124             =item options
125              
126             The I configure the behaviour of syslog; see L for
127             details.
128              
129             The default is C, which includes the PID of the current process after
130             the process name:
131              
132             example-process[2345]: something amazing!
133              
134             The most likely addition to that is C which causes syslog to also
135             send a copy of all log messages to the controlling terminal of the process.
136              
137             B If you pass a defined value you are setting, not augmenting, the
138             options. So, if you want C as well as other flags, pass them all.
139              
140             =item facility
141              
142             The I determines where syslog sends your messages. The default is
143             C, which is not the most useful value ever, but is less bad than
144             assuming the fixed facilities.
145              
146             See L and L for details on the available facilities.
147              
148             =back
149              
150             =head1 AUTHORS
151              
152             =over 4
153              
154             =item *
155              
156             Daniel Pittman
157              
158             =item *
159              
160             Stephen Thirlwall
161              
162             =back
163              
164             =head1 COPYRIGHT AND LICENSE
165              
166             This software is copyright (c) 2013 by Stephen Thirlwall.
167              
168             This is free software; you can redistribute it and/or modify it under
169             the same terms as the Perl 5 programming language system itself.
170              
171             =cut
172              
173              
174             __END__