0

I have a FileHandler set up:

Logger logger = Logger.getLogger(this.class.getName());
FileHandler handler = new FileHandler(myFile, true);
handler.setFormatter(new SimpleFormatter());
logger.addHandler(handler);
logger.setUseParentHandlers(false);

However my server is in UTC, so in my log file, the timestamps look like: Nov 9, 2012 5:12:17 PM

This is harder to understand in a pinch, so I would like to know if it is possible

1) To convert this to 24 hour time, so at least I would see 17:12:17 instead of 5:12:17PM 2) To instead use a different time zone

I think it might be something about how I set up the formatter, but I just don't know how.

Thank you!

2 Answers2

0

I suggest you to read the javadoc of SimpleFormatter.format(), you may specify your own format via java.util.logging.SimpleFormatter.format property in the application logging configuration file.

The default properties file is <Java home>\jre7\lib\logging.properties. Copy it and put it wherever you want.

The Java Logging Overview is a good reading too.

The following code

System.setProperty(
   "java.util.logging.SimpleFormatter.format", "%4$s: %5$s [%1$tc]%n" );
Logger l = Logger.getLogger("");
l.warning( "Hello" );

outputs

WARNING: Hello [ven. nov. 09 20:26:30 CET 2012]
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • I don't understand how this works. Right now I don't have a logging configuration file, or a `LogRecord` object (which `format` requires). Is there something more like `SimpleFormatter().format("HH:mm:ss");` I might be able to use? –  Nov 09 '12 at 19:08
  • Thank you! I found the file and property in my environment, and changed it as you recommended. I restarted my Java process, but I'm still seeing the same in the old format. Anything else I might need to restart to get the new property to take? –  Nov 09 '12 at 19:47
  • The properties file you've edited is not in use, your application use another file. search for 'logging.properties' in the app directory – Aubin Nov 09 '12 at 20:00
  • I searched for 'logging.properties' in the '/' directory. I saw two, one in `usr/lib/jvm/java-6-openjdk/jre/lib/` and one in `/etc/java-6-openjdk/` and both look the same: `java.util.logging.SimpleFormatter.format=%4$s: [%1$tc]%n %5$s` –  Nov 09 '12 at 20:08
  • ? No, there is no comment in front of them –  Nov 09 '12 at 20:13
  • Try to set the property via the code in the main() of your application, before any call to Logger or LoggerManager – Aubin Nov 09 '12 at 20:18
  • Yes, I was able to get it to work in a simple case, copy pasting what you have above. However, in my example, I am keeping a static reference to the logger, defined above the entry point for the application which I think might prevent me from getting this to work –  Nov 09 '12 at 20:25
0

I was able to get it to work by creating my own formatter using an example I found:

Custom logger formatter