Skip to main content

Posts

Showing posts from August, 2015

How to customize log message using application data in log4Perl using MDC

In log messages it is often need to pass application data which is common to a set of log messages. One example is to log a requestID of an API call which uniquely identifies all log messages belongs to processing a particular API call. One approach is to pass the requestID in all log messages by appending the log message with requestID. Another approach is to use log4Perl::MDC (Mapped Diagnostic Context). Using MDC we can store Thread Specific data and which can be accessed by logger. The thread specific data of a parent thread is accessible to its child thread as well. Below program explains this. use Log::Log4perl; Log::Log4perl::init("log4perl.conf"); method(); sub method { Log::Log4perl::MDC->put("requestId","req00008"); my $logger = Log::Log4perl->get_logger("");   $logger->info("Received a request"); $logger->debug("Received a request"); } Here we have set a Th

How to create a custom log level using log4Perl

This blog explains how to create a auditlog with a custom log level 'AUDIT'. log4perl.rootLogger=TRACE, auditlog   log4perl.filter. AUDITFILTER = Log::Log4perl::Filter::LevelMatch log4perl.filter. AUDITFILTER .LevelToMatch = AUDIT log4perl.filter. AUDITFILTER .AcceptOnMatch = true   log4perl.appender.auditlog=Log::Log4perl::Appender::File log4perl.appender.auditlog.filename=c:/perl/log-dir/audit.log log4perl.appender.auditlog.mode=append log4perl.appender.auditlog.Filter = AUDITFILTER log4perl.appender.auditlog.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.auditlog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n We have created a custom filter AUDITFILTER  which matches the level AUDIT in the appender. Now we need to create a custom level which need to be done in code as below. use Log::Log4perl; Log::Log4perl::Logger::create_custom_level("AUDIT", "DEBUG"); Log::Log4perl::init("log4perl.con

How to redirect different levels to different log files using log4Perl

It is sometimes required in a project to redirect log messages to different files based on their severity. This can be easily done in log4Perl using the below mentioned configuration file entries. log4perl.rootLogger=TRACE, errorlog, consolelog , debuglog   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   log4perl.appender.consolelog=Log::Log4perl::Appender::Screen log4perl.appender.Screen.stderr = 0 log4perl.appender.consolelog.mode=append log4perl.appender.consolelog.layout=Log::Log4perl::Layout::PatternLayout log4perl.appender.consolelog.layout.ConversionPattern=%d{ISO8601}|%C|%m%n   log4perl.appender.debuglog=Log::Log4perl::Appender::File log4perl.

Log::Log4perl - Introduction to logging in Perl

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 This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x86-multi-t hread Copyright 1987-2010, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl".  If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page. C:\perl> If perl is not installed then you can install perl on windows using the below link: http://learn.perl.org/installing/windows.html Next we need to install log4Perl. Open a command prompt and run any one of the below command. C:\perl>ppm in