User Tools

Site Tools


java:log4j

Example of configuration of log4j

I've read excelent article of Ceki Gülcü about Log for Java and I've did my configuration file, just for the sake of keep the basic configuration tips handy:

# 
# log4j.properties - log for java configuration file 
#
# See complete example at http://www.supermanhamuerto.com/doku.php?id=java:log4j
 
 
# log levels
#
# if you put here that "any messages above info will  
# be displayed", what are we talking about???
#
# DEBUG < INFO < WARN < ERROR < FATAL
#
# we are talking that only DEBUG messages will be omitted
#
 
#
#
# LOGGERS
#
#
 
# root logger
# 
# the root logger is the father of all the loggers in log4j
# If you create a logger called "com.foo" and don't provide 
# any configuration, this logger will take the configuration 
# the root logger 
 
# specify that the root logger will be send messages 
# to the "console" appender and the "A1" appender
# Think of appenders as printers where log messages 
# will be printed
#
# See section "appenders" for the configuration of these 
# appenders
#
log4j.rootLogger = INFO, console, A1
 
# let's say we have created a log called "com.supermanhamuerto"
# if we do nothing, all the logging messages will be trated 
# with the "rootLogger" configuration. 
# BUT we can make a proper configuration for them: 
# 
log4j.logger.com.supermanhamuerto = DEBUG, my_own_appender
 
# some common filter for well known
# classes
log4j.logger.org.springframework=ERROR
log4j.logger.org.hibernate=ERROR
log4j.logger.aop.framework=ERROR
log4j.logger.loader=ERROR
 
# and configure the appender "my_own_appender" accordingly. 
# or just leave: 
#
# log4j.com.supermanhamuerto = WARN
#
# to avoid INFO messages from this class
 
#
#
# APPENDERS
#
#
 
# see at the end the list of available appenders
 
# console appender 
# 
# here we will define what will be do the console appender 
# In our case, it will vomit the messages directly into the 
# console
log4j.appender.console = org.apache.log4j.ConsoleAppender
 
# set a layout for this appender (see at botton list of available layouts)
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d %-5p %-40c{3} - %m%n
 
# A1 appender
#
# in this case, we will output he content to a file appender 
log4j.appender.A1 = org.apache.log4j.FileAppender
log4j.appender.A1.file = c\:/temp/filename.log
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d %-5p %-40c{3} - %m%n
 
 
#
# to output in HTML format:
#
# log4j.appender.A1 = org.apache.HTMLAppender
# log4j.appender.A1.file = /home/user/public_html/log.html
# log4j.appender.A1.layout = HTMLLayout
# log4j.appender.A1.layout.title = Hello, this is the title of the page
# 
 
#
# Available appenders
#
# org.apache.log4j.ConsoleAppender
# org.apache.log4j.DailyRollingFileAppender
# org.apache.log4j.FileAppender
# org.apache.log4j.RollingFileAppender
# org.apache.log4j.WriterAppender
#
 
 
#
# Available layouts
#
# org.apache.log4j.PatternLayout
# org.apache.log4j.HTMLLayout
# org.apache.log4j.TTCCLayout
#

Specification of appenders

An example of a rolling file appender

This appender is called “A1” as the previous example. It will roll the log file if it becomes greater than 1Mb in size and every 10 files, it will delete the older, keeping a reasonable size of logging information.

#
# Rolling Appender: when the log file surpass 1Mb, it will "roll"
# and store the content in a file filename.log.1..10. When it 
# comes to 10 files, it will delete the oldest and create a new 
# one, saving space on disk
#
log4j.appender.A1 = org.apache.log4j.RollingFileAppender
log4j.appender.A1.file = PUT-HERE-THE-FILENAME
log4j.appender.A1.maxFileSize = 1MB
log4j.appender.A1.maxBackupIndex = 10
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %d{MM/dd HH:mm:ss} %-40c{3} - %m%n

Working in an SMTP appender

http://www.adictosaltrabajo.com/tutoriales/tutoriales.php?pagina=smtpLog4j

log4j.logger.SOME.CLASS = INFO, smtp
 
#
# SMTP Appender: in certain ocassions it is good to have this implemented: 
# it will send an email with the log information. You can use this appender
# to alert you if certain errors appear in the log inmediatly.
#
log4j.appender.smtp = org.apache.log4j.net.SMTPAppender
# The BufferSize option takes a positive integer representing the maximum 
# number of logging events to collect in a cyclic buffer
log4j.appender.smtp.BufferSize = 100
# The EvaluatorClass option takes a string value representing the name of 
# the class implementing the TriggeringEventEvaluator interface.
# http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/TriggeringEventEvaluator.html
#log4j.appender.smtp.EvaluatorClass = PENDING
log4j.appender.smtp.From = example@example.com
log4j.appender.smtp.To = example@example.com
log4j.appender.smtp.Subject = Error en log de blah, blah
log4j.appender.smtp.LocationInfo = false
log4j.appender.smtp.SmtpUsername =
log4j.appender.smtp.SmtpPassword = 
log4j.appender.smtp.SmtpDebug = false
log4j.appender.smtp.SMTPHost = smtp.example.com
log4j.appender.smtp.layout = org.apache.log4j.PatternLayout
log4j.appender.smtp.layout.ConversionPattern = %d{MM/dd HH:mm:ss} %-40c{3} - %m%n

Specification of Layouts

java/log4j.txt · Last modified: 2014/12/24 10:42 (external edit)