当没有活动时,log4j不会旋转日志

时间:2014-01-02 09:38:34

标签: tomcat log4j

当没有活动时,

log4j没有旋转日志(tomcat服务器日志catalina.log和localhost.log)。这是我的log4j.properties。但如果日志有活动,则日志正在旋转。请建议任何帮助赞赏

  

Log4j版本: - log4j-1.2.17.jar

 log4j.rootLogger=INFO, CATALINA

    log4j.appender.CATALINA=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.out
    log4j.appender.CATALINA.Append=true
    log4j.appender.CATALINA.Encoding=UTF-8
    log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH-mm
    log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
    log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

    log4j.appender.LOCALHOST=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.log
    log4j.appender.LOCALHOST.Append=true
    log4j.appender.LOCALHOST.Encoding=UTF-8
    log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd-HH-mm
    log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
    log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

    log4j.appender.MANAGER=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.MANAGER.File=${catalina.base}/logs/manager.log
    log4j.appender.MANAGER.Append=true
    log4j.appender.MANAGER.Encoding=UTF-8
    log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
    log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
    log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

    log4j.appender.HOST-MANAGER=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.log
    log4j.appender.HOST-MANAGER.Append=true
    log4j.appender.HOST-MANAGER.Encoding=UTF-8
    log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
    log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
    log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.Encoding=UTF-8
    log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

    # Configure which loggers log to which appenders
    log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
    log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
      INFO, MANAGER
    log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
      INFO, HOST-MANAGER

1 个答案:

答案 0 :(得分:0)

这是预期的DailyRollingFileAppender,因为当实际记录某些内容时,作为subAppend()方法的一部分发生翻转。此方法的Javadoc声明:

  

在实际记录之前,此方法将检查是否到时候   翻滚。如果是,它将安排下一个翻转时间和   翻滚。

如果您要求文件在午夜轮换 - 最简单的方法是在00:00:00安排日志消息。这将触发日志文件翻转。如果您正在使用JEE,您可以执行以下操作:

import javax.ejb.Schedule;
import javax.ejb.Singleton;

@Singleton
public class RollLogs {
        static Logger logger = Logger.getLogger(RollLogs.class);
    @Schedule(second="0", minute="0", hour="0", dayOfWeek="*", persistent=false)
    public void rollLogs() {
        logger.info("midnight");
    }
}