David M. Karr
David M. Karr

Reputation: 15235

Setting java.util.logging level to FINER, but a "log.fine()" doesn't get printed, only "log.severe()"

I'm trying to use java.util.logging inside a slf4j filter, as I can't use slf4j loggers there.

I'm using Java 11 and building with Maven.

For some reason, I can get SEVERE, INFO, and WARNING logs to appear in my log (console), but not FINE. I'm setting my log level, either with a system property on the command line, or in my logging.properties file, to FINER.

For simplicity, I just create this Logger:

private static Logger   logger  = Logger.getLogger("abc");

I created src/main/resources/logging.properties with this content:

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

abc.level=FINER

In a method that gets executed, I have this:

    logger.log(Level.FINE, "Loading security filter config file: " + fileName + " with override contents: " + overrideContents);
    logger.log(Level.SEVERE, "This is a test.");
    logger.severe("This is a test.");
    logger.config("This is a test.");
    logger.info("This is a test.");
    logger.warning("This is a test.");
    logger.fine("Loading security filter config file: " + fileName + " with override contents: " + overrideContents);

This is what I see in my console log:

Apr 26, 2024 3:45:36 PM com.att.idp.logging.provider.filter.SecurityFilter loadSecurityFilterConfig
SEVERE: This is a test.
Apr 26, 2024 3:45:54 PM com.att.idp.logging.provider.filter.SecurityFilter loadSecurityFilterConfig
SEVERE: This is a test.
Apr 26, 2024 3:45:54 PM com.att.idp.logging.provider.filter.SecurityFilter loadSecurityFilterConfig
INFO: This is a test.
Apr 26, 2024 3:45:54 PM com.att.idp.logging.provider.filter.SecurityFilter loadSecurityFilterConfig
WARNING: This is a test.

It doesn't emit either of the FINE-level logs.

What else can I show that would be useful?

Update:

I've read the comments and answer, and sort of made progress, but it's not ideal.

I would like to have a situation where the "logging.properties" sets some "foundational properties", but leaves the logging level as the default. Then, the logging level is only elevated with command-line arguments. I've sort of been able to achieve this, but in a somewhat awkward way.

I have the following in "src/main/resources/logging.properties":

handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

abc.level=FINEST

If I run the application with no additional command-line arguments, the additional logs do not show. If I add the following:

-Djava.util.logging.config.file=src/main/resources/logging.properties

Then I get the additional log entries.

I've tried not having the "level" properties in the file, and setting them on the command line. That results in no log entries. It seems like it ignores the ".level" property settings on the command line, or perhaps it ignores those entirely and only respects the "java.util.logging.ConsoleHandler.level = ALL" setting in the properties file.

I find the "handler" property in the properties file curious. I didn't try setting the "handler" property on the command line. The fact that it doesn't have "java.util.logging" in the name is odd.

Upvotes: 0

Views: 391

Answers (1)

Roman C
Roman C

Reputation: 1

You should configure the ConsoleHandler's level in this way:

java.util.logging.ConsoleHandler.level = FINER

The configuration file logging.properties by default is in your JDK/JRE folder. Until you override it with the custom file.

There're several ways to add a custom configuration file using a system property. Check this answer.

For that you need to add -Djava.util.logging.config.file=\path\to\logging.properties option to the server launch configuration.

Upvotes: 1

Related Questions