How to rotate a log4j log manually

前端 未结 3 1835
忘了有多久
忘了有多久 2021-01-20 15:04

I have log4j configured to rotate the log every day.

In special situations I would like to trigger an additional log rotation manually.

Is this possible - an

3条回答
  •  攒了一身酷
    2021-01-20 15:53

    For log4j2, you can use following.

        org.apache.logging.log4j.Logger logManagerLogger = LogManager.getLogger();
        Map appenders = ((org.apache.logging.log4j.core.Logger) logManagerLogger).getAppenders();
        appenders.forEach((appenderName, appender) -> {
            if (appender instanceof RollingFileAppender) {
                logger.info("Rolling over appender " + appenderName);
                ((RollingFileAppender) appender).getManager().rollover();
            }
        });
    

提交回复
热议问题