English 中文(简体)
Specify time zone of log4j s date
原标题:

Is it possible to specify the time zone that log4j will use? I need the dates in the log file to be a different time zone than the application s. log4j s PatternLayout uses SimpleDateFormat. Unfortunately there doesn t appear to be a way to control SimpleDateFormat s time zone via the pattern string (DateFormat has setTimeZone method but that doesn t help).

I looked at log4j s source and SimpleDateFormat is being instiantiated in PatternParser.finalizeConverter. Unfortunately there s not an easy way to get a hold of the DateFormat to set the time zone.

最佳回答

If you use the Log4J extras JAR file on your classpath, the EnhancedPatternLayout class supports this configuration option. See the Javadoc at this link. It s handled as part of the %d pattern component like this:

log4j.appender.stdout.layout.ConversionPattern=%d{}{America/New_York} %p [%c] - %m%n

You can download the extras package here.

问题回答

My Case... must change the patternLayout to EnhancedPatternLayout. (log4j-1.2.17.jar)

log4j.appender.logfile.layout=org.apache.log4j.EnhancedPatternLayout log4j.appender.logfile.layout.ConversionPattern=[%d{ISO8601}{GMT+9}]%-5p - %m%n

The log pattern above has right idea but not fully correct (you don t get any timestamp in log).
Use this pattern for sure:
%d{ISO8601}{America/New_York} %p [%c] - %m%n
or
%d{ISO8601}{GMT-5} %p [%c] - %m%n

It is preferable to use something like {America/New_York} rather than {GMT-5} because by specifying a timezone an automatic adjustment will be made if daylight savings is operational. Specifying something like GMT-5 will simply adjust the GMT time zone by the specified amount of hours.

Also if you whant to dinamicaly obtein the default time zone, you can extend EnhancedPatternLayout and overwrite the method "setConversionPattern" like this:

@Override
public void setConversionPattern(String conversionPattern) {
    String defaultTimeZoneId = TimeZone.getDefault().getID();
    String conversionPatternModif = conversionPattern.replaceAll(
        "\%d\{([^\{\}]*)\}([ ]*[^\{]*)", 
        "%d{$1}{"+defaultTimeZoneId+"}$2");

    super.setConversionPattern(conversionPatternModif);
}

Just append the timezone surrounded by curly braces to %d in your date pattern.

E.g. %d{DEFAULT}{America/New_York} instead of %d{DEFAULT}

(In log4j 2 you can use regular PatternLayout. In log4j 1 since 1.2.16, EnhancedPatternLayout is included and recommended.)





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签