我在这个软件包中包含了mypack和MyClass。我想仅记录此类的INFO级别并尝试使用log4j.properties进行设置:
log4j.debug=true
log4j.rootLogger=ALL, debugLogfile
log4j.rootCategory=, debugLogFile
#log4j.category.mypack =INFO
log4j.logger.mypack =INFO
log4j.appender.debugLogfile=org.apache.log4j.RollingFileAppender
log4j.appender.debugLogfile.File=mylog.log
log4j.appender.debugLogfile.Threshold=ALL
log4j.appender.debugLogfile.MaxFileSize=100MB
log4j.appender.debugLogfile.MaxBackupIndex=4
log4j.appender.debugLogfile.layout=org.apache.log4j.PatternLayout
log4j.appender.debugLogfile.layout.ConversionPattern=%d %-4r [%t] %-5p %c %x - %m%n
不幸的是,类也记录了DEBUG级别。有什么问题?
我以这种方式创建记录器:
public final Logger log = Logger.getLogger(getClass());
答案 0 :(得分:8)
您也可以选择使用XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC
"-//log4j/log4j Configuration//EN" "log4j.dtd" >
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- console -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="ALL" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern"
value="%d{yyyyMMdd-HHmmss.SSS} %-5p (%c.java:%L).%M - %m%n" />
</layout>
</appender>
<!-- categories -->
<category name="org.hibernate">
<priority value="WARN" />
</category>
<category name="org.hibernate.type">
<priority value="TRACE" />
</category>
<category name="mypack">
<priority value="INFO" />
</category>
<!-- root -->
<root>
<priority value="ALL" />
<appender-ref ref="STDOUT" />
</root>
</log4j:configuration>
使用您的设置,文件的追加器:
<!-- file -->
<appender name="ROLLOUT" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="mylog.log" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="4" />
<param name="threshold" value="ALL" />
<param name="encoding" value="UTF-8" />
<layout class="org.apache.log4j.PatternLayout">
<param name="conversionPattern" value="%d %-4r [%t] %-5p %c %x - %m%n" />
</layout>
</appender>
另见:
答案 1 :(得分:3)
您需要在package
或logger
级别设置Category
Logger
访问权限,以便尝试以下操作:
# make default and debugLogFile both as root category
log4j.rootCategory=, debugLogFile
# set package wide logger Level (via Category, older way)
log4j.category.mypack=INFO
# OR set package wide logger Level (via Logger, newer way)
log4j.logger.mypack=INFO
# an example of turning all apache package logs to WARN
log4j.category.org.apache=WARN
答案 2 :(得分:0)
试试
log4j.rootLogger = DEBUG,stdoutdebugLogfile
log4j.logger.mypack = INFO
而不是
log4j.appender.debugLogfile.mypack = INFO
为整个应用设置DEBUG等级,为'mypack'包设置INFO。
答案 3 :(得分:0)
据我记得你可以像这样指定它
log4j.logger.com.proj.eqd.exo.http.filter = INFO
答案 4 :(得分:0)
尝试添加:
log4j.logger.mypack =INFO, debugLogfile
log4j.additivity.mypack = false
log4j.rootLogger=ALL, debugLogfile
您的类正在使用rootLogger的配置以及它自己的配置。通过添加additivity参数,您将避免它。
希望它有所帮助。