【问题标题】:Get log from LoggerFactory从 LoggerFactory 获取日志
【发布时间】:2019-05-15 13:28:14
【问题描述】:

可以这么简单,但我已经浪费了很多时间来寻找任何解决方案。

我有

package net.rubyeye.xmemcached;

...
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...

public class XMemcachedClient implements XMemcachedClientMBean, MemcachedClient {

    private static final Logger log = LoggerFactory
            .getLogger(XMemcachedClient.class);
....

使用 Log4j,我可以从 apache-servicemix 获取所有日志。

我尝试过类似的东西

log4j.logger.net.rubyeye.xmemcached.XMemcachedClient=All, xmemcachedLog

log4j.appender.xmemcachedLog=org.apache.log4j.RollingFileAppender
log4j.appender.xmemcachedLog.File=${karaf.data}/log/spring/xmemcachedLog.log
log4j.appender.xmemcachedLog.ImmediateFlush=true
log4j.appender.xmemcachedLog.maxFileSize = 10MB
log4j.appender.xmemcachedLog.maxBackupIndex = 10
log4j.appender.xmemcachedLog.layout=org.apache.log4j.PatternLayout
log4j.appender.xmemcachedLog.layout.ConversionPattern=%d{dd-MM-yyyy_HH:mm:ss} %-5p [%t] - %m%n

但我什么也得不到。我想获取有关我在第 1335 行获得的异常的信息

key = this.preProcessKey(key);

实际上,我想准确地记录那个类并不重要。在我的应用程序中,我还有其他具有 LoggerFactory.getLogger(...);

的类

主要问题是 如何从 Logger log = LoggerFactory 获取日志 .getLogger(SomeClass.class);

现在,我的 rootLogger 看起来像

# Root logger 
log4j.rootLogger=info, out, sift, osgi:VmLogAppender 
log4j.throwableRenderer=org.apache.log4j.OsgiThrowableRenderer

【问题讨论】:

    标签: java log4j slf4j


    【解决方案1】:

    如果您使用的是 java EE 应用程序,您应该有一个 logback.xml 来决定是否显示您的日志。

    尝试在其中添加这行代码:

    <logger name="net.rubyeye.xmemcached" level="DEBUG"/>
    

    它将激活此包中所有类的调试日志。 如果它仍然不起作用,则可能您的类路径中没有该文件,您可能必须将其添加到 jvm 参数中。

    【讨论】:

    • 这有点不正确,因为我在 org.ops4j.pax.logging.cfg 中有那个配置。它的 apache-servicemix 文件。另外,我已经列出了你展示的内容。它看起来像 log4j.logger.net.rubyeye.xmemcached.XMemcachedClient=All, xmemcachedLog
    【解决方案2】:

    我的记录器没有问题。我只是没有任何 log.error() 或 log.smth(),所以我的文件中没有任何行。

    所以它可以工作,例如,在 XMemcachedClient 中的那个方法中

    public void setTimeoutExceptionThreshold(int timeoutExceptionThreshold) {
            if (timeoutExceptionThreshold <= 0) {
                throw new IllegalArgumentException(
                        "Illegal timeoutExceptionThreshold value "
                                + timeoutExceptionThreshold);
            }
            if (timeoutExceptionThreshold < 100) {
                log.warn("Too small timeoutExceptionThreshold value may cause connections disconnect/reconnect frequently.");
            }
            this.timeoutExceptionThreshold = timeoutExceptionThreshold;
        }
    

    它向我显示“timeoutExceptionThreshold 值太小可能导致连接频繁断开/重新连接。” 在我的 ${karaf.data}/log/spring/xmemcachedLog.log 中 timeoutExceptionThreshold

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 2019-04-23
      • 2021-01-24
      • 2018-06-25
      • 2018-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      相关资源
      最近更新 更多