View | Details | Raw Unified | Return to bug 8190
Collapse All | Expand All

(-)a/Koha/Utils/Logger.pm (-33 / +129 lines)
Lines 1-22 Link Here
1
package Koha::Utils::Logger;
1
package Koha::Utils::Logger;
2
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;
3
use Modern::Perl;
21
use Log::LogLite;
4
use Log::LogLite;
22
5
Lines 68-74 sub new { Link Here
68
    return bless( $self, $class );
51
    return bless( $self, $class );
69
}
52
}
70
53
71
sub write {
54
sub _write {
72
    my ( $self, $msg, $log_level, $dump, $cb ) = @_;
55
    my ( $self, $msg, $log_level, $dump, $cb ) = @_;
73
56
74
    if ( not $self->{LOGGER} ) {
57
    if ( not $self->{LOGGER} ) {
Lines 92-137 sub write { Link Here
92
75
93
sub unusable {
76
sub unusable {
94
    my ( $self, $msg, $dump ) = @_;
77
    my ( $self, $msg, $dump ) = @_;
95
    my $cb = $self->called_by();
78
    my $cb = $self->_called_by();
96
    $self->write( $msg, $UNUSABLE_LOG_LEVEL, $dump, $cb );
79
    $self->_write( $msg, $UNUSABLE_LOG_LEVEL, $dump, $cb );
97
}
80
}
98
81
99
sub critical {
82
sub critical {
100
    my ( $self, $msg, $dump ) = @_;
83
    my ( $self, $msg, $dump ) = @_;
101
    my $cb = $self->called_by();
84
    my $cb = $self->_called_by();
102
    $self->write( $msg, $CRITICAL_LOG_LEVEL, $dump, $cb );
85
    $self->_write( $msg, $CRITICAL_LOG_LEVEL, $dump, $cb );
103
}
86
}
104
87
105
sub error {
88
sub error {
106
    my ( $self, $msg, $dump ) = @_;
89
    my ( $self, $msg, $dump ) = @_;
107
    my $cb = $self->called_by();
90
    my $cb = $self->_called_by();
108
    $self->write( $msg, $ERROR_LOG_LEVEL, $dump, $cb );
91
    $self->_write( $msg, $ERROR_LOG_LEVEL, $dump, $cb );
109
}
92
}
110
93
111
sub warning {
94
sub warning {
112
    my ( $self, $msg, $dump ) = @_;
95
    my ( $self, $msg, $dump ) = @_;
113
    my $cb = $self->called_by();
96
    my $cb = $self->_called_by();
114
    $self->write( $msg, $WARNING_LOG_LEVEL, $dump, $cb );
97
    $self->_write( $msg, $WARNING_LOG_LEVEL, $dump, $cb );
115
}
98
}
116
99
117
sub log {
100
sub log {
118
    my ( $self, $msg, $dump ) = @_;
101
    my ( $self, $msg, $dump ) = @_;
119
    $self->write( $msg, $NORMAL_LOG_LEVEL, $dump );
102
    $self->_write( $msg, $NORMAL_LOG_LEVEL, $dump );
120
}
103
}
121
104
122
sub normal {
105
sub normal {
123
    my ( $self, $msg, $dump ) = @_;
106
    my ( $self, $msg, $dump ) = @_;
124
    $self->write( $msg, $NORMAL_LOG_LEVEL, $dump );
107
    $self->_write( $msg, $NORMAL_LOG_LEVEL, $dump );
125
}
108
}
126
109
127
sub info {
110
sub info {
128
    my ( $self, $msg, $dump ) = @_;
111
    my ( $self, $msg, $dump ) = @_;
129
    $self->write( $msg, $INFO_LOG_LEVEL, $dump );
112
    $self->_write( $msg, $INFO_LOG_LEVEL, $dump );
130
}
113
}
131
114
132
sub debug {
115
sub debug {
133
    my ( $self, $msg, $dump ) = @_;
116
    my ( $self, $msg, $dump ) = @_;
134
    $self->write( $msg, $DEBUG_LOG_LEVEL, $dump );
117
    $self->_write( $msg, $DEBUG_LOG_LEVEL, $dump );
135
}
118
}
136
119
137
sub level {
120
sub level {
Lines 142-148 sub level { Link Here
142
      : ( $self->{LEVEL} = @_ ? shift : $self->{LEVEL} );
125
      : ( $self->{LEVEL} = @_ ? shift : $self->{LEVEL} );
143
}
126
}
144
127
145
sub called_by {
128
sub _called_by {
146
    my $self  = shift;
129
    my $self  = shift;
147
    my $depth = 2;
130
    my $depth = 2;
148
    my $args;
131
    my $args;
Lines 165-171 sub called_by { Link Here
165
            last;
148
            last;
166
        }
149
        }
167
        $depth++;
150
        $depth++;
168
        $line = (3) ? "$file:" . $line . "-->" : "";
151
        $line = "$file:" . $line . "-->";
169
        push( @subr, $line . $subr );
152
        push( @subr, $line . $subr );
170
    }
153
    }
171
    @subr = reverse(@subr);
154
    @subr = reverse(@subr);
Lines 184-186 sub get_messages { Link Here
184
}
167
}
185
168
186
1;
169
1;
187
- 
170
171
__END__
172
173
=pod
174
175
=head1 NAME
176
177
Koha::Utils::Logger - Logging module for Koha
178
179
=head1 DESCRIPTION
180
181
This module is the logging engine that prints your logs to the koha log file or to stderr.
182
183
=head1 USAGE
184
185
New instance:
186
    my $logger = Koha::Utils::Logger->new(
187
        {
188
            file  => $my_log_filepath,
189
            level => $my_level
190
        }
191
    );
192
193
The default level is 'NORMAL'.
194
The default file is the environment variable KOHA_LOG ($ENV{KOHA_LOG}).
195
If the previous created instance has the same filename, the previous instance is returned.
196
197
If no file is given, the logs are send to stderr.
198
199
7 levels are available:
200
    DEBUG, INFO, NORMAL, WARN, ERROR, CRIT, UNUS
201
202
You can call the 7 log methods with a second parameter (defined and != 0) to dump your message with the Data::Dumper module.
203
Useful to display a complex structure.
204
205
=head1 METHODS
206
207
=head2 new
208
209
The constructor method
210
211
=head2 _write
212
213
private method to write a message.
214
Writes the log message to the file or stderr.
215
216
=head2 unusable
217
218
add a message with an unusable level
219
220
=head2 critical
221
222
add a message with a critical level
223
224
=head2 error
225
226
add a message with an error level
227
228
=head2 warning
229
230
add a message with a warning level
231
232
=head2 log
233
234
add a message with a normal level
235
236
=head2 normal
237
238
add a message with a normal level
239
240
=head2 info
241
242
add a message with an information level
243
244
=head2 debug
245
246
add a message with a debug level
247
248
=head2 level
249
250
set or get a level for this instance
251
252
=head2 _called_by
253
254
This private method returns a callback trace.
255
256
=head2 get_messages
257
258
Returns all messages for this instance.
259
260
=head1 AUTHORS
261
262
This module has been written by Jonathan Druart.
263
264
=head1 COPYRIGHT
265
266
Copyright 2012 Biblibre SARL
267
268
=head1 LICENSE
269
270
This file is part of Koha.
271
272
Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
273
Foundation; either version 2 of the License, or (at your option) any later version.
274
275
You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
276
Fifth Floor, Boston, MA 02110-1301 USA.
277
278
=head1 DISCLAIMER OF WARRANTY
279
280
Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
281
A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
282
283
=cut

Return to bug 8190