在进程 运行 时写入日志文件

Write into log file while process is running

可能这很明显,这就是为什么我找不到这个问题的答案...当程序是 运行 时,我如何写入我的日志文件。我将 log4j-API 与以下内容一起使用 log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Properties>
        <Property name="log-path">./logs/</Property>
    </Properties>
    <Appenders>
        <RollingFile name="fileLogger" fileName="${log-path}/test-${date:dd-MM-yyyy}.log"
                 filePattern="${log-path}/test-%d{dd-MM-yyyy}%i.log">
        <PatternLayout>
            <pattern>[%-5level] %d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="100 MB"/>
        </Policies>
        <DefaultRolloverStrategy max="4"/>
    </RollingFile>

    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout pattern="[%-5level] %d{dd-MM-yyyy HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
    </Console>
</Appenders>
<Loggers>
    <Logger name="net.wunds" level="all" additivity="true">
        <appender-ref ref="fileLogger"/>
    </Logger>
</Loggers>

日志记录本身工作正常,我也在控制台中看到我的日志,但我的日志文件只有在我的程序终止时才会更新。可以请有人告诉我为什么会这样吗?我很绝望...

您应该在 RollingFile 上设置 immediateFlush = true。

Log4j Appenders