Log4j, a popular logging package for java. The package is distributed
under the Apache Software License, a fully-fledge open source license.
Drawbacks :
slow down an application, If
two verbose.
Advantage : It provides the precise context
about a run of the application. One inserted into code, the generation of
logging output requires no human intervention (involvement). Log output can be
saved in persistent medium to be studied at a later time. Now it was important
component of the development cycle.
It has three main components:
- Logger
- Appender
- Layout
These three type of component work together
to enable developers to log message according to message type and level and to
control at runtime how these message are formatted and where they are reported.
Logger:
Logger is main class which provides
functionality to log information for that purpose it provides several methods.
This class extends Category class. In log4j version 1.2, Category class is
replaced by Logger class as main logging class. So do not use Category.getCategory
to get a logger. This is deprecated.
org.apache.log4j.Logger
class provides methods to get Logger:
Logger(String name) [constructor]
getRootLogger() : Logger
getLogger(String name):Logger
getLogger(Class class) :Logger
To log(register)
message Methods that used by the Logger
is defined its super class Category :
debug(Object Message) : void
info(Object Message) : void
warn(Object Message) : void
error(Object Message) : void
fatal(Object Message) : void
trace(Object Message):void [this method in Logger class]
log(Priority level, Object message) : void
log() is very useful method that provide to
log any type of message by defining message level.
Level is sub
class of Priority which defines message level as a final static constant:
Level.DEBUG
Level.INFO
Level.WARN
Level.ERROR
Level.FATAL
Level.TRACE
Level.ALL
Level.OFF
Level
Description
all
All levels including custom levels
trace (since log4j 1.2.12)
developing only, can be used to follow the
program execution.
debug
developing only, for debugging purpose
info
Production optionally, Course grained
(rarely written informations), I use it to print that a configuration is
initialized, a long running import job is starting and ending.
warn
Production, simple application error or unexpected
behaviour. Application can
continue. I warn for example in case of bad
login attemps, unexpected data during import jobs
error
Production, application error/exception but
application can continue. Part of the application
is probably not working.
Fatal
Production, fatal application error/exception,
application cannot continue, for example
database is down.
no
Do not log at all.
Appender:
Log4j
allow logging results print to multiple destinations, an output destination is
called an appender. An appender specifies
where your log messages are written to. There is a wide choice of appenders
available. All appenders are direct or indirect subclasses of the
AppenderSkeleton.
ConsoleAppender
Logs to console
FileAppender
Logs to a file
SMTPAppender
Logs by email
RollingFileAppender
Logs to a file, starts a new file
once the max size is reached. (An alternative is the DailyRollingFileAppender
which creates on file per day)
But
there are as well:
AsyncAppender,
JDBCAppender, JMSAppender, LF5Appender, NTEventLogAppender,
NullAppender,
NullAppender, SMTPAppender, SocketAppender, SocketHubAppender,
SyslogAppender,
TelnetAppender, DailyRollingFileAppender, RollingFileAppender.
Custom
appenders can be created as well. The log4j download comes with a whole bunch
of
samples
in the examples directory.
Layout:
The
layout specifies how a log message looks like.
DateLayout
HTMLLayout
PatternLayout
SimpleLayout
XMLLayout
SimpleLayout
has no properties to be set. It is simple. We used PatternLayout in our example
and we set a property named ConversionPattern. This property allows us to
define the log output.
Steps:
- First you define the layout.
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
- The pattern layout requires another parameter, i.e.
the pattern.
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE}
%5p %c{1}:%L - %m%n
%d{ABSOLUTE} Date in Format Absolute
%5p %5 defines a right
justified print with 5 characters, p prints the priority of the log message
%c{1}:%L - %m%n And the other settings. Very
simple. They are all explained in the API.
Steps to use log4j:
- Create a Java project.
- Add the log4j.jar to the build path of the project.(add in
project library)
- Create Configuration file (log4j.xml or log4j.properties) in src
folder
- Create a class to getLogger and add log
message
Using properties file to configure log4j
###
direct log messages to stdout using
simple layout###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.rootLogger=debug,
stdout
###
direct log messages to stdout using
pattern layout###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
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
log4j.rootLogger=debug,
stdout
###
file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=test.log
log4j.appender.file.threshold=info
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE}
%5p %c{1}:%L - %m%n
log4j.rootLogger=warn,
file
Log
messages with Level info to fatal to a file and send messages from error to
fatal by email. The
file
should be rolled every 100 KB.
#email
appender
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
#defines
how othen emails are send
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost="smtp.myservername.xx"
log4j.appender.mail.From=fromemail@myservername.xx
log4j.appender.mail.To=toemail@myservername.xx
log4j.appender.mail.Subject=Log
...
log4j.appender.mail.threshold=error
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE}
%5p %c{1}:%L - %m%n
log4j.rootLogger=warn,
mail
You
need mail.jar and activation.jar libraries from J2EE to send emails. Further
properties of the
SmtpAppender
are described above.
Using
xml is best option to configure Log4j
1 Add
log4j.jar file
2 Create
log4j.xml in src folder and Copy the log4j.dtd into the source folder as well.
3 Create a class to getLogger and add log message
< log4j:configuration > is root element
which is used to configure log4j
<appender>
sub element of < log4j:configuration> is used to configure appender
<appender
name=”” and class=””>
<layout>
sub element of <appender> is used to configure layout
<layout
class=””>
<root>sub
element of < log4j:configuration> is used to configure root logger
Examples:-
Using console
appender with simple layout and pattern
layout:-
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender
name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout
class="org.apache.log4j.SimpleLayout"></layout>
</appender>
<root>
<priority
value="debug"></priority>
<appender-ref
ref="stdout"/>
</root>
</log4j:configuration>
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender
name="stdout" class="org.apache.log4j.ConsoleAppender">
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L -
%m%n"/>
</layout>
</appender>
<root>
<priority
value="debug"></priority>
<appender-ref
ref="stdout"/>
</root>
</log4j:configuration>
Using Rolling file appender:
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender
name="file"
class="org.apache.log4j.RollingFileAppender">
<param
name="maxFileSize" value="100KB" />
<param
name="maxBackupIndex" value="5" />
<param
name="File" value="d:/test.log" />
<param
name="threshold" value="info"/>
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L =
%m%n" />
</layout>
</appender>
<root>
<priority
value="debug"></priority>
<appender-ref
ref="file"/>
</root>
</log4j:configuration>
Using Rolling file appender and mail appender together
<?xml
version="1.0" encoding="UTF-8"?>
<!DOCTYPE
log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender
name="file"
class="org.apache.log4j.RollingFileAppender">
<param
name="maxFileSize" value="100KB" />
<param
name="maxBackupIndex" value="5" />
<param
name="File" value="test.log" />
<param
name="threshold" value="info"/>
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern"
value="%d{ABSOLUTE}
%5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender
name="mail" class="org.apache.log4j.net.SMTPAppender">
<param
name="SMTPHost" value="smtp.myservername.xx" />
<param
name="From" value="email@fromemail.xx" />
<param
name="To" value="toemail@toemail.xx" />
<param
name="Subject" value="[LOG] ..." />
<param
name="BufferSize" value="1" />
<param
name="threshold" value="error" />
<layout
class="org.apache.log4j.PatternLayout">
<param
name="ConversionPattern"
value="%d{ABSOLUTE}
%5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority
value="debug"></priority>
<appender-ref
ref="file" />
<appender-ref
ref="mail"/>
</root>
</log4j:configuration>
<root>
<priority
value="debug"></priority>
<appender-ref
ref="file" />
<appender-ref
ref="mail"/>
</root>
</log4j:configuration>