Thursday, July 3, 2008

Logging with log4j : Options and Features


Here are some tricks and options how log4j logger can be configured.
If you are new to Apache Log4j, you should consider this post first... logging-basics-with-log4j

Separating logger.


You may need different log output behavior from different classes.
Suppose your application uses Hibernate and you want all hibernate-generated logs to go in a special file, say "app.hibernate.log". So, what you need is to define separate logger configuration for Hibernate classes.

log4j.rootLogger=DEBUG, A1, F1
...
..
log4j.logger.org.hibernate=DEBUG, H1
log4j.appender.H1=org.apache.log4j.FileAppender
log4j.appender.H1.File=logs/app.hibernate.log
....
..


That is Logger of class org.hibernate.Session , Logger class of org.hibernate.criterion.DetachedCriteria will use the configuration for log4j.logger.org.hibernate.

One more trick... if you don't want to include the hibernate log to be added in root logger, add this in config.
log4j.additivity.org.hibernate=false

Level inheritance of loggers

If logger's level is not specified, it will inherit it's parent logger's level.
if you specify org.hibernate logger like this log4j.logger.org.hibernate will inherit level of log4j.rootLogger
log4j.logger.org.hibernate=, H1
or
log4j.logger.org.hibernate=INHERITED, H1

What is appender?

If logger's level is not specified in the configuration it will inherit it's parent logger's level.

Appender is a component in log4j. Appender decides
  • where to put your log output ( file, console, ..)
    log4j.appender.C1=org.apache.log4j.ConsoleAppender
  • what is the format of your log.
    log4j.appender.C1.layout=org.apache.log4j.PatternLayout
  • to which level log output should be filtered.
    log4j.appender.E1.threshold=ERROR
You can specify these things in logger configuration.
Mind it, A logger can have multiple appenders.

Using file appender

File Appenders are used to store log output in files. There are several file appenders in log4j with different file creation policy and layout . Configuration of file appenders includes file name, file creation policy, output pattern. Here are some common issues with file appenders.

When you don't wanna loose previous log

By default, Every time you restart your program, your previous log file is removed by new log. But if you want your previous log to persist and new logs to be appended in that file, use following configuration.
log4j.appender.F1.Append=true

Manage fixed size log files.

Hmm.. you are not loosing any log, but aren't you creating enormous log file. Don't worry. You can configure appender to split log file when they grow upto a predefined size. For this, you have to use RollingFileAppender instead of FileAppender and define the threshold size of the log file.

log4j.appender.X1=org.apache.log4j.RollingFileAppender
log4j.appender.X1.MaxFileSize=10240KB
log4j.appender.X1.MaxBackupIndex=12
This configuration will create log files of at most 10MB and will not create more than 12 log files.

Manage daily log files

You want your server to create and use new log file every day. Try DailyRollingFileAppender
log4j.appender.F1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.F1.File=logs/server_daily.log
log4j.appender.F1.DatePattern='_'yyyy-MM-dd'.log'


Send mail for error logs

You want your application to notify you when it puts a log of FATAL level? ( you can do it for other levels too). You don't need to write code for sending mail. You can configure SMTPAppender to do this for you.
log4j.appender.E2=org.apache.log4j.net.SMTPAppender
log4j.appender.E2.layout=org.apache.log4j.PatternLayout
log4j.appender.E2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
log4j.appender.E2.SMTPHost=exchange.spectrum-bd.com
log4j.appender.E2.To=rkzico@spectrum-bd.com, ruby@spectrum-bd.com
log4j.appender.E2.From=error@spectrum-bd.com
log4j.appender.E2.threshold=ERROR


Appender Layouts

Other than pattern layout There are some nice Layouts.
An appender with HTMLLayout lets you see your log output in web browser in a table format.
sample.
log4j.rootLogger=DEBUG, htm
...
log4j.appender.htm.layout=org.apache.log4j.HTMLLayout
..


You can have your log to be saved as XML data by using XMLLayout. You can make intelligent use of this XML log information and also see log files in tools like Apache chainsaw.