Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard Log format: Method and line is not working

I'm using Dropwizard 0.7.1 and I have problems with my custom log format. The output does not contain method name and line number.

My config looks like this:

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

Here is an example outputline:

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

%method:%line does not work. Does anyone knows why?

like image 695
heaphach Avatar asked Dec 01 '22 15:12

heaphach


1 Answers

I was having the same problem with 0.8.0 and it turns out that to log line numbers, class and method's name, logback must be configured to include caller data. This is not on by default on dropwizard because it makes logging more expensive. There's a fix targeted for version 0.9 that would allow this to be configured in the .yml file, but for now I'm using this workaround:

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);
        // (...)
    }

}

The first few statements still won't show the method and line number because they're logged before the app's run method is called. Doing this on the initialize method won't work because the async appender doesn't seem to be available yet.

Also, the name of the appender used in root.getAppender will change if you're not using - type: console in the configuration. Since I'm not sure the names will always follow this async-<type>-appender format, I believe the best way to check out which name to use is by debugging your application. Inspect the root logger's attributes looking for it's AppenderAttachableImpl, then check the appender list. Use the name of the async appender you want to set method name and line number logging.

like image 89
andrepnh Avatar answered Dec 16 '22 12:12

andrepnh