The proper way to setting gRPC java logs programmatically

41 views Asked by At

Perhaps the title of this question should be: How to disable/set gRPC java logs programmatically in 2024?

Several credible sources from a few years ago say that:

gRPC uses java.util.Logging (grpc-io mailing list)

At runtime you can do something like Logger.getLogger("io.grpc").setLevel(Level.INFO) (typing from memory). (grpc-java issue #2994)

The following is my attempt at finding out the registered loggers and then disabling them one by one:

java.util.logging.Logger logger;
Enumeration<String> iter = java.util.logging
                                    .LogManager
                                    .getLogManager()
                                    .getLoggerNames();
while (iter.hasMoreElements())
{
      String loggerName = iter.nextElement();
      System.out.println(loggerName); // whois logging?
      logger = java.util.logging.Logger.getLogger(loggerName);
              logger.setLevel(Level.OFF);
      for (Handler h : logger.getHandlers()) {
          h.setLevel(Level.OFF);
      }   
}

and we've found the following loggers registered with JDK's logging facility (see the end of the output). But, the above code did not disable them:

2024-03-06 19:16:50,460 DEBUG [main] io.netty.util.internal.logging.InternalLoggerFactory: Using Log4J as the default logging framework
2024-03-06 19:16:50,461 DEBUG [main] io.netty.util.internal.PlatformDependent0: -Dio.netty.noUnsafe: false
v2024-03-06 19:16:50,462 DEBUG [main] io.netty.util.internal.PlatformDependent0: Java version: 19
...
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.maxCachedBufferCapacity: 32768
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.maxCachedBufferCapacity: 32768
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.cacheTrimInterval: 8192
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.cacheTrimIntervalMillis: 0
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.useCacheForAllThreads: false
2024-03-06 19:16:50,569 DEBUG [main] io.netty.buffer.PooledByteBufAllocator: -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
2024-03-06 19:16:50,591 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.channel.DefaultChannelId: -Dio.netty.processId: 22423 (auto-detected)
2024-03-06 19:16:50,592 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.util.NetUtil: -Djava.net.preferIPv4Stack: false
2024-03-06 19:16:50,592 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.util.NetUtil: -Djava.net.preferIPv6Addresses: false
2024-03-06 19:16:50,593 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.util.NetUtilInitializations: Loopback interface: lo (lo, 0:0:0:0:0:0:0:1%lo)
2024-03-06 19:16:50,594 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.util.NetUtil: /proc/sys/net/core/somaxconn: 4096
2024-03-06 19:16:50,595 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.channel.DefaultChannelId: -Dio.netty.machineId: 48:5b:39:ff:fe:7f:9d:15 (auto-detected)
2024-03-06 19:16:50,606 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.buffer.ByteBufUtil: -Dio.netty.allocator.type: pooled
2024-03-06 19:16:50,606 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.buffer.ByteBufUtil: -Dio.netty.threadLocalDirectBufferSize: 0
2024-03-06 19:16:50,606 DEBUG [grpc-nio-boss-ELG-1-1] io.netty.buffer.ByteBufUtil: -Dio.netty.maxThreadLocalCharBufferSize: 16384
2024-03-06 19:16:50,621 INFO  [main] root: Server listening on host /[0:0:0:0:0:0:0:0]:12345:12345

<From my System.out.println()s>
io.grpc.Context
io.grpc.internal.ReflectionLongAdderCounter
io.grpc.internal.ServerImplBuilder
global
io.grpc.internal.InternalServer
io.grpc.netty.ProtocolNegotiators
io.grpc.netty.Utils
io.grpc.internal.ProxyDetectorImpl
io.grpc.internal.ServerImpl
io.grpc.internal.GrpcUtil
io.grpc.Metadata
io.grpc.InternalChannelz
java.io.serialization

Clearly, there are some gRPC elements using JDK's standard logging facility, but I guess there aren't any log messages from those loggers in the above output. I am just not sure, as I am not too familiar with logging. At least I am not sure which log messages are coming from which loggers/services.

Finally, I was able to turn off all logs we earlier saw (above) by disablinig l4j2's root logger: this.logger.getRootLogger().setLevel(Level.OFF);

Q1) Is gRPC now using l4j2 as their logger (instead of JDK's standard logging facility)?

Q2) What is the proper way to set/disable log messages from grpc-java programmatically?

FYI: I definitely would like to turn off the io.netty stuff among the many others

0

There are 0 answers