我花了很多天试图用slf4j解决以下日志记录问题:
Class Sample {
....
private static final org.slf4j.Logger A = LoggerFactory.getLogger(Sample.class);
// A = LoggerFactory.getLogger(“FileLogger”)没有区别
private static final Logger B = Logger.getLogger("FileLogger");
...
}
Slf4j记录器A总是向stdout输入,永远不会进入日志文件。
Log4j记录器B按预期工作,在日志文件中生成所有条目。
有没有办法/配置Slf4j logger可以使用相同的appender log4j使用?我无法升级到logback。有人遇到过这个问题吗?
我确保所有必需的具有相同版本的slf4j库都在项目路径中:
log4j xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="fileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="false"/>
<param name="file" value="sample.log"/>
<param name="datePattern" value="'.'yyyy-MM-dd" />
<rollingPolicy class="rollingPolicy class="org.apache.log4j.rolling.FixedWindowRollingPolicy">
<param name="FileNamePattern" value="sample.%i.log.gz"/>
<param name="MaxIndex" value="5"/>
<param name="MinIndex" value="1"/>
</rollingPolicy>
<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy">
<param name="MaxFileSize" value="100"/>
</triggeringPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMMdd HH:mm:ss,SSS} %-5p %c %t" />
</layout>
</appender>
<logger name="FileLogger">
<level value="INFO" />
<appender-ref ref="fileAppender" />
</logger>
<root>
<level value="INFO" />
</root>
</log4j:configuration>