Customizing portal-log4j.xml

Adjusting your Liferay's log (portal-log4j.xml) settings

Let's say for instance you need to modify the level used by a logger created by Liferay and not exposed for customization/modification in an easy to change location, one could create ext plugins or use "clever" ways to adjust the configuration files somehow.  But the good news is that Liferay does expose an easy to access point for customization of any logger configured in portal-log4j.xml, although they use an ancient set of names to define it (like category, instead of logger). Liferay lets you change how its log will be generated by extending its internal configuration file, which one can find at: https://github.com/liferay/liferay-portal/blob/7.1.x/portal-impl/src/META-INF/portal-log4j.xml


Example 1

Changing a logger level

Having that file as reference, let's get the first entry to show how one can customize the level for a given logger by just creating a simple -ext.xml file. The logger we will personalize is: aQute.bnd.osgi


<category name="aQute.bnd.osgi">
    <priority value="WARN" />
</category>

It is a trivial piece of code, and our task is as simple as this code is easy to read. Basically, we want it to look like the following snipped to set the level to ERROR.


<category name="aQute.bnd.osgi">
    <priority value="ERROR" />
</category>

To achieve our goal, we just need to create an ext file at: ./webapps/ROOT/WEB-INF/classes/META-INF/portal-log4j-ext.xml; the name tells you exactly what it is, an extension file for portal-log4j.xml.


<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <category name="aQute.bnd.osgi">
        <priority value="ERROR" />
    </category>

</log4j:configuration>
 


Example 2

Disabling XML Log Files in Liferay 7.x

A less trivial case is how to disable the XML log files in Liferay 7.x, which are created with redundant and unnecessary information for those that are not parsing their logs with a system that requires XML. So, let's save some resources by disabling it:



<?xml version="1.0"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="XML_FILE" class="org.apache.log4j.varia.NullAppender"/>

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

    <appender class="org.apache.log4j.rolling.RollingFileAppender" name="TEXT_FILE">
        <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
            <param name="FileNamePattern" value="@liferay.home@/logs/liferay@spi.id@.%d{yyyy-MM-dd}.log"/>
        </rollingPolicy>

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

    <category name="com.liferay.portal.spring.context.PortalContextLoaderListener">
        <priority value="WARN"/>
    </category>

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

</log4j:configuration>



That is all there is to it, you just need to extend the file to customize it to your needs.

More Blog Entries

0 Comments