0

I need to rotate or backup the current log file on startup but can't work out how to do it.

I have a typical log config:

appenders:
    - type: console
      threshold: INFO
    - type: file
      threshold: DEBUG
      logFormat: "%-6level [%d{HH:mm:ss.SSS}] [%t] %logger{5} - %X{code} %msg %n"
      currentLogFilename: /var/log/dw-service.log
      archive: true
      archivedLogFilenamePattern: /var/log/tmp/dw-service-%d{yyyy-MM-dd}-%i.log.gz
      archivedFileCount: 3
      timeZone: UTC
      maxFileSize: 10MB

Ideally I would like to cause an archive of the logfile immediately on startup but failing that, I would settle for getting a handle to the appender and copying the file.

I can't work out how to do either. Please could somebody offer any help?

I've got this far:

private void backupLogFile() {

    ch.qos.logback.classic.Logger log = (ch.qos.logback.classic.Logger)LoggerFactory.getLogger(LOG.ROOT_LOGGER_NAME);

    Iterator<Appender<ILoggingEvent>> itr = log.iteratorForAppenders();
    List<Appender<ILoggingEvent>> appenders = new LinkedList<Appender<ILoggingEvent>>();
    while (itr.hasNext()) {
        appenders.add(itr.next());

    }


    for(int i=0;i<appenders.size();++i){
        if(appenders.get(i).getName().equals("async-file-appender"))
        {
            LOG.info("FOUND FILE APPENDER");

            FileAppender myFile = (FileAppender)appenders.get(i);
                  String filename = myFile.getFile();

        }

    }



}

At runtime, Java tells me

Exception in thread "main" java.lang.ClassCastException: ch.qos.logback.classic.AsyncAppender incompatible with ch.qos.logback.core.FileAppender

I can't work out how to get the configured currentLogFilename, anybody know how do this?

Thanks

pandaadb
  • 6,306
  • 2
  • 22
  • 41
HAL2000
  • 1
  • 2
  • If you wanted to do this on startup, you would need to look into overwriting the AppenderFactory (file appender) that you are using. Particularily the build() file. Otherwise you'll need to get the root looger and unpack the correct appender which will likely result in heaps of instanceof calls (I think) and then do it there – pandaadb Feb 14 '17 at 14:52
  • How can I get the root logger? The LoggerFactory class only provides getLogger. I have tried to get the file appender from this without any success – HAL2000 Feb 15 '17 at 09:16
  • In my setup: org.apache.log4j.Logger.getRootLogger(); – pandaadb Feb 15 '17 at 10:25
  • I've got this far: – HAL2000 Feb 15 '17 at 13:22
  • http://stackoverflow.com/questions/33996115/how-to-rotate-a-log4j-log-manually this might be of help – pandaadb Feb 15 '17 at 13:30
  • Look in AppenderFactory. Unless you specify it, all appenders are wrapped in an async appender. This is so that your logging doesn't cripple the application. You will have to cast it to that and then unwrap your logger there. – pandaadb Feb 15 '17 at 13:41
  • Additionally, if you wanted to look into how to extend the Factory abilities (because then it simply becomes 1 line in the build method), I have described that here: http://stackoverflow.com/questions/27483442/dropwizard-doesnt-log-custom-loggers-to-file/33085996#33085996 – pandaadb Feb 15 '17 at 14:45

0 Answers0