File Coverage

blib/lib/Dancer2/Logger/Syslog.pm
Criterion Covered Total %
statement 26 30 86.6
branch 1 2 50.0
condition 5 16 31.2
subroutine 7 7 100.0
pod 1 1 100.0
total 40 56 71.4


line stmt bran cond sub pod time code
1             package Dancer2::Logger::Syslog;
2             # ABSTRACT: Dancer2 logger engine for Sys::Syslog
3             $Dancer2::Logger::Syslog::VERSION = '0.4';
4              
5 3     3   434215 use Moo;
  3         34944  
  3         18  
6 3     3   4138 use File::Basename 'basename';
  3         8  
  3         396  
7 3     3   1387 use Sys::Syslog qw(:standard :extended);
  3         46328  
  3         703  
8              
9 3     3   1347 use Dancer2::Core::Types;
  3         22769  
  3         2361  
10              
11             with 'Dancer2::Core::Role::Logger';
12              
13             has 'log_open' => (
14             is => 'ro',
15             isa => Bool,
16             lazy => 1,
17             builder => '_syslog_open',
18             );
19              
20             # Set by calling function
21             has facility => (
22             is => 'ro',
23             isa => Str,
24             lazy => 1,
25             );
26              
27             has ident => (
28             is => 'ro',
29             isa => Str,
30             lazy => 1,
31             );
32              
33             has logopt => (
34             is => 'ro',
35             isa => Str,
36             lazy => 1,
37             );
38              
39             has host => (
40             is => 'ro',
41             isa => Str,
42             lazy => 1,
43             );
44              
45             has type => (
46             is => 'ro',
47             isa => Str,
48             lazy => 1,
49             );
50              
51             has port => (
52             is => 'ro',
53             isa => Str,
54             lazy => 1,
55             );
56              
57              
58             sub _syslog_open {
59 2     2   1487 my $self = shift;
60              
61 2   50     29 my $facility = $self->facility || 'USER';
62 2   33     35 my $ident = $self->ident
63             || $self->app_name
64             || $ENV{DANCER_APPDIR}
65             || basename($0);
66 2   50     16 my $logopt = $self->logopt || 'pid';
67              
68 2 50 33     20 if ( $self->host && ( $Sys::Syslog::VERSION >= 0.33 )) {
69 0         0 my $host = $self->host;
70 0   0     0 my $type = $self->type || 'udp';
71 0   0     0 my $port = $self->port || 53;
72 0         0 setlogsock( { host => $host, type => $type, port => $port } )
73             }
74 2         13 openlog($ident, $logopt, $facility);
75 2         559 1; # openlog() will have croaked if it can't connect
76             }
77              
78 1     1   337 sub DESTROY { closelog() }
79              
80             sub log {
81 7     7 1 152076 my ($self, $level, $message) = @_;
82              
83 7         133 $self->log_open;
84              
85 7         141 my $syslog_levels = {
86             core => 'debug',
87             debug => 'debug',
88             warning => 'warning',
89             error => 'err',
90             info => 'info',
91             };
92              
93 7   50     23 $level = $syslog_levels->{$level} || 'debug';
94 7         28 my $fm = $self->format_message($level => $message);
95 7         2905 syslog($level, $fm);
96             }
97              
98             1;
99              
100             __END__