log4j2自动删除 历史日志 配置

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

status="ALL" monitorInterval="30">
    
        
        
        name="LOG_HOME">logs112
        name="ERROR_LOG_FILE_NAME">${LOG_HOME}/error
        name="WARN_LOG_FILE_NAME">${LOG_HOME}/warn
        name="INFO_LOG_FILE_NAME">${LOG_HOME}/info
        name="DEBUG_LOG_FILE_NAME">${LOG_HOME}/debug

        name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} [%M-%L-%t] %-5level %logger{36} - %msg%n
        
        
    

    
        
        name="Console" target="SYSTEM_OUT">
            
            level="trace" onMatch="ACCEPT"
                             onMismatch="DENY"/>
            
            
                                pattern="${PATTERN}"/>
        

        
        
        name="log" fileName="${LOG_HOME}/test.log" append="false">
                                pattern="${PATTERN}"/>
        
        
        name="RollingFileDebug" fileName="${DEBUG_LOG_FILE_NAME}/debug.log"
                     filePattern="${DEBUG_LOG_FILE_NAME}/$${date:yyyy-MM}/debug-%d{yyyy-MM-dd}-%i.log">
            
                
                level="debug"/>
                level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
            

                                pattern="${PATTERN}"/>
            
                
                size="2 kB"/>
            
            
            
            max="100">
                basePath="${DEBUG_LOG_FILE_NAME}" maxDepth="2">
                    glob="*/debug-*.log">
                        age="7d">
                            
                                exceeds="100 GB" />
                                exceeds="10" />
                            
                        
                    
                
            
        

        name="RollingFileInfo" fileName="${INFO_LOG_FILE_NAME}/info.log"
                     filePattern="${INFO_LOG_FILE_NAME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            
                
                level="info"/>
                level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
            
            
            
                                pattern="${PATTERN}"/>
            
                
                
                
                
                
                
                 size="2 kB" />
            
            max="100">
                basePath="${INFO_LOG_FILE_NAME}" maxDepth="2">
                    glob="*/info-*.log">
                        age="7d">
                            
                                exceeds="100 GB" />
                                exceeds="10" />
                            
                        
                    
                
            
        

        name="RollingFileWarn" fileName="${WARN_LOG_FILE_NAME}/warn.log"
                     filePattern="${WARN_LOG_FILE_NAME}/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
            
                
                level="warn"/>
                level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
            
            
                                pattern="${PATTERN}"/>
            
                
                size="2 kB"/>
            
            
            max="100">
                basePath="${WARN_LOG_FILE_NAME}" maxDepth="2">
                    glob="*/warn-*.log">
                        age="7d">
                            
                                exceeds="100 GB" />
                                exceeds="10" />
                            
                        
                    
                
            
        

        name="RollingFileError" fileName="${ERROR_LOG_FILE_NAME}/error.log"
                     filePattern="${ERROR_LOG_FILE_NAME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd-HH-mm}-%i.log">
            
                
                level="error"/>
                
            
            
                                pattern="${PATTERN}"/>
            
                
                interval="1" modulate="true"/>
                
            
            max="100">
                basePath="${ERROR_LOG_FILE_NAME}" maxDepth="2">
                    glob="*/error-*.log">
                        age="7d">
                            
                                exceeds="100 GB" />
                                exceeds="10" />
                            
                        
                    
                
            
        

        
        name="thread">
            
            pattern="$${ctx:userId120231}">
                
                    name="RollingFile-${ctx:userId120231}" fileName="${LOG_HOME}/${ctx:userId120231}.log"
                                 filePattern="${LOG_HOME}/${ctx:userId120231}-%d{HH-mm-ss}.log" append="false">
                        
                        pattern="%X{userId120231} ${PATTERN}"/>
                        
                            size="100M"/>
                        
                        max="100"/>
                        
                                    
                        
                    
                
                ref="STDOUT" key="${ctx:userId120231}"/>
            
        

    

    
    
        


        
  


        
        level="all">
            ref="Console"/>
            ref="RollingFileDebug"/>
            ref="RollingFileInfo"/>
            ref="RollingFileWarn"/>
            ref="RollingFileError"/>
            ref="thread"/>
        

    



package test.data.com.main;

import org.apache.logging.log4j.*;

import java.util.*;

/**
 * @param
 * @param
 * @param
 * @author HUAZAI
 * @title:
 * @description:
 * @return 返回类型 :
 * 
    *
  • *
  • *
      * @throws * @date 18-4-2 上午11:42 */ public class MainTest { private static final org.apache.logging.log4j.Logger logger1 = org.apache.logging.log4j.LogManager.getLogger(MainTest.class); // private static final org.apache.log4j.Logger logger1 = org.apache.log4j.Logger.getLogger(MainTest.class); private static void testLogger(){ int i = 1; String uuid1 = UUID.randomUUID().toString(); String uuid2 = UUID.randomUUID().toString(); while (true) { ThreadContext.put("userId120231", uuid1);//log4j2日志的MDC分线程写日志 ThreadContext.put("userId132018", uuid2); logger1.info("log4j2 info"); logger1.info("这是第 " + ((i-1) * 2) + " "); logger1.debug("log4j2 debug"); logger1.warn("log4j2 warn"); // logger1.error("log4j2 error"); logger1.trace("log4j2 trace"); logger1.fatal("log4j2 fatal"); i++; try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } // int i = 0; // try { // i /= i; // } catch (Exception e) { // e.printStackTrace(); // logger1.error("出现异常", e); // } } public static void main(String[] args) { testLogger(); } }

你可能感兴趣的:(log4j2使用)