Does log4j provide any mechanism to daily archive

2019-06-19 00:17发布

问题:

Does log4j 1.2 provide any mechanism to daily archive log?

Everybody say that i can do it via org.apache.log4j.rolling.TimeBasedRollingPolicy but in sources of 1.2.15 i don't see any TimeBasedRollingPolicy class.

I found a resolution :

<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
   <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>

   <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
       <param name="ActiveFileName" value="${jboss.server.log.dir}/server.log"/>
       <!-- roll log file once a day -->
       <param name="FileNamePattern" value="${jboss.server.log.dir}/archives/server.log.%d.gz"/>
   </rollingPolicy>

   <!-- A PatternLayout that limits the number of lines in stack traces -->
   <layout class="com.mtvi.log4j.StackTraceLimitingPatternLayout">
       <!-- The full pattern: Date MS Priority [Category] (Thread) Message\n -->
       <param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
   </layout>
</appender>

回答1:

You need to define your appender as DailyRollingFileAppender and define the date pattern to be up to day granularity. The following is an example appender named 'file' that outputs to application.log and rolls the file daily by appending the date to the end after midnight and starting a new file.

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=application.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] - %m%n

You will then need to define your loggers (or rootLogger) to output to this appender. For example:

log4j.rootLogger=debug, file


回答2:

What you ask can be done using DailyRollingFileAppender.



回答3:

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=application.log
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %5p [%t] - %m%n

Configuring log4j.Appender.file as per above in my application.properties file doesn't seem to work in my SpringBootApplication. In the end, I used the logback-spring.xml file solution. The file is placed in src/main/recources (the default folder where you usually put your application.properties file).

No maven dependency required in your pom.xml but create a /logs/archived folder in the root directory of your application. The file 'nameOfYourLog.log' will be automatically archived on the next day into the ./logs/archived folder. Since this folder is positioned outside of your jar file, it is easily accessible to view the log (for the desired date) when it is deployed into a production server.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

    <property name="DEV_HOME" value="./logs" />
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>
    <!--<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
    <appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/nameOfYourLog.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
            </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
    </appender>

    <logger name="com.the.package.you.wish.to.log" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE-AUDIT" />
    </logger>

    <root level="error">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE-AUDIT" />
    </root>

</configuration>


回答4:

Use log4j-extras:

<appender name="FILE" class="org.apache.log4j.rolling.RollingFileAppender">
   <errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>

   <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
       <param name="ActiveFileName" value="${jboss.server.log.dir}/server.log"/>
       <!-- roll log file once a day -->
       <param name="FileNamePattern" value="${jboss.server.log.dir}/archives/server.log.%d.gz"/>
   </rollingPolicy>

   <!-- A PatternLayout that limits the number of lines in stack traces -->
   <layout class="com.mtvi.log4j.StackTraceLimitingPatternLayout">
       <!-- The full pattern: Date MS Priority [Category] (Thread) Message\n -->
       <param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
   </layout>
</appender>

Note: This answer was extracted from the OP's question, in order to preserve the correct format mandated by Stack Exchange. (The OP clearly didn't respond to jbx's comment.)