Rishabh
Rishabh

Reputation: 400

How to identify the cause of logs not getting logged?

I am looking for a checklist of some kind, that would help me in solving the below problem:

I have a webapp running on Tomcat 8. I use Log4J2 v2.1 and Apache Commons Logging v1.2. My applications logs fine for a few days, then abruptly the logging stops. There aren't any exceptions being logged. Nothing!

I am unable to understand why this happens and what could be root cause. Also I don't know how to work towards finding the root cause.

BTW, the system has enough free disk space ~500GB and the tomcat logs, like localhost_access logs and others continue to log uninterrupted.

EDIT: As requested, posting the log4j2 configuration -

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%p [%t] %c{1}.%M(%L) | %m%n"/>
        </Console>

        <RollingFile name="RollingFile" fileName="../logs/app.log"
                                 filePattern="../app/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <Pattern>%d{ISO8601} [REQ_ID|%X{REQUEST_UUID}] %p [%t] %c{1}.%M(%L) | %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <TimeBasedTriggeringPolicy interval="6" modulate="true"/>
                <SizeBasedTriggeringPolicy size="5 MB"/>
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Logger ... />
        ...
        <Root level="debug">
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>
</Configuration>

Upvotes: 0

Views: 835

Answers (1)

Aaron Digulla
Aaron Digulla

Reputation: 328584

The usual culprit here is that the file is renamed by a log rotation tool. If it happens after a few days, then the log rotation tool is configured to move the file away when it has reached a certain size. The tools then often compress the log file.

What happens is:

  1. Java process opens log file. OS allocates a file descriptor.
  2. After some time, the log rotation tool renames the file. Since Java writes to the file descriptor, it doesn't notice or care about the rename (the file descriptor stays the same).
  3. Then the tool compresses the file. For this, a new file (the compressed one) is created.
  4. When the log is compressed, the tool deletes the original log file.
  5. Java continues to write to the file descriptor. Since the descriptor is no longer connected to a directory entry, you can't see it.

Solution: Configure log rotation in your Java logging tool or configure it to work together with the external log rotation tool.

Upvotes: 1

Related Questions