NAME

Log::ger::Manual::Tutorial::600_Format - Format plugins

VERSION

version 0.040.000

DESCRIPTION

WHAT IS A FORMAT PLUGIN?

Log::ger is flexible; it allows you to customize what arguments you want to pass to the logger routines and how to format them into string (if at all).

The default behaviour of Log::ger is the sprintf-style formatting:

log_warn "single argument, string as-is";
log_warn("more than 1 argument, first arg is sprintf format, rest are arguments: %s %d",
         [data => {structure=>1}], 1234);

Aside from sprintf, data structure arguments are also dumped (using Data::Dmp if available, or Data::Dumper otherwise). Undef arguments are rendered as <undef>.

This default behavior is chosen because back when I was using Log::Any, I find that 90-95% of the time I'm using the sprintf methods (warnf, debugf, and so on).

THE BLOCK FORMAT

Format plugins can change how logger routines process/format the arguments. For example, the Log::ger::Format::Block format plugin causes your logger routines to accept a block as the first argument, and the rest of the arguments will be passed to that block. The block will only be executed if message is to be produced.

log_debug { require Some::Object; my $obj = Some::Object->new; "Object is ".$obj->as_string };
log_info { do_heavy_calculation(@_) }, "arg", "more arg";

This style is suitable if you often do something calculation-heavy to produce the log message. Another logging framework that uses this style is Log::Contextual.

To use this style:

package My::App;
use Log::ger::Format 'Block';
use Log::ger;

log_warn { ... };

When imported, format plugins only affect the calling package.

OTHER FORMAT PLUGINS

Search CPAN for the other available Log::ger::Format::* modules.

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.