| 1 |
NAME |
| 2 |
|
| 3 |
Log::Handler - Log messages to several outputs. |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
|
| 7 |
use Log::Handler; |
| 8 |
|
| 9 |
my $log = Log::Handler->new(); |
| 10 |
|
| 11 |
$log->add( |
| 12 |
file => { |
| 13 |
filename => "file.log", |
| 14 |
maxlevel => "debug", |
| 15 |
minlevel => "warning", |
| 16 |
} |
| 17 |
); |
| 18 |
|
| 19 |
$log->warning("message"); |
| 20 |
|
| 21 |
Or |
| 22 |
|
| 23 |
use Log::Handler; |
| 24 |
|
| 25 |
my $log = Log::Handler->new( |
| 26 |
screen => { |
| 27 |
log_to => "STDOUT", |
| 28 |
maxlevel => "debug", |
| 29 |
minlevel => "debug", |
| 30 |
message_layout => "%T [%L] %m (%C)", |
| 31 |
}, |
| 32 |
screen => { |
| 33 |
log_to => "STDOUT", |
| 34 |
maxlevel => "info", |
| 35 |
minlevel => "notice", |
| 36 |
}, |
| 37 |
screen => { |
| 38 |
log_to => "STDERR", |
| 39 |
maxlevel => "warning", |
| 40 |
minlevel => "emergency", |
| 41 |
}, |
| 42 |
); |
| 43 |
|
| 44 |
Or |
| 45 |
|
| 46 |
use Log::Handler; |
| 47 |
|
| 48 |
my $log = Log::Handler->new(); |
| 49 |
|
| 50 |
$log->config( config => "logger.conf" ); |
| 51 |
|
| 52 |
# and maybe later |
| 53 |
|
| 54 |
$log->reload( config => "logger.conf" ); |
| 55 |
|
| 56 |
Or |
| 57 |
|
| 58 |
# create a application wide logger |
| 59 |
package MyApp; |
| 60 |
use Log::Handler; |
| 61 |
my $log = Log::Handler->create_logger("myapp"); |
| 62 |
$log->add(screen => { maxlevel => "info" }); |
| 63 |
$log->info("info message"); |
| 64 |
|
| 65 |
# get logger with get_logger() |
| 66 |
package MyApp::Admin; |
| 67 |
use Log::Handler; |
| 68 |
my $log = Log::Handler->get_logger("myapp"); |
| 69 |
$log->info("info message from MyApp::Admin"); |
| 70 |
|
| 71 |
DESCRIPTION |
| 72 |
|
| 73 |
The Log::Handler is a object oriented handler for logging, tracing and |
| 74 |
debugging. It is very easy to use and provides a simple interface for |
| 75 |
multiple output objects with lots of configuration parameters. You can |
| 76 |
easily filter the amount of logged information on a per-output base, |
| 77 |
define priorities, create patterns to format the messages and reload |
| 78 |
the complete logging machine. |
| 79 |
|
| 80 |
See the documentation for details. |
| 81 |
|
| 82 |
IMPORTANT NOTES |
| 83 |
|
| 84 |
Note that the default for option newline is now set to TRUE and |
| 85 |
newlines will be appended automatically to each message if no newline |
| 86 |
exists. |
| 87 |
|
| 88 |
A long time I thought about this serious change and have come to the |
| 89 |
decision to change it. |
| 90 |
|
| 91 |
The default for option mode from Log::Handler::Output::File is now |
| 92 |
append and not excl anymore. |
| 93 |
|
| 94 |
The methods reload() and validate() are new since version 0.62. I |
| 95 |
tested it with Screen.pm, File.pm and DBI.pm and it runs fine. If you |
| 96 |
find bugs then open a bug report please :-) |
| 97 |
|
| 98 |
LOG LEVELS |
| 99 |
|
| 100 |
There are eigth levels available: |
| 101 |
|
| 102 |
7 debug |
| 103 |
6 info |
| 104 |
5 notice |
| 105 |
4 warning, warn |
| 106 |
3 error, err |
| 107 |
2 critical, crit |
| 108 |
1 alert |
| 109 |
0 emergency, emerg |
| 110 |
|
| 111 |
debug is the highest and emergency is the lowest level. |
| 112 |
|
| 113 |
Level debug is the highest level because it basically says to log every |
| 114 |
peep. |
| 115 |
|
| 116 |
LOG LEVEL METHODS |
| 117 |
|
| 118 |
Level methods |
| 119 |
|
| 120 |
debug() |
| 121 |
|
| 122 |
info() |
| 123 |
|
| 124 |
notice() |
| 125 |
|
| 126 |
warning(), warn() |
| 127 |
|
| 128 |
error(), err() |
| 129 |
|
| 130 |
critical(), crit() |
| 131 |
|
| 132 |
alert() |
| 133 |
|
| 134 |
emergency(), emerg() |
| 135 |
|
| 136 |
The call of a log level method is very simple: |
| 137 |
|
| 138 |
$log->info("Hello World! How are you?"); |
| 139 |
|
| 140 |
Or maybe: |
| 141 |
|
| 142 |
$log->info("Hello World!", "How are you?"); |
| 143 |
|
| 144 |
Both calls would log - if level INFO is active: |
| 145 |
|
| 146 |
Feb 01 12:56:31 [INFO] Hello World! How are you? |
| 147 |
|
| 148 |
is_* methods |
| 149 |
|
| 150 |
is_debug() |
| 151 |
|
| 152 |
is_info() |
| 153 |
|
| 154 |
is_notice() |
| 155 |
|
| 156 |
is_warning(), is_warn() |
| 157 |
|
| 158 |
is_error(), is_err() |
| 159 |
|
| 160 |
is_critical(), is_crit() |
| 161 |
|
| 162 |
is_alert() |
| 163 |
|
| 164 |
is_emergency(), is_emerg() |
| 165 |
|
| 166 |
These twelve methods could be very useful if you want to kwow if the |
| 167 |
current level would log the message. All methods returns TRUE if the |
| 168 |
current set of minlevel and maxlevel would log the message and FALSE if |
| 169 |
not. |
| 170 |
|
| 171 |
SPECIAL LOG METHODS |
| 172 |
|
| 173 |
fatal, is_fatal |
| 174 |
|
| 175 |
trace |
| 176 |
|
| 177 |
dump |
| 178 |
|
| 179 |
die |
| 180 |
|
| 181 |
log |
| 182 |
|
| 183 |
For a full list take a look into the documentation of |
| 184 |
Log::Handler::Levels. |
| 185 |
|
| 186 |
METHODS |
| 187 |
|
| 188 |
new() |
| 189 |
|
| 190 |
Call new() to create a new log handler object. |
| 191 |
|
| 192 |
my $log = Log::Handler->new(); |
| 193 |
|
| 194 |
add() |
| 195 |
|
| 196 |
Call add() to add a new output object. |
| 197 |
|
| 198 |
The method expects 2 parts of options; the options for the handler and |
| 199 |
the options for the output module you want to use. The output modules |
| 200 |
got it's own documentation for all options. |
| 201 |
|
| 202 |
Example: |
| 203 |
|
| 204 |
use Log::Handler; |
| 205 |
|
| 206 |
my $log = Log::Handler->new(); |
| 207 |
|
| 208 |
$log->add( |
| 209 |
|
| 210 |
# Add "file output" |
| 211 |
file => { |
| 212 |
|
| 213 |
# handler options (see Log::Handler) |
| 214 |
timeformat => "%Y/%m/%d %H:%M:%S", |
| 215 |
message_layout => "%T [%L] %S: %m", |
| 216 |
maxlevel => "debug", |
| 217 |
minlevel => "emergency", |
| 218 |
die_on_errors => 1, |
| 219 |
debug_trace => 0, |
| 220 |
debug_mode => 2, |
| 221 |
debug_skip => 0, |
| 222 |
|
| 223 |
# file options (see Log::Handler::Output::File) |
| 224 |
filename => "file.log", |
| 225 |
filelock => 1, |
| 226 |
fileopen => 1, |
| 227 |
reopen => 1, |
| 228 |
autoflush => 1, |
| 229 |
permissions => "0660", |
| 230 |
utf8 => 1, |
| 231 |
|
| 232 |
} |
| 233 |
); |
| 234 |
|
| 235 |
Take a look to Log::Handler::Examples for more examples. |
| 236 |
|
| 237 |
The following options are possible for the handler: |
| 238 |
|
| 239 |
maxlevel and minlevel |
| 240 |
|
| 241 |
With these options it's possible to set the log levels for your |
| 242 |
program. |
| 243 |
|
| 244 |
Example: |
| 245 |
|
| 246 |
maxlevel => "error" |
| 247 |
minlevel => "emergency" |
| 248 |
|
| 249 |
# or |
| 250 |
|
| 251 |
maxlevel => "err" |
| 252 |
minlevel => "emerg" |
| 253 |
|
| 254 |
# or |
| 255 |
|
| 256 |
maxlevel => 3 |
| 257 |
minlevel => 0 |
| 258 |
|
| 259 |
It's possible to set the log level as string or as number. The |
| 260 |
default setting for maxlevel is warning and the default setting for |
| 261 |
minlevel is emergency. |
| 262 |
|
| 263 |
Example: If maxlevel is set to warning and minlevel to emergency then |
| 264 |
the levels warning, error, critical, alert and emergency would be |
| 265 |
logged. |
| 266 |
|
| 267 |
You can set both to 8 or nothing if you want to disable the logging |
| 268 |
machine. |
| 269 |
|
| 270 |
timeformat |
| 271 |
|
| 272 |
The option timeformat is used to set the format for the placeholder |
| 273 |
%T. The string is converted with POSIX::strftime. The default format |
| 274 |
is set to "%b %d %H:%M:%S" and looks like |
| 275 |
|
| 276 |
Feb 01 12:56:31 |
| 277 |
|
| 278 |
If you would set the format to "%Y/%m/%d %H:%M:%S" it would looks |
| 279 |
like |
| 280 |
|
| 281 |
2007/02/01 12:56:31 |
| 282 |
|
| 283 |
dateformat |
| 284 |
|
| 285 |
This options works like timeformat. You can set a format that is used |
| 286 |
for the placeholder %D. It's just useful if you want to split the |
| 287 |
date and time: |
| 288 |
|
| 289 |
$log->add(file => { |
| 290 |
filename => "file.log", |
| 291 |
dateformat => "%Y-%m-%d", |
| 292 |
timeformat => "%H:%M:%S", |
| 293 |
message_layout => "%D %T %L %m", |
| 294 |
}); |
| 295 |
|
| 296 |
$log->error("an error here"); |
| 297 |
|
| 298 |
This looks like |
| 299 |
|
| 300 |
2007-02-01 12:56:31 ERROR an error here |
| 301 |
|
| 302 |
This option is not used by default. |
| 303 |
|
| 304 |
newline |
| 305 |
|
| 306 |
newline is a very helpful option. It let the logger appends a newline |
| 307 |
to the message if a newline doesn't exist. |
| 308 |
|
| 309 |
0 - do nothing |
| 310 |
1 - append a newline if not exist (default) |
| 311 |
|
| 312 |
Example: |
| 313 |
|
| 314 |
$log->add( |
| 315 |
screen => { |
| 316 |
newline => 1, |
| 317 |
maxlevel => "info", |
| 318 |
} |
| 319 |
); |
| 320 |
|
| 321 |
$log->info("message\n"); |
| 322 |
$log->info("message"); |
| 323 |
|
| 324 |
In both cases the message would be logged with a newline at the end. |
| 325 |
|
| 326 |
message_layout |
| 327 |
|
| 328 |
With this option it's possible to create your own message layout with |
| 329 |
different placeholders in printf() style. The available placeholders |
| 330 |
are: |
| 331 |
|
| 332 |
%L Log level |
| 333 |
%T Time or full timestamp (option timeformat) |
| 334 |
%D Date (option dateformat) |
| 335 |
%P PID |
| 336 |
%H Hostname |
| 337 |
%U User name |
| 338 |
%G Group name |
| 339 |
%N Newline |
| 340 |
%S Program name |
| 341 |
%C Caller - filename and line number |
| 342 |
%p Caller - package name |
| 343 |
%f Caller - file name |
| 344 |
%l Caller - line number |
| 345 |
%s Caller - subroutine name |
| 346 |
%r Runtime in seconds since program start |
| 347 |
%t Time measurement - replaced with the time since the last call of $log->$level |
| 348 |
%m Message |
| 349 |
%% Percent |
| 350 |
|
| 351 |
The default message layout is set to "%T [%L] %m". |
| 352 |
|
| 353 |
As example the following code |
| 354 |
|
| 355 |
$log->alert("foo bar"); |
| 356 |
|
| 357 |
would log |
| 358 |
|
| 359 |
Feb 01 12:56:31 [ALERT] foo bar |
| 360 |
|
| 361 |
If you set message_layout to |
| 362 |
|
| 363 |
message_layout => "%T foo %L bar %m (%C)" |
| 364 |
|
| 365 |
and call |
| 366 |
|
| 367 |
$log->info("baz"); |
| 368 |
|
| 369 |
then it would log |
| 370 |
|
| 371 |
Feb 01 12:56:31 foo INFO bar baz (script.pl, line 40) |
| 372 |
|
| 373 |
Traces will be appended after the complete message. |
| 374 |
|
| 375 |
You can create your own placeholders with the method set_pattern(). |
| 376 |
|
| 377 |
message_pattern |
| 378 |
|
| 379 |
This option is just useful if you want to forward messages to output |
| 380 |
modules that needs the parts of a message as a hash reference - as |
| 381 |
example Log::Handler::Output::Forward, Log::Handler::Output::DBI or |
| 382 |
Log::Handler::Output::Screen. |
| 383 |
|
| 384 |
The option expects a list of placeholders: |
| 385 |
|
| 386 |
# as a array reference |
| 387 |
message_pattern => [ qw/%T %L %H %m/ ] |
| 388 |
|
| 389 |
# or as a string |
| 390 |
message_pattern => "%T %L %H %m" |
| 391 |
|
| 392 |
The patterns will be replaced with real names as hash keys. |
| 393 |
|
| 394 |
%L level |
| 395 |
%T time |
| 396 |
%D date |
| 397 |
%P pid |
| 398 |
%H hostname |
| 399 |
%U user |
| 400 |
%G group |
| 401 |
%N newline |
| 402 |
%r runtime |
| 403 |
%C caller |
| 404 |
%p package |
| 405 |
%f filename |
| 406 |
%l line |
| 407 |
%s subroutine |
| 408 |
%S progname |
| 409 |
%t mtime |
| 410 |
%m message |
| 411 |
|
| 412 |
Here a full code example: |
| 413 |
|
| 414 |
use Log::Handler; |
| 415 |
|
| 416 |
my $log = Log::Handler->new(); |
| 417 |
|
| 418 |
$log->add(forward => { |
| 419 |
forward_to => \&my_func, |
| 420 |
message_pattern => [ qw/%T %L %H %m/ ], |
| 421 |
message_layout => "%m", |
| 422 |
maxlevel => "info", |
| 423 |
}); |
| 424 |
|
| 425 |
$log->info("a forwarded message"); |
| 426 |
|
| 427 |
# now you can access it |
| 428 |
|
| 429 |
sub my_func { |
| 430 |
my $msg = shift; |
| 431 |
print "Timestamp: $msg->{time}\n"; |
| 432 |
print "Level: $msg->{level}\n"; |
| 433 |
print "Hostname: $msg->{hostname}\n"; |
| 434 |
print "Message: $msg->{message}\n"; |
| 435 |
} |
| 436 |
|
| 437 |
prepare_message |
| 438 |
|
| 439 |
prepare_message is useful if you want to do something with the |
| 440 |
message before it will be logged... maybe you want to create your own |
| 441 |
layout because message_layout doesn't meet your claim. |
| 442 |
|
| 443 |
$log->add( |
| 444 |
screen => { |
| 445 |
newline => 1, |
| 446 |
message_layout => "%m (%t)", |
| 447 |
message_pattern => [ qw/%T %L %H %m/ ], |
| 448 |
prepare_message => \&format, |
| 449 |
} |
| 450 |
); |
| 451 |
|
| 452 |
$log->error("foo"); |
| 453 |
$log->error("bar"); |
| 454 |
$log->error("baz"); |
| 455 |
|
| 456 |
sub format { |
| 457 |
my $m = shift; |
| 458 |
|
| 459 |
$m->{message} = sprintf("%-20s %-20s %-20s %s", |
| 460 |
$m->{time}, $m->{level}, $m->{hostname}, $m->{message}); |
| 461 |
} |
| 462 |
|
| 463 |
The output looks like |
| 464 |
|
| 465 |
Mar 08 15:14:20 ERROR h1434036 foo (0.039694) |
| 466 |
Mar 08 15:14:20 ERROR h1434036 bar (0.000510) |
| 467 |
Mar 08 15:14:20 ERROR h1434036 baz (0.000274) |
| 468 |
|
| 469 |
priority |
| 470 |
|
| 471 |
With this option you can set the priority of your output objects. |
| 472 |
This means that messages will be logged at first to the outputs with |
| 473 |
a higher priority. If this option is not set then the default |
| 474 |
priority begins with 10 and will be increased +1 with each output. |
| 475 |
Example: |
| 476 |
|
| 477 |
We add a output with no priority |
| 478 |
|
| 479 |
$log->add(file => { filename => "file1.log" }); |
| 480 |
|
| 481 |
This output gets the priority of 10. Now we add another output |
| 482 |
|
| 483 |
$log->add(file => { filename => "file2.log" }); |
| 484 |
|
| 485 |
This output gets the priority of 11... and so on. |
| 486 |
|
| 487 |
Messages would be logged at first to the output with the priority of |
| 488 |
10 and then to the output with the priority of 11. Now you can add |
| 489 |
another output and set the priority to 1. |
| 490 |
|
| 491 |
$log->add(screen => { dump => 1, priority => 1 }); |
| 492 |
|
| 493 |
Messages would be logged now at first to the screen. |
| 494 |
|
| 495 |
die_on_errors |
| 496 |
|
| 497 |
Set die_on_errors to 0 if you don't want that the handler dies on |
| 498 |
failed write operations. |
| 499 |
|
| 500 |
0 - to disable it |
| 501 |
1 - to enable it |
| 502 |
|
| 503 |
If you set die_on_errors to 0 then you have to control it yourself. |
| 504 |
|
| 505 |
$log->info("info message") or die $log->errstr(); |
| 506 |
|
| 507 |
# or Log::Handler->errstr() |
| 508 |
# or Log::Handler::errstr() |
| 509 |
# or $Log::Handler::ERRSTR |
| 510 |
|
| 511 |
remove_on_reload |
| 512 |
|
| 513 |
This option is set to 1 by default. |
| 514 |
|
| 515 |
Take a look to the description of the method reload for more |
| 516 |
information about this option. |
| 517 |
|
| 518 |
filter_message |
| 519 |
|
| 520 |
With this option it's possible to set a filter. If the filter is set |
| 521 |
then only messages will be logged that match the filter. You can pass |
| 522 |
a regexp, a code reference or a simple string. Example: |
| 523 |
|
| 524 |
$log->add(file => { |
| 525 |
filename => "file.log", |
| 526 |
maxlevel => 6, |
| 527 |
filter_message => qr/log this/, |
| 528 |
# or |
| 529 |
# filter_message => "log this", |
| 530 |
# filter_message => '^log only this$', |
| 531 |
}); |
| 532 |
|
| 533 |
$log->info("log this"); |
| 534 |
$log->info("but not that"); |
| 535 |
|
| 536 |
If you pass your own code then you have to check the message |
| 537 |
yourself. |
| 538 |
|
| 539 |
$log->add(file => { |
| 540 |
filename => "file.log", |
| 541 |
maxlevel => 6, |
| 542 |
filter_message => \&my_filter |
| 543 |
}); |
| 544 |
|
| 545 |
# return TRUE if you want to log the message, FALSE if not |
| 546 |
sub my_filter { |
| 547 |
my $msg = shift; |
| 548 |
$msg->{message} =~ /your filter/; |
| 549 |
} |
| 550 |
|
| 551 |
It's also possible to define a simple condition with matches. Just |
| 552 |
pass a hash reference with the options matchN and condition. Example: |
| 553 |
|
| 554 |
$log->add(file => { |
| 555 |
filename => "file.log", |
| 556 |
maxlevel => 6, |
| 557 |
filter_message => { |
| 558 |
match1 => "log this", |
| 559 |
match2 => qr/with that/, |
| 560 |
match3 => "(?:or this|or that)", |
| 561 |
condition => "(match1 && match2) || match3", |
| 562 |
} |
| 563 |
}); |
| 564 |
|
| 565 |
NOTE that re-eval in regexes is not valid! Something like |
| 566 |
|
| 567 |
match1 => '(?{unlink("file.txt")})' |
| 568 |
|
| 569 |
would cause an error! |
| 570 |
|
| 571 |
skip_message |
| 572 |
|
| 573 |
This is the opposite of option filter_message, but it's only possible |
| 574 |
to set a simple string or regular expression. |
| 575 |
|
| 576 |
$log->add(file => { |
| 577 |
filename => "file.log", |
| 578 |
maxlevel => 6, |
| 579 |
skip => '^do not log this.+$' |
| 580 |
}); |
| 581 |
|
| 582 |
category |
| 583 |
|
| 584 |
The parameter category works like filter_caller but is much easier to |
| 585 |
configure. You can set a comma separated list of modules. As example |
| 586 |
if you would set the category to |
| 587 |
|
| 588 |
category => "MyApp::User" |
| 589 |
|
| 590 |
then all messages of MyApp::User and the submodules would be logged. |
| 591 |
|
| 592 |
Example: |
| 593 |
|
| 594 |
my $log = Log::Handler->new(); |
| 595 |
|
| 596 |
$log->add( |
| 597 |
screen => { |
| 598 |
maxlevel => "info", |
| 599 |
category => "MyApp::User, MyApp::Session" |
| 600 |
} |
| 601 |
); |
| 602 |
|
| 603 |
package MyApp; |
| 604 |
$log->info(__PACKAGE__); |
| 605 |
|
| 606 |
package MyApp::Products; |
| 607 |
$log->info(__PACKAGE__); |
| 608 |
|
| 609 |
package MyApp::User; |
| 610 |
$log->info(__PACKAGE__); |
| 611 |
|
| 612 |
package MyApp::Users; |
| 613 |
$log->info(__PACKAGE__); |
| 614 |
|
| 615 |
package MyApp::User::Settings; |
| 616 |
$log->info(__PACKAGE__); |
| 617 |
|
| 618 |
package MyApp::Session; |
| 619 |
$log->info(__PACKAGE__); |
| 620 |
|
| 621 |
package MyApp::Session::Settings; |
| 622 |
$log->info(__PACKAGE__); |
| 623 |
|
| 624 |
The messages of MyApp and MyApp::Products would not be logged. |
| 625 |
|
| 626 |
The usage of categories is much faster than to filter by caller. |
| 627 |
|
| 628 |
filter_caller |
| 629 |
|
| 630 |
You can use this option to set a package name. Only messages from |
| 631 |
this packages will be logged. |
| 632 |
|
| 633 |
Example: |
| 634 |
|
| 635 |
my $log = Log::Handler->new(); |
| 636 |
|
| 637 |
$log->add(screen => { |
| 638 |
maxlevel => "info", |
| 639 |
filter_caller => qr/^Foo::Bar\z/, |
| 640 |
# or |
| 641 |
# filter_caller => "^Foo::Bar\z", |
| 642 |
}); |
| 643 |
|
| 644 |
package Foo::Bar; |
| 645 |
$log->info("log this"); |
| 646 |
|
| 647 |
package Foo::Baz; |
| 648 |
$log->info("but not that"); |
| 649 |
|
| 650 |
1; |
| 651 |
|
| 652 |
This would only log the message from the package Foo::Bar. |
| 653 |
|
| 654 |
except_caller |
| 655 |
|
| 656 |
This option is just the opposite of filter_caller. |
| 657 |
|
| 658 |
If you want to log messages from all callers but Foo::Bar: |
| 659 |
|
| 660 |
except_caller => qr/^Foo::Bar\z/ |
| 661 |
|
| 662 |
alias |
| 663 |
|
| 664 |
You can set an alias if you want to get the output object later. |
| 665 |
Example: |
| 666 |
|
| 667 |
my $log = Log::Handler->new(); |
| 668 |
|
| 669 |
$log->add(screen => { |
| 670 |
maxlevel => 7, |
| 671 |
alias => "screen-out", |
| 672 |
}); |
| 673 |
|
| 674 |
my $screen = $log->output("screen-out"); |
| 675 |
|
| 676 |
$screen->log(message => "foo"); |
| 677 |
|
| 678 |
# or in one step |
| 679 |
|
| 680 |
$log->output("screen-out")->log(message => "foo"); |
| 681 |
|
| 682 |
debug_trace |
| 683 |
|
| 684 |
You can activate a debugger that writes caller() information about |
| 685 |
each active log level. The debugger is logging all defined values |
| 686 |
except hints and bitmask. Set debug_trace to 1 to activate the |
| 687 |
debugger. The debugger is set to 0 by default. |
| 688 |
|
| 689 |
debug_mode |
| 690 |
|
| 691 |
There are two debug modes: line(1) and block(2) mode. The default |
| 692 |
mode is 1. |
| 693 |
|
| 694 |
The line mode looks like this: |
| 695 |
|
| 696 |
use strict; |
| 697 |
use warnings; |
| 698 |
use Log::Handler; |
| 699 |
|
| 700 |
my $log = Log::Handler->new() |
| 701 |
|
| 702 |
$log->add(file => { |
| 703 |
filename => "*STDOUT", |
| 704 |
maxlevel => "debug", |
| 705 |
debug_trace => 1, |
| 706 |
debug_mode => 1 |
| 707 |
}); |
| 708 |
|
| 709 |
sub test1 { $log->warning() } |
| 710 |
sub test2 { &test1; } |
| 711 |
|
| 712 |
&test2; |
| 713 |
|
| 714 |
Output: |
| 715 |
|
| 716 |
Apr 26 12:54:11 [WARNING] |
| 717 |
CALL(4): package(main) filename(./trace.pl) line(15) subroutine(main::test2) hasargs(0) |
| 718 |
CALL(3): package(main) filename(./trace.pl) line(13) subroutine(main::test1) hasargs(0) |
| 719 |
CALL(2): package(main) filename(./trace.pl) line(12) subroutine(Log::Handler::__ANON__) hasargs(1) |
| 720 |
CALL(1): package(Log::Handler) filename(/usr/local/share/perl/5.8.8/Log/Handler.pm) line(713) subroutine(Log::Handler::_write) hasargs(1) |
| 721 |
CALL(0): package(Log::Handler) filename(/usr/local/share/perl/5.8.8/Log/Handler.pm) line(1022) subroutine(Devel::Backtrace::new) hasargs(1) wantarray(0) |
| 722 |
|
| 723 |
The same code example but the debugger in block mode would looks like |
| 724 |
this: |
| 725 |
|
| 726 |
debug_mode => 2 |
| 727 |
|
| 728 |
Output: |
| 729 |
|
| 730 |
Apr 26 12:52:17 [DEBUG] |
| 731 |
CALL(4): |
| 732 |
package main |
| 733 |
filename ./trace.pl |
| 734 |
line 15 |
| 735 |
subroutine main::test2 |
| 736 |
hasargs 0 |
| 737 |
CALL(3): |
| 738 |
package main |
| 739 |
filename ./trace.pl |
| 740 |
line 13 |
| 741 |
subroutine main::test1 |
| 742 |
hasargs 0 |
| 743 |
CALL(2): |
| 744 |
package main |
| 745 |
filename ./trace.pl |
| 746 |
line 12 |
| 747 |
subroutine Log::Handler::__ANON__ |
| 748 |
hasargs 1 |
| 749 |
CALL(1): |
| 750 |
package Log::Handler |
| 751 |
filename /usr/local/share/perl/5.8.8/Log/Handler.pm |
| 752 |
line 681 |
| 753 |
subroutine Log::Handler::_write |
| 754 |
hasargs 1 |
| 755 |
CALL(0): |
| 756 |
package Log::Handler |
| 757 |
filename /usr/local/share/perl/5.8.8/Log/Handler.pm |
| 758 |
line 990 |
| 759 |
subroutine Devel::Backtrace::new |
| 760 |
hasargs 1 |
| 761 |
wantarray 0 |
| 762 |
|
| 763 |
debug_skip |
| 764 |
|
| 765 |
This option let skip the caller() information the count of |
| 766 |
debug_skip. |
| 767 |
|
| 768 |
output() |
| 769 |
|
| 770 |
Call output($alias) to get the output object that you added with the |
| 771 |
option alias. |
| 772 |
|
| 773 |
It's possible to access a output directly: |
| 774 |
|
| 775 |
$log->output($alias)->log(message => "booo"); |
| 776 |
|
| 777 |
For more information take a look to the option alias. |
| 778 |
|
| 779 |
flush() |
| 780 |
|
| 781 |
Call flush() if you want to send flush to all outputs that can flush. |
| 782 |
|
| 783 |
Flush means to flush buffers and/or close and re-open outputs. |
| 784 |
|
| 785 |
If you want to send it only to some outputs you can pass the aliases. |
| 786 |
|
| 787 |
$log->flush(); # flush all |
| 788 |
$log->flush("foo", "bar"); # flush only foo and bar |
| 789 |
|
| 790 |
If option "die_on_errors" is set to 0 then you can intercept errors |
| 791 |
with: |
| 792 |
|
| 793 |
$log->flush or die $log->errstr; |
| 794 |
|
| 795 |
errstr() |
| 796 |
|
| 797 |
Call errstr() if you want to get the last error message. This is useful |
| 798 |
if you set die_on_errors to 0 and the handler wouldn't die on failed |
| 799 |
write operations. |
| 800 |
|
| 801 |
use Log::Handler; |
| 802 |
|
| 803 |
my $log = Log::Handler->new(); |
| 804 |
|
| 805 |
$log->add(file => { |
| 806 |
filename => "file.log", |
| 807 |
maxlevel => "info", |
| 808 |
die_on_errors => 0, |
| 809 |
}); |
| 810 |
|
| 811 |
$log->info("Hello World!") or die $log->errstr; |
| 812 |
|
| 813 |
Or |
| 814 |
|
| 815 |
unless ( $log->info("Hello World!") ) { |
| 816 |
$error_string = $log->errstr; |
| 817 |
# do something with $error_string |
| 818 |
} |
| 819 |
|
| 820 |
The exception is that the handler dies in any case if the call of new() |
| 821 |
or add() fails because on missing or wrong settings! |
| 822 |
|
| 823 |
config() |
| 824 |
|
| 825 |
With this method it's possible to load your output configuration from a |
| 826 |
file. |
| 827 |
|
| 828 |
$log->config(config => "file.conf"); |
| 829 |
|
| 830 |
Or |
| 831 |
|
| 832 |
$log->config(config => { |
| 833 |
file => [ |
| 834 |
{ |
| 835 |
alias => "error_log", |
| 836 |
filename => "error.log", |
| 837 |
maxlevel => "warning", |
| 838 |
minlevel => "emerg", |
| 839 |
priority => 1 |
| 840 |
}, |
| 841 |
{ |
| 842 |
alias => "common_log", |
| 843 |
filename => "common.log", |
| 844 |
maxlevel => "info", |
| 845 |
minlevel => "emerg", |
| 846 |
priority => 2 |
| 847 |
}, |
| 848 |
], |
| 849 |
screen => { |
| 850 |
alias => "screen", |
| 851 |
maxlevel => "debug", |
| 852 |
minlevel => "emerg", |
| 853 |
log_to => "STDERR", |
| 854 |
}, |
| 855 |
}); |
| 856 |
|
| 857 |
The key "default" is used here to define default parameters for all |
| 858 |
file outputs. All other keys (error_log, common_log) are used as |
| 859 |
aliases. |
| 860 |
|
| 861 |
Take a look into the documentation of Log::Handler::Config for more |
| 862 |
information. |
| 863 |
|
| 864 |
reload() |
| 865 |
|
| 866 |
With the method reload() it's possible to reload the logging machine. |
| 867 |
Just pass the complete new configuration for all outputs, it works |
| 868 |
exaclty like config(). |
| 869 |
|
| 870 |
At first you should know that it's highly recommended to set a alias |
| 871 |
for each output. If you don't set a alias then the logger doesn't know |
| 872 |
which output-objects to reload. If a output-objects doesn't have a |
| 873 |
alias then the objects will be removed and the new configuration will |
| 874 |
be added. |
| 875 |
|
| 876 |
Example: |
| 877 |
|
| 878 |
logger.conf |
| 879 |
|
| 880 |
<file> |
| 881 |
alias = debug |
| 882 |
filename = debug.log |
| 883 |
maxlevel = debug |
| 884 |
minlevel = emerg |
| 885 |
</file> |
| 886 |
|
| 887 |
<file> |
| 888 |
alias = common |
| 889 |
filename = common.log |
| 890 |
maxlevel = info |
| 891 |
minlevel = emerg |
| 892 |
</file> |
| 893 |
|
| 894 |
Load the configuration |
| 895 |
|
| 896 |
$log->config(config => "logger.conf"); |
| 897 |
|
| 898 |
Now change the configuration in logger.conf |
| 899 |
|
| 900 |
<file> |
| 901 |
alias = common |
| 902 |
filename = common.log |
| 903 |
maxlevel = notice |
| 904 |
minlevel = emerg |
| 905 |
</file> |
| 906 |
|
| 907 |
<sendmail> |
| 908 |
alias = sendmail |
| 909 |
from = [email protected] |
| 910 |
to = [email protected] |
| 911 |
subject = your subject |
| 912 |
</sendmail> |
| 913 |
|
| 914 |
What happends now... |
| 915 |
|
| 916 |
The file-output with the alias debug will be removed, the file-output |
| 917 |
with the alias common will be reloaded and the output with the alias |
| 918 |
sendmail will be added. |
| 919 |
|
| 920 |
If you don't want that output-objects will be removed because they were |
| 921 |
added internal, then you can set the option remove_on_reload to 0. |
| 922 |
|
| 923 |
Example: |
| 924 |
|
| 925 |
$log->config(config => "logger.conf"); |
| 926 |
|
| 927 |
$log->add( |
| 928 |
forward => { |
| 929 |
forward_to => \&my_func, |
| 930 |
remove_on_reload => 0, |
| 931 |
} |
| 932 |
); |
| 933 |
|
| 934 |
The forward-output is not removed after a reload. |
| 935 |
|
| 936 |
validate() |
| 937 |
|
| 938 |
The method validate() expects the same arguments like config() and |
| 939 |
reload(). |
| 940 |
|
| 941 |
Maybe you want to validate your options before you pass them to |
| 942 |
config() or reload(). |
| 943 |
|
| 944 |
Example: |
| 945 |
|
| 946 |
my $log = Log::Handler->new(); |
| 947 |
|
| 948 |
$log->config( config => \%config ); |
| 949 |
|
| 950 |
# and maybe later |
| 951 |
|
| 952 |
if ( $log->validate( config => \%new_config ) ) { |
| 953 |
$log->reload( config => \%new_config ); |
| 954 |
} else { |
| 955 |
warn "unable to reload configuration"; |
| 956 |
warn $log->errstr; |
| 957 |
} |
| 958 |
|
| 959 |
set_pattern() |
| 960 |
|
| 961 |
With this option you can set your own placeholders. Example: |
| 962 |
|
| 963 |
$log->set_pattern("%X", "key_name", sub { "value" }); |
| 964 |
|
| 965 |
# or |
| 966 |
|
| 967 |
$log->set_pattern("%X", "key_name", "value"); |
| 968 |
|
| 969 |
Then you can use this pattern in your message layout: |
| 970 |
|
| 971 |
$log->add(file => { |
| 972 |
filename => "file.log", |
| 973 |
message_layout => "%X %m%N", |
| 974 |
}); |
| 975 |
|
| 976 |
Or use it with message_pattern: |
| 977 |
|
| 978 |
sub func { |
| 979 |
my $m = shift; |
| 980 |
print "$m->{key_name} $m->{message}\n"; |
| 981 |
} |
| 982 |
|
| 983 |
$log->add(forward => { |
| 984 |
forward_to => \&func, |
| 985 |
message_pattern => "%X %m", |
| 986 |
}); |
| 987 |
|
| 988 |
Note: valid character for the key name are: [%\w\-\.]+ |
| 989 |
|
| 990 |
set_level() |
| 991 |
|
| 992 |
With this method it's possible to change the log level at runtime. |
| 993 |
|
| 994 |
To change the log level it's necessary to use a alias - see option |
| 995 |
alias. |
| 996 |
|
| 997 |
$log->set_level( |
| 998 |
$alias => { # option alias |
| 999 |
minlevel => $new_minlevel, |
| 1000 |
maxlevel => $new_maxlevel, |
| 1001 |
} |
| 1002 |
); |
| 1003 |
|
| 1004 |
set_default_param() |
| 1005 |
|
| 1006 |
With this methods it's possible to overwrite the default settings for |
| 1007 |
new outputs. |
| 1008 |
|
| 1009 |
Normally you would do something like |
| 1010 |
|
| 1011 |
$log->add( |
| 1012 |
file => { |
| 1013 |
filename => "debug.log", |
| 1014 |
maxlevel => "info", |
| 1015 |
timeformat => "%b %d %Y %H:%M:%S", |
| 1016 |
message_layout => "[%T] %L %P %t %m (%C)" |
| 1017 |
} |
| 1018 |
); |
| 1019 |
|
| 1020 |
$log->add( |
| 1021 |
file => { |
| 1022 |
filename => "error.log", |
| 1023 |
maxlevel => "error", |
| 1024 |
timeformat => "%b %d %Y %H:%M:%S", |
| 1025 |
message_layout => "[%T] %L %P %t %m (%C)" |
| 1026 |
} |
| 1027 |
); |
| 1028 |
|
| 1029 |
Now you can simplify it with |
| 1030 |
|
| 1031 |
$log->set_default_param( |
| 1032 |
timeformat => "%b %d %Y %H:%M:%S", |
| 1033 |
message_layout => "[%T] %L %P %t %m (%C)" |
| 1034 |
); |
| 1035 |
|
| 1036 |
$logg->add( |
| 1037 |
file => { |
| 1038 |
filename => "debug.log", |
| 1039 |
maxlevel => "info" |
| 1040 |
} |
| 1041 |
); |
| 1042 |
|
| 1043 |
$log->add( |
| 1044 |
file => { |
| 1045 |
filename => "error.log", |
| 1046 |
maxlevel => "error" |
| 1047 |
} |
| 1048 |
); |
| 1049 |
|
| 1050 |
create_logger() |
| 1051 |
|
| 1052 |
create_logger() is the same like new() but it creates a global logger. |
| 1053 |
|
| 1054 |
my $log = Log::Handler->create_logger("myapp"); |
| 1055 |
|
| 1056 |
get_logger() |
| 1057 |
|
| 1058 |
With get_logger() it's possible to get a logger that was created with |
| 1059 |
create_logger() or with |
| 1060 |
|
| 1061 |
use Log::Handler "myapp"; |
| 1062 |
|
| 1063 |
Just call |
| 1064 |
|
| 1065 |
my $log = Log::Handler->get_logger("myapp"); |
| 1066 |
|
| 1067 |
If the logger does not exists then a new logger will be created and |
| 1068 |
returned. |
| 1069 |
|
| 1070 |
exists_logger() |
| 1071 |
|
| 1072 |
With exists_logger() it's possible to check if a logger exists and it |
| 1073 |
returns TRUE or FALSE. |
| 1074 |
|
| 1075 |
EXAMPLES |
| 1076 |
|
| 1077 |
Log::Handler::Examples |
| 1078 |
|
| 1079 |
BENCHMARK |
| 1080 |
|
| 1081 |
The benchmark (examples/benchmark/benchmark.pl) runs on a Intel Core |
| 1082 |
i7-920 with the following result: |
| 1083 |
|
| 1084 |
simple pattern output took : 1 wallclock secs ( 1.26 usr + 0.01 sys = 1.27 CPU) @ 78740.16/s (n=100000) |
| 1085 |
default pattern output took : 2 wallclock secs ( 2.08 usr + 0.15 sys = 2.23 CPU) @ 44843.05/s (n=100000) |
| 1086 |
complex pattern output took : 4 wallclock secs ( 3.22 usr + 0.23 sys = 3.45 CPU) @ 28985.51/s (n=100000) |
| 1087 |
message pattern output took : 3 wallclock secs ( 2.72 usr + 0.16 sys = 2.88 CPU) @ 34722.22/s (n=100000) |
| 1088 |
suppressed output took : 0 wallclock secs ( 0.08 usr + 0.00 sys = 0.08 CPU) @ 1250000.00/s (n=100000) |
| 1089 |
filtered caller output took : 2 wallclock secs ( 2.10 usr + 0.68 sys = 2.78 CPU) @ 35971.22/s (n=100000) |
| 1090 |
suppressed caller output took : 1 wallclock secs ( 0.54 usr + 0.00 sys = 0.54 CPU) @ 185185.19/s (n=100000) |
| 1091 |
filtered messages output took : 3 wallclock secs ( 2.62 usr + 0.08 sys = 2.70 CPU) @ 37037.04/s (n=100000) |
| 1092 |
|
| 1093 |
EXTENSIONS |
| 1094 |
|
| 1095 |
Send me a mail if you have questions. |
| 1096 |
|
| 1097 |
PREREQUISITES |
| 1098 |
|
| 1099 |
Prerequisites for all modules: |
| 1100 |
|
| 1101 |
Carp |
| 1102 |
Data::Dumper |
| 1103 |
Fcntl |
| 1104 |
Params::Validate |
| 1105 |
POSIX |
| 1106 |
Time::HiRes |
| 1107 |
Sys::Hostname |
| 1108 |
UNIVERSAL |
| 1109 |
|
| 1110 |
Recommended modules: |
| 1111 |
|
| 1112 |
Config::General |
| 1113 |
Config::Properties |
| 1114 |
DBI |
| 1115 |
IO::Socket |
| 1116 |
Net::SMTP |
| 1117 |
YAML |
| 1118 |
|
| 1119 |
Just for the test suite: |
| 1120 |
|
| 1121 |
File::Spec |
| 1122 |
Test::More |
| 1123 |
|
| 1124 |
EXPORTS |
| 1125 |
|
| 1126 |
No exports. |
| 1127 |
|
| 1128 |
REPORT BUGS |
| 1129 |
|
| 1130 |
Please report all bugs to <jschulz.cpan(at)bloonix.de>. |
| 1131 |
|
| 1132 |
AUTHOR |
| 1133 |
|
| 1134 |
Jonny Schulz <jschulz.cpan(at)bloonix.de>. |
| 1135 |
|
| 1136 |
QUESTIONS |
| 1137 |
|
| 1138 |
Do you have any questions or ideas? |
| 1139 |
|
| 1140 |
MAIL: <jschulz.cpan(at)bloonix.de> |
| 1141 |
|
| 1142 |
IRC: irc.perl.org#perl |
| 1143 |
|
| 1144 |
If you send me a mail then add Log::Handler into the subject. |
| 1145 |
|
| 1146 |
COPYRIGHT |
| 1147 |
|
| 1148 |
Copyright (C) 2007-2009 by Jonny Schulz. All rights reserved. |
| 1149 |
|
| 1150 |
This program is free software; you can redistribute it and/or modify it |
| 1151 |
under the same terms as Perl itself. |
| 1152 |
|