简体   繁体   中英

How to convert log time stamps to specific timezone in log4j2?

I am using log4j2 in an application for logging and the application is deployed in servers running from London, Tokyo, New York etc.

I aggregate the logs later for analysis. In order to make it uniform, I want to convert the timestamps of the logs to a single time zone, say GMT or EST. How can I do that in log4j2?

<Appenders>
    <RollingRandomAccessFile name="RandomAccessFileOutput" fileName="${sys:host.log}" 
        filePattern="${sys:host.log}.%d{yyyy-MM-dd-hh-mm-ss}.gz">
        <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="10 MB"/>
            <CronTriggeringPolicy schedule="0 0 0 * * ?"/>
        </Policies>
    </RollingRandomAccessFile>
    <File name="File" fileName="console.log" append="false" >
        <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
    </File>
    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n"/>
    </Console>

</Appenders>

According to the Log4j 2 manual , you can specify a timezone in the pattern. For example:

%d{yyyy-MM-dd HH:mm:ss}{GMT+0}

would format the date in the "GMT+0" timezone. The string in the second (optional) pair of braces is a standard time zone id, as understood by java.util.TimeZone.getTimeZone(String) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM