gradle boot log4j2

[list]
  • 1. First remove the logback dependency and add the following in gradle
  • configurations {
    	all*.exclude module: 'spring-boot-starter-logging'
        all*.exclude module: 'logback-classic'
        all*.exclude module: 'log4j-over-slf4j'
    }
    
  • 2. Add log4j2 dependency and log4j2 read yaml file dependency
  • compile ('org.springframework.boot:spring-boot-starter-log4j2')	
    compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.2'
    
  • 3. Add the file log4j2.yaml below the classpath, the example content is as follows (Please ignore the prompt of Unknown property 'Configuration'). Change the package path and log write disk location by yourself
  • Configuration:  
      status: warn  
      
      Properties: # define global variables  
        Property: # Default configuration (for development environment). Other environments need to be specified in the VM parameters, as follows:  
          #测试:-Dlog.level.console=warn -Dlog.level.ccl=trace  
          #生产:-Dlog.level.console=warn -Dlog.level.ccl=info        
          - name: log.level.console  
            value: trace  
          - name: log.level.ccl  
            value: trace         
          - name: log.path  
            value: C:/logs  
          - name: project.name  
            value: my-spring-boot  
        
      Appenders:  
        Console: #Output to console  
          name: CONSOLE  
          target: SYSTEM_OUT  
          ThresholdFilter:  
            level: ${sys:log.level.console} # "sys:" means: if this variable value is not specified in the VM parameters, the default global variable value defined in this file is used  
            onMatch: ACCEPT  
            onMismatch: DENY  
          PatternLayout:  
            pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %X{user} %t (%F:%L) - %m%n"  
        RollingFile: # output to file, archive over 128MB  
          - name: ROLLING_FILE  
            ignoreExceptions: false  
            fileName: ${log.path}/${project.name}.log  
            filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"  
            PatternLayout:  
              pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"  
            Policies:  
              SizeBasedTriggeringPolicy:  
                size: "128 MB"  
            DefaultRolloverStrategy:  
              max: 1000  
      
      Loggers:  
        Root:  
          level: info  
          AppenderRef:  
            - ref: CONSOLE  
            - ref: ROLLING_FILE  
        Logger: # Configure a special log level for the com.ccl package to facilitate debugging  
          - name: com.ccl  
            additivity: false  
            level: ${sys:log.level.ccl}  
            AppenderRef:  
              - ref: CONSOLE  
              - ref: ROLLING_FILE  
    
  • 4. Configure the output level in application.yaml. The following log is invalid for app startup (it is still valid in the test case):
  • At this time, refer to the name:value under [Define Global Variables] in log4j.yaml to configure the boot log.
    logging:
      level:
        root: warn
    
  • 5. Test Cases
  • 
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.slf4j.MDC;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.ccl.Application;
    
    @SpringBootTest(classes = Application.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class Log4j2Test {
    
    	private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    	@Test
    	public void test() throws Exception {
    		MDC.put("user", "I don't live while driving, I'll find my brother when I'm in trouble.");//Corresponding to the %X{user} variable in the pattern of the configuration file
    		logger.trace("I am trace log.");
    		logger.debug("I am debug log.");
    		logger.warn("I am warn log.");
    		logger.error("I am error log.");
    	}
    }
    

    [/list]

    Guess you like

    Origin http://10.200.1.11:23101/article/api/json?id=326654805&siteId=291194637