Log::Sprintf - Format strings the way Log::log4perl does, without all the weight
version 0.001003
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.
Log::log4perl
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.
%p{1}
%{1}p
%m{chomp}
%{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.
Log::Sprintf
format - the format to use for logging. See "formats" for what's available.
format
category - what category we are logging to
category
priority - the priority or level we are logging to (trace, debug, etc)
priority
package - the package you are logging from
package
date - the date the log happened
date
file - the file you are logging from
file
host - the host you are logging from
host
line - the line you are logging from
line
subroutine - the subroutine you are logging from
subroutine
pid - the pid you are logging from
pid
priority - the priority (level) you are logging at
milliseconds_since_start - milliseconds since program start
milliseconds_since_start
milliseconds_since_last_log - milliseconds since previous log
milliseconds_since_last_log
stacktrace - full stacktrace
stacktrace
C - "package"
C
c - "category"
c
d - "date", in the format of localtime or gmtime
d
F - "file"
F
H - "host"
H
L - "line"
L
l - "location"
l
M - "subroutine"
M
m - "message"
m
{chomp}m - "message", but with any trailing newline removed
{chomp}m
n - "newline"
n
P - "pid"
P
p - "priority"
p
{1}p - "priority", but just the first character
{1}p
r - "milliseconds_since_start"
r
R - "milliseconds_since_last_log"
R
T - "stacktrace", an arrayref of arrayrefs in the format of caller($x), ordered by deeper to shallower in the trace
T
caller($x)
Takes 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.
message
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.
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")
$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) 2015 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.
To install Log::Sprintf, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Log::Sprintf
CPAN shell
perl -MCPAN -e shell install Log::Sprintf
For more information on module installation, please visit the detailed CPAN module installation guide.