2

I'm trying to setup a logger for my application I want to do that through logger.properties file specified for this project. I use a following code to set it up:

InputStream loggerProps = getClass().getResourceAsStream("/logger.properties");
LogManager.getLogManager().readConfiguration(loggerProps);

That file contain following text:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level = ALL
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$-7s] %5$s %n
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
#part about FileHandler omitted

And the problem is the given format isn't applied to the logged messages at all. However, it only doesn't in Java 8 (maybe earlier versions as well, haven't checked it). When I try the above in Java 9 or 10 it works just fine.

From what I can tell the file is loaded properly. Getting rid of ConsoleHandler in the first line causes logs not to be displayed in the console. Changing handler's level works as expected as well.

If I call the following method after setting properties:

LogManager.getLogManager().getProperty("java.util.logging.SimpleFormatter.format")

it returns the correct format string.

I've tried placing the format string inside both double quotes and single quotes, neither does work.

What am I doing wrong? Where is the problem? Isn't the format supported in Java version lower than 9?

Amongalen
  • 3,101
  • 14
  • 20
  • Generally you're expected to give minimal code that reproduces the issue and if possible, the output you got – Roy Shahaf Sep 03 '18 at 15:47
  • That property is [loaded into a static field](https://stackoverflow.com/questions/25532155/simpleformatter-ignoring-the-java-util-logging-simpleformatter-format-property) by the first caller. Once set, the pattern can not be changed. So you have to watch out for cases where the pattern is loaded before your logging.properties is read. – jmehrens Sep 05 '18 at 13:53

2 Answers2

0

So I couldn't reproduce your issue..

Test.java:

import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Test {
    private static Logger LOGGER;

    public static void main(String[] args) {
        InputStream loggerProps = Test.class.getResourceAsStream("/logger.properties");
        try {
            LogManager.getLogManager().readConfiguration(loggerProps);
            LOGGER= Logger.getLogger(Test.class.getName());
            LOGGER.info("hey");
        } catch (SecurityException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

logger.properties:

handlers= java.util.logging.ConsoleHandler.level= INFO
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%4$7s] %5$s %n

console:

[2018-09-03 18:52:26] [   INFO] hey 

I've also verified that altering the format alters the console message format.

You should verify that: 1. your imports are correct 2. your logger.properties is in a source folder

Good luck.

Roy Shahaf
  • 494
  • 5
  • 10
  • And I don't know why you would use JUL anyway, when there are much better (opinionated, I know) alternatives.. – Roy Shahaf Sep 03 '18 at 15:57
  • What is the best alternative in your opinion and why? – Amongalen Sep 04 '18 at 08:02
  • In the code I would recommend using slf4j whenever possible. Then you can choose different implementations to use on top of slf4j. Recommended implementations include logback and log4j2. I would recommend using slf4j in the code since it allows you to easily: 1. supply your code as a library 2. swap logger implementation if you feel like I would recommend logback/log4j2 over JUL since they are much faster and support many features that JUL does not. – Roy Shahaf Sep 04 '18 at 23:07
0

I've managed to solve the issue. It seems that the problem was with creating a new ConsoleHandler (which I did in other class which was initialized before setting the logger properties). Here is what I mean:

public final class Test {
    public static void main(String[] args) {
        Logger LOGGER = Logger.getLogger(Test.class.getName());
        try {
//            ConsoleHandler handler = new ConsoleHandler();
            InputStream loggerProps = new FileInputStream(new File("logger.properties"));
            LogManager.getLogManager().readConfiguration(loggerProps);
            LOGGER.info("hey");
        } catch (SecurityException | IOException e) {
            e.printStackTrace();
        }
    }
}

If you uncomment that one commented line the logger formatting won't work. In Java 9 or 10 it works just fine.

Amongalen
  • 3,101
  • 14
  • 20