log4j, appenders, different files

I seem to end up doing the same searches on Google each time I want to set up a log4j-configuration; and mostly I want to be able to do the same things each time.

Thought I’d just post an example log4j.properties which does want I want…

  • One dedicated file for DB-logging. replicate for anything you want logged to a specific file.
  • included appender for STDOUT, even though it’s not in use in this particular example
  • TRACE-log-level for my own classes/loggers
  • WARN-levels for everything else. The line with org.apache is redundant.
  • rotation (and deletion) of logfiles
  • Customized log-file-format (ConversionPattern)
  • Only print clasname in default (file) appender.
log4j.rootLogger=WARN, file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern= \
    %d{yyyyddmm HH:mm:ss.SSS} [%t] %-5p %c{1} - %m%n

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=200KB
log4j.appender.file.maxBackupIndex=10
log4j.appender.file.File=kpimon.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= \
    %d{yyyyddmm HH:mm:ss.SSS} [%t] %-5p %c{1} - %m%n

log4j.appender.db=org.apache.log4j.RollingFileAppender
log4j.appender.db.maxFileSize=100KB
log4j.appender.db.maxBackupIndex=10
log4j.appender.db.File=db.log
log4j.appender.db.threshold=debug
log4j.appender.db.layout=org.apache.log4j.PatternLayout
log4j.appender.db.layout.ConversionPattern= \
     %d{yyyyddmm HH:mm:ss.SSS} [%t] %-5p %c - %m%n

log4j.additivity.org.springframework.jdbc=false
log4j.logger.org.springframework.jdbc=DEBUG, db

log4j.logger.org.apache=WARN
log4j.logger.my.package=TRACE

Leave a Reply

Your email address will not be published.
Required fields are marked:*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>