Poet::Log(3pm)

πŸ“¦
version 0.16 Β· 2 versions
  • 0.16 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.16 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Poet::Log β€” Poet logging. and yes bestie, that's the whole Poet::Log(3pm) page, buckle up πŸŽ€


πŸ¦‹ SYNOPSIS πŸ¦‹

okay so first up, in a conf file:

# In a conf file...
log:
  defaults:
    level: info
    output: poet.log
    layout: "%d{dd/MMM/yyyy:HH:mm:ss.SS} [%p] %c - %m - %F:%L - %P%n"
  class:
    CHI:
      level: debug
      output: chi.log
      layout: "%d{dd/MMM/yyyy:HH:mm:ss.SS} %m - %P%n"
    MyApp::Foo:
      output: stdout

then in a script:

# In a script...
use Poet::Script qw($log);

in a module:

# In a module...
use Poet qw($log);

in a component:

# In a component...
my $log = $m->log;

for an arbitrary category:

# For an arbitrary category...
my $log = Poet::Log->get_logger(category => 'MyApp::Bar');

and then:

# then...
$log->error("an error occurred");

$log->debugf("arguments are: %s", \@_)
    if $log->is_debug();

🌸 DESCRIPTION 🌸

so Poet does its logging with Log::Any and Log::Log4perl, and it gives you simplified configuration for the common case. slay ✨

Log::Any is a logging abstraction β€” it lets CPAN modules log without having to know which logging framework is actually running. it supports the standard logging methods ($log->debug, $log->is_debug) plus the sprintf variants ($log->debugf). obsessed tbh.

Log4perl is a powerful logging package that provides just about any logging-related feature you'd want. its one real drawback? the configuration is kind of a lot, lowkey cumbersome. so Poet gives you a way to configure Log4perl simply through Poet configuration if you just want the common features. no cap 🫢

note bestie: Log4perl is not a strict dependency for Poet. Log messages will simply not get logged until you install it or until you modify logging for your app. πŸ’€

πŸŽ€ CONFIGURATION πŸŽ€

the configs below can go in any Poet conf file, e.g. local.cfg or global/log.cfg. literally anywhere in your conf.

here's a simple configuration that caches everything to logs/poet.log at info level. this is also the default if no configuration is present:

log:
  defaults:
    level: info
    output: poet.log
    layout: %d{dd/MMM/yyyy:HH:mm:ss.SS} [%p] %c - %m - %F:%L - %P%n

and here's a more involved configuration that maintains the same default, but adds several categories that are logged differently:

log:
  defaults:
    level: info
    output: poet.log
    layout: "%d{dd/MMM/yyyy:HH:mm:ss.SS} [%p] %c - %m - %F:%L - %P%n"
  category:
    CHI:
      level: debug
      output: chi.log
      layout: "%d{dd/MMM/yyyy:HH:mm:ss.SS} %m - %P%n"
    MyApp::Foo:
      output: stdout

for the default and for each category, you can specify three different settings:

  • level β€” one of the valid log4perl levels (fatal, error, warn, info, debug, trace)
  • output β€” can be a relative filename (which will be placed in the Poet log directory), an absolute filename, or the special names stdout or stderr
  • layout β€” a valid log4perl PatternLayout string.

if a setting isn't defined for a specific category then it falls back to the default. in this example, MyApp::Foo will inherit the default level and layout. it's giving inheritance, it's giving main character energy πŸ‘‘

notice that we use '::' instead of '.' to specify hierarchical category names, because '.' would interfere with Poet::Conf dot notation. important!! don't be out here using dots bestie πŸ™ˆ

finally, if you must use a full Log4perl configuration file, you can specify it this way:

log:
  log4perl_conf: /path/to/log4perl.conf

✨ USAGE ✨

Obtaining log handle

  • in a script (log category will be 'main'):
use Poet::Script qw($log);
  • in a module MyApp::Foo (log category will be 'MyApp::Foo'):
use Poet qw($log);
  • in a component /foo/bar (log category will be 'Mason::Component::foo::bar'):
my $log = $m->log;
  • manually, for an arbitrary log category:
my $log = Poet::Log->get_logger(category => 'Some::Category');

# or

my $log = MyApp::Log->get_logger(category => 'Some::Category');

Using log handle

$log->error("an error occurred");

$log->debugf("arguments are: %s", \@_)
    if $log->is_debug();

see Log::Any|Log::Any for more details. go read it, it's iconic πŸ“š


🌈 MODIFIABLE METHODS 🌈

these methods are not intended to be called externally, but they may be useful to override or modify with method modifiers in subclasses. their APIs will be kept as stable as possible. promise 🀞

initialize_logging

called once when the Poet environment is initialized. by default, it initializes log4perl with the results of generate_log4perl_config and then calls Log::Any::Adapter->set('Log4perl'). you can modify this to initialize log4perl in your own way, or use a different Log::Any adapter, or use a completely different logging system. your log, your rules πŸ’…

generate_log4perl_config

returns a log4perl config string based on Poet configuration. you can modify this to construct and return your own config. build your own destiny bestie 🌈


πŸ‘€ SEE ALSO πŸ‘€

Poet


🫢 AUTHOR 🫢

Jonathan Swartz <swartz@pobox.com>


this software is copyright (c) 2012 by Jonathan Swartz.

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


and that's a wrap on the Poet::Log(3pm) page, brought to you by perl v5.34.0 on 2022-06-18 β€” thanks for reading, bestie, go log something gorgeous πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from