简体   繁体   English

Log4j2 RollingFileAppender 不将日志写入文件

[英]Log4j2 RollingFileAppender not writing logs to file

I have the following log4j2 config in log4j2.xml which is not writing logs to file.我在log4j2.xml中有以下 log4j2 配置,它没有将日志写入文件。 I'm using windows 10. What am I doing wrong?我正在使用 windows 10. 我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <RollingFile name="RollingFileAppender" fileName="/var/log/abc/test.log"
filePattern="/var/log/abc/test-%d{yyyy-MM-dd}-%i.log" append="true">
            <PatternLayout pattern="%d{DEFAULT} %-5p %c - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="10 MB" />
            </Policies>
            <DefaultRolloverStrategy max="31"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="Info">
            <AppenderRef ref="RollingFileAppender"/>
        </Root>
    </Loggers>
</Configuration>

Following are the dependencies that I'm using:以下是我正在使用的依赖项:

compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.14.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.14.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.2'

Getting the following in my logs:在我的日志中获取以下内容:

log4j:WARN No appenders could be found for logger (com.xyz.pqr).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Sample code:示例代码:

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public final class MyUtil { 
      private static Logger logger = LoggerFactory.getLogger(MyUtil.class); 

      if (condition == true) {
          logger.debug("Condition is true."); 
          return false;
      }
}

Am I missing something?我错过了什么吗?

These messages这些消息

log4j:WARN No appenders could be found for logger (com.xyz.pqr).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

indicate that log4j 1 is present and something is trying to use it.表明 log4j 1 存在并且正在尝试使用它。 To resolve that you need to remove the log4j 1.x dependency and replace it with Log4j2's log4j-1.2-api dependency.要解决此问题,您需要删除 log4j 1.x 依赖项并将其替换为 Log4j2 的 log4j-1.2-api 依赖项。

Your sample code indicates that you are using the SLF4J API.您的示例代码表明您使用的是 SLF4J API。 To bind that with Log4j2 you have to include either log4j-slf4j-impl or log4j-slf4j18-impl depending on the version of SLF4J you are using.要将其与 Log4j2 绑定,您必须包含 log4j-slf4j-impl 或 log4j-slf4j18-impl,具体取决于您使用的 SLF4J 版本。

Once you have those dependencies resolved if you still don't see logging add -Dlog4j2.debug=true to the command line.一旦解决了这些依赖关系,如果您仍然看不到日志记录,请将 -Dlog4j2.debug=true 添加到命令行。 This will log Log4j's initialization to wherever System.out is directed.这会将 Log4j 的初始化记录到 System.out 指向的任何位置。 If you don't see anything then nothing is calling it.如果你什么也没看到,那就什么都没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM