Changing the Log Time Zone Without Changing the JVM Time Zone

This article is a legacy article. It applies to previous versions of the Liferay product. While the article is no longer maintained, the information may still be applicable.

This article documents how a customer can adjust the time zone for the log files by customizing the portal-log4j.xml file. Please be aware that the following steps will allow customers to modify the Liferay_home/logs files as opposed to the server log files (e.g., in tomcat_home/logs).

Resolution

  1. Get a copy of the portal-log4j.xml file by extracting it from the portal-impl.jar OR finding it from in the source code. In this case, we will use the first option. The directory of this file in the portal-impl.jar is [Liferay_HOME]\[Tomcat_HOME]\webapps\ROOT\WEB-INF\lib\portal-impl.jar\META-INF\
  2. Get a copy of the log4j.dtd file from the same location.
    • Note for Liferay DXP 7.2: log4j.dtd may not be present in this location, but it can be ignored for these steps
  3. Rename your copy of the portal-log4j.xml file to portal-log4j-ext.xml
  4. Since your own portal-log4j-ext.xml file is supposed to override the original properties from the portal-log4j.xml file in the portal-impl.jar, we will only keep the following contents in the portal-log4j-ext.xml file.
    <?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>
  5. 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.xml file, there are actually 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 .

  6. In both sections:
    1. 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
    2. 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" />

    You can find the required values for TIME ZONE at http://en.wikipedia.org/wiki/List_of_zoneinfo_timezones.

  7. Place the modified portal-log4j-ext.xml file and the log4j.dtd file (if applicable) at [Liferay_HOME]/[Tomcat_HOME]/webapps/ROOT/WEB-INF/classes/META-INF.
  8. Start the portal.

After you have started 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 changed to the time in the specified new time zone. Please also note that all the information starting from the database connection section will be recorded in the log file of Liferay ([Liferay_HOME/logs]).

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 existing ext-impl.jar file, it goes in ${EXT}/docroot/WEB-INF/ext-impl/src/META-INF of the source code of the ext- and then the ext will have to be compiled and replaced.

Note: The method outlined 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

Never modify the time zone settings at the JVM level (see setenv.bat or setenv.sh in tomcat) since it will affect all of the processes run in that JVM. More specifically, the calendar portlet may not function correctly if the time zone is not configured to GMT at the JVM level.

set "JAVA_OPTS"=%JAVA_OPTS% -Dfile.encoding=UTF8 -Djava.net.preferIPv4Stack=true -Dorg.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES=false -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m"

这篇文章有帮助吗?
2 人中有 2 人觉得有帮助