下面是log4j属性和Java代码。所有日志仅在Debug中记录。我甚至尝试添加阈值,但没有运气。我需要根据日志级别将日志语句记录在单独的文件中。
所有日志仅在调试日志文件中记录,如果我删除调试日志,则会记录信息日志文件。
给予阈值选项也不起作用。
log4j.rootLogger = OFF
#define the console appender
log4j.appender.consoleAppender = org.apache.log4j.ConsoleAppender
# now define the layout for the appender
log4j.appender.consoleAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# now define the rollingFIle appender
log4j.category.com=error, myErrorappender
log4j.additivity.com=false
log4j.appender.myErrorappender=org.apache.log4j.RollingFileAppender
log4j.appender.myErrorappender.File=/Users/uthaman/Documents/workspace/Logging/myErrorlog.log
log4j.appender.myErrorappender.MaxFileSize=2MB
log4j.appender.myErrorappender.MaxBackupIndex=2
log4j.appender.myErrorappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myErrorappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n
log4j.category.com=info,myInfoappender
log4j.additivity.com=false
log4j.appender.myInfoappender=org.apache.log4j.RollingFileAppender
log4j.appender.myInfoappender.File=/Users/user/Documents/workspace/Logging/myInfolog.log
log4j.appender.myInfoappender.MaxFileSize=2MB
log4j.appender.myInfoappender.MaxBackupIndex=2
log4j.appender.myInfoappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myInfoappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n
log4j.category.com=debug,myDebugappender
log4j.additivity.com=false
log4j.appender.myDebugappender=org.apache.log4j.RollingFileAppender
log4j.appender.myDebugappender.File=/Users/user/Documents/workspace/Logging/myDebuglog.log
log4j.appender.myDebugappender.MaxFileSize=2MB
log4j.appender.myDebugappender.MaxBackupIndex=2
log4j.appender.myDebugappender.layout = org.apache.log4j.PatternLayout
log4j.appender.myDebugappender.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c (%F:%L) - %m%n
这是记录日志语句的java代码:
public class Logging {
private static Logger logger = Logger.getLogger(Logging.class);
public static void main(String[] args) {
logger.error("This is error");
logger.info("This is info");
logger.debug("This is debug");
}
}
答案 0 :(得分:1)
我更喜欢使用log4j.xml
配置而不是log4j.properties
。以下配置(基本配置)按预期工作...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="fileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="log.info" />
<param name="datePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true" />
<param name="threshold" value="info"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
</layout>
</appender>
<appender name="fileAppenderDebug" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="log.debug" />
<param name="datePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true" />
<param name="threshold" value="debug"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
</layout>
</appender>
<appender name="fileAppenderError" class="org.apache.log4j.DailyRollingFileAppender">
<param name="file" value="log.error" />
<param name="datePattern" value="'.'yyyy-MM-dd" />
<param name="append" value="true" />
<param name="threshold" value="error"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} - %m%n" />
</layout>
</appender>
<root>
<priority value="debug" />
<appender-ref ref="fileAppender" />
<appender-ref ref="fileAppenderDebug" />
<appender-ref ref="fileAppenderError" />
</root>
</log4j:configuration>
答案 1 :(得分:1)
<强>问题:强>
您已在log4j配置中定义了重复键:
log4j.category.com=error, myErrorappender
log4j.additivity.com=false
log4j.category.com=info,myInfoappender
log4j.additivity.com=false
log4j.category.com=debug,myDebugappender
log4j.additivity.com=false
<强>解决方案:强>
log4j.category.com=debug,myDebugappender,myInfoappender,myErrorappender
log4j.additivity.com=false
log4j.appender.myDebugappender.Threshold=DEBUG
log4j.appender.myInfoappender.Threshold=INFO
log4j.appender.myErrorappender.Threshold=ERROR
如果您必须按精确级别匹配过滤事件:
log4j.appender.myDebugappender.filter=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.myDebugappender.filter.levelToMatch=DEBUG
...
答案 2 :(得分:1)
您可以尝试在配置文件中使用filter
来指定写入specific level
的{{1}}。请附上示例代码
appender