基本的配置

来源:互联网 发布:pptv网络电视在线观看 编辑:程序博客网 时间:2024/05/21 21:35

#### 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

#### 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)

#log文件的索引的个数
log4j.appender.R.MaxBackupIndex=1
#设置R的输出格式
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

. 如何让Log4J使用指定的配置文件
在启动你的应用时植入系统属性。例如,可以把上面的log4j.properties文件放到/properties的相对路径下,并改名为log.properties,此时如果让Log4J能够找到这个配置文件并正确地初始化,需要这样运行程序:

D:/../java -Dlog4j.configuration=. /properties/log.properties YourAppName

为什么一定要使用系统属性,而不在配置文件中指定呢?很显然,如果把它写入配置文件,那么,Log4J读到它时已经迟了。
2. 如何查看到Log4J的配置过程
可以类似1中的那样,设置系统属性log4j.debug=true,从而打开Log4J的Verbose模式,此时会输出Log4J的初始化过程,这样就会对Log4J的启动有一个更详细的了解。下面是Log4J启动信息的一个示例:

原创粉丝点击