Set HazelCast logging level with SLF4J using JUL provider

92 Views Asked by At

I'm struggling to set HazelCast log levels correctly. I'd like to set com.hazelcast logging to a different level than some other packages, but it's not working.

I am currently using SLF4J with JUL provider defined in build.gradle dependencies

implementation "org.slf4j:slf4j-api:2.0.3"
implementation "org.slf4j:slf4j-jdk14:2.0.3"

Then in my code to initialize logging I set

java.lang.System.setProperty( "hazelcast.logging.type", "slf4j" );
java.util.logging.Level level = Level.WARNING;
java.util.logging.Logger.getGlobal().setLevel( level );
java.util.logging.Logger.getLogger( "com.hazelcast" ).setLevel( level );

I can see in my logs that HazelCast is using SLF4J, but it's still logging out at INFO level instead of WARNING.

Oct 25, 2022 12:31:29 PM com.hazelcast.logging.Slf4jFactory$Slf4jLogger log
INFO: [LOCAL] [dev] [4.1.1] Resolving domain name 'localhost' to address(es): [127.0.0.1, 0:0:0:0:0:0:0:1]
Oct 25, 2022 12:31:29 PM com.hazelcast.logging.Slf4jFactory$Slf4jLogger log
INFO: [LOCAL] [dev] [4.1.1] Interfaces is disabled, trying to pick one address from TCP-IP config addresses: [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1]
Oct 25, 2022 12:31:29 PM com.hazelcast.logging.Slf4jFactory$Slf4jLogger log
INFO: [localhost]:5702 [dev] [4.1.1] Hazelcast 4.1.1 (20201222 - 27af558) starting at [localhost]:5702

On the HazelCast documentation, it provides examples for Log4J and JUL, but not for SLF4J, and following the "reference" snippet for the JUL example stomps on all my logs preventing me from setting different levels for different packages.

I'm not sure what I'm doing wrong or where to go from here. I'm hoping someone has overcome this before.

1

There are 1 best solutions below

0
wackozacko On

Apparently I posted too soon as I was able to answer my own question. For whatever reason, HazelCast (as opposed to other libraries I'm using) falls prey to the weak reference garbage collection noted in the JavaDoc. Creating a hard reference to the logger and then setting the log level seems to resolve this problem.

public class MyLogger {
    private static final Logger hazelCastLogger = Logger.getLogger( "com.hazelcast" );

    static {
        System.setProperty( "hazelcast.logging.type", "slf4j" );
        Level level = Level.WARNING;
        hazelCastLogger.setLevel( level );
    }
}