logo

log4j.properties explained part 1

log4j.properties explained - levels of logging, appenders - Console, File and RollingFile appenders.

First you must define level of logging. On one hand if you set level to ALL, you will log everything, on the other hand if you set level to OFF, you will turn off logging completely. Next you must define appenders. They define what will happen with a logging information. ConsoleAppender will print log to standard output. FileAppender will save log to a file. RollingFileAppender will save log to a file and will split this file if it reaches certain size. And much more. You can name your appender any way you like.

ConsoleAppender:

log4j.rootLogger=DEBUG, stdout
# Direct log messages to stdout
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 %5p %c:%L - %m%n

FileAppender:

log4j.rootLogger=DEBUG, file
# Log messages in the admin.log file.
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=admin.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern= %-4r [%t] %-5p %c %x - %m%n

RollingFileAppender:

log4j.rootLogger=DEBUG, rfile
# Log messages in the logs/applog.log file. Max size will be 100KB
log4j.appender.rfile = org.apache.log4j.RollingFileAppender
log4j.appender.rfile.File = logs/applog.html
log4j.appender.rfile.MaxFileSize = 100KB
log4j.appender.rfile.Append = true
log4j.appender.rfile.layout = org.apache.log4j.HTMLLayout


comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search