
Log::Fine::Formatter::Template - Format log messages using template

Formats log messages for output using a user-defined template spec.
use Log::Fine::Formatter::Template;
use Log::Fine::Handle::Console;
# Instantiate a handle
my $handle = Log::Fine::Handle::Console->new();
# Instantiate a formatter
my $formatter = Log::Fine::Formatter::Template
->new(
name => 'template0',
template => "[%%TIME%%] %%LEVEL%% (%%FILENAME%%:%%LINENO%%) %%MSG%%\n",
timestamp_format => "%y-%m-%d %h:%m:%s"
);
# Set the formatter
$handle->formatter( formatter => $formatter );
# When displaying user or group information, use the effective
# user ID
my $formatter = Log::Fine::Formatter::Template
->new(
name => 'template0',
template => "[%%TIME%%] %%USER%%@%%HOSTNAME%% %%%LEVEL%% %%MSG%%\n",
timestamp_format => "%y-%m-%d %h:%m:%s",
use_effective_id => 1,
);
# Format a msg
my $str = $formatter->format(INFO, "Resistence is futile", 1);
# Create a template with a custom placeholder
my $counter = 0;
# Function that's invoked by the template engine
sub foobar { ++$counter; }
my $formatter = Log::Fine::Formatter::Template
->new(
name => 'template0',
template => "[%%TIME%%] %%LEVEL%% (%%FILENAME%%:%%LINENO%%) (COUNT:%%FOOBAR%%) %%MSG%%\n",
timestamp_format => "%y-%m-%d %h:%m:%s",
custom_placeholders => {
FOOBAR => \&foobar,
});

The template formatter allows the user to specify the log format via a template, using placeholders as substitutions. This provides the user an alternative way of formatting their log messages without the necessity of having to write their own formatter object.
Note that if you desire speed, consider rolling your own Log::Fine::Formatter module.

Placeholders are case-insensitive. %%msg%% will work just as well as %%MSG%%
+---------------+-----------------------------------+
| %%TIME%% | Timestamp |
+---------------+-----------------------------------+
| %%LEVEL%% | Log Level |
+---------------+-----------------------------------+
| %%MSG%% | Log Message |
+---------------+-----------------------------------+
| %%PACKAGE%% | Caller package |
+---------------+-----------------------------------+
| %%FILENAME%% | Caller filename |
+---------------+-----------------------------------+
| %%LINENO%% | Caller line number |
+---------------+-----------------------------------+
| %%SUBROUT%% | Caller Subroutine |
+---------------+-----------------------------------+
| %%HOSTLONG%% | Long Hostname including domain |
+---------------+-----------------------------------+
| %%HOSTSHORT%% | Short Hostname |
+---------------+-----------------------------------+
| %%LOGIN%% | User Login |
+---------------+-----------------------------------+
| %%GROUP%% | User Group |
+---------------+-----------------------------------+

Custom placeholders may be defined as follows:
my $counter = 0;
sub foobar { return ++$counter; } # foobar()
# Define a template formatter with a custom keyword, FOOBAR
my $template = Log::Fine::Formatter::Template
->new(name => 'template2',
template => "[%%TIME%%] %%LEVEL%% (count:%%FOOBAR%%) %%MSG%%\n",
custom_placeholders => {
FOOBAR => \&foobar,
});
Note that $template->{custom_placeholders} is a hash ref with each key representing a new placeholder that points to a function ref. Like regular placeholders, custom placeholders are case-insensitive.

Formats the given message for the given level
Level at which to log (see Log::Fine::Levels)
Message to log
Controls caller skip level
The formatted log message as specified by {template}

Under Microsoft Windows operating systems (WinXP, Win2003, Vista, Win7, etc), Log::Fine::Formatters::Template will use the following environment variables for determining user and group information:
$UID$EUID$GID$EGIDUnder MS Windows, these values will invariably be set to 0.

Please report any bugs or feature requests to bug-log-fine at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Fine. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can find documentation for this module with the perldoc command.
perldoc Log::Fine
You can also look for information at:

Christopher M. Fuhrman, <cfuhrman at panix.com>


Copyright (c) 2010-2011, 2013 Christopher M. Fuhrman, All rights reserved.
This program is free software licensed under the...
The BSD License
The full text of the license can be found in the LICENSE file included with this module.