package Log::ger::Manual::Tutorial::200_LoggingWithLogGer;


# ABSTRACT: Logging with Log::ger



=encoding UTF-8

=head1 NAME

Log::ger::Manual::Tutorial::200_LoggingWithLogGer - Logging with Log::ger

=head1 VERSION

version 0.033.000





As shown in the previous post, logging with Log::ger is as simple as loading
Log::ger with C<use Log::ger> and then using one of the C<log_trace>,
C<log_debug>, C<log_info>, C<log_warn>, C<log_error>, C<log_fatal> statements.
These subroutines are the I<logger> subroutines; there is one for each level.
They accept a string:

 log_warn("This is a log message");

or a sprintf template and one or more arguments (so in other words, it will do
sprintf if there are two or more arguments):

 log_warn("User %s does not exist", $user);
 log_warn("HTTP status code %d, message %s, result structure %s",
          $code, $message, $res);
 log_warn("Subroutine arguments(%s)", \@_);

In sprintf mode, data structure will automatically be dumped and C<undef> will
also be shown as C<< <undef> >>.

In addition to the logger subroutines, there are also the I<level detection>
routines: C<log_is_trace>, C<log_is_debug>, C<log_is_info>, C<log_is_warn>,
C<log_is_error>, C<log_is_fatal>. They are sometimes used if you want to do some
complex logging:

 if (log_is_trace()) {
     require Blah;
     my $x = Blah->new(foo=>1, bar=>2);
     log_trace("Blah is %s", $x->as_string);

=head1 OO-STYLE

Some other logging frameworks use OO-style, where you first request a logger
object then log (or detect level) by calling methods on the logger object. You
can do this with Log::ger too, although I personally do not recommend this
except when wanting to customize category (will be explained in later posts):

 use Log::ger (); # don't import log_* and log_is_* subroutines

 my $log = Log::ger->get_logger;
 if ($log->is_trace) {

The logger methods are named according to the level names (so there is no
C<log_> prefix as in the subroutine version). The level detection methods are
named I<is_LEVEL>, e.g. C<is_trace>, C<is_debug>, and so on.

Why do I prefer procedural style? First, it's shorter to type and you can omit
the parentheses:

 log_warn "blah";

It's more convenient to convert a C<print> or C<printf> statement to logging
statement, or vice versa.

Second, if you use subroutine form it's possible to use
L<Log::ger::Plugin::OptAway> to remove logging statements completely during

=head1 AUTHOR

perlancar <>


This software is copyright (c) 2020, 2019, 2018, 2017 by

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