1#-----------------------------------------------------------------
2# Monitor::Simple::Log
3# Author: Martin Senger <martin.senger@gmail.com>
4# For copyright and disclaimer see below.
5#
6# ABSTRACT: See documentation in Monitor::Simple
7# PODNAME: Monitor::Simple::Log
8#-----------------------------------------------------------------
9
10package Monitor::Simple::Log;
11
12use warnings;
13use strict;
14use Log::Log4perl qw(:easy);
15
16our $VERSION = '0.2.8'; # VERSION
17
18my $default_loglevel  = 'INFO';
19my $default_logfile   = 'smonitor.log';
20#my $default_logformat = '%d (%r) %p> %F{1}:%L - %m%n';
21my $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()
25my $logging_options = {};
26
27# -----------------------------------------------------------------
28# Return current logging options.
29# -----------------------------------------------------------------
30sub get_logging_options {
31    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# -----------------------------------------------------------------
42sub log_init {
43    my ($self, $args) = @_;
44    $logging_options = $args;
45
46    my $logger_conf = {};
47
48    # log level
49    my $opt_loglevel = $args->{level};
50    if ($opt_loglevel) {
51        my $level = Log::Log4perl::Level::to_priority (uc ($opt_loglevel));
52        $logger_conf->{level} = $level;
53    } else {
54        $logger_conf->{level} = Log::Log4perl::Level::to_priority ($default_loglevel);
55    }
56
57    # log file
58    my $opt_logfile = $args->{file};
59    $opt_logfile ||= $default_logfile;
60    $opt_logfile =~ s{^[><]+}{};   # I had problems when '>' was there; it created binary log file (TBD?)
61    if ($opt_logfile ne 'STDOUT' and $opt_logfile ne 'STDERR' and $opt_logfile !~ m{^>}) {
62        $opt_logfile = ">>$opt_logfile";
63    }
64    $logger_conf->{file} = $opt_logfile;
65
66    # log layout pattern
67    my $opt_logformat = $args->{layout};
68    $opt_logformat ||= $default_logformat;
69    $logger_conf->{layout} = $opt_logformat;
70
71    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# -----------------------------------------------------------------
79sub logging_args {
80    my ($self) = @_;
81    my @logging_args = ();
82    push (@logging_args, '-logfile',   $logging_options->{file})   if $logging_options->{file};
83    push (@logging_args, '-loglevel',  $logging_options->{level})  if $logging_options->{level};
84    push (@logging_args, '-logformat', $logging_options->{layout}) if $logging_options->{layout};
85    return (@logging_args);
86}
87
881;
89
90__END__
91=pod
92
93=head1 NAME
94
95Monitor::Simple::Log - See documentation in Monitor::Simple
96
97=head1 VERSION
98
99version 0.2.8
100
101=head1 AUTHOR
102
103Martin Senger <martin.senger@gmail.com>
104
105=head1 COPYRIGHT AND LICENSE
106
107This software is copyright (c) 2013 by Martin Senger, CBRC-KAUST (Computational Biology Research Center - King Abdullah University of Science and Technology) All Rights Reserved.
108
109This is free software; you can redistribute it and/or modify it under
110the same terms as the Perl 5 programming language system itself.
111
112=cut
113
114