
Log::Sprintf - Format strings the way Log::log4perl does, without all the weight

version 0.001002

my $log_formatter = Log::Sprintf->new({
category => 'DeployMethod',
format => '[%L][%p][%c] %m',
});
$log_formatter->sprintf({
line => 123,
package => 'foo',
priority => 'trace',
message => 'starting connect',
});
Or to add or override flags, make a subclass and use it instead:
package SuprLogr;
use base 'Log::Sprintf';
sub codes {
return {
c => 'coxyx',
x => 'xylophone',
}
}
sub coxyx { 'COXYX' }
sub xylophone { 'doink' }
and elsewhere...
my $log_formatter = SuprLogr->new({ format => '[%c][%x] %m' });
$log_formatter->sprintf({ message => 'GOGOGO' });

This module is meant as a mostly drop in replacement for the log formatting system that Log::log4perl uses; it doesn't bring in all of the weight of Log::log4perl and allows you to add new flags in subclasses.

Instead of %p{1} for a single character priority, this uses %{1}p. Similarly, instead of %m{chomp} for a message with a trailing newline removed, this uses %{chomp}m.

my $log_formatter = Log::Sprintf->new({
category => 'WebServer',
format => '[%L][%C] %m',
priority => 'trace',
})
returns a freshly instantiated Log::Sprintf object. Currently it has the following options, none of which are required.
format - the format to use for logging. See "formats" for what's available.category - what category we are logging topriority - the priority or level we are logging to (trace, debug, etc)package - the package you are logging fromdate - the date the log happenedfile - the file you are logging fromhost - the host you are logging fromline - the line you are logging fromsubroutine - the subroutine you are logging frompid - the pid you are logging frompriority - the priority (level) you are logging atmilliseconds_since_start - milliseconds since program startmilliseconds_since_last_log - milliseconds since previous logstacktrace - full stacktraceC - "package"c - "category"d - "date", in the format of localtime or gmtimeF - "file"H - "host"L - "line"l - "location"M - "subroutine"m - "message"{chomp}m - "message", but with any trailing newline removedn - "newline"P - "pid"p - "priority"{1}p - "priority", but just the first characterr - "milliseconds_since_start"R - "milliseconds_since_last_log"T - "stacktrace", an arrayref of arrayrefs in the format of caller($x), ordered by deeper to shallower in the traceTakes the exact same arguments as "new" with the additional message argument. Returns a formatted string. Note that if a flag is included in your format but its corresponding value is not included in the call to sprintf you will get lots of warnings.
Returns the current format

This module was designed from the start to be subclassed. All you need to know to subclass it (to add or change formatting codes) is that the codes subroutine should be defined in your subclass, and should return a hashref where keys are codes and values are the names of methods your class defines to fill in the values of those codes.

returns milliseconds since instantiation
returns milliseconds since last log
returns line
returns file
returns package
returns subroutine
returns category
returns message; if passed "chomp" it will remove a trailing newline from message
returns priority; if passed a true value it will only return the first character
returns date formatted as YYYY-MM-DD HH:MM:SS
returns host
returns location (as in "$subroutine $file:$line")
returns newline
returns process id

this module has a lot of really neat ideas
you can use this module to fill in the values for "sprintf"

Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>

This software is copyright (c) 2012 by Arthur Axel "fREW" Schmidt.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.