Dropwizard doesn't log custom loggers to file

Carmine Giangregorio picture Carmine Giangregorio · Dec 15, 2014 · Viewed 11.4k times · Source

I have a dropwizard app, where I configured logger appenders to file as follows:

logging:
  level: INFO

  loggers:
    "mylogger": INFO
    "com.path.to.class": INFO

  appenders:
    - type: file
      currentLogFilename: .logs/mylogs.log
      archivedLogFilenamePattern: .logs/archive.%d.log.gz
      archivedFileCount: 14

And, created logger in my app:

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

private final Logger OpLogger = LoggerFactory.getLogger("mylogger");
(and)
private final Logger ClassLogger = LoggerFactory.getLogger(pathToClass.class);

Do some test logging in main():

OpLogger.info("test 1");
ClassLogger.info("test 2);

The application starts and runs without problems; but I don't get any logs (except for Jetty access logs, of course, that are correctly printed to mylogs.log), neither in stdout or in mylogs.log file. Instead, if I remove the loggers configuration in configuration.yml, I get all logs printed to stdout. Perhaps it's a problem of dropwizard or I have to add something to configuration.yml? I'm using Dropwizard 0.8.0

Answer

Kartik Jajal picture Kartik Jajal · Dec 16, 2014

You can implement separate logger with the dropwizard using logback.

1.Configure logger in your Application class (i.e application start point with main method) like below.

LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
context.reset();
ContextInitializer initializer = new ContextInitializer(context);
initializer.autoConfig();

2.Configure logback.xml like below.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="OpLogger " class="ch.qos.logback.core.FileAppender">
    <file>/var/log/applicationname-mylogger.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <append>false</append>
    <encoder>
        <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>
<appender name="classLogger" class="ch.qos.logback.core.FileAppender">
    <file>/var/log/applicationame-com.path.to.class.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
        <!-- keep 30 days' worth of history -->
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <append>false</append>
    <encoder>
        <pattern>%-5relative %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>
<logger name="mylogger">
    <level value="INFO" />
    <appender-ref ref="OpLogger" />
</logger>
<logger name="com.path.to.class">
    <level value="INFO" />
    <appender-ref ref="classLogger" />
</logger>
</configuration>

3.Now use logger

static final Logger OpLogger = LoggerFactory.getLogger("mylogger");
static final Logger classLogger = LoggerFactory.getLogger("com.path.to.class");

EDIT :

I have try to implement the same logger in my example project. It works fine in my case. We can not use the LOGGER before the Dropwizard application initialize. The Dropwizard initialized only when you call

  new ExampleApplication().run(args);

So, if logger is used before Dropwizard initialized, your log will not be there. I have tried to implemented scenario with main method. The first log statement is not printed as we have used logger before the Dropwizard initialization, but the second log statement will be printed.

  OpLogger.info("test 1");
  new ExampleApplication().run(args);
  ClassLogger.info("test 2);

Hope this will help you out to solve your problem.