使用log4j2,控制台可以正常输出日志,但是log4j2日志文件为空?

时间:2021-08-08 21:51:13

Log4j-2-does-not-write-to-file.md

问题: 使用log4j2, 控制台可以正常输出日志,但是log4j2日志文件为空?

配置文件log4j2.xml配置如下:

<!-- log4j2.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>  
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyTestLog" fileName="MyTestLog.txt" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.log4j.xml" level="debug">
            <AppenderRef ref="MyTestLog"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console" level="error"/>
        </Root>
    </Loggers>
</Configuration>

Java文件名为Log4jDemo.java, 代码如下:

// Log4jDemo.java
import org.apache.logging.log4j.*;

public class Log4jDemo {
    static Logger logger = LogManager.getLogger(Log4jDemo.class.getName());
    public static void main(String[] args) {
        System.out.println("=> Hello Log4jDemo...");
        logger.trace("trace level");
        logger.debug("debug level");
        logger.info("info level");
        logger.warn("warn level");
        logger.error("error level");
        logger.fatal("fatal level");
    }
}

网上搜了好多资料都没能看明白,只看看到这段文字:

It is not writing to your file because the Logger you are using to log is named log4j2Demo.LogDemo but the logger you have configured is named “a.demo.of.log4j2”.
Since the names don’t match it will use the root logger, which you have configured to log to the console.

解决办法1:

重新修改配置文件,将Loggers中Logger的name改成”Log4jDemo”, 与java类文件同名:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>  
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyTestLog" fileName="MyTestLog.txt" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <Logger name="Log4jDemo" level="debug">
            <AppenderRef ref="MyTestLog"/>
        </Logger>
        <Root level="all">
            <AppenderRef ref="Console" level="error"/>
        </Root>
    </Loggers>
</Configuration>

解决办法2:

Loggers里Root下配置两个AppenderRef,一个指向Console,一个指向MyTestLog:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>  
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyTestLog" fileName="MyTestLog.txt" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
    </Appenders>
    <Loggers>
        <Root level="all">
            <AppenderRef ref="Console" level="error"/>
            <AppenderRef ref="MyTestLog" level="info" />
        </Root>
    </Loggers>
</Configuration>

控制台输出:

=> Hello Log4jDemo...
18-01-24 15:57:09.547 [main] ERROR Log4jDemo - error level
18-01-24 15:57:09.547 [main] FATAL Log4jDemo - fatal level

查看日志文件,已经有内容了:

$ tail MyTestLog.txt
18-01-24 15:57:09.544 [main] INFO  Log4jDemo - info level
18-01-24 15:57:09.547 [main] WARN  Log4jDemo - warn level
18-01-24 15:57:09.547 [main] ERROR Log4jDemo - error level
18-01-24 15:57:09.547 [main] FATAL Log4jDemo - fatal level

注意到,控制台和日志文件中的内容并不一样,这是因为我们配置的日志级别不同,Console配置的是error, MyTestLog配置的是info

解决办法3: AsyncAppender with Console and File

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>  
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="MyTestLog" fileName="MyTestLog.txt" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </File>
        <Async name="ASYNC">
            <AppenderRef ref="MyTestLog"/>
            <AppenderRef ref="Console"/>
            </Async>
    </Appenders>
    <Loggers>
        <Root level="all">
            <AppenderRef ref="ASYNC"/>
        </Root>
    </Loggers>
</Configuration>

参考链接

https://logging.apache.org/log4j/2.x/manual/migration.html
https://codedump.io/share/UwYEUh9AtRfg/1/log4j2-output-not-written-to-log-file
https://*.com/questions/25118993/log4j-2-doesnt-write-to-file

[END]