
Log::Info - Single interface for log output

use Log::Info qw( :DEFAULT :log_levels :default_channels );
Log (CHAN_INFO, LOG_ERR, "A fatal error occurred");
Logf (CHAN_INFO, LOG_INFO, "Loading file: %s", $filename);
Log::Info::add_sink (CHAN_STATS, 'stats-file', 'FILE',
LOG_INFO,
{ fn => "$ENV{HOME}/stats",
maxsize => 10 * 1024**2, # 1M,
});
Log::Info::add_sink (CHAN_DEBUG, 'stderr', 'FH', LOG_INFO,
{ fh => *STDERR{IO} })
if $opt_debug;
Log::Info::set_sink_out_level (CHAN_INFO, SINK_STDERR, LOG_INFO);
Log::Info::add_channel ('MYLOG', $fh);
Log::Info::set_channel_out_level ('MYLOG', LOG_WARNING);
Log::Info::add_sink ('MYLOG', 'mysink', 'FILE', LOG_ERR,
{ fn => '/tmp/mylog' });
Log ('MYLOG', LOG_INFO, 'I got to here...');
Log::Info::delete_sink ('MYLOG', 'outf');
Log::Info::delete_channel ('MYLOG');

Log::Info is intended to be a single interface for all logging action. Each instance of Log::Info is intended to be an output for a particular type of log; some defaults are provided, and custom ones may be generated.
Log::Info exports functions Log and Logf by default.
Log::Info is a package, not a class. There is only one logging mechanism in a running program; this is considered to be a good thing. Log::Info knows of channels, which have sinks; channels are named log facilities, whilst sinks are the output points.
The idea is that modules each log their messages to some (set of) channels, each channel representing some type of message (general information, statistics, progress, etc.), and the controlling script just sets the output levels and directions of the sinks according to configuration. Thus, the communication between the script and the modules is somewhat simplified.
Under these circumstances, the module need only call Log/Logf directly, and whether it is used as part of a daemon process logging to syslog, or a standalone unit dumping errors to stderr, the choices are made purely by the calling script.
The only thing left to decide is policy, regarding what messages are sent to which channel, and at what level. The module enforces no policy, but does attempt to provide a start point in a set of default channels, and a little suggested guidance on the use of levels within those channels. This is intended to be helpful; any suggestions to enhance these to be more so are welcomed by the author.
For those wishing to use a different set of policies for whatever reason, channel creation, etc. are all completely available to the user.
All items are exported on request, except where noted
Not really an export, but a pragma. Add handlers to warn(), die(), to log messages to the log system.
The die handler logs the message to CHAN_INFO at LOG_ERR. The die message is still propogated up the call stack, so will typically appear on stderr. If CHAN_INFO is directed to stderr, then the error message will appear twice.
The warn handler logs the message to CHAN_INFO at LOG_WARNING.
This also traps Carp messages, as long as this is installed after Carp --- so do the use Carp before the use Log::Info qw( :trap );
Exported by default
Exported by default

The following constants are available for use as arguments to the level attribute of the Log or Logf call (listed in descending order). The constants are stolen shamelessly from syslog, and all are guranteed to be valid levels for a SYSLOG-type sink. All of these constants will be imported inidividually on request, or grouped together with the :log_levels tag.
system is unusable
action must be taken immediately
critical conditions
error conditions
warning conditions
normal, but significant, condition
informational message
debug-level message
The following constants are available for use as arguments to the facility attribute of the SYSLOG sink type. All of these constants will be imported inidividually on request, or grouped together with the :syslog_facilities tag.
Each of the following channels exist by default, and have their channel level set to undef. Only CHAN_INFO has a sink by default; called SINK_STDERR (a name exported with the :default_channels tag), which is a filehandle to STDERR, and is set at level LOG_WARNING.
Each channel and sink name will be exported upon request, or together using the :default_channels tag.
Intended for progress reports, e.g., done 1 of 3 files, or 20% through.
Default level: LOG_WARNING
Intended for debugging messages, such as those you might output with --debug flag on.
Default level: LOG_WARNING
Intended for output of statistical information; e.g., found 300 items or output file is 30M, parsing took 79s.
Default level: LOG_WARNING
Intended for warning and error messages, and those that would be output by -v.
Messages that would be used with warn should be logged at level LOG_WARNING, those for a -v flag with level LOG_INFO (and LOG_DEBUG|"LOG_DEBUG" for increased verbosity).
die messages should be logged at LOG_ERR|"LOG_ERR" level. LOG_EMERG should be reserved for conditions detected which have a significant, time-critical effect on the operating system as a whole (e.g., anything which will cause the operating system to hang or crash).
LOG_ALERT should be used for conditions which may affect the correct operation of the operating system, but will not cause the system to fail (e.g., detected filesystem faults).
LOG_CRIT should be used to indicate that some problem has been identified that is likely to adversely affect the correct operation of a system (other than the operating system) of which this program is a part, not including that this program is going to fail. An example of this is an error in a shared configuration file.
LOG_NOTICE should be used for abnormal, but not worrying conditions. For example, if a grep-like program might log a message for each file read at level LOG_INFO, but log at LOG_NOTICE files which it has not permissions to read.
Here is a fine kettle of fish.
This sink sniffs its filehandle (upon sink creation), and if it smells like a TTY, it uses it as a progress bar. Otherwise, it just sets up a file/filehandle sink as usual.
In progress-bar mode, incoming messages are examined. If they look like
m!\[([\d_,.]+/[\d_,.]+|[\d_,.]+%)(\s+[^]]*)?\s+Done\]!
Then that is treated as progress information, and the bar updated accordingly.
Default translator units provided for communal edification.
(UDT => "Un*x-Date-Time"). Prefix each message with the date and time, first in Un*x (seconds since Jan 1, 1970) format, then as the scalar gmtime output. gmtime is deliberately chosen to avoid weirdness over, say, daylight-savings time changes.

