Log4J 2 not writing into file

偶尔善良 提交于 2020-02-08 04:14:25

问题


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.

My 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>

My 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>

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");

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

The xml configuration is under src/main/resources.

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


回答1:


Your configuration looks fine. One possible cause is that the configuration file is not found and the default configuration (only errors to the console).

To verify, change your configuration to start with <Configuration status="trace" monitorInterval="60">. This will print internal log4j2 debug logging to the console.

Is your log4j2.xml configuration file located in src/main/resources? - update: thanks for the update, so that's a yes, that's good.


Can you try configuring the file appender with an absolute path first?

Can you show the full package name of the class doing the logging?




回答2:


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

  1. Change log4j2.xml.

Configuration status="trace" monitorInterval="60"

Then check the console output.

  1. Change Console-Appender's PatternLayout.

%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n

Then check the console output.

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

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



来源:https://stackoverflow.com/questions/42045361/log4j-2-not-writing-into-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!