|
Line 0
Link Here
|
|
|
1 |
package Koha::Utils::Logger; |
| 2 |
|
| 3 |
# Copyright 2012 Biblibre SARL |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it under the |
| 8 |
# terms of the GNU General Public License as published by the Free Software |
| 9 |
# Foundation; either version 2 of the License, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License along |
| 17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Log::LogLite; |
| 22 |
|
| 23 |
use base 'Exporter'; |
| 24 |
our @EXPORT_OK = qw($log); |
| 25 |
|
| 26 |
my $UNUSABLE_LOG_LEVEL = 1; |
| 27 |
my $CRITICAL_LOG_LEVEL = 2; |
| 28 |
my $ERROR_LOG_LEVEL = 3; |
| 29 |
my $WARNING_LOG_LEVEL = 4; |
| 30 |
my $NORMAL_LOG_LEVEL = 5; |
| 31 |
my $INFO_LOG_LEVEL = 6; |
| 32 |
my $DEBUG_LOG_LEVEL = 7; |
| 33 |
|
| 34 |
my $LEVEL_STR = { |
| 35 |
$UNUSABLE_LOG_LEVEL => 'UNUS ', |
| 36 |
$CRITICAL_LOG_LEVEL => 'CRIT ', |
| 37 |
$ERROR_LOG_LEVEL => 'ERROR ', |
| 38 |
$WARNING_LOG_LEVEL => 'WARN ', |
| 39 |
$NORMAL_LOG_LEVEL => 'NORMAL', |
| 40 |
$INFO_LOG_LEVEL => 'INFO ', |
| 41 |
$DEBUG_LOG_LEVEL => 'DEBUG ', |
| 42 |
}; |
| 43 |
|
| 44 |
use Data::Dumper; |
| 45 |
|
| 46 |
our $log = undef; |
| 47 |
|
| 48 |
sub new { |
| 49 |
my ( $proto, $params ) = @_; |
| 50 |
|
| 51 |
return $log if $log and ( not defined $params->{file} or not defined $log->{FILE_PATH} or $params->{file} eq $log->{FILE_PATH} ); |
| 52 |
my $class = ref($proto) || $proto; |
| 53 |
my $self = {}; |
| 54 |
my $LOG_PATH = defined $ENV{LOG} ? $ENV{LOG} : undef; |
| 55 |
$self->{FILE_PATH} = defined $params->{file} ? $params->{file} : $LOG_PATH; |
| 56 |
$self->{LEVEL} = defined $params->{level} ? $params->{level} : $INFO_LOG_LEVEL; |
| 57 |
if ( not defined $self->{FILE_PATH} ) { |
| 58 |
return bless($self, $class); |
| 59 |
} |
| 60 |
eval { |
| 61 |
$self->{LOGGER} = Log::LogLite->new($self->{FILE_PATH}, $self->{LEVEL}); |
| 62 |
}; |
| 63 |
die "Log system is not correctly configured ($@)" if $@; |
| 64 |
return bless( $self, $class ); |
| 65 |
} |
| 66 |
|
| 67 |
sub write { |
| 68 |
my ($self, $msg, $log_level, $dump, $cb) = @_; |
| 69 |
|
| 70 |
if ( not $self->{LOGGER} ) { |
| 71 |
if($log_level <= $self->{LEVEL}) { |
| 72 |
print STDERR "[" . localtime() . "] " |
| 73 |
. "$LEVEL_STR->{$log_level}: " |
| 74 |
. $msg |
| 75 |
. ( $cb ? " (" . $cb . ")" : "" ) |
| 76 |
. "\n"; |
| 77 |
} |
| 78 |
return; |
| 79 |
} |
| 80 |
my $template = "[<date>] $LEVEL_STR->{$log_level}: <message>"; |
| 81 |
$template .= " (caller: $cb)" if $cb; |
| 82 |
$template .= "\n"; |
| 83 |
$self->{LOGGER}->template($template); |
| 84 |
$msg = "\n" . Dumper $msg if $dump; |
| 85 |
$self->{LOGGER}->write($msg, $log_level); |
| 86 |
} |
| 87 |
|
| 88 |
sub unusable { |
| 89 |
my ($self, $msg, $dump) = @_; |
| 90 |
my $cb = $self->called_by(); |
| 91 |
$self->write($msg, $UNUSABLE_LOG_LEVEL, $dump, $cb); |
| 92 |
} |
| 93 |
|
| 94 |
sub critical { |
| 95 |
my ($self, $msg, $dump) = @_; |
| 96 |
my $cb = $self->called_by(); |
| 97 |
$self->write($msg, $CRITICAL_LOG_LEVEL, $dump, $cb); |
| 98 |
} |
| 99 |
|
| 100 |
sub error { |
| 101 |
my ($self, $msg, $dump) = @_; |
| 102 |
my $cb = $self->called_by(); |
| 103 |
$self->write($msg, $ERROR_LOG_LEVEL, $dump, $cb); |
| 104 |
} |
| 105 |
|
| 106 |
sub warning { |
| 107 |
my ($self, $msg, $dump) = @_; |
| 108 |
my $cb = $self->called_by(); |
| 109 |
$self->write($msg, $WARNING_LOG_LEVEL, $dump, $cb); |
| 110 |
} |
| 111 |
|
| 112 |
sub log { |
| 113 |
my ($self, $msg, $dump) = @_; |
| 114 |
$self->write($msg, $NORMAL_LOG_LEVEL, $dump); |
| 115 |
} |
| 116 |
|
| 117 |
sub normal { |
| 118 |
my ($self, $msg, $dump) = @_; |
| 119 |
$self->write($msg, $NORMAL_LOG_LEVEL, $dump); |
| 120 |
} |
| 121 |
|
| 122 |
sub info { |
| 123 |
my ($self, $msg, $dump) = @_; |
| 124 |
$self->write($msg, $INFO_LOG_LEVEL, $dump); |
| 125 |
} |
| 126 |
|
| 127 |
sub debug { |
| 128 |
my ($self, $msg, $dump) = @_; |
| 129 |
$self->write($msg, $DEBUG_LOG_LEVEL, $dump); |
| 130 |
} |
| 131 |
|
| 132 |
sub level { |
| 133 |
my $self = shift; |
| 134 |
|
| 135 |
return $self->{LOGGER} |
| 136 |
? $self->{LOGGER}->level(@_) |
| 137 |
: ($self->{LEVEL} = @_ ? shift : $self->{LEVEL}); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
sub called_by { |
| 142 |
my $self = shift; |
| 143 |
my $depth = 2; |
| 144 |
my $args; |
| 145 |
my $pack; |
| 146 |
my $file; |
| 147 |
my $line; |
| 148 |
my $subr; |
| 149 |
my $has_args; |
| 150 |
my $wantarray; |
| 151 |
my $evaltext; |
| 152 |
my $is_require; |
| 153 |
my $hints; |
| 154 |
my $bitmask; |
| 155 |
my @subr; |
| 156 |
my $str = ""; |
| 157 |
while (1) { |
| 158 |
($pack, $file, $line, $subr, $has_args, $wantarray, $evaltext, |
| 159 |
$is_require, $hints, $bitmask) = caller($depth); |
| 160 |
unless (defined($subr)) { |
| 161 |
last; |
| 162 |
} |
| 163 |
$depth++; |
| 164 |
$line = (3) ? "$file:".$line."-->" : ""; |
| 165 |
push(@subr, $line.$subr); |
| 166 |
} |
| 167 |
@subr = reverse(@subr); |
| 168 |
foreach $subr (@subr) { |
| 169 |
$str .= $subr; |
| 170 |
$str .= " > "; |
| 171 |
} |
| 172 |
$str =~ s/ > $/: /; |
| 173 |
return $str; |
| 174 |
} # of called_by |
| 175 |
|
| 176 |
1; |