1

So Basically what I want to do is to log logs with different logs levels like INFO DEBUG WARN etc to different log files.

I have found something similar for slf4j here but dropwizard uses logback as logging framework.

Can anybody tell me how to do it in dropwizard with logback? What changes should I make in my conifg.yml file?

 level: INFO
  loggers:
    "io.dropwizard": INFO
    "org.hibernate.SQL":
      level: DEBUG
      additive: false
      appenders:
        - type: file
          currentLogFilename: /var/log/myapplication-sql.log
          archivedLogFilenamePattern: /var/log/myapplication-sql-%d.log.gz
          archivedFileCount: 5
Aditya Verma
  • 428
  • 6
  • 22

1 Answers1

0

Here's what you need to do:

  1. Implement or find some existing code for custom logback filter.
    Here's an example of filter that passes only ERROR-level messages:
package foo;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.filter.FilterFactory;

@JsonTypeName("errors-only")
public class ErrorsOnlyFilterFactory implements FilterFactory<ILoggingEvent> {
    @Override
    public Filter<ILoggingEvent> build() {
        return new Filter<>() {
            @Override
            public FilterReply decide(ILoggingEvent event) {
                return event.getLevel() == Level.ERROR ?
                        FilterReply.ACCEPT :
                        FilterReply.DENY;
            }
        };
    }
}
  1. Create file
    <resources>/META-INF/services/io.dropwizard.logging.filter.FilterFactory
    and populate it with filter class name. For my example, file contains only one line:
foo.ErrorsOnlyFilterFactory
  1. Add value of @JsonTypeName annotation to appender config, under filterFactories/type (note: type is an array, so you can specify multiple filters per appender if needed):
logging:
  appenders:
    - type: file
      currentLogFilename: errors.log
      ...
      filterFactories:
        - type: errors-only

For every logging level that you want to log separately, you should:

  • create new filter class
  • add new line to META-INF/services/io.dropwizard.logging.filter.FilterFactory
  • add new appender config
Slava Medvediev
  • 1,431
  • 19
  • 35