File Coverage

blib/lib/Log/Syslog/Fast/Simple.pm
Criterion Covered Total %
statement 70 70 100.0
branch 2 4 50.0
condition 21 35 60.0
subroutine 16 16 100.0
pod 2 2 100.0
total 111 127 87.4


line stmt bran cond sub pod time code
1             package Log::Syslog::Fast::Simple;
2              
3 2     2   69334 use strict;
  2         11  
  2         56  
4 2     2   10 use warnings;
  2         4  
  2         62  
5              
6 2     2   428 use Log::Syslog::Fast ':all';
  2         5  
  2         474  
7 2     2   979 use Sys::Hostname;
  2         2061  
  2         201  
8              
9             require Exporter;
10             our @ISA = qw(Exporter);
11             our @EXPORT = qw();
12             our %EXPORT_TAGS = %Log::Syslog::Fast::Constants::EXPORT_TAGS;
13             our @EXPORT_OK = @Log::Syslog::Fast::Constants::EXPORT_OK;
14              
15 2     2   15 use constant _LOGGERS => 0;
  2         4  
  2         103  
16 2     2   10 use constant _ARGS => 1;
  2         4  
  2         84  
17              
18 2     2   12 use constant _PROTO => 0;
  2         3  
  2         82  
19 2     2   11 use constant _HOSTNAME => 1;
  2         3  
  2         119  
20 2     2   13 use constant _PORT => 2;
  2         3  
  2         105  
21 2     2   11 use constant _FACILITY => 3;
  2         5  
  2         94  
22 2     2   12 use constant _SEVERITY => 4;
  2         11  
  2         98  
23 2     2   12 use constant _SENDER => 5;
  2         3  
  2         97  
24 2     2   15 use constant _NAME => 6;
  2         17  
  2         94  
25 2     2   11 use constant _FORMAT => 7;
  2         4  
  2         951  
26              
27             sub new {
28 2     2 1 483 my $what = shift;
29 2   33     10 my $class = ref $what || $what;
30              
31 2         5 my $default_name = $0;
32 2         10 $default_name =~ s,.*/,,;
33 2         14 $default_name =~ s/[^\w.-_]//g;
34              
35 2 50 33     12 my $args = (@_ == 1 && ref $_[0] eq 'HASH') ? $_[0] : {@_};
36              
37 2   50     13 $args->{proto} ||= LOG_UDP;
38 2   50     9 $args->{hostname} ||= '127.0.0.1';
39 2   100     9 $args->{port} ||= 514;
40 2   66     10 $args->{facility} ||= LOG_LOCAL0;
41 2   66     9 $args->{severity} ||= LOG_INFO;
42 2   66     10 $args->{sender} ||= Sys::Hostname::hostname;
43 2   66     26 $args->{name} ||= $default_name;
44 2   50     9 $args->{format} ||= LOG_RFC3164;
45              
46             return bless [
47             [], # loggers
48 2         4 [@{ $args }{qw/
  2         19  
49             proto hostname port facility severity sender name format
50             /}],
51             ], $class;
52             }
53              
54             sub send {
55 2   66 2 1 895 my $severity = $_[3] || $_[0][_ARGS][_SEVERITY];
56 2   66     8 my $facility = $_[4] || $_[0][_ARGS][_FACILITY];
57              
58 2         6 my $logger = $_[0][_LOGGERS][$facility][$severity];
59 2 50       5 if (!$logger) {
60 2         4 my @args = @{ $_[0][_ARGS] };
  2         7  
61 2         3 $args[_FACILITY] = $facility;
62 2         4 $args[_SEVERITY] = $severity;
63              
64 2         2 my $format = pop(@args);
65 2         367 $logger = $_[0][_LOGGERS][$facility][$severity] = Log::Syslog::Fast->new(@args);
66 2         47 $logger->set_format($format);
67             }
68              
69 2   66     189 return $logger->send($_[1], $_[2] || time);
70             }
71              
72             1;
73             __END__