wikimix
wikimix

Reputation: 467

Rolling Logs in LIFERAY DXP

I'm facing an issue while trying to customise Log rolling strategy in Liferay DXP. In portal-log4j-ext.xml file , I wanted to :

Above the portal-log4j-ext.xml :

<appender name="PROJECT" class="org.apache.log4j.RollingFileAppender">
    <!-- The active file to log to -->
    <param name="file" value="@liferay.home@/logs/logging/[email protected]@.%d.log" />
    <param name="MaxFileSize" value="5KB"/>
    <param name="MaxBackupIndex" value="100"/>
    <param name="append" value="true" />
    <param name="encoding" value="UTF-8" />

    <!-- Keep one backup file -->
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern" value="@liferay.home@/logs/logging/archive/[email protected]@.%d.log.gz" />
    </rollingPolicy>

    <layout class="org.apache.log4j.EnhancedPatternLayout">
        <param name="ConversionPattern" value="[%d{dd.MM.yyyy HH:mm:ss}{Europe/Paris}] %-5p [%t][%c{1}:%L] %m%n" />
    </layout>
</appender>

<root>
    <priority value="INFO" />
    <appender-ref ref="PROJECT" />
    <!--appender-ref ref="CONSOLE" /-->
    <!--appender-ref ref="XML_FILE" /-->
    <!--<appender-ref ref="TEXT_FILE" />-->
</root>

Here the generated file logs:

enter image description here

The problem is that the date is not appended to generated files, and TimeBasedRollingPolicy is not working.

I'm using Log4j 1.2

Do you have any suggestion?

Thanks in advance.

Upvotes: 1

Views: 1277

Answers (2)

Victor
Victor

Reputation: 3698

You portal-log4j-ext.xml is missing components in the hierarchy:

log4j:configuration < rollingPolicy < appender Like in:

<appender class="org.apache.log4j.rolling.RollingFileAppender" name="TEXT_FILE">
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
    </rollingPolicy>

    <layout class="org.apache.log4j.EnhancedPatternLayout">
    </layout>
</appender>

Example from: https://www.e-systems.tech/blog/-/blogs/customizing-portal-log4j-xml

A second detail is that you seem to be creating a new appender, you cannot do that from -ext.xml, at least not without reconfiguring the loggers from .xml to use it. In practice, this is the job of the original file not the ext.

Upvotes: 0

qutax
qutax

Reputation: 898

Reconsider your Strategy

Before I write down the answer: you should reconsider your logging strategy. You will not have "the daily log file" based on your current configuration because there could be up to 100 daily log files, each with a size of 5KB.

So in the worst case you could have 100 files containing logs of the same day with another n files missing because they would have exceded your MaxBackupIndex.

FileAppender

If you have very limited storage, you should use the org.apache.log4j.RollingFileAppender with MaxFileSize and MaxBackupIndex. However you cannot define a DatePattern here.

If you want to have the date added to the log file's name, you should use the org.apache.log4j.DailyRollingFileAppender. However, you can neither define MaxFileSize nor MaxBackupIndex using this FileAppender, so you'll have to create your own custom FileAppender.

RollingPolicy

Your RollingPolicy configuration seems to be fine. My guess is that you'll have to add the apache-log4j-extras dependency to your project, which contains the RollingPolicy Interface and it's implementations (e.g. the TimeBasedRollingPolicy).

Upvotes: 1

Related Questions