Introduction
By default, Liferay's JVM uses the UTC (GMT) timezone for scheluding jobs and displaying the correct dates in the presentation layer. However, you can change the time zone for the log files without modifying this default configuration.
In this article, you'll customize the portal-log4j.xml file within the Liferay_home/logs/ folder to adjust the time zone for your Liferay instance's logs.
Please be aware that you shouldn't adjust the time zone at the JVM level to avoid major scheduling issues and incorrect behavior (e.g., incorrect dates in the Calendar page element).
Environment
- DXP 7.1, 7.2, and 7.3
- Portal 6
Steps
- Go to the
[LIFERAY_HOME]/[TOMCAT_HOME]/webapps/ROOT/WEB-INF/lib/portal-impl.jar/META-INF/folder. - Get a copy of the
portal-log4j.xmlfile by extracting it from theportal-impl.jarfile. - Get a copy of the
log4j.dtdfile from the same location.NOTE
For Liferay DXP 7.2, thelog4j.dtdfile may not be present in this location. You can safely ignore this step. - Rename your copy of the
portal-log4j.xmlfile toportal-log4j-ext.xml.
Because the copied file is supposed to override the original properties from theportal-log4j.xmlfile in theportal-impl.jar, we will only keep the following contents in theportal-log4j-ext.xmlfile:<?xml version="1.0"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.EnhancedPatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" /> </layout> </appender> <appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender"> <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{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" /> </layout> </appender> <root> <priority value="INFO" /> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </log4j:configuration> - Find the following section:
<layout class="org.apache.log4j.EnhancedPatternLayout"> <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" /> </layout>As you can see from your own
portal-log4j-ext.xmlfile, there are two copies of this section.The first section is found inside the
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> </appender>node, and the configurations made to it will be applied on the Console log files.The second section is found within the
<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender"></appender>node, and the configurations made to it will be applied on the log files stored in the specified file system location, which in this case is@liferay.home@/logs/liferay.%d{yyyy-MM-dd}.log. - In both sections:
- Change
<layout class="org.apache.log4j.PatternLayout">to<layout class="org.apache.log4j.EnhancedPatternLayout">NOTE
For Liferay DXP 7.2, this change may already be made, so it can be skipped. - Change
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}:%L] %m%n" />to<param name="ConversionPattern" value="%d{ABSOLUTE}{TIMEZONE} %-5p [%t][%c{1}:%L] %m%n" />.
- Change
-
Change the
TIMEZONEvalue to your desired time zone.
You can find the values at https://en.wikipedia.org/wiki/List_of_tz_database_time_zones. - Place the modified
portal-log4j-ext.xmlfile and thelog4j.dtdfile (if applicable) at[LIFERAY_HOME]/[TOMCAT_HOME]/webapps/ROOT/WEB-INF/lib/portal-impl.jar/META-INF. - Start the portal.
After starting the portal, the initial section of the startup log will still be in GMT (this should still be controlled by the JVM’s time zone settings), but when it gets to the database connection section, the time zone will be the time in the time zone you specified. All the information from the database connection log section will be recorded in your Liferay instance's log file ([LIFERAY_HOME/logs]).
The steps above works well for development as the end user can make changes easily, or remove the logging change without having to recompile the
ext.Additional Information
- If you have already deployed a custom logging file like above, you can add these changes to this file, or it can be included as part of an
ext-impl.jar. If you have an existingext-impl.jarfile, make these changes in the${EXT}/docroot/WEB-INF/ext-impl/src/META-INFlocation of the source code of theextfile. Then, compile and replace theextfile. - Always configure time zones at the instance or user levels. The JVM's default time zone (UTC) should not be changed to avoid job scheduling issues and incorrect behavior. See the Things Liferay Won't Say blog post for more information.