DESCRIPTION

The "new()" method creates a new PatternLayout, specifying its log
format. The format
string can contain a number of placeholders which will be
replaced by the logging engine when it's time to log the message:

%c Category of the logging event.
%C Fully qualified package (or class) name of the caller
%d Current date in yyyy/MM/dd hh:mm:ss format
%d{...} Current date in customized format (see below)
%F File where the logging event occurred
%H Hostname (if Sys::Hostname is available)
%l Fully qualified name of the calling method followed by the
callers source the file name and line number between
parentheses.
%L Line number within the file where the log statement was issued
%m The message to be logged
%m{chomp} Log message, stripped off a trailing newline
%m{indent} Log message, multi-lines indented so they line up with first
%m{indent=n} Log message, multi-lines indented by n spaces
%M Method or function where the logging request was issued
%n Newline (OS-independent)
%p Priority of the logging event (%p{1} shows the first letter)
%P pid of the current process
%r Number of milliseconds elapsed from program start to logging
event
%R Number of milliseconds elapsed from last logging event to
current logging event
%T A stack trace of functions called
%x The topmost NDC (see below)
%X{key} The entry 'key' of the MDC (see below)
%% A literal percent (%) sign

NDC

and

MDC

are explained in ``Nested Diagnostic Context (

NDC

)'' in Log::Log4perl
and ``Mapped Diagnostic Context (

MDC

)'' in Log::Log4perl.

The granularity of time values is milliseconds if Time::HiRes is available.
If not, only full seconds are used.

Every once in a while, someone uses the ``%m%n'' pattern and
additionally provides an extra newline in the log message (e.g.
"->log("message\n")". To avoid printing an extra newline in
this case, the PatternLayout will chomp the message, printing only
one newline. This option can be controlled by PatternLayout's
"message_chomp_before_newline" option. See ``Advanced options''
for details.

Quantify placeholders

All placeholders can be extended with formatting instructions,
just like in printf:

%20c Reserve 20 chars for the category, right-justify and fill
with blanks if it is shorter
%-20c Same as %20c, but left-justify and fill the right side
with blanks
%09r Zero-pad the number of milliseconds to 9 digits
%.8c Specify the maximum field with and have the formatter
cut off the rest of the value

Fine-tuning with curlies

Some placeholders have special functions defined if you add curlies
with content after them:

%c{1} Just show the right-most category component, useful in large
class hierarchies (Foo::Baz::Bar -> Bar)
%c{2} Just show the two right most category components
(Foo::Baz::Bar -> Baz::Bar)
%F Display source file including full path
%F{1} Just display filename
%F{2} Display filename and last path component (dir/test.log)
%F{3} Display filename and last two path components (d1/d2/test.log)
%M Display fully qualified method/function name
%M{1} Just display method name (foo)
%M{2} Display method name and last path component (main::foo)

In this way, you're able to shrink the displayed category or
limit file/path components to save space in your logs.

Fine-tune the date

If you're not happy with the default %d format for the date which
looks like

For an exhaustive list of all supported date features, look at
Log::Log4perl::DateFormat.

Custom cspecs

First of all, ``cspecs'' is short for ``conversion specifiers'', which is
the log4j and the printf(3) term for what Mike is calling ``placeholders.''
I suggested ``cspecs'' for this part of the api before I saw that Mike was
using ``placeholders'' consistently in the log4perl documentation. Ah, the
joys of collaboration ;=) --kg

If the existing corpus of placeholders/cspecs isn't good enough for you,
you can easily roll your own:

When the log message is being put together, your anonymous sub
will be called with these arguments:

($layout, $message, $category, $priority, $caller_level);
layout: the PatternLayout object that called it
message: the logging message (%m)
category: e.g. groceries.beverages.adult.beer.schlitz
priority: e.g. DEBUG|WARN|INFO|ERROR|FATAL
caller_level: how many levels back up the call stack you have
to go to find the caller

Please note that the subroutines you're defining in this way are going
to be run in the "main" namespace, so be sure to fully qualify functions
and variables if they're located in different packages. Also make sure
these subroutines aren't using Log4perl, otherwise Log4perl will enter
an infinite recursion.

With Log4perl 1.20 and better, cspecs can be written with parameters in
curly braces. Writing something like

will cause the cspec function defined for %U to be called twice, once
with the parameter 'user' and then again with the parameter 'id',
and the placeholders in the cspec string will be replaced with
the respective return values.

The parameter value is available in the 'curlies' entry of the first
parameter passed to the subroutine (the layout object reference).
So, if you wanted to map %U{xxx} to entries in the

This feature means arbitrary perl code can be embedded in the config file.
In the rare case where the people who have access to your config file are
different from the people who write your code and shouldn't have execute
rights, you might want to set

$Log::Log4perl::Config->allow_code(0);

before you call init(). Alternatively you can supply a restricted set of
Perl opcodes that can be embedded in the config file as described in
``Restricting what Opcodes can be in a Perl Hook'' in Log::Log4perl.

Advanced Options

The constructor of the "Log::Log4perl::Layout::PatternLayout" class
takes an optional hash reference as a first argument to specify
additional options in order to (ab)use it in creative ways:

Takes a reference to a function returning the time for the time/date
fields, either in seconds
since the epoch or as an array, carrying seconds and
microseconds, just like "Time::HiRes::gettimeofday" does.

message_chomp_before_newline

If a layout contains the pattern ``%m%n'' and the message ends with a newline,
PatternLayout will chomp the message, to prevent printing two newlines.
If this is not desired, and you want two newlines in this case,
the feature can be turned off by setting the
"message_chomp_before_newline" option to a false value:

Getting rid of newlines

If your code contains logging statements like

# WRONG, don't do that!
$logger->debug("Some message\n");

then it's usually best to strip the newlines from these calls. As explained
in ``Logging newlines'' in Log::Log4perl, logging statements should never contain
newlines, but rely on appender layouts to add necessary newlines instead.

If changing the code is not an option, use the special PatternLayout
placeholder %m{chomp} to refer to the message excluding a trailing
newline:

log4perl.appender.App.layout.ConversionPattern = %d %m{chomp}%n

This will add a single newline to every message, regardless if it
complies with the Log4perl newline guidelines or not (thanks to
Tim Bunce for this idea).

Multi Lines

If a log message consists of several lines, like

$logger->debug("line1\nline2\nline3");

then by default, they get logged like this (assuming the the layout is
set to ``%d>%m%n''):

# layout %d>%m%n
2014/07/27 12:46:16>line1
line2
line3

If you'd rather have the messages aligned like

# layout %d>%m{indent}%n
2014/07/27 12:46:16>line1
line2
line3

then use the %m{indent} option for the %m specifier. This option
can also take a fixed value, as in %m{indent=2}, which indents
subsequent lines by two spaces:

# layout %d>%m{indent=2}%n
2014/07/27 12:46:16>line1
line2
line3

Note that you can still add the "chomp" option for the %m specifier
in this case (see above what it does), simply add it after a
separating comma, like in %m{indent=2,chomp}.