getting a name of module or script which logs messages using log4perl

83 Views Asked by At

If a particular module is logging using log4perl then is there any way to get the name of that module using log4perl internally. or message is logged from srcipt(.pl) can we get the name of script also using log4perl only apart from using caller or any function which doesnt use log4perl function explicitly.

1

There are 1 best solutions below

3
simbabque On

For classes or packages, you can do that. It's documented with Log::Log4perl::Layout::PatternLayout. In your log pattern, use one of the following.

%C Fully qualified package (or class) name of the caller

And for the name of a script:

%F File where the logging event occurred

These ones are also useful:

%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

The documentation of Log::Log4perl::Layout::PatternLayout itself has even more details.

Here's an example of how to use this with a config file.

log4perl.appender.LOGFILE.layout=PatternLayout
log4perl.appender.LOGFILE.layout.ConversionPattern=[%r] %F %L %c - %m%n