如何配置Netty日志工厂以查看LoggingHandler的输出?

14

我正在使用logback的slf4j日志记录,在我的应用程序开头编写了以下代码:

InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory());

然后我向管道中添加了一个新的 LoggingHandler(InternalLogLevel.DEBUG) 实例。 不幸的是,这仍然没有记录任何内容。我正在进行调试,发现问题在于调试级别,它只是跳过了日志记录本身。

我应该如何设置才能让这个 LoggingHandler 正常工作?


你能详细说明一下DEBUG存在的问题吗? - Norman Maurer
我看不出为什么以DEBUG级别记录日志不起作用。您自己以DEBUG级别记录日志是否有效?如果是,请提供能够重现问题的具体代码。 - trustin
你的项目类路径中是否有 slf4j-api.jarsl4j 的某些实现(例如 logback)?你还需要在实现特定的配置文件中配置日志级别(例如 logback.xml 用于 logback)。 - Abhinav Sarkar
你能否尝试将上述代码放置在 static {} 初始化代码块中? - Tomasz Nurkiewicz
1个回答

12

我做了一个小例子,它可以正常工作 - 我把它放在了GitHub的gist上。它使用logback作为slf4j的后端。需要注意的时刻是:

  • InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE); should be executed right on the entry point of your program. Sometimes this "entry point" may be very hard to determine.
  • Proper configuration of slf4j backend. Since slf4j is only a wrapper it does not deal with configuration at all - it is specific for log4j, logback or java.util.logging (or maybe some custom backend - I have worked heavily with the custom "java commons logging" configurator, so really anything really can be used)
  • Even without LoggingHandler you should see two debug messages from static initializer of org.jboss.netty.channel.socket.nio.SelectorUtil (with Netty version 3.6.0.Final):

    11:54:00.959 [main] DEBUG o.j.n.c.socket.nio.SelectorUtil - Using select timeout of 500
    11:54:00.962 [main] DEBUG o.j.n.c.socket.nio.SelectorUtil - Epoll-bug workaround enabled = false
    

Netty没有使用我的根记录器,我有以下内容:10:36:40,447 |-WARN in Logger[io.netty.channel.MultithreadEventLoopGroup] - 在记录器[io.netty.channel.MultithreadEventLoopGroup]的上下文[default]中没有附加程序。 - Edmondo
Slf4JLoggerFactory 似乎已经被弃用。 - Sip
1
你需要使用 InternalLoggerFactory.setDefaultFactory(Slf4JLoggerFactory.INSTANCE); - Nat
请注意,像代码片段中所示添加 LoggingHandler(InternalLogLevel.DEBUG) 这样的日志处理程序将会将记录器名称设置为 LoggingHandler 的 FQN,这几乎永远不是我们想要的。要设置记录器名称,请将 ClassString 作为第一个参数传递。 - Abhijit Sarkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接