MaikB
MaikB

Reputation: 115

Write into log file while process is running

Probably it is obvious and that's why I can't find an answer to this question...How can I write into my log file WHILE the programm is running. I'm using the log4j-API with the following 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>

The logging itself works fine, I also see my logs in the console, but my log file only gets updated when my programm is terminated. Can please someone tell my why that is the case? I'm dersperate...

Upvotes: 2

Views: 68

Answers (1)

PowerStat
PowerStat

Reputation: 3821

You should set immediateFlush = true on the RollingFile.

See Log4j Appenders

Upvotes: 4

Related Questions