本文介绍了Log4J 2未写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用log4j 2进行一些日志记录.控制台输出正常,但是当我尝试将一些日志写入文件时,我将无法对其进行管理.

I am trying to implement some logging using log4j 2. The console output is ok, but when I try to write some logs into file I cannot manage it.

我的pom.xml:

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

我的log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
    <Properties>
        <Property name="log-path">logs</Property>
        <Property name="archive">${log-path}/archive</Property>
    </Properties>
    <Appenders>
        <Console name="Console-Appender" target="SYSTEM_OUT">
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>>
            </PatternLayout>
        </Console>
        <File name="File" fileName="${log-path}/xmlfilelog.log" immediateFlush="true" >
            <PatternLayout>
                <pattern>
                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                </pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="les.dam" level="trace">
            <AppenderRef ref="File" level="trace"/>
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console-Appender"/>
        </Root>
    </Loggers>
</Configuration>

我通过以下方式使用log4j:

I use log4j the following way:

private static Logger logger = LogManager.getLogger();
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");

我使用记录器的类在les.dam包中.

The class where I use the logger is under les.dam package.

xml配置位于src/main/resources下.

The xml configuration is under src/main/resources.

logs文件夹与源文件夹处于同一级别.

The logs folder is at the same level as source folder.

推荐答案

您必须检查log4j2使用的是您提供的log4j2.xml,而不是默认配置.

You must check that log4j2 is using log4j2.xml which you provide, not default configuration.

  1. 更改log4j2.xml.

然后检查控制台输出.

  1. 更改控制台-Appender的PatternLayout.

然后检查控制台输出.

如果log4j使用log4j2.xml,请检查FileAppender配置 https://logging.apache.org/log4j/2. x/manual/appenders.html#FileAppender .

If log4j is using log4j2.xml, check FileAppender configurationhttps://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender.

如果log4j未使用log4j2.xml,请 https://logging.apache.org/log4j/2.x/manual/configuration.html .-Dlog4j.configurationFile = src/main/resources/log4j2.xml

If log4j is not using log4j2.xml, https://logging.apache.org/log4j/2.x/manual/configuration.html.-Dlog4j.configurationFile=src/main/resources/log4j2.xml

这篇关于Log4J 2未写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:36
查看更多