Pregunta

I sorely miss information on the log level of every line in Log4perl's easy_init. I'm running a large migration script which fails at some special records, so I need to grep for WARNings and ERRORs, but need the INFOrmation context to fix those.

Using this code:

#!/usr/bin/perl
use strict; use warnings;
use Log::Log4perl qw(:easy);

Log::Log4perl->easy_init($INFO);

TRACE "bar";
DEBUG "baz";
INFO  "qux";
WARN  "quux";
ERROR "corge";
FATAL "grault";

Right now, I get this output:

2013/06/02 13:08:48 qux
2013/06/02 13:08:48 quux
2013/06/02 13:08:48 corge
2013/06/02 13:08:48 grault

I do not want to add this information in the log message, as it's already included in which log function I call.

How to add the log level with easy_init? I do not want to initialize the "regular" Log4perl with a configuration file (even if included inline) in a rather small script and I like easy_init for its clear and brief usage.

¿Fue útil?

Solución

easy_init also accepts hashes as parameters, which enable extended configuration, for example log files and the layout. %-5p will print the log level, left-aligned to five characters.

#!/usr/bin/perl
use strict; use warnings;
use Log::Log4perl qw(:easy);

Log::Log4perl->easy_init({
    level   => $INFO,
    layout  => '%d %-5p %m%n'
});

TRACE "bar";
DEBUG "baz";
INFO  "qux";
WARN  "quux";
ERROR "corge";
FATAL "grault";

Output will now be

2013/06/02 13:08:56 INFO  qux
2013/06/02 13:08:56 WARN  quux
2013/06/02 13:08:56 ERROR corge
2013/06/02 13:08:56 FATAL grault

For further placeholders, have a look at Log4perl's PatternLayout documentation.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top