This article will take you to logging in Perl using Log4perl in windows.
First check whether perl is installed on your machine. You can do this by running the following command on command prompt.
C:\perl>perl -v
log4Perl which is similar to log4J in Java uses two different approaches for configuration. One is using a configuration file and another is using Perl code. I personally prefer configuration approach because it is much cleaner and configuration changes would not need a code change.
Create log4perl.conf and place it in the same folder as the code. You can ofcourse mention the full path to the configuration file if the path of the file is somewhere else.
log4perl.conf
Run the program using following command.
C:\perl>perl testperl.pl
Which will print INFO log to the log file mentioned.
Below is the explanation of configuration parameters.
log4perl.appender.errorlog.filename=c:/perl/log-dir/error.log
log file name
log4perl.appender.errorlog.mode=append
Indicates that the log messages will be written to the end of the file if file is present
log4perl.appender.errorlog.Threshold = INFO
This appender will log all messages which are equal to INFO or more specific than INFO. In other words TRACE or DEBUG messages are not written to the file. This is a way to direct log messages to different files based on their levels.
log4perl.appender.errorlog.layout=Log::Log4perl::Layout::PatternLayout
Indicates that log messages are following a specific pattern which is specified using ConversionPattern.
log4perl.appender.errorlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
Specifies the pattern of the log messages.
Details can be found in : http://search.cpan.org/~mschilli/Log-Log4perl-1.46/lib/Log/Log4perl.pm
Above is a basic log4Perl configuration. Each project may require different configurations and below I am trying to provide some more advanced configurations.
How to redirect different levels to different log files using log4Perl: Here
How to create a custom log level using log4Perl:Here
How to customize log message using application data in log4Perl using MDC:Here
First check whether perl is installed on your machine. You can do this by running the following command on command prompt.
C:\perl>perl -v
use Log::Log4perl;
Log::Log4perl::init("log4perl.conf");
method();
sub method {
my $logger = Log::Log4perl->get_logger("");
$logger->info("Received a request");
$logger->debug("Received a request");
}
log4Perl which is similar to log4J in Java uses two different approaches for configuration. One is using a configuration file and another is using Perl code. I personally prefer configuration approach because it is much cleaner and configuration changes would not need a code change.
Create log4perl.conf and place it in the same folder as the code. You can ofcourse mention the full path to the configuration file if the path of the file is somewhere else.
log4perl.conf
log4perl.rootLogger=TRACE, errorlog
log4perl.appender.errorlog=Log::Log4perl::Appender::File
log4perl.appender.errorlog.filename=c:/perl/log-dir/error.log
log4perl.appender.errorlog.mode=append
log4perl.appender.errorlog.Threshold = INFO
log4perl.appender.errorlog.layout=Log::Log4perl::Layout::PatternLayout
log4perl.appender.errorlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
Run the program using following command.
C:\perl>perl testperl.pl
Which will print INFO log to the log file mentioned.
Below is the explanation of configuration parameters.
log4perl.rootLogger=TRACE, errorlog
This indicates that the rootLogger which is the main or upper level logger accepts
all log messages which are equal to TRACE or more specific than TRACE. In other words it accepts
all log messages.
errorlog - indicates it is an appender
log4perl.appender.errorlog=Log::Log4perl::Appender::FileThis indicates that this appender is going to write the logs to a File.
log4perl.appender.errorlog.filename=c:/perl/log-dir/error.log
log file name
log4perl.appender.errorlog.mode=append
Indicates that the log messages will be written to the end of the file if file is present
log4perl.appender.errorlog.Threshold = INFO
This appender will log all messages which are equal to INFO or more specific than INFO. In other words TRACE or DEBUG messages are not written to the file. This is a way to direct log messages to different files based on their levels.
log4perl.appender.errorlog.layout=Log::Log4perl::Layout::PatternLayout
Indicates that log messages are following a specific pattern which is specified using ConversionPattern.
log4perl.appender.errorlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n
Specifies the pattern of the log messages.
Details can be found in : http://search.cpan.org/~mschilli/Log-Log4perl-1.46/lib/Log/Log4perl.pm
Above is a basic log4Perl configuration. Each project may require different configurations and below I am trying to provide some more advanced configurations.
How to redirect different levels to different log files using log4Perl: Here
How to create a custom log level using log4Perl:Here
How to customize log message using application data in log4Perl using MDC:Here
Comments
Post a Comment