停止日志消息出现在命令行中 [英] Stop log messages from appearing in the command line
问题描述
我有一个具有以下依赖项的Maven项目,用于记录日志:
I have a maven project with the following dependencies for logging:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
我的属性文件如下:
### Default logger configuration ###
log4j.rootLogger = INFO, fa
log4j.appender.fa = org.apache.log4j.FileAppender
log4j.appender.fa.File = logs/ServerLogs.log
log4j.appender.fa.layout = org.apache.log4j.PatternLayout
log4j.appender.fa.layout.ConversionPattern = %d{ISO8601} %p [%t] %c (%F:%L) - %m%n
### Set Settings for Hibernate Logging ###
log4j.logger.org.hibernate = ERROR
### Set Settings for Spring Framework Logging ###
log4j.logger.org.springframework= ERROR
问题在于,当我执行如下语句时:
The problem is that when I execute a statement like the following :
logger.info(首次初始化数据库连接");
logger.info("Initializing DB connection for the first time");
此日志消息写在我在属性文件中指定的文件中,但它也附加在命令行中.如何禁用此行为?我希望消息仅在文件中打印,而不在命令行中打印.
This log message is written in the file that I specified in the properties file but it is also appended in the command line. How can I disable this behavior ? I want the message to printed only in the file , not in the command line .
有什么想法吗?
推荐答案
我发现这完全禁用了log4j:
I found out that this disables the log4j completely:
org.apache.log4j.LogManager.resetConfiguration();
org.apache.log4j.LogManager.getRootLogger().addAppender(new NullAppender());
如果您仍然想要日志记录但定向到文件,则可以执行以下操作:
If you still want the logging but directed to a file you can do this:
LogManager.resetConfiguration();
LogManager.getRootLogger().addAppender(new FileAppender(new PatternLayout(), "log.log"));
这篇关于停止日志消息出现在命令行中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!