logback & log4j asynchronous log configuration

logback
原始配置


配置 appender,  控制文件的滚动方式,日志的输出格式。
 <appender name="method-time-appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${saipan.logger.home}/method_profiling.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${saipan.logger.home}/method_profiling.log.%d{yyyy-MM-dd}-%i
            </fileNamePattern>
            <!-- keep 7 days' worth of history -->
            <maxHistory>7</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>1GB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder charset="utf-8">
            <pattern>%d{HH:mm:ss} - %msg%n</pattern>
        </encoder>
    </appender>

配置logger, 指向上面的appender
<logger name="method_profiling" additivity="false">
        <level value="info"/>
        <appender-ref ref="method-time-appender"/>
</logger>
修改为如下的配置

修改配置appender,
    <appender name="method-time-appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${saipan.logger.home}/method_profiling.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>${saipan.logger.home}/method_profiling.log.%d{yyyy-MM-dd}-%i
            </fileNamePattern>
            <!-- keep 7 days' worth of history -->
            <maxHistory>7</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy
                    class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>1GB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </ rollingPolicy> 
        <encoder charset="utf-8">
            <pattern>%d{HH:mm:ss} - %msg%n</pattern>
        </ Encoder> 
    </ the appender> 

followed by an asynchronous configuration append, and points above the appender
     <the appender name = "ASYNC-the METHOD-the TIME-Appender" class = "ch.qos.logback.classic.AsyncAppender" > 
        <! - the internal implementation is a bounded ArrayBlockingQueue, queueSize a queue size. The default value will affect the performance value 256 -.> 
        <QueueSize> 512 </ QueueSize> 
        <-! When the remaining capacity of the queue is less than this threshold and the current log level TRACE, DEBUG or INFO, these logs are discarded. The default is 20% queueSize size. -> 
        <discardingThreshold> 0 </ discardingThreshold> 
        <-! = NeverBlock to true then write queue when logging method calls clog offer queue instead put, if the queue is full then return directly, instead of blocking, that is, the log is discarded. -> 
        <NeverBlock> to true </ NeverBlock> 
        <! 
        <REF-REF = the appender "Method-Time-the appender" />

    <logger name="method_profiling" additivity="false">
        <level value="info"/>
        <appender-ref ref="ASYNC-METHOD-TIME-APPENDER"/>
    </logger>
log4j 

If you are using org.apache.log4j.DailyRollingFileAppender to configure, your configuration may be such a.
<! - log output to a file, you can configure how long to generate a new log file -> <appender name = "dailyRollingAppender" class = "org.apache.log4j.DailyRollingFileAppender"> <! - file full path name -> <param name = "File" value = "/ Data / applogs / own / dailyRollingAppender.log" /> <param name = "the Append" value = "to true" /> <-! log backup set frequency, a default : a daily log file -> <param name = "DatePattern" value = " '.' the MM-YYYY-dd'.log '" /> ! <- a backup per minute -> <- <! param name = "DatePattern" value = " '.' yyyy-mM-dd-HH-mm ' <param name="ConversionPattern" value="[%p][%d{HH:mm:ss SSS}][%c]-[%m]%n"/> </layout> </appender> 和logback一样,在logger和appender之间增加 AsyncAppender 配置。 <appender name="SYSTEM" class="com.alibaba.common.logging.spi.log4j.DailyRollingFileAppender"> <param name="file" value="${relationplatform.logging.root}/relationplatform-sys.log"/> <param name="append" value="true"/> <param name="encoding" value="GBK"/> <param name="threshold" value="WARN"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%p]%d{yyyy-MM-dd HH:mm:ss,SSS} %C.%M(%L)| %m%n"/> </ layout> </ the appender> class = "org.apache.log4j.AsyncAppender"> <-! internal implementation is a list, BufferSize a list size. This value will affect the performance of the default value is 128 -.> <Param name = "BufferSize" value = "512" /> <-! = False Blocking the write queue is full when the log does not block the calling thread. Defaults to true -> <param name = "Blocking" value = "to false" /> <param name = "LocationInfo" value = "to true" /> <REF-REF = the appender "the SYSTEM" /> </ the appender> < name = Logger "RELATION_PLATFORM_SERIOUS_LOG" = the additivity "to false"> <Level value = "$ {relationplatform.loggingLevel}" /> < <appender name="ASYNC-SYSTEM"

 

Guess you like

Origin www.cnblogs.com/sidesky/p/11354355.html