【问题标题】:Logback configuration is coming as nullLogback 配置为空
【发布时间】:2018-01-12 16:52:23
【问题描述】:

我正在使用 logback 1.2.3。我将 logback 配置传递为-Dlogback.configurationFile=$SERVICE_PATH/restapilayer.logback.xml

我的restapilayer.logback.xml 如下所示

<configuration debug="true">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <Target>System.out</Target>
            <encoder>
               <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="STDOUT" />
    </root>

</configuration>

在我的日志中,我还看到正在加载配置文件。

10:51:42,695 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [/home/ubuntu/build-target/restapilayer/restapilayer.logback.xml] at [file:/home/ubuntu/build-target/restapilayer/restapilayer.logback.xml]
10:51:42,860 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
10:51:42,863 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
10:51:42,869 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
10:51:42,894 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
10:51:42,895 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
10:51:42,895 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
10:51:42,896 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@3fd7a715 - Registering current configuration as safe fallback point
2018-01-13 10:51:42 [main] INFO  com.vnera.common.utils.CommonUtils - adding uncaught exception handler hook...
INFO [2018-01-13 10:51:44] o.e.j.u.log:[?:?:?] - [main] - Logging initialized @1792ms    

然后在尝试记录时,我看到 logback 配置为 null,并且似乎 logback 正在使用默认配置运行

INFO [2018-01-12 16:25:34] c.v.r.c.v.r.VneraBackendService:[?:?:?] - [main] - Logback used _null_ as the configuration file_

我正在使用下面的代码来获取 logback 配置

logger.debug("Testing Debug logging");
LoggerContext loggerContext = ((ch.qos.logback.classic.Logger)logger).getLoggerContext();
URL mainURL = ConfigurationWatchListUtil.getMainWatchURL(loggerContext);
System.out.println(mainURL);
// or even
logger.info("Logback used '{}' as the configuration file.", mainURL);

有人可以告诉我出了什么问题吗?如何调试此问题?

Maven依赖树添加here.

【问题讨论】:

标签: java logback dropwizard


【解决方案1】:

我试图在 dropwizard 应用程序中使用 logback(使用 logback.xml)。在应用程序初始化期间,每当调用 io.dropwizard.Application.run(BackendServiceConfiguration, Environment) 时,它都会丢弃先前初始化的记录器并尝试使用在 dropwizard 配置文件 (config.yaml) 中定义的记录器。

为了在run() 之后解决这个问题,我必须按照here 的说明调用以下代码。

public static void relaodLogback() {
        String loggingConfig = System.getProperty("logback.configurationFile");
        if(loggingConfig == null) {
            System.out.println("Logging Config is null");
        }

        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        loggerContext.reset();
        JoranConfigurator configurator = new JoranConfigurator();

        try {
            InputStream configStream = FileUtils.openInputStream(new File(loggingConfig));
            configurator.setContext(loggerContext);
            configurator.doConfigure(configStream); // loads logback file
            configStream.close();
            System.out.println("Loaded configuration file");
        } catch (JoranException | IOException e) {
            e.printStackTrace();
            System.out.println("Failed to log configuration file");
            System.exit(1);
        }
    }

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 2014-07-02
    • 1970-01-01
    • 2019-12-16
    • 2015-01-26
    • 2012-12-14
    相关资源
    最近更新 更多