2

I currently have this code in my main().

Logger logger = Logger.getLogger("My Log");
FileHandler fh;
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.info("Test");

This outputs 2 lines. One with datetime, and the other is test. I read many other questions on this site, but i do not get how to implement it to my current code.

UPDATE: I have created a new file called logging.properties and updated Logger logger = Logger.getLogger("logging.properties");

Inside the file i inputted these values

handlers= java.util.logging.ConsoleHandler
.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
javax.jms.connection.level = INFO
java.util.logging.SimpleFormatter.format=[%1$tF %1$tr] %3$s %4$s:  %5$s %n

But when i run i still see the date line. What am i missing?

ps4one
  • 57
  • 1
  • 2
  • 9

2 Answers2

0

You need to add a file called logging.properties to your Java project. In there, you can define the format to be used:

java.util.logging.SimpleFormatter.format=[%1$tF %1$tr] %3$s %4$s:  %5$s %n

The documentation here explains what the %... specifiers mean: http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
0

If you want to do it programatically, you could do 2 things:

Community
  • 1
  • 1
Andrés Oviedo
  • 1,388
  • 1
  • 13
  • 28