NAME

Log::ger::Manual::ForLog4perl - Log::ger for Log::Log4perl users

VERSION

version 0.040.000

WHY LOG::LOG4PERL

Log4perl is a port of Log4j, a logging library for Java. It's mature (first release is in 2002, Log4j is even older), featureful, and quite performant. Log4perl influenced other logging libraries in Perl, at least in some aspects, including Log::Any.

You can send logs to multiple outputs, decorate (layout) the message with various extra information, and filter by level and category. Different outputs can be applied different filters. You can define your own logging levels.

For logging in modules, you can avoid specifying level/output and just start logging:

use Log::Log4perl ':easy';

sub foo {
    DEBUG "Entering foo ...";
    ...
}

WHY LOG::GER OVER LOG::LOG4PERL

Log::ger is significantly more lightweight (in terms of lines of code, startup overhead, as well as null/stealth logging overhead) while still having the abovementioned features. See Benchmark::Scenarios::LogGer for more benchmarks. This makes Log::ger more suitable when you want to add logging to modules instead of application as Log::ger creates minimum impact to your module users.

IMO, Log::Log4perl is not very Perlish, which is understandable since it is a port of a Java library. With Perl you can configure and accomplish stuffs using a more compact syntax.

Log::ger allows you to log in a custom format, e.g. using block a la Log::Contextual, log raw data structure as JSON, etc. Note: you can also layout message as JSON in Log4perl using Log::Log4perl::Layout::JSON but your message is still a string.

MIGRATING

Some modules can ease migration. Log::ger::Plugin::Log4perl provides uppercase subroutine names: TRACE, DEBUG, INFO, ERROR, WARN, FATAL like what you get when you "use Log::Log4perl ':easy'" instead of the Log::ger default log_trace(), log_debug(), log_info(), log_warn(), log_error(), log_fatal(). It also provides additional log methods: log(), logdie(), logwarn(), error_warn(), error_die(), logcarp(), logcluck(), logcroak(), logconfess().

The layout module Log::ger::Layout::Pattern uses many of the same placeholder names like %c for category, %C for package/class name, and so on, although some details differ.

FAQ

I want logdie() (and logcroak(), logwarn(), ...)!

Unlike Log4perl, Log::ger (as well as Log::Any and Log::Contextual) by default do not provide logger routines that log + warn|die. Either you do it manually:

log_fatal("Fatal!");
die "Fatal!";

or you can use Log::ger::Plugin::Log4perl that will create LOGDIE, LOGCROAK, LOGWARN et al for you instead of the default names:

use Log::ger::Plugin::Log4perl;
use Log::ger;

# instead of log_warn() now you get WARN et al
WARN "blah ...";
INFO "blah blah ...";

# as well as LOGDIE, LOGCROAK, et al
LOGDIE "Argh...";

or you can also use one or more of Log::ger::Plugin::WithWarn, Log::ger::Plugin::WithDie, Log::ger::Plugin::WithCarp.

LGP:WithWarn will add "*_warn" variants to some of the logger routines (i.e. those at level == 30), so you will get:

log_warn_warn()

which will warn() in addition to log.

LGP:WithDie will add "*_die" variants to some of the logger routines (i.e. those at level 0 < x <= 20), so you will get:

log_error_die()
log_fatal_die()

which will die() in addition to log.

LGP:WithCarp will add Carp variants to some of the logger routines, so you will get:

log_warn_carp()
log_warn_cluck()

log_error_croak()
log_error_confess()
log_fatal_croak()
log_fatal_confess()

which will call the appropriate Carp method in addition to log.

What about contextual logging?

TODO

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2022, 2020, 2019, 2018, 2017 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.