File Coverage

blib/lib/Monitor/Simple/Log.pm
Criterion Covered Total %
statement 9 34 26.4
branch 0 10 0.0
condition 0 12 0.0
subroutine 3 6 50.0
pod 0 3 0.0
total 12 65 18.4


line stmt bran cond sub pod time code
1             #-----------------------------------------------------------------
2             # Monitor::Simple::Log
3             # Author: Martin Senger
4             # For copyright and disclaimer see below.
5             #
6             # ABSTRACT: See documentation in Monitor::Simple
7             # PODNAME: Monitor::Simple::Log
8             #-----------------------------------------------------------------
9              
10             package Monitor::Simple::Log;
11              
12 6     6   31 use warnings;
  6         11  
  6         181  
13 6     6   29 use strict;
  6         9  
  6         181  
14 6     6   8902 use Log::Log4perl qw(:easy);
  6         397711  
  6         42  
15              
16             our $VERSION = '0.2.8'; # VERSION
17              
18             my $default_loglevel = 'INFO';
19             my $default_logfile = 'smonitor.log';
20             #my $default_logformat = '%d (%r) %p> %F{1}:%L - %m%n';
21             my $default_logformat = '%d (%r) %p> %m%n';
22              
23             # will be filled by calling log_init() - they are just a copy of $args
24             # given in log_init()
25             my $logging_options = {};
26              
27             # -----------------------------------------------------------------
28             # Return current logging options.
29             # -----------------------------------------------------------------
30             sub get_logging_options {
31 0     0 0   return $logging_options;
32             }
33              
34             # -----------------------------------------------------------------
35             # Initiate logging. Arguments is a hashref (some values may be
36             # undefined). For example:
37             #
38             # Monitor::Simple::Log->log_init ({ level => $opt_loglevel,
39             # file => $opt_logfile,
40             # layout => $opt_logformat });
41             # -----------------------------------------------------------------
42             sub log_init {
43 0     0 0   my ($self, $args) = @_;
44 0           $logging_options = $args;
45              
46 0           my $logger_conf = {};
47              
48             # log level
49 0           my $opt_loglevel = $args->{level};
50 0 0         if ($opt_loglevel) {
51 0           my $level = Log::Log4perl::Level::to_priority (uc ($opt_loglevel));
52 0           $logger_conf->{level} = $level;
53             } else {
54 0           $logger_conf->{level} = Log::Log4perl::Level::to_priority ($default_loglevel);
55             }
56              
57             # log file
58 0           my $opt_logfile = $args->{file};
59 0   0       $opt_logfile ||= $default_logfile;
60 0           $opt_logfile =~ s{^[><]+}{}; # I had problems when '>' was there; it created binary log file (TBD?)
61 0 0 0       if ($opt_logfile ne 'STDOUT' and $opt_logfile ne 'STDERR' and $opt_logfile !~ m{^>}) {
      0        
62 0           $opt_logfile = ">>$opt_logfile";
63             }
64 0           $logger_conf->{file} = $opt_logfile;
65              
66             # log layout pattern
67 0           my $opt_logformat = $args->{layout};
68 0   0       $opt_logformat ||= $default_logformat;
69 0           $logger_conf->{layout} = $opt_logformat;
70              
71 0           Log::Log4perl->easy_init ($logger_conf);
72              
73             }
74              
75             # -----------------------------------------------------------------
76             # Convert (and return) my $logging_options to the command-line
77             # arguments used for plugins and notifiers.
78             # -----------------------------------------------------------------
79             sub logging_args {
80 0     0 0   my ($self) = @_;
81 0           my @logging_args = ();
82 0 0         push (@logging_args, '-logfile', $logging_options->{file}) if $logging_options->{file};
83 0 0         push (@logging_args, '-loglevel', $logging_options->{level}) if $logging_options->{level};
84 0 0         push (@logging_args, '-logformat', $logging_options->{layout}) if $logging_options->{layout};
85 0           return (@logging_args);
86             }
87              
88             1;
89              
90             __END__