Skip to main content

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-thread
Copyright 1987-2010, Larry Wall
Perl may be copied only under the terms of either the Artistic License or theGNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found onthis system using "man perl" or "perldoc perl".  If you have access to theInternet, 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:

Next we need to install log4Perl. Open a command prompt and run any one of the below command.

C:\perl>ppm install Log-Log4perl
or
C:\perl>cpan Log::Log4perl

Once the installation is successful then we are ready to start a test program using log4Perl logging.

testperl.pl

 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::File 
This 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

Popular posts from this blog

How to create a minikube single node cluster for learning Kubernetes

In this post I will explain how to setup a minikube single node kubernetes cluster using AWS EC2 instance which would help anyone who is trying to learn kubernetes and also help them to gain practical knowledge in kubernetes by running kubernetes commands, creating kubernetes objects etc. Minikube is a single node kubernetes cluster which means a kubernetes cluster with only one node that is a single VM. Minikube is only used for learning purposes and it is not an alternative for a real kubernetes cluster and should not be used for development and production usage. In this example I have launched an AWS EC2 instance with below configuration where I will install minikube and related tools. AWS EC2 Instance Configuration AMI: Ubuntu Free tier eligible 64 bit Instance type : t2-large ( For me t2-small or t2-micro is giving performance issues due to less memory) Once the EC2 instance is up and running, login to the instance using below command on terminal. If you are using wi...

log4j - How to write log to multiple log files using log4j.properties

In Java applications some times you may need to write your log messages to specific log files with its own specific log properties. If you are using log4j internally then first step that you need to do is to have a proper log4j.properties file. Below example shows 2 log4j appenders which write to 2 different log files, one is a debug log and another one is a reports log. Debug log file can have all log messages and reports log can have log messages specific to reporting on say splunk monitoring. # Root logger option log4j.rootLogger=ALL,STDOUT,debugLog log4j.logger.reportsLogger=INFO,reportsLog log4j.additivity.reportsLogger=false     log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout log4j.appender.STDOUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %C:%L - %m%n     # Direct log messages to a log file log4j.appender.debugLog=org.apache.log4j.RollingFileAppen...

Create SNMP Client in JAVA Using SNMP4j

Please follow the below mentioned steps to write a very simple SNMP4J application using Java and eclipse IDE. First you need to download a copy of SNMP4J.jar. You can get it from http://www.snmp4j.org/html/download.html Then you need to decide how you are going to test your application. Check whether you have access to any real network devices like Cisco routers or Juniper routers or whether you are aware of any SNMP agent running on any of your servers.There is nothing to worry if you cannot find one , you can start SNMP agent on your desktop machine as well. Here I am giving an example of how you can use your Windows machine to test your application by starting the SNMP service on the machine. I am using a Windows XP machine and below are the steps needed to start SNMP agent on the machine. 1. Go to Start->Control Panel -> Add or Remove Programs -> Add or Remove Windows Components      Select or Check "Management and Monitoring Tools". ...