/
Minimal.pm
421 lines (305 loc) · 9.21 KB
/
Minimal.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package Log::Minimal;
use strict;
use warnings;
use base qw/Exporter/;
use Term::ANSIColor qw//;
our $VERSION = '0.09';
our @EXPORT = map { ($_.'f', $_.'ff') } qw/crit warn info debug croak/;
push @EXPORT, 'ddf';
our $PRINT = sub {
my ( $time, $type, $message, $trace, $raw_message) = @_;
warn "$time [$type] $message at $trace\n";
};
our $DIE = sub {
my ( $time, $type, $message, $trace, $raw_message) = @_;
die "$time [$type] $message at $trace\n";
};
our $DEFAULT_COLOR = {
info => { text => 'green', },
debug => {
text => 'red',
background => 'white',
},
'warn' => {
text => 'black',
background => 'yellow',
},
'critical' => {
text => 'black',
background => 'red'
},
'error' => {
text => 'red',
background => 'black'
}
};
our $ENV_DEBUG = "LM_DEBUG";
our $AUTODUMP = 0;
our $LOG_LEVEL = 'DEBUG';
our $TRACE_LEVEL = 0;
our $COLOR = $ENV{LM_COLOR} || 0;
my %log_level_map = (
DEBUG => 1,
INFO => 2,
WARN => 3,
CRITICAL => 4,
MUTE => 0,
ERROR => 99,
);
sub critf {
_log( "CRITICAL", 0, @_ );
}
sub warnf {
_log( "WARN", 0, @_ );
}
sub infof {
_log( "INFO", 0, @_ );
}
sub debugf {
return if !$ENV{$ENV_DEBUG} || $log_level_map{DEBUG} < $log_level_map{uc $LOG_LEVEL};
_log( "DEBUG", 0, @_ );
}
sub critff {
_log( "CRITICAL", 1, @_ );
}
sub warnff {
_log( "WARN", 1, @_ );
}
sub infoff {
_log( "INFO", 1, @_ );
}
sub debugff {
return if !$ENV{$ENV_DEBUG} || $log_level_map{DEBUG} < $log_level_map{uc $LOG_LEVEL};
_log( "DEBUG", 1, @_ );
}
sub croakf {
local $PRINT = $DIE;
_log( "ERROR", 0, @_ );
}
sub croakff {
local $PRINT = $DIE;
_log( "ERROR", 1, @_ );
}
sub _log {
my $tag = shift;
my $full = shift;
my $_log_level = $log_level_map{uc $LOG_LEVEL} || return;
return unless $log_level_map{$tag} >= $_log_level;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
localtime(time);
my $time = sprintf(
"%04d-%02d-%02dT%02d:%02d:%02d",
$year + 1900,
$mon + 1, $mday, $hour, $min, $sec
);
my $trace;
if ( $full ) {
my $i=$TRACE_LEVEL+1;
my @stack;
while ( my @caller = caller($i) ) {
push @stack, "$caller[1] line $caller[2]";
$i++;
}
$trace = join " ,", @stack
}
else {
my @caller = caller($TRACE_LEVEL+1);
$trace = "$caller[1] line $caller[2]";
}
my $messages = '';
if ( @_ == 1 && defined $_[0]) {
$messages = $AUTODUMP ? ''.Log::Minimal::Dumper->new($_[0]) : $_[0];
}
elsif ( @_ >= 2 ) {
$messages = sprintf(shift, map { $AUTODUMP ? Log::Minimal::Dumper->new($_) : $_ } @_);
}
$messages =~ s/\x0d/\\r/g;
$messages =~ s/\x0a/\\n/g;
$messages =~ s/\x09/\\t/g;
my $raw_message = $messages;
if ( $COLOR ) {
$messages = Term::ANSIColor::color($DEFAULT_COLOR->{lc($tag)}->{text})
. $messages . Term::ANSIColor::color("reset")
if $DEFAULT_COLOR->{lc($tag)}->{text};
$messages = Term::ANSIColor::color("on_".$DEFAULT_COLOR->{lc($tag)}->{background})
. $messages . Term::ANSIColor::color("reset")
if $DEFAULT_COLOR->{lc($tag)}->{background};
}
$PRINT->(
$time,
$tag,
$messages,
$trace,
$raw_message
);
}
sub ddf {
my $value = shift;
Log::Minimal::Dumper::dumper($value);
}
1;
package
Log::Minimal::Dumper;
use strict;
use warnings;
use base qw/Exporter/;
use Data::Dumper;
use Scalar::Util qw/blessed/;
use overload
'""' => \&stringfy,
'0+' => \&numeric,
fallback => 1;
sub new {
my ($class, $value) = @_;
bless \$value, $class;
}
sub stringfy {
my $self = shift;
my $value = $$self;
if ( blessed($value) && (my $stringify = overload::Method( $value, '""' ) || overload::Method( $value, '0+' )) ) {
$value = $stringify->($value);
}
dumper($value);
}
sub numeric {
my $self = shift;
my $value = $$self;
if ( blessed($value) && (my $numeric = overload::Method( $value, '0+' ) || overload::Method( $value, '""' )) ) {
$value = $numeric->($value);
}
$value;
}
sub dumper {
my $value = shift;
if ( defined $value && ref($value) ) {
local $Data::Dumper::Terse = 1;
local $Data::Dumper::Indent = 0;
$value = Data::Dumper::Dumper($value);
}
$value;
}
1;
__END__
=head1 NAME
Log::Minimal - Minimal but customizable logger.
=head1 SYNOPSIS
use Log::Minimal;
critf("%s","foo"); # 2010-10-20T00:25:17 [CRITICAL] foo at example.pl line 12
warnf("%d %s %s", 1, "foo", $uri);
infof('foo');
debugf("foo"); print if $ENV{LM_DEBUG} is true
# with full stack trace
critff("%s","foo");
# 2010-10-20T00:25:17 [CRITICAL] foo at lib/Example.pm line 10, example.pl line 12
warnff("%d %s %s", 1, "foo", $uri);
infoff('foo');
debugff("foo"); print if $ENV{LM_DEBUG} is true
my $serialize = ddf({ 'key' => 'value' });
# die with formatted message
croakf('foo');
croakff('%s %s', $code, $message);
=head1 DESCRIPTION
Log::Minimal is Minimal but customizable log module.
=head1 EXPORT FUNCTIONS
=over 4
=item critf(($message:Str|$format:Str,@list:Array));
critf("could't connect to example.com");
critf("Connection timeout timeout:%d, host:%s", 2, "example.com");
Display CRITICAL messages.
When two or more arguments are passed to the function,
the first argument is treated as a format of printf.
local $Log::Minimal::AUTODUMP = 1;
critf({ foo => 'bar' });
critf("dump is %s", { foo => 'bar' });
If $Log::Minimal::AUTODUMP is true, reference or object message is serialized with
Data::Dumper automatically.
=item warnf(($message:Str|$format:Str,@list:Array));
Display WARN messages.
=item infof(($message:Str|$format:Str,@list:Array));
Display INFO messages.
=item debugf(($message:Str|$format:Str,@list:Array));
Display DEBUG messages, if $ENV{LM_DEBUG} is true.
=item critff(($message:Str|$format:Str,@list:Array));
critff("could't connect to example.com");
critff("Connection timeout timeout:%d, host:%s", 2, "example.com");
Display CRITICAL messages with stack trace.
=item warnff(($message:Str|$format:Str,@list:Array));
Display WARN messages with stack trace.
=item infoff(($message:Str|$format:Str,@list:Array));
Display INFO messages with stack trace.
=item debugff(($message:Str|$format:Str,@list:Array));
Display DEBUG messages with stack trace, if $ENV{LM_DEBUG} is true.
=item croakf(($message:Str|$format:Str,@list:Array));
die with formatted $message
croakf("critical error");
# 2011-06-10T16:27:26 [ERROR] critical error at sample.pl line 23
=item croakff(($message:Str|$format:Str,@list:Array));
die with formatted $message with stack trace
=item ddf($value:Any)
Utility method that serializes given value with Data::Dumper;
my $serialize = ddf($hashref);
=back
=head1 ENVIRONMENT
To print debugf and debugff messages, $ENV{LM_DEBUG} must be true.
=head1 CUSTOMIZE
=over 4
=item $Log::Minimal::COLOR
Coloring log messages. Disabled by default.
=item $Log::Minimal::PRINT
To change the method of outputting the log, set $Log::Minimal::PRINT.
# with PSGI Application. output log with request uri.
my $app = sub {
my $env = shift;
local $Log::Minimal::PRINT = sub {
my ( $time, $type, $message, $trace,$raw_message) = @_;
$env->{psgi.errors}->print(
"$time [$env->{SCRIPT_NAME}] [$type] $message at $trace\n");
};
run_app(...);
}
$message includes color sequences, If you want raw message text, use $raw_message.
default is
sub {
my ( $time, $type, $message, $trace,$raw_message) = @_;
warn "$time [$type] $message at $trace\n";
}
=item $Log::Minimal::DIE
To change the format of die message, set $Log::Minimal::DIE.
local $Log::Minimal::PRINT = sub {
my ( $time, $type, $message, $trace) = @_;
die "[$type] $message at $trace\n"; # not need time
};
default is
sub {
my ( $time, $type, $message, $trace) = @_;
die "$time [$type] $message at $trace\n";
}
=item $Log::Minimal::LOG_LEVEL
Set level to output log.
local $Log::Minimal::LOG_LEVEL = "WARN";
infof("foo"); #print nothing
warnf("foo");
Support levels are DEBUG,INFO,WARN,CRITICAL and NONE.
If NONE is set, no output. Default log level is DEBUG.
=item $Log::Minimal::AUTODUMP
Serialize message with Data::Dumper.
warnf("%s",{ foo => bar}); # HASH(0x100804ed0)
local $Log::Minimal::AUTODUMP = 1;
warnf("dump is %s", {foo=>'bar'}); #dump is {foo=>'bar'}
my $uri = URI->new("http://search.cpan.org/");
warnf("uri: '%s'", $uri); # uri: 'http://search.cpan.org/'
If message is object and has overload methods like '""' or '0+',
Log::Minimal uses it instead of Data::Dumper.
=item $Log::Minimal::TRACE_LEVEL
Like a $Carp::CarpLevel, this variable determines how many additional call frames are to be skipped.
Defaults to 0.
=back
=head1 AUTHOR
Masahiro Nagano E<lt>kazeburo {at} gmail.comE<gt>
=head1 THANKS TO
Yuji Shimada (xaicron)
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut