2

I want to change the way my log looks (java.util.logging.Logger) and I found this:

System.setProperty("java.util.logging.SimpleFormatter.format","[%1$tF %1$tT] [%4$-7s] %5$s %n");

So my log entries look now like this:

[2017-08-24 15:55:40] [INFORMATION] Hello World!

I'd like to set the format to following:

[24.08.2017 15:55:40]

I've been trying for so long, shouldn't be that hard. Can anybody help me or sending me some good and easy documentations/examples?

Tamas Rev
  • 7,008
  • 5
  • 32
  • 49
user2871190
  • 241
  • 2
  • 5
  • 19

2 Answers2

3

From documentation Class Formatter

Instead of :

[%1$tF %1$tT]

You can use :

[%1$te.%1$tm.%1$tY %1$tT]

You can see the result in this example :

Calendar c = Calendar.getInstance();
String example = String.format("[%1$te.%1$tm.%1$tY %1$tT]", c);
System.out.println(example);

Output

[24.08.2017 15:19:30]
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
2

I've been trying for so long, shouldn't be that hard.

SimpleFormatter.format is a static property that is set at class loading time. You can only use System.setProperty to change the format if that is done before the java.util.logging.SimpleFormatter class is loaded. Instead you should modify your logging.properties or modify your startup script to set the property during the JVM startup.

If there is an error in your format the behavior is to just use a default format. This makes it hard to debug because you can't tell the difference between an error or just not setting the property correctly.

Your best bet is to use a test program like YCF_L made and then apply the format to the startup script.

jmehrens
  • 10,580
  • 1
  • 38
  • 47