Dropwizard日志格式:方法和行无效

时间:2014-12-17 10:00:42

标签: logging logback dropwizard

我使用的是Dropwizard 0.7.1,我的自定义日志格式出现问题。输出不包含方法名称和行号。

我的配置如下:

...
  appenders:
    - type: console
      threshold: TRACE
      logFormat: "%-5level [%date{ISO8601}] [%X{MDC_VAR}] [%thread]: %c:%method:%line- %msg%n"
...

以下是输出行示例:

INFO  [2014-12-17 10:58:00,838] [] [main]: io.dropwizard.jersey.DropwizardResourceConfig:?:?- The following paths were found for the configured resources:

%method:%line不起作用。有谁知道为什么?

3 个答案:

答案 0 :(得分:6)

我遇到了与0.8.0相同的问题,事实证明,要记录行号,类和方法的名称,必须将logback配置为包含调用者数据。默认情况下,它不会在dropwizard上打开,因为它会使日志记录更加昂贵。 There's a fix针对版本0.9,允许在.yml文件中进行配置,但是现在我正在使用此解决方法:

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.Logger;

public class App extends Application<AppConfiguration> {

    @Override
    public void run(AppConfiguration configuration, Environment environment)
            throws Exception {
        Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
        AsyncAppender appender = (AsyncAppender) root.getAppender("async-console-appender");
        appender.setIncludeCallerData(true);
        // (...)
    }

}

前几个语句仍然不会显示方法和行号,因为它们是在调用应用程序的run方法之前记录的。在initialize方法上执行此操作将无效,因为async appender似乎尚未可用。

此外,如果您未在配置中使用root.getAppender- type: console中使用的appender的名称也会更改。由于我不确定名称是否始终遵循此async-<type>-appender格式,因此我认为查看要使用的名称的最佳方法是调试应用程序。检查根记录器的属性,查找它的AppenderAttachableImpl,然后检查appender列表。使用要设置方法名称和行号记录的异步appender的名称。

答案 1 :(得分:5)

Dropwizard 0.9.0及以上

现在很简单。只需将includeCallerData: true添加到您的appender配置即可。

logging:
  appenders:
    - type: console
    threshold: TRACE
    logFormat: "%-5level [%date{ISO8601}] [%X{MDC_VAR}] [%thread]: %c:%method:%line- %msg%n"
    includeCallerData: true

答案 2 :(得分:3)

@andrepnh 干得好!!

@heaphach

例如,在我的应用程序中

。 dropwizard 0.8.1

appenders:
- type: console
  threshold: ALL
  timeZone: UTC
  target: stdout
  logFormat: '[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%class{0}:%method:%line] [%thread] [-[%msg]-] %n'
- type: file
  currentLogFilename: '${env.app_log_dir!"./log"}/blog/${env.container_uuid!"container_uuid"}/application.log'
  threshold: ALL
  archive: true
  archivedLogFilenamePattern: '${env.app_log_dir!"./log"}/blog-bak/%d{yyyyMMdd}/${env.container_uuid!"container_uuid"}/application.log'
  archivedFileCount: 30
  timeZone: UTC
  logFormat: '[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%-5level] [%class{0}:%method:%line] [%thread] [-[%msg]-] %n'

在[%class {0}:%method:%line]中发现了[?:?:?]。

    Logger root = (Logger)   LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    AsyncAppender consoleAppender = (AsyncAppender) root.getAppender("async-console-appender");
    consoleAppender.setIncludeCallerData(true);

    AsyncAppender fileAppender = (AsyncAppender) root.getAppender("async-file-appender");
    fileAppender.setIncludeCallerData(true);

执行ok后更新。

[2015-08-19 16:13:30.117] [INFO ] [LoggerWriterTaskJob:execute:25] [qbScheduler-1] [-[write log with SizeAndTimeBasedFNATP]-] 
[2015-08-19 16:13:31.122] [INFO ] [LoggerWriterTaskJob:execute:25] [qbScheduler-1] [-[write log with SizeAndTimeBasedFNATP]-] 
[2015-08-19 16:13:32.126] [INFO ] [LoggerWriterTaskJob:execute:25] [qbScheduler-1] [-[write log with SizeAndTimeBasedFNATP]-] 
[2015-08-19 16:13:33.130] [INFO ] [LoggerWriterTaskJob:execute:25] [qbScheduler-1] [-[write log with SizeAndTimeBasedFNATP]-]