【问题标题】:Can't disable quartz-scheduler logging using SLF4J无法使用 SLF4J 禁用石英调度程序日志记录
【发布时间】:2014-11-17 02:37:55
【问题描述】:

将quartz-scheduler 添加到项目后,Tomcat 的服务器日志中会出现以下消息:

[INFO] [talledLocalContainer] 12:15:06.319 [DefaultQuartzScheduler_QuartzSchedulerThread] 调试 o.quartz.core.QuartzSchedulerThread - 批量获取0个触发器

我正在尝试禁用该日志消息,它每 25 秒左右重复一次。对于同一问题,我已经回答了许多其他问题,例如:

...建议的方法都不起作用。

我在pom.xml 中声明了以下依赖项:

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.5</version>
    </dependency>

我已将以下log4j.properties 设置添加到我的项目中:

log4j.rootLogger=OFF
log4j.logger.quartz=OFF
log4j.logger.o.quartz=OFF
log4j.logger.org.quartz=OFF

...还有以下simplelogger.properties:

org.slf4j.simpleLogger.defaultLogLevel=error

除了尝试链接答案之一建议的编程解决方案外,它应该禁用所有日志记录,大致如下:

List<Logger> loggers = Collections.<Logger>list(LogManager.getCurrentLoggers());
loggers.add(LogManager.getRootLogger());
for ( Logger logger : loggers ) {
    logger.setLevel(Level.OFF);
}

这似乎禁用了一切除了石英运行时的日志消息。

有没有办法从石英中删除日志消息,除了修改石英源代码来删除它吗?

【问题讨论】:

    标签: java maven logging quartz-scheduler slf4j


    【解决方案1】:

    上面的日志条目看起来不像 Log4J。我认为那是 ACL 或 JUL。使用 Slf4j 转 Log4J 时,还需要redirect these frameworks to Slf4j。将这些依赖项也添加到您的项目中:

    <!--Redirect Apache Commons Logging to Slf4J -->
    <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>jcl-over-slf4j</artifactId>
     <version>1.7.5</version>
    </dependency>
    <!--Redirect Java Util Logging to Slf4J -->
    <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>jul-to-slf4j</artifactId>
     <version>1.7.5</version>
    </dependency>
    

    阅读this doc page 也了解将 Jul 重定向到 Slf4J。请务必通过 Maven 从您的项目中排除任何现有的 commons-logging.jar 文件。

    (来自Slf4J documentation page

    如果一切都失败了,试试下面的代码:

    //see if we can find the offending logger through slf4j's LoggerFactory
    org.slf4j.Logger logger = 
        LoggerFactory.getLogger(Class.forName("org.quartz.core.QuartzSchedulerThread"));
    if (logger != null && logger instanceof ch.qos.logback.classic.Logger) {
        //the slf4j Logger interface doesn't expose any configuration API's, but 
        //we can cast to a class that does; so cast it and disable the logger
        ((ch.qos.logback.classic.Logger)logger).setLevel(
            ch.qos.logback.classic.Level.OFF);
    }
    

    【讨论】:

    • 感谢您的建议。它本身并不能很好地工作,但是在进行此更改后我能够添加一些代码以最终使石英记录器静音。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    • 1970-01-01
    • 2013-01-21
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多