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