如何让java.util.logging发送日志到Logback? [英] How to make java.util.logging send logs to Logback?

查看:169
本文介绍了如何让java.util.logging发送日志到Logback?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用slf4j api记录的应用程序:

I'm working on an app that logs using the slf4j api:

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

...

private static final Logger LOG = LoggerFactory.getLogger(FreemarkerEmailPreviewGenerator.class);

...

LOG.error("Error generating email preview", e);

(上面显示的代码显示正在使用的类和包,但很标准的东西。)

(Code above posted to show classes and packages in use, but pretty standard stuff.)

我们使用如下配置的logback:

We use logback configured as follows:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS}] [%thread] [%-5level %logger{26} - %msg]%n
            </pattern>
        </encoder>
    </appender>

    <root>
        <level value="debug" />
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

我们的一些代码使用了使用java.util.logging记录的第三方库 - 特别是freemarker 。从下面的控制台日志条目中可以看出,logback和jul都记录到控制台,但是他们没有使用相同的配置(使用我们的模式的logback条目,jul没有)

Some of our code makes use of 3rd party libraries that logs with java.util.logging - specifically freemarker. As you can see from the following console log entries, both logback and j.u.l are logging to the console, but they are not using the same config (the logback entries use our pattern, the j.u.l ones don't)

[12:24:38.842] [pool-2-thread-19] [INFO  u.o.n.r.l.s.e.t.TemplateLoaderFromService - Finding template workflow/mail/templates/common/workflow-macros.ftl]
[12:24:38.859] [pool-2-thread-19] [INFO  u.o.n.r.l.s.e.t.TemplateLoaderFromService - Loaded template workflow/mail/templates/common/workflow-macros.ftl as /workflow/mail/templates/common/workflow-macros.ftl from RegistryMailTemplateService.]
11-Jan-2017 12:24:38 freemarker.log.JDK14LoggerFactory$JDK14Logger error
SEVERE:

Expression domainContact is undefined on line 9, column 74 in workflow/mail/templates/common/workflow-macros.ftl.
The problematic instruction:
----------
==> ${domainContact.name} [on line 9, column 72 in workflow/mail/templates/common/workflow-macros.ftl]

是否有可能使jul日志记录使用logback配置,以便我们为整个应用程序提供一个一致的日志记录配置?

Is it possible to make j.u.l logging use the logback config so that we have a single consistent logging config for the whole app?

推荐答案

您的应用程序需要以下jar:

Your application needs to have the following jars:

应用程序 - > Freemarker - > java.util.logging - > SLF4J Api: jul-to-slf4j.jar

Application -> Freemarker -> java.util.logging -> SLF4J Api: jul-to-slf4j.jar

应用程序 - > SLF4J API: slf4j-api.jar

Application -> SLF4J API: slf4j-api.jar

SLF4J API - > logback: logback-classic.jar和logback-core.jar

SLF4J API -> logback: logback-classic.jar and logback-core.jar

因为你的应用程序已经包含slf4j-api.jar和logback-classic.jar,你可能只需要添加jul-to-slf4j.jar

Since your application already contains slf4j-api.jar and logback-classic.jar, you probably only have to add the jul-to-slf4j.jar

如果使用maven:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.22</version>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.8</version>
</dependency>

logback classic将传递添加logback-core和slf4j-api

logback classic will transitively add logback-core and slf4j-api

这篇关于如何让java.util.logging发送日志到Logback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