Sometimes when we want to print the timestamp in the logs of the time zone we need, since by default the timestamp printed is of the server in which the application is running. Below is the solution for this.

This is useful in the cloud deployments for example AWS EC2, where sometimes the timezone in which we are deploying is not the same.

log4j.logger.com.myapp=DEBUG, consoleAppender, fileAppender

log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.consoleAppender.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.consoleAppender.layout.ConversionPattern=[%d{ISO8601}{GMT+8}][%t] %-5p %c %x - %m%n

log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.fileAppender.layout.ConversionPattern=[%d{ISO8601}{GMT+8}][%t] %-5p %c %x - %m%n
log4j.appender.fileAppender.File=c:/applogs.log


Things to be configured

Make sure the layout which is used is below one,

log4j.appender.fileAppender.layout=org.apache.log4j.EnhancedPatternLayout

And the conversion pattern should mention the timezone information with %d formatter. This decides the timezone, and using this the timestamp of that zone is printed in the logs. For now it prints out the singapore timezone timestamp.

[%d{ISO8601}{GMT+8}]


References

http://stackoverflow.com/questions/1785725/specify-time-zone-of-log4js-date