繁体   English   中英

Log4J 2 XML配置未写入文件

[英]Log4J 2 XML configuration not writing to file

我在SpringBoot应用程序中使用Log4J 2。

执行日志记录的类是:

package guru.springframework.blog.log4jproperties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class PropertiesConfigurationDemo {
    private static Logger logger =LogManager.getLogger("PropertiesConfigurationDemo.class");
    public void performSomeTask(){
      logger.debug("This is a debug message");
      logger.info("This is an info message");
      logger.warn("This is a warn message");
      logger.error("This is an error message");
      logger.fatal("This is a fatal message");
   }
}

我的log4j2.xml在src-> main-> resources中。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Properties>
    <Property name="log-path">logs</Property>
</Properties>
<Appenders>
    <File name="A1" fileName="${log-path}/A1.log" >
        <PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>
</Appenders>
<Loggers>
    <Logger name="guru.springframework.blog.log4jproperties" level="debug" additivity="false">
        <AppenderRef ref="A1" level="debug"/>
    </Logger>
    <Root level="info" additivity="false">
        <AppenderRef ref="STDOUT"/>
    </Root>
 </Loggers>
</Configuration>

当我在PropertiesConfigurationDemo上调用performSomeTask()方法时,日志消息将以以下方式发送到控制台:

7:39:12.619 [main] DEBUG PropertiesConfigurationDemo.class - This is a debug message
07:39:12.643 [main] INFO  PropertiesConfigurationDemo.class - This is an info message
07:39:12.647 [main] WARN  PropertiesConfigurationDemo.class - This is a warn message
07:39:12.653 [main] ERROR PropertiesConfigurationDemo.class - This is an error message
07:39:12.655 [main] ERROR PropertiesConfigurationDemo.class - This is a fatal message 

为什么日志消息未发送到文件? 即使将根记录器的AppenderRef设置为A1,消息仍将发送到控制台。

<Root level="info" additivity="false">
<AppenderRef ref="A1"/>
</Root>

看来我的log4j2.xml存在一些问题,或者根本没有被拾起。 任何帮助将由衷地感谢。

顺便说一句,我的MAVEN POM中有以下log4j依赖项。

<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-api</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>org.apache.logging.log4j</groupId>
  <artifactId>log4j-core</artifactId>
  <version>2.5</version>
</dependency>

感谢Simanta

我完成了 问题在于POM依赖性。 我排除了默认的经典logback并添加了log4j2。 这是我修改过的。

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
</dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

我的log4j2.xml在src-> main-> resources中。

<?xml version="1.0" encoding="UTF-8"?>
  <Configuration>
    <Properties>
      <Property name="log-path">logs</Property>
</Properties>
<Appenders>
    <File name="File" fileName="${log-path}/log4j2.log" >
        <PatternLayout>
            <pattern>
              [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>
        </PatternLayout>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout>
            <pattern>
            [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
            </pattern>>
        </PatternLayout>
    </Console>

</Appenders>
<Loggers>
    <Logger name="guru.springframework.blog.log4jproperties"  level="debug"  additivity="false">
        <AppenderRef ref="File"  level="info"/>
        <AppenderRef ref="STDOUT" level="debug"/>
    </Logger>
    <Root level="all">
        <AppenderRef ref="STDOUT"/>
    </Root>
</Loggers>
</Configuration>

在我的应用程序类中,我将Logger检索为。

private static Logger logger = LogManager.getLogger("guru.springframework.blog.log4jproperties");

现在,日志记录正定向到控制台和文件。

暂无
暂无

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

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