Create a new channel.
chan is not already a channel name $chan =~ /^[\w-]+$/;
delete an existing channel. Implicitly deletes all attached sinks.
chan is an existing channel name
name of channel to delete
Channel name to test for
Whether the name channel is known to Log::Info
set output cutoff level on channel
Add a translator to a channel.
The channel to add the translator to.
The translator to add. The translator will be called in order after any previously added translators, and will be given the results of the log string having been through those translators. The results of the translation provided by this translator will be passed to any translators installed after this one, and to any sink-specific translators.
$chan is an existing channel name $sink =~ /^[\w-]+$/;
channel to add sink to
name of sink
sink type as string. See params for acceptable types.
Output cutoff level. Set to 'undef' to accept any messages accepted by the channel. This level is checked after the channel level; therefore, if this level is higher than the channel level, it will have no effect.
A hashref of type-specific parameters. Recognized keys are type specific:
Output to file. If the file exists, it will be appended to. Each message (call to Log) will be newline-terminated. Keys are:
Output to filehandle. Creation of, and closing of, the filehandle are the responsibility of the client. Do not delete the filehandle without closing the sink first. Each message (call to Log) will be newline-terminated. Keys are:
Filehandle to output to. May be an IO handle (*foo{IO}), a glob ref, a glob, or an instance of IO::Handle.
Callback subroutine. Keys are:
Log to syslog service. Any LOG_X value provided by this module is a valid syslog level; any level that is provided that is not valid for syslog is rounded down to the nearest value. Any level that is less than all valid values is defaulted to LOG_EMERG. The message is logged with the basename of the running script, and pid.
Due to an artifact of Sys::Syslog, messages have a space appended when they appear in the log.
Keys are:
Optional; facility to pass to syslog to log messages under. Valid values are the FTY_ constants.
Remove a sink from a channel.
set output cutoff level on channel
Add a translator to a channel sink.
The channel to add the translator to.
The sink to add the translator to.
The translator to add. The translator will be called in order after any previously added (sink-specific) translators, all of which are called after any channel translators, and will be given the results of the log string having been through those translators. The results of the translation provided by this translator will be passed to any (sink-specific) translators installed after this one.

log a message
channel to log to
message log level. Only if the log level is equal to or less than the channel log level will it be logged. For each sink, if the sink also has a level, the message will be logged to that sink only if the message level is equal to or below the sink level as well as the channel level.
The string to log. Do not append a line terminator; the sinks will do so themselves if necessary.
As for Log
As for Log
As for "sprintf" in sprintf.
As for "sprintf" in sprintf.

Add handlers to warn(), die(), to log messages to the log system. Any existing handlers are invoked after those added.
The die handler logs the message to CHAN_INFO at LOG_ERR. The warn handler logs the message to CHAN_INFO at LOG_WARNING.
This also traps Carp messages.
None
Set up output channel (for string based command-line options).
name of the channel to log to.
value of option presented by user. If this option looks like a simple number, it is treated as a log level (see below). If this option looks like a simple file name (m!^[A-Za-z0-9_.\\/-]+$), it will be treated as an output file (but output with the 'FH' type, so no auto-rotate, and special files will work). If this option looks like m!^:\d+!, the numeric value will be treated as a file descriptor, and output sent there. If this value is defined, but a blank string, then output will be sent to stderr.
If a value of the form \+\d+ precedes a file descriptor, or succeeds a filename, then the numeric value is used to set the log level of the output sink. If not set, it defaults to LOG_INFO, which is equivalent to +1. Hence, +0 is equivalent to LOG_INFO - 1.
If this value is not defined, then no action is taken (this is to allow compatibility with options processors, where a value is left undefined if its option is never invoked).
If this value is defined but empty (''), then the log level is set to LOG_INFO (first time), and the output sent to STDERR. If the option is seen again, still with an empty string value, and with the same channel & sink names, then the log level is increased one place. This is to allow -v -v -v(or -vvv)-style options.
name of the option invoked (used for error messages).
the name of the sink to create.
Optional If true, generate a sink with SINK_TERM_PROGRESS


%m strings will get expanded to $! in SYSLOG sinks; this is a bug, and may get fixed at any time.
Email the author.

Martyn J. Pearce fluffy@cpan.org

Copyright (c) 2001, 2002, 2003, 2005 Martyn J. Pearce. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
