0

I need to set maxfile size for my application but currently i am using Dropwizard core version 0.8.4 and its file appender does not support this feature.

So I approached like below by writing a custom appender as updating to latest dropwizard(which support my need) version not an option right now.

   private void initLogging(Configuration configuration) throws JoranException {
    final File logDir = new File("/tmp/enforcer");
    final File logFile = new File(logDir, "wallet.log");

    final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender<ILoggingEvent> rollingFileAppender = new RollingFileAppender<ILoggingEvent>();
    rollingFileAppender.setFile(logFile.getAbsolutePath());
    rollingFileAppender.setName("com.documents4j.logger.server.file");
    rollingFileAppender.setContext(loggerContext);

    FixedWindowRollingPolicy fixedWindowRollingPolicy = new FixedWindowRollingPolicy();
    fixedWindowRollingPolicy.setFileNamePattern(logFile.getAbsolutePath() +"%i.gz");
    fixedWindowRollingPolicy.setMaxIndex(7);
    fixedWindowRollingPolicy.setContext(loggerContext);
    fixedWindowRollingPolicy.setParent(rollingFileAppender);
    fixedWindowRollingPolicy.start();

    SizeBasedTriggeringPolicy<ILoggingEvent> sizeBasedTriggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>();
    sizeBasedTriggeringPolicy.setMaxFileSize("2KB");
    sizeBasedTriggeringPolicy.setContext(loggerContext);
    sizeBasedTriggeringPolicy.start();



    rollingFileAppender.setRollingPolicy(fixedWindowRollingPolicy);
    rollingFileAppender.setTriggeringPolicy(sizeBasedTriggeringPolicy);

    rollingFileAppender.start();

    System.out.println("Logging: The log is written to " + logFile);
    final ch.qos.logback.classic.Logger log = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    log.setLevel(Level.DEBUG);
    log.addAppender(rollingFileAppender);
}


 @Override
public void run(Configuration configuration, Environment environment) throws Exception
{

   initLogging(configuration);
}

My yaml file config is

logging:
  level: INFO
  org.springframework.retry.support.RetryTemplate: DEBUG
  appenders:
  - type: file
  currentLogFilename: /tmp/enforcer.log
  threshold: ALL
  archive: true
  archivedLogFilenamePattern: /tmp/enforcer-%d.log
  archivedFileCount: 5
  timeZone: UTC
  logFormat: '%-5level [%date{yyyy-MM-dd HH:mm:ss,SSS}] [%X{realdocRequestId}] %logger{15}: %m%n'

Now when i run my application i noticed, even though custom log file (/tmp/enforcer/wallet.log) is created in the particular directory but actual log is not dumped i.e. wallet.log file size is 0 kb where as the log file configured in yaml is created and size is certain kb and goes on increasing as log event is generated.

I am not able figure out what is wrong im doing, help will be appreciated.

RIPAN
  • 3,326
  • 4
  • 17
  • 28
  • 1
    All dropwizard version support custom loggers. You just need to create them and tell DW about them. I have demonstrated how to do that here: http://stackoverflow.com/questions/27483442/dropwizard-doesnt-log-custom-loggers-to-file/33085996#33085996 You do not need to overwrite the factory. You just need to create a new Logger impl, annotate it correctly and add it to the META-INF location so DW can find and initialise it. – pandaadb Mar 23 '16 at 17:23
  • @pandaadb thanks for your answer, that helps but i want to know why my code fails??What im missing here?? – RIPAN Mar 30 '16 at 09:55

1 Answers1

1

your logger doesn't log anything because you never set an encoder to it. Set a breakpoint in:

OutputStreamAppender#start() and you will see that there is no encoder.

It will work once you add:

        LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
        DropwizardLayout formatter = new DropwizardLayout(loggerContext, TimeZone.getDefault());
        formatter.setPattern("[%level] [%h] %d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', UTC} [%thread] [%logger] %msg%n");
        layoutEncoder.setLayout(formatter);
        formatter.start();
        rollingFileAppender.setEncoder(layoutEncoder);

Of course you can define whichever format you like (and formatter).

But keep in mind that this is a fairly hacky example of what you try to achieve. You now have 2 points in code where you configure logging (DW and your own). You have yaml configured logging as well as your own. I recommend investing a bit of work and adding a proper AppenderFactory that you can use.

Hope that helps,

Artur

pandaadb
  • 6,306
  • 2
  • 22
  • 41