log4j2 RollingRandomAccessFile configuration

1. Background of demand
1. Logs are compressed hourly into zip files.
2. Only save the historical compressed files of the last 24 hours from the current time.
3. Compress the archived zip file, and correct the deviation according to the zero point as the reference point.
4. Output the logs of the com.roadway.acceptor.base.DebugUtils class to the specified file and no longer output to other files.
 
Second, log4j2 configuration implementation
<?xml version="1.0" encoding="UTF-8"?>

<Configuration status="INFO" monitorInterval="120">
    <properties>  
        <property name="MSG_LOG_HOME">/data/gpslog</property>
    </properties>

    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%t] %-5p [%c] - %m%n" />
        </Console>

        <RollingRandomAccessFile name="msgAppender" immediateFlush="true"
            fileName="${MSG_LOG_HOME}/msg.log"
            filePattern="${MSG_LOG_HOME}/backup/msg.%d{yyyyMMddHH}.zip">

            <Filters>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <PatternLayout pattern="%m%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>  

            <DefaultRolloverStrategy max="24">
                <Delete basePath="${MSG_LOG_HOME}" maxDepth="2">
                  <IfFileName glob="*/msg.*.zip" />
                  <IfLastModified age="24H" />
                </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
    </Appenders>

    <Loggers>
        <AsyncLogger name="com.roadway.DebugUtils" additivity="FALSE" level="INFO">
            <appender-ref ref="msgAppender" />
        </AsyncLogger>
    </Loggers>
</Configuration>
 
 3. Configuration instructions
1. monitorInterval , the blog configuration is 120, and the unit is seconds. That is, if the log4j2 configuration file is modified during the service running, log4j2 can reload the configuration within the monitorInterval time range without restarting the application.
 
2. The declaration of the global attribute in the property configuration file is used as follows: ${declared attribute name}.
    ${sys:catalina.home} is the tomcat deployment path, for example: /data/tomcat.
 
3. RollingRandomAccessFile basic properties
    name: Appender name
    immediateFlush: Whether log4j2 immediately flushes the log to disk when it receives a log event. Defaults to true.
    fileName: log storage path
    filePattern: History log archive path. %d{yyyyMMddHH} indicates the time unit for storing historical logs (currently, the unit is hours, yyyy indicates years, MM indicates months, dd indicates days, HH indicates hours, mm indicates minutes, ss indicates seconds, and SS indicates milliseconds). Pay attention to the suffix, log4j2 automatically recognizes suffixes such as zip, indicating that historical logs need to be compressed.
 
4. TimeBasedTriggeringPolicy
interval: Indicates the historical log archive interval, the unit is the unit value set by filePattern
modulate: Indicates whether the historical log generation time is corrected, and the correction is performed based on the zero point. For example: msg.2017041715.zip file is generated at 15:16, then msg.2017041716.zip will be generated at 16:00 after correction
 
5. ThresholdFilter
     level, indicating the minimum acceptable log level, the blog is configured as INFO, that is, we expect to print logs above the INFO level.
     onMatch, which indicates what to do when the log level of the log event is the same as the level. Usually ACCEPT, which means acceptance.
     onMismatch, indicates what to do when the log level of the log event is inconsistent with the level. Generally, it is DENY, which means refusal. Can also be neutral for NEUTRAL.
 
6. Save 24 hours of historical logs, but don't want to use file indexing
<DefaultRolloverStrategy max="24">
    <Delete basePath="${MSG_LOG_HOME}" maxDepth="2">
        <IfFileName glob="*/msg.*.zip" />
        <IfLastModified age="24H" />
    </Delete>
</DefaultRolloverStrategy>
 
Remark:
1. The unit of age: D, H, M, S, representing days, hours, minutes, seconds, respectively
2. basePath represents the base directory of log storage, and maxDepth=“1” represents the current directory. Because the history log we archived is in the backup directory in basePath, maxDepth is set to 2.
 
7. RollingRandomAccessFile setting bufferSize does not take effect
a. The log4j2 configuration is as follows:
<RollingRandomAccessFile name="msgAppender"
   immediateFlush="false"
   bufferSize="512"
   fileName="${MSG_LOG_HOME}/msg.log"
   filePattern="${MSG_LOG_HOME}/backup/msg.%d{yyyyMMddHH}.zip">

   ......
b. Use the asynchronous Logger method to output logs
......

<AsyncLogger name="com.roadway.DebugUtils" additivity="FALSE" level="INFO">
    <appender-ref ref="msgAppender" />
</AsyncLogger>

......
 
c. Verify
After repeated testing and verification, the log is always flushed to disk in real time, why? Looking at the log4j2 documentation found:
Asynchronous loggers and appenders will automatically flush at the end of a batch of events, even if immediateFlush is set to false. This also guarantees the data is written to disk but is more efficient.
 
Therefore, if you expect to use RollingRandomAccessFile prints the output log asynchronously. The bufferSize cannot take effect and there is no need to use the buffer method.
 
Please refer to the log4j2 official website address: http://logging.apache.org/log4j/2.x/manual/appenders.html
 
RollingRandomAccessFile official website description
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326248249&siteId=291194637