我正在使用带有log4j的slf4j,并且出于某种原因显示除了我在我的代码中编写的日志之外的所有日志 - Spring日志,Hibernate日志,您将其命名为日志并显示在log4j.properties中配置的级别。然而,我的日志没有显示。
Maven依赖项: log4j的 log4j的 1.2.17 测试
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
与-Dlog4j.debug
一起运行时,我看到正在使用我的log4j.propertes文件
log4j: Trying to find [log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@11a5ee7c.
log4j: Trying to find [log4j.xml] using sun.misc.Launcher$AppClassLoader@11a5ee7c class loader.
log4j: Trying to find [log4j.xml] using ClassLoader.getSystemResource().
log4j: Trying to find [log4j.properties] using context classloader sun.misc.Launcher$AppClassLoader@11a5ee7c.
log4j: Using URL [ile:/C:/x/y/myProject/target/test-classes/log4j.properties] for automatic log4j configuration.
log4j: Reading configuration from URL file:/C:/x/y/myProject/target/test-classes/log4j.properties
我正在使用以下日志代码:
private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);
LOG.warn("Very informative message");
我的log4j.properties文件:
# Define the root logger to the system property "hbase.root.logger".
log4j.rootLogger=DEBUG, console
# Logging Threshold
log4j.threshhold=ALL
#
# console appender
# Add "console" to rootLogger above if you want to use this
#
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %-5p [%t] %C{2}(%L): %m%n
为记录器添加特定行也没有帮助
log4j.logger.com.my.path=DEBUG, console
我可以看到它得到了认可:
Parsing for [com.my.path] with value=[DEBUG, console].
log4j: Level token is [DEBUG].
log4j: Category com.my.path set to DEBUG
但仍然没有记录任何内容。
答案 0 :(得分:0)
替换此行:
log4j.rootLogger=DEBUG, console
用这个:
log4j.rootLogger = ALL, Console
删除这两行:
log4j.threshhold=ALL
log4j.appender.console.target=System.out
不确定所有这种模式是做什么的,所以我会替换这一行
log4j.appender.console.layout.ConversionPattern=%d %-5p [%t] %C{2}(%L): %m%n
用这个:
log4j.appender.Console.layout.conversionPattern=%m%n
然后这样做:
final static Logger logger = Logger.getLogger(MyClassName.class);
logger.info("This is info");