所有的配置工作应该在log4j.properties文件中完成,而该文件一般须放在应用程序的相同的目录中。
在日志系统使用之前,我们必须首先配置log4j.配置log4j意味着增加附件器到Category并且为每一个Category设置一个Layout。
category之间是有继承关系,但他们增加到log4j.properties文件中的顺序是不固定的。
示例一:
#############################################################
# 设置log4j的根category所使用的预设层次是DEBUG,而只使用A1这个附件器.
log4j.rootCategory=DEBUG, A1
#附件器A1被设置为控制台附件器。
log4j.appender.A1=org.apache.log4j.ConsoleAppender
#附件器使用的布局是PatternLayout,即模式布局
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
#附件器A1的模式是%-4r [%t] %-5p %c %x - %m%n,其中%m代表消息字符串,%n代表换行符,其它以%开头的字符代表的含义如下文。
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
#########################################################
示例二:
#########################################################
#### Use two appenders, one to log to console, another to log to a file
log4j.rootCategory=debug, stdout, R
# Print only messages of priority WARN or higher for your category
log4j.category.your.category.name=WARN
# Specifically inherit the priority level
#log4j.category.your.category.name=INHERITED
#### First appender writes to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#### Second appender writes to a file
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
########################################################

