use v5.20; use warnings; package Log::Dispatchouli 3.007; # ABSTRACT: a simple wrapper around Log::Dispatch use experimental 'postderef'; # Not dangerous. Is accepted without changed. use Carp (); use File::Spec (); use Log::Dispatch; use Log::Fmt (); use Params::Util qw(_ARRAY0 _HASH0 _CODELIKE); use Scalar::Util qw(blessed weaken); use String::Flogger; use Try::Tiny 0.04; require Log::Dispatchouli::Proxy; our @CARP_NOT = qw(Log::Dispatchouli::Proxy); #pod =head1 SYNOPSIS #pod #pod my $logger = Log::Dispatchouli->new({ #pod ident => 'stuff-purger', #pod facility => 'daemon', #pod to_stdout => $opt->{print}, #pod debug => $opt->{verbose} #pod }); #pod #pod $logger->log([ "There are %s items left to purge...", $stuff_left ]); #pod #pod $logger->log_debug("this is extra often-ignored debugging log"); #pod #pod $logger->log_fatal("Now we will die!!"); #pod #pod =head1 DESCRIPTION #pod #pod Log::Dispatchouli is a thin layer above L and meant to make it #pod dead simple to add logging to a program without having to think much about #pod categories, facilities, levels, or things like that. It is meant to make #pod logging just configurable enough that you can find the logs you want and just #pod easy enough that you will actually log things. #pod #pod Log::Dispatchouli can log to syslog (if you specify a facility), standard error #pod or standard output, to a file, or to an array in memory. That last one is #pod mostly useful for testing. #pod #pod In addition to providing as simple a way to get a handle for logging #pod operations, Log::Dispatchouli uses L to process the things to #pod be logged, meaning you can easily log data structures. Basically: strings are #pod logged as is, arrayrefs are taken as (sprintf format, args), and subroutines #pod are called only if needed. For more information read the L #pod docs. #pod #pod =head1 LOGGER PREFIX #pod #pod Log messages may be prepended with information to set context. This can be set #pod at a logger level or per log item. The simplest example is: #pod #pod my $logger = Log::Dispatchouli->new( ... ); #pod #pod $logger->set_prefix("Batch 123: "); #pod #pod $logger->log("begun processing"); #pod #pod # ... #pod #pod $logger->log("finished processing"); #pod #pod The above will log something like: #pod #pod Batch 123: begun processing #pod Batch 123: finished processing #pod #pod To pass a prefix per-message: #pod #pod $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!') #pod #pod # Logs: Batch 123: Sub-Item 234: error! #pod #pod If the prefix is a string, it is prepended to each line of the message. If it #pod is a coderef, it is called and passed the message to be logged. The return #pod value is logged instead. #pod #pod L also have their own prefix #pod settings, which accumulate. So: #pod #pod my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' }); #pod #pod $proxy->set_prefix('Page 9: '); #pod #pod $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.'); #pod #pod ...will log... #pod #pod Batch 123: Subsystem 12: Page 9: Paragraph 6: Done. #pod #pod =method new #pod #pod my $logger = Log::Dispatchouli->new(\%arg); #pod #pod This returns a new logger, a Log::Dispatchouli object. #pod #pod Valid arguments are: #pod #pod ident - the name of the thing logging (mandatory) #pod to_self - log to the logger object for testing; default: false #pod to_stdout - log to STDOUT; default: false #pod to_stderr - log to STDERR; default: false #pod facility - to which syslog facility to send logs; default: none #pod #pod to_file - log to PROGRAM_NAME.YYYYMMDD in the log path; default: false #pod log_file - a leaf name for the file to log to with to_file #pod log_path - path in which to log to file; defaults to DISPATCHOULI_PATH #pod environment variable or, failing that, to your system's tmpdir #pod #pod file_format - this optional coderef is passed the message to be logged #pod and returns the text to write out #pod #pod log_pid - if true, prefix all log entries with the pid; default: true #pod fail_fatal - a boolean; if true, failure to log is fatal; default: true #pod muted - a boolean; if true, only fatals are logged; default: false #pod debug - a boolean; if true, log_debug method is not a no-op #pod defaults to the truth of the DISPATCHOULI_DEBUG env var #pod quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both #pod fatal log messages will not be logged to these #pod (default: stderr) #pod config_id - a name for this logger's config; rarely needed! #pod syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native" #pod #pod The log path is either F or the value of the F env var. #pod #pod If the F env var is true, we don't log to syslog. #pod #pod =cut sub new { my ($class, $arg) = @_; my $ident = $arg->{ident} or Carp::croak "no ident specified when using $class"; my $config_id = defined $arg->{config_id} ? $arg->{config_id} : $ident; my %quiet_fatal; for ('quiet_fatal') { %quiet_fatal = map {; $_ => 1 } grep { defined } exists $arg->{$_} ? _ARRAY0($arg->{$_}) ? @{ $arg->{$_} } : $arg->{$_} : ('stderr'); }; my $log = Log::Dispatch->new; my $self = bless { dispatcher => $log, log_pid => (exists $arg->{log_pid} ? $arg->{log_pid} : 1), } => $class; if ($arg->{to_file}) { require Log::Dispatch::File; my $log_file = File::Spec->catfile( ($arg->{log_path} || $self->env_value('PATH') || File::Spec->tmpdir), $arg->{log_file} || do { my @time = localtime; sprintf('%s.%04u%02u%02u', $ident, $time[5] + 1900, $time[4] + 1, $time[3]) } ); $log->add( Log::Dispatch::File->new( name => 'logfile', min_level => 'debug', filename => $log_file, mode => 'append', callbacks => do { if (my $format = $arg->{file_format}) { sub { my $message = {@_}->{message}; $message = "[$$] $message" if $self->{log_pid}; $format->($message) }; } else { # The time format returned here is subject to change. -- rjbs, # 2008-11-21 sub { my $message = {@_}->{message}; $message = "[$$] $message" if $self->{log_pid}; (localtime) . " $message\n"; }; } }, ) ); } if ($arg->{facility} and not $self->env_value('NOSYSLOG')) { $self->setup_syslog_output( facility => $arg->{facility}, socket => $arg->{syslog_socket}, ident => $ident, ); } if ($arg->{to_self}) { $self->{events} = []; require Log::Dispatch::Array; $log->add( Log::Dispatch::Array->new( name => 'self', min_level => 'debug', array => $self->{events}, ($self->{log_pid} ? (callbacks => sub { "[$$] ". {@_}->{message} }) : ()) ), ); } $self->{prefix} = $arg->{prefix}; $self->{ident} = $ident; $self->{config_id} = $config_id; DEST: for my $dest (qw(err out)) { next DEST unless $arg->{"to_std$dest"}; my $method = "enable_std$dest"; $self->$method; } $self->{debug} = exists $arg->{debug} ? ($arg->{debug} ? 1 : 0) : ($self->env_value('DEBUG') ? 1 : 0); $self->{muted} = $arg->{muted}; $self->{quiet_fatal} = \%quiet_fatal; $self->{fail_fatal} = exists $arg->{fail_fatal} ? $arg->{fail_fatal} : 1; return $self; } for my $dest (qw(out err)) { my $name = "std$dest"; my $code = sub { return if $_[0]->dispatcher->output($name); my $callback = $_[0]->{log_pid} ? sub { "[$$] " . ({@_}->{message}) . "\n" } : sub { ({@_}->{message}) . "\n" }; $_[0]->dispatcher->add( $_[0]->stdio_dispatcher_class->new( name => "std$dest", min_level => 'debug', stderr => ($dest eq 'err' ? 1 : 0), callbacks => $callback, ($_[0]{quiet_fatal}{"std$dest"} ? (max_level => 'info') : ()), ), ); }; no strict 'refs'; *{"enable_std$dest"} = $code; } sub setup_syslog_output { my ($self, %arg) = @_; require Log::Dispatch::Syslog; $self->{dispatcher}->add( Log::Dispatch::Syslog->new( name => 'syslog', min_level => 'debug', facility => $arg{facility}, ident => $arg{ident}, logopt => ($self->{log_pid} ? 'pid' : ''), socket => $arg{socket} || 'native', callbacks => sub { ( my $m = {@_}->{message} ) =~ s/\n//g; $m }, ), ); } #pod =method log #pod #pod $logger->log(@messages); #pod #pod $logger->log(\%arg, @messages); #pod #pod This method uses L on the input, then I logs #pod the result. Each message is flogged individually, then joined with spaces. #pod #pod If the first argument is a hashref, it will be used as extra arguments to #pod logging. It may include a C entry to preprocess the message by #pod prepending a string (if the prefix is a string) or calling a subroutine to #pod generate a new message (if the prefix is a coderef). #pod #pod =cut sub _join { shift; join q{ }, @{ $_[0] } } sub log { my ($self, @rest) = @_; my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; my $message; if ($arg->{fatal} or ! $self->get_muted) { try { my $flogger = $self->string_flogger; my @flogged = map {; $flogger->flog($_) } @rest; $message = @flogged > 1 ? $self->_join(\@flogged) : $flogged[0]; my @prefix = _ARRAY0($arg->{prefix}) ? @{ $arg->{prefix} } : $arg->{prefix}; for (reverse grep { defined } $self->get_prefix, @prefix) { if (_CODELIKE( $_ )) { $message = $_->($message); } else { $message =~ s/^/$_/gm; } } $self->dispatcher->log( level => $arg->{level} || 'info', message => $message, ); } catch { $message = '(no message could be logged)' unless defined $message; die $_ if $self->{fail_fatal}; }; } Carp::croak $message if $arg->{fatal}; return; } #pod =method log_fatal #pod #pod This behaves like the C method, but will throw the logged string as an #pod exception after logging. #pod #pod This method can also be called as C, to match other popular logging #pod interfaces. B and not C>. #pod #pod =cut sub log_fatal { my ($self, @rest) = @_; my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'error'; local $arg->{fatal} = defined $arg->{fatal} ? $arg->{fatal} : 1; $self->log($arg, @rest); } #pod =method log_debug #pod #pod This behaves like the C method, but will only log (at the debug level) if #pod the logger object has its debug property set to true. #pod #pod This method can also be called as C, to match other popular logging #pod interfaces. B and not C>. #pod #pod =cut sub log_debug { my ($self, @rest) = @_; return unless $self->is_debug; my $arg = _HASH0($rest[0]) ? shift(@rest) : {}; # for future expansion local $arg->{level} = defined $arg->{level} ? $arg->{level} : 'debug'; $self->log($arg, @rest); } #pod =method log_event #pod #pod This method is like C, but is used for structured logging instead of free #pod form text. It's invoked like this: #pod #pod $logger->log($event_type => $data_ref); #pod #pod C<$event_type> should be a simple string, probably a valid identifier, that #pod identifies the kind of event being logged. It is suggested, but not required, #pod that all events of the same type have the same kind of structured data in them. #pod #pod C<$data_ref> is a set of key/value pairs of data to log in this event. It can #pod be an arrayref (in which case the ordering of pairs is preserved) or a hashref #pod (in which case they are sorted by key). #pod #pod The logged string will be in logfmt format, meaning a series of key=value #pod pairs separated by spaces and following these rules: #pod #pod =for :list #pod * an "identifier" is a string of printable ASCII characters between C and #pod C<~>, excluding C<\> and C<=> #pod * keys must be valid identifiers #pod * if a key is empty, C<~> is used instead #pod * if a key contains characters not permitted in an identifier, they are #pod replaced by C #pod * values must I be valid identifiers, or be quoted #pod * quoted value start and end with C<"> #pod * in a quoted value, C<"> becomes C<\">, C<\> becomes C<\\>, newline and #pod carriage return become C<\n> and C<\r> respectively, and other control #pod characters are replaced with C<\u{....}> where the contents of the braces are #pod the hex value of the control character #pod #pod When values are undef, they are represented as C<~>. #pod #pod When values are array references, the index/values are mapped over, so that: #pod #pod key => [ 'a', 'b' ] #pod #pod becomes #pod #pod key.0=a key.1=b #pod #pod When values are hash references, the key/values are mapped, with keys sorted, #pod so that: #pod #pod key => { b => 2, a => 1 } #pod #pod becomes #pod #pod key.a=1 key.b=2 #pod #pod This expansion is performed recursively. If a value itself recurses, #pod appearances of a reference after the first time will be replaced with a string #pod like C<&foo.bar>, pointing to the first occurrence. I It's just here to help you be a little #pod lazy. Don't push the limits. #pod #pod If the value in C<$data_ref> is a code reference, it will be called and its #pod result logged. If its result is also a code reference, you get whatever #pod garbage that code reference stringifies to. #pod #pod If the value in C<$data_ref> is a reference reference, then the referenced #pod scalar will be passed to String::Flogger, and the resulting string will be used #pod as the value to log. That string will be quoted as described above, if needed. #pod #pod =cut sub log_event { my ($self, $type, $data) = @_; return $self->_log_event($type, undef, $data); } sub _compute_proxy_ctx_kvstr_aref { return []; } sub _log_event { my ($self, $type, $ctx, $data) = @_; return if $self->get_muted; my $kv_aref = Log::Fmt->_pairs_to_kvstr_aref([ event => $type, (_ARRAY0($data) ? @$data : $data->%{ sort keys %$data }) ]); splice @$kv_aref, 1, 0, @$ctx if $ctx; $self->dispatcher->log( level => 'info', message => join q{ }, @$kv_aref, ); return; } #pod =method log_debug_event #pod #pod This method is just like C, but will log nothing unless the logger #pod has its C property set to true. #pod #pod =cut sub log_debug_event { my ($self, $type, $data) = @_; return unless $self->get_debug; $self->log_event($type, $data); } #pod =method set_debug #pod #pod $logger->set_debug($bool); #pod #pod This sets the logger's debug property, which affects the behavior of #pod C. #pod #pod =cut sub set_debug { return($_[0]->{debug} = $_[1] ? 1 : 0); } #pod =method get_debug #pod #pod This gets the logger's debug property, which affects the behavior of #pod C. #pod #pod =cut sub get_debug { return $_[0]->{debug} } #pod =method clear_debug #pod #pod This method does nothing, and is only useful for L #pod objects. See L, below. #pod #pod =cut sub clear_debug { } sub mute { $_[0]{muted} = 1 } sub unmute { $_[0]{muted} = 0 } #pod =method set_muted #pod #pod $logger->set_muted($bool); #pod #pod This sets the logger's muted property, which affects the behavior of #pod C. #pod #pod =cut sub set_muted { return($_[0]->{muted} = $_[1] ? 1 : 0); } #pod =method get_muted #pod #pod This gets the logger's muted property, which affects the behavior of #pod C. #pod #pod =cut sub get_muted { return $_[0]->{muted} } #pod =method clear_muted #pod #pod This method does nothing, and is only useful for L #pod objects. See L, below. #pod #pod =cut sub clear_muted { } #pod =method get_prefix #pod #pod my $prefix = $logger->get_prefix; #pod #pod This method returns the currently-set prefix for the logger, which may be a #pod string or code reference or undef. See L. #pod #pod =method set_prefix #pod #pod $logger->set_prefix( $new_prefix ); #pod #pod This method changes the prefix. See L. #pod #pod =method clear_prefix #pod #pod This method clears any set logger prefix. (It can also be called as #pod C, but this is deprecated. See L. #pod #pod =cut sub get_prefix { return $_[0]->{prefix} } sub set_prefix { $_[0]->{prefix} = $_[1] } sub clear_prefix { $_[0]->unset_prefix } sub unset_prefix { undef $_[0]->{prefix} } #pod =method ident #pod #pod This method returns the logger's ident. #pod #pod =cut sub ident { $_[0]{ident} } #pod =method config_id #pod #pod This method returns the logger's configuration id, which defaults to its ident. #pod This can be used to make two loggers equivalent in Log::Dispatchouli::Global so #pod that trying to reinitialize with a new logger with the same C as the #pod current logger will not throw an exception, and will simply do no thing. #pod #pod =cut sub config_id { $_[0]{config_id} } #pod =head1 METHODS FOR SUBCLASSING #pod #pod =head2 string_flogger #pod #pod This method returns the thing on which F will be called to format log #pod messages. By default, it just returns C #pod #pod =cut sub string_flogger { 'String::Flogger' } #pod =head2 env_prefix #pod #pod This method should return a string used as a prefix to find environment #pod variables that affect the logger's behavior. For example, if this method #pod returns C then when checking the environment for a default value for the #pod C parameter, Log::Dispatchouli will first check C, then #pod C. #pod #pod By default, this method returns C<()>, which means no extra environment #pod variable is checked. #pod #pod =cut sub env_prefix { return; } #pod =head2 env_value #pod #pod my $value = $logger->env_value('DEBUG'); #pod #pod This method returns the value for the environment variable suffix given. For #pod example, the example given, calling with C will check #pod C. #pod #pod =cut sub env_value { my ($self, $suffix) = @_; my @path = grep { defined } ($self->env_prefix, 'DISPATCHOULI'); for my $prefix (@path) { my $name = join q{_}, $prefix, $suffix; return $ENV{ $name } if defined $ENV{ $name }; } return; } #pod =head1 METHODS FOR TESTING #pod #pod =head2 new_tester #pod #pod my $logger = Log::Dispatchouli->new_tester( \%arg ); #pod #pod This returns a new logger that logs only C. It's useful in testing. #pod If no C arg is provided, one will be generated. C is off by #pod default, but can be overridden. #pod #pod C<\%arg> is optional. #pod #pod =cut sub new_tester { my ($class, $arg) = @_; $arg ||= {}; return $class->new({ ident => "$$:$0", log_pid => 0, %$arg, to_stderr => 0, to_stdout => 0, to_file => 0, to_self => 1, facility => undef, }); } #pod =head2 events #pod #pod This method returns the arrayref of events logged to an array in memory (in the #pod logger). If the logger is not logging C this raises an exception. #pod #pod =cut sub events { Carp::confess "->events called on a logger not logging to self" unless $_[0]->{events}; return $_[0]->{events}; } #pod =head2 clear_events #pod #pod This method empties the current sequence of events logged into an array in #pod memory. If the logger is not logging C this raises an exception. #pod #pod =cut sub clear_events { Carp::confess "->events called on a logger not logging to self" unless $_[0]->{events}; @{ $_[0]->{events} } = (); return; } #pod =head1 METHODS FOR PROXY LOGGERS #pod #pod =head2 proxy #pod #pod my $proxy_logger = $logger->proxy( \%arg ); #pod #pod This method returns a new proxy logger -- an instance of #pod L -- which will log through the given logger, but #pod which may have some settings localized. #pod #pod C<%arg> is optional. It may contain the following entries: #pod #pod =for :list #pod = proxy_prefix #pod This is a prefix that will be applied to anything the proxy logger logs, and #pod cannot be changed. #pod = proxy_ctx #pod This is data to be inserted in front of event data logged through the proxy. #pod It will appear I the C key but before the logged event data. It #pod can be in the same format as the C<$data_ref> argument to C. #pod = debug #pod This can be set to true or false to change the proxy's "am I in debug mode?" #pod setting. It can be changed or cleared later on the proxy. #pod #pod =cut sub proxy_class { return 'Log::Dispatchouli::Proxy'; } sub proxy { my ($self, $arg) = @_; $arg ||= {}; my $proxy = $self->proxy_class->_new({ parent => $self, logger => $self, proxy_prefix => $arg->{proxy_prefix}, (exists $arg->{debug} ? (debug => ($arg->{debug} ? 1 : 0)) : ()), }); if (my $ctx = $arg->{proxy_ctx}) { $proxy->{proxy_ctx} = _ARRAY0($ctx) ? [ @$ctx ] : [ $ctx->%{ sort keys %$ctx } ]; } return $proxy; } #pod =head2 parent #pod #pod =head2 logger #pod #pod These methods return the logger itself. (They're more useful when called on #pod proxy loggers.) #pod #pod =cut sub parent { $_[0] } sub logger { $_[0] } #pod =method dispatcher #pod #pod This returns the underlying Log::Dispatch object. This is not the method #pod you're looking for. Move along. #pod #pod =cut sub dispatcher { $_[0]->{dispatcher} } #pod =method stdio_dispatcher_class #pod #pod This method is an experimental feature to allow you to pick an alternate #pod dispatch class for stderr and stdio. By default, Log::Dispatch::Screen is #pod used. B #pod #pod =cut sub stdio_dispatcher_class { require Log::Dispatch::Screen; return 'Log::Dispatch::Screen'; } #pod =head1 METHODS FOR API COMPATIBILITY #pod #pod To provide compatibility with some other loggers, most specifically #pod L, the following methods are provided. You should not use #pod these methods without a good reason, and you should never subclass them. #pod Instead, subclass the methods they call. #pod #pod =begin :list #pod #pod = is_debug #pod #pod This method calls C. #pod #pod = is_info #pod #pod = is_fatal #pod #pod These methods return true. #pod #pod = info #pod #pod = fatal #pod #pod = debug #pod #pod These methods redispatch to C, C, and C #pod respectively. #pod #pod =end :list #pod #pod =cut sub is_debug { $_[0]->get_debug } sub is_info { 1 } sub is_fatal { 1 } sub info { shift()->log(@_); } sub fatal { shift()->log_fatal(@_); } sub debug { shift()->log_debug(@_); } use overload '&{}' => sub { my ($self) = @_; sub { $self->log(@_) } }, fallback => 1, ; #pod =head1 SEE ALSO #pod #pod =for :list #pod * L #pod * L #pod #pod =cut 1; __END__ =pod =encoding UTF-8 =head1 NAME Log::Dispatchouli - a simple wrapper around Log::Dispatch =head1 VERSION version 3.007 =head1 SYNOPSIS my $logger = Log::Dispatchouli->new({ ident => 'stuff-purger', facility => 'daemon', to_stdout => $opt->{print}, debug => $opt->{verbose} }); $logger->log([ "There are %s items left to purge...", $stuff_left ]); $logger->log_debug("this is extra often-ignored debugging log"); $logger->log_fatal("Now we will die!!"); =head1 DESCRIPTION Log::Dispatchouli is a thin layer above L and meant to make it dead simple to add logging to a program without having to think much about categories, facilities, levels, or things like that. It is meant to make logging just configurable enough that you can find the logs you want and just easy enough that you will actually log things. Log::Dispatchouli can log to syslog (if you specify a facility), standard error or standard output, to a file, or to an array in memory. That last one is mostly useful for testing. In addition to providing as simple a way to get a handle for logging operations, Log::Dispatchouli uses L to process the things to be logged, meaning you can easily log data structures. Basically: strings are logged as is, arrayrefs are taken as (sprintf format, args), and subroutines are called only if needed. For more information read the L docs. =head1 PERL VERSION This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years. Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl. =head1 METHODS =head2 new my $logger = Log::Dispatchouli->new(\%arg); This returns a new logger, a Log::Dispatchouli object. Valid arguments are: ident - the name of the thing logging (mandatory) to_self - log to the logger object for testing; default: false to_stdout - log to STDOUT; default: false to_stderr - log to STDERR; default: false facility - to which syslog facility to send logs; default: none to_file - log to PROGRAM_NAME.YYYYMMDD in the log path; default: false log_file - a leaf name for the file to log to with to_file log_path - path in which to log to file; defaults to DISPATCHOULI_PATH environment variable or, failing that, to your system's tmpdir file_format - this optional coderef is passed the message to be logged and returns the text to write out log_pid - if true, prefix all log entries with the pid; default: true fail_fatal - a boolean; if true, failure to log is fatal; default: true muted - a boolean; if true, only fatals are logged; default: false debug - a boolean; if true, log_debug method is not a no-op defaults to the truth of the DISPATCHOULI_DEBUG env var quiet_fatal - 'stderr' or 'stdout' or an arrayref of zero, one, or both fatal log messages will not be logged to these (default: stderr) config_id - a name for this logger's config; rarely needed! syslog_socket - a value for Sys::Syslog's "socket" arg; default: "native" The log path is either F or the value of the F env var. If the F env var is true, we don't log to syslog. =head2 log $logger->log(@messages); $logger->log(\%arg, @messages); This method uses L on the input, then I logs the result. Each message is flogged individually, then joined with spaces. If the first argument is a hashref, it will be used as extra arguments to logging. It may include a C entry to preprocess the message by prepending a string (if the prefix is a string) or calling a subroutine to generate a new message (if the prefix is a coderef). =head2 log_fatal This behaves like the C method, but will throw the logged string as an exception after logging. This method can also be called as C, to match other popular logging interfaces. B and not C>. =head2 log_debug This behaves like the C method, but will only log (at the debug level) if the logger object has its debug property set to true. This method can also be called as C, to match other popular logging interfaces. B and not C>. =head2 log_event This method is like C, but is used for structured logging instead of free form text. It's invoked like this: $logger->log($event_type => $data_ref); C<$event_type> should be a simple string, probably a valid identifier, that identifies the kind of event being logged. It is suggested, but not required, that all events of the same type have the same kind of structured data in them. C<$data_ref> is a set of key/value pairs of data to log in this event. It can be an arrayref (in which case the ordering of pairs is preserved) or a hashref (in which case they are sorted by key). The logged string will be in logfmt format, meaning a series of key=value pairs separated by spaces and following these rules: =over 4 =item * an "identifier" is a string of printable ASCII characters between C and C<~>, excluding C<\> and C<=> =item * keys must be valid identifiers =item * if a key is empty, C<~> is used instead =item * if a key contains characters not permitted in an identifier, they are replaced by C =item * values must I be valid identifiers, or be quoted =item * quoted value start and end with C<"> =item * in a quoted value, C<"> becomes C<\">, C<\> becomes C<\\>, newline and carriage return become C<\n> and C<\r> respectively, and other control characters are replaced with C<\u{....}> where the contents of the braces are the hex value of the control character =back When values are undef, they are represented as C<~>. When values are array references, the index/values are mapped over, so that: key => [ 'a', 'b' ] becomes key.0=a key.1=b When values are hash references, the key/values are mapped, with keys sorted, so that: key => { b => 2, a => 1 } becomes key.a=1 key.b=2 This expansion is performed recursively. If a value itself recurses, appearances of a reference after the first time will be replaced with a string like C<&foo.bar>, pointing to the first occurrence. I It's just here to help you be a little lazy. Don't push the limits. If the value in C<$data_ref> is a code reference, it will be called and its result logged. If its result is also a code reference, you get whatever garbage that code reference stringifies to. If the value in C<$data_ref> is a reference reference, then the referenced scalar will be passed to String::Flogger, and the resulting string will be used as the value to log. That string will be quoted as described above, if needed. =head2 log_debug_event This method is just like C, but will log nothing unless the logger has its C property set to true. =head2 set_debug $logger->set_debug($bool); This sets the logger's debug property, which affects the behavior of C. =head2 get_debug This gets the logger's debug property, which affects the behavior of C. =head2 clear_debug This method does nothing, and is only useful for L objects. See L, below. =head2 set_muted $logger->set_muted($bool); This sets the logger's muted property, which affects the behavior of C. =head2 get_muted This gets the logger's muted property, which affects the behavior of C. =head2 clear_muted This method does nothing, and is only useful for L objects. See L, below. =head2 get_prefix my $prefix = $logger->get_prefix; This method returns the currently-set prefix for the logger, which may be a string or code reference or undef. See L. =head2 set_prefix $logger->set_prefix( $new_prefix ); This method changes the prefix. See L. =head2 clear_prefix This method clears any set logger prefix. (It can also be called as C, but this is deprecated. See L. =head2 ident This method returns the logger's ident. =head2 config_id This method returns the logger's configuration id, which defaults to its ident. This can be used to make two loggers equivalent in Log::Dispatchouli::Global so that trying to reinitialize with a new logger with the same C as the current logger will not throw an exception, and will simply do no thing. =head2 dispatcher This returns the underlying Log::Dispatch object. This is not the method you're looking for. Move along. =head2 stdio_dispatcher_class This method is an experimental feature to allow you to pick an alternate dispatch class for stderr and stdio. By default, Log::Dispatch::Screen is used. B =head1 LOGGER PREFIX Log messages may be prepended with information to set context. This can be set at a logger level or per log item. The simplest example is: my $logger = Log::Dispatchouli->new( ... ); $logger->set_prefix("Batch 123: "); $logger->log("begun processing"); # ... $logger->log("finished processing"); The above will log something like: Batch 123: begun processing Batch 123: finished processing To pass a prefix per-message: $logger->log({ prefix => 'Sub-Item 234: ' }, 'error!') # Logs: Batch 123: Sub-Item 234: error! If the prefix is a string, it is prepended to each line of the message. If it is a coderef, it is called and passed the message to be logged. The return value is logged instead. L also have their own prefix settings, which accumulate. So: my $proxy = $logger->proxy({ proxy_prefix => 'Subsystem 12: ' }); $proxy->set_prefix('Page 9: '); $proxy->log({ prefix => 'Paragraph 6: ' }, 'Done.'); ...will log... Batch 123: Subsystem 12: Page 9: Paragraph 6: Done. =head1 METHODS FOR SUBCLASSING =head2 string_flogger This method returns the thing on which F will be called to format log messages. By default, it just returns C =head2 env_prefix This method should return a string used as a prefix to find environment variables that affect the logger's behavior. For example, if this method returns C then when checking the environment for a default value for the C parameter, Log::Dispatchouli will first check C, then C. By default, this method returns C<()>, which means no extra environment variable is checked. =head2 env_value my $value = $logger->env_value('DEBUG'); This method returns the value for the environment variable suffix given. For example, the example given, calling with C will check C. =head1 METHODS FOR TESTING =head2 new_tester my $logger = Log::Dispatchouli->new_tester( \%arg ); This returns a new logger that logs only C. It's useful in testing. If no C arg is provided, one will be generated. C is off by default, but can be overridden. C<\%arg> is optional. =head2 events This method returns the arrayref of events logged to an array in memory (in the logger). If the logger is not logging C this raises an exception. =head2 clear_events This method empties the current sequence of events logged into an array in memory. If the logger is not logging C this raises an exception. =head1 METHODS FOR PROXY LOGGERS =head2 proxy my $proxy_logger = $logger->proxy( \%arg ); This method returns a new proxy logger -- an instance of L -- which will log through the given logger, but which may have some settings localized. C<%arg> is optional. It may contain the following entries: =over 4 =item proxy_prefix This is a prefix that will be applied to anything the proxy logger logs, and cannot be changed. =item proxy_ctx This is data to be inserted in front of event data logged through the proxy. It will appear I the C key but before the logged event data. It can be in the same format as the C<$data_ref> argument to C. =item debug This can be set to true or false to change the proxy's "am I in debug mode?" setting. It can be changed or cleared later on the proxy. =back =head2 parent =head2 logger These methods return the logger itself. (They're more useful when called on proxy loggers.) =head1 METHODS FOR API COMPATIBILITY To provide compatibility with some other loggers, most specifically L, the following methods are provided. You should not use these methods without a good reason, and you should never subclass them. Instead, subclass the methods they call. =over 4 =item is_debug This method calls C. =item is_info =item is_fatal These methods return true. =item info =item fatal =item debug These methods redispatch to C, C, and C respectively. =back =head1 SEE ALSO =over 4 =item * L =item * L =back =head1 AUTHOR Ricardo SIGNES =head1 CONTRIBUTORS =for stopwords Charlie Garrison Christopher J. Madsen Dagfinn Ilmari Mannsåker Dan Book George Hartzell Jon Stuart Matt Phillips Olivier Mengué Randy Stauner Ricardo Signes Sawyer X =over 4 =item * Charlie Garrison =item * Christopher J. Madsen =item * Dagfinn Ilmari Mannsåker =item * Dan Book =item * George Hartzell =item * Jon Stuart =item * Matt Phillips =item * Olivier Mengué =item * Randy Stauner =item * Ricardo Signes =item * Ricardo Signes =item * Sawyer X =back =head1 COPYRIGHT AND LICENSE This software is copyright (c) 2023 by Ricardo SIGNES. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut