Log4j Comprehensive tutorial

Posted: March 1, 2011 in Uncategorized
Tags: , ,

As logging required in every project.  Log4j provided by Apache to add logging to your system in best way.  I also required logging to my application and decided to use Log4j but could find any comprehensive tutorial about logging as somewhere basic were forund and advance use on other site. the way you want to add logging into your application is also vary to normal system sometime. thats why i had to read many tutorial to implement loggin as my requirements. here i will cover applying logging in different and hope it will cover your requirement of logging. here goes my comprehensive tutorial for Log4j.

I will cover following thing in this Post/tutorial

-Simple logging tutorial

- Logging on console and File

- How to manage logging in different files and size

- How to do logging on different file for different package or for different functional functionality withing the application.

let me first introduce you LOG4J

Log4j is a Reliable, Fast and Flexible Logging Framework (APIs) written in Java which is distributed under the Apache Software License.

Log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

Log4j is highly configurable through external configuration files at runtime. It views the logging process in terms of levels of priorities and offers mechanisms to direct logging information to a great variety of destinations, such as a database, file, console, UNIX Syslog etc.

Log4j has three main components:

what you will achieve here

- Console loggign

- File logging

- define multiple log files within application

you can do it by two way.

3.1 -Define multipal log files

- for each log file  define package of the application(mean “first.log”  is for  ”com.mypackage1 and come.mypakage2″  etc)

to implement this functionality yoz need to change log4j propertiues files and in the code intilization of the logger.

log4j.properties file:

# Define the root logger with appender file
log4j.rootLogger = stdout ,FILE, EXPORT

log4j.appender.stdout=org.apache.log4j.ConsoleAppender // Console logging

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

# Define the file appender

log4j.appender.FILE=org.apache.log4j.RollingFileAppender //file logging

log4j.appender.FILE.File=first.log //log file name

log4j.appender.FILE.MaxFileSize=2MB //log file size, if size increase to this then new file will be created

log4j.appender.FILE.MaxBackupIndex=2 //backup will be persist until two files
# Define the layout for file appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.conversionPattern=%m%n

log4j.logger.com.mypackage1=info, EXPORT

log4j.logger.com.mypackage2=info, EXPORT

log4j.logger.com.mypackage1=info, EXPORT
# Define the file appender

log4j.appender.EXPORT=org.apache.log4j.RollingFileAppender

log4j.appender.EXPORT.Threshold=fatal

log4j.appender.EXPORT.File=second.log // second log file

log4j.appender.EXPORT.MaxFileSize=2MB

log4j.appender.EXPORT.MaxBackupIndex=2
# Define the layout for file appender

log4j.appender.EXPORT.layout=org.apache.log4j.PatternLayoutlog4j.appender.EXPORT.layout.conversionPattern=%m%n

to implement logging to specific to package  is marked as RED .

now initialize the logger like this.

static Logger log =Logger.getLogger(“FILE”); for  first log file , use this intialization in the code where you wanted to perform logging in first file

static Logger log =Logger.getLogger(“EXPORT”); for  second log file , use this intialization in the code where you wanted to perform logging in second file

logging statements in the code you dont need to change(except you change the logging level)

3.2- Define different debug level for different files.

- for first.log  define “fatal” logging level like this “log4j.appender.FIRST.Threshold=fatal”

- for second.log  define “info” logging level like this “log4j.appender.SECOND.Threshold=debug”

how it helps, where you wnated to write limited log or related to some functionality use first logger becouse its lowses loggign level so its only print the

log.fatal(…..)  statements log in the file.

for the rest of the application used second logger because its logging level is debug and its cover almost all logging level, therefor its write all log statement of application in log file.

log.debug(…..)

log.info(…..) etc you can check in above colorfull table how much logging level debug covers.

how you can implement in your application.  for this you have to Chane your log4j. properties file, logging initialization and log statement in the code

log4j.properties :

# Define the root logger with appender file
log4j.rootLogger = stdout ,FILE, EXPORT

log4j.appender.stdout=org.apache.log4j.ConsoleAppender // Console logging

log4j.appender.stdout.Target=System.out

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L – %m%n

# Define the file appender

log4j.appender.FILE=org.apache.log4j.RollingFileAppender //file logging

log4j.appender.FILE.File=first.log //log file name

log4j.appender.FILE.MaxFileSize=2MB //log file size, if size increase to this then new file will be created

log4j.appender.FILE.MaxBackupIndex=2 //backup will be persist until two files
# Define the layout for file appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.conversionPattern=%m%n
# Define the file appender

log4j.appender.EXPORT=org.apache.log4j.RollingFileAppender

log4j.appender.EXPORT.Threshold=fatal

log4j.appender.EXPORT.File=second.log // second log file

log4j.appender.EXPORT.MaxFileSize=2MB

log4j.appender.EXPORT.MaxBackupIndex=2
# Define the layout for file appender

log4j.appender.EXPORT.layout=org.apache.log4j.PatternLayoutlog4j.appender.EXPORT.layout.conversionPattern=%m%n

here you can see two log file.

what you have to change to intialize loggign in code. see below

static Logger log =Logger.getLogger(“FILE“); for  first log file , use this intialization in the code where you wanted to perform logging in first file

static Logger log =Logger.getLogger(“EXPORT“); for  second log file , use this intialization in the code where you wanted to perform logging in second file

becouse the second file we have define logging leve is FATAL, functionality in second filespecificthis line to perform logging for some write

log.fatal(“………”);

for the fist logging you can use any statement like log.debug, log.info because first logging level is debug which covers all.

Hope it helps you!!! if you like this post please comment and share with other.

Advertisement
Comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s