Spingboot use log4j2

1.pom.xml join log4j2, while the spring boot the default logging removed, all attention must be excluded to avoid an error.

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
      </dependency>

2.log4j2.xml configuration, the default location is classpath: log4j2.xml, if not the default location you need to configure the default name or properties in yml

logging:
        config: classpath:log4j2.xml  


or
logging.config=classpath:log4j2.xml  
<? Xml Version = "1.0" encoding = "UTF-8" ?> 
<-! Log level and prioritize: OFF> FATAL> ERROR> WARN> INFO> DEBUG> the TRACE> ALL -> 
<-! Configuration Status later, the setting information output log4j2 own internal, may not be provided, when set to trace, one will see in detail the various internal outputs log4j2 -> 
<-! MonitorInterval: Log4j can automatically detect modification profiles and reconfigure itself to set the number of seconds -> 
< the configuration Status = "WARN" MonitorInterval = "30" > 
    <-! define all appender -> 
    < the appenders in > 
    <-! this output console configuration -> 
        < Console name = "Console" target="SYSTEM_OUT">
        <! - Output log format -> 
            < the PatternLayout pattern = "[% D {HH: mm: SS: the SSS}] [% P] -% m -% n-C%" /> 
        </ Console > 
    <! - file will print out all the information, the log will be automatically cleared each time you run the program, the append attribute decision, this is also quite used for temporary test -> 
    < file name = "log" fileName = "log / the test .log " the append =" to false " > 
       < the PatternLayout pattern ="% D {HH: mm: ss.SSS%} {36} class -5level% L% M% -%% XEX% n-MSG " /> 
    </ File > 
    <!- This will print out all the info and the following levels of information, each larger than size, this size is the size of the log will be automatically credited by year - File month created folder below and compression, as an archive -> 
        < RollingFilename = "RollingFileInfo" fileName = "$ {SYS: user.home} /logs/info.log" 
                     filePattern = "$ {SYS: user.home} / logs / $$ {DATE: the MM-YYYY} / info-% the MM-YYYY-{D} dd - i.log% " > 
            <-! console just above the level of the output level and the information (onMatch), other direct rejection (onMismatch) ->         
            < ThresholdFilter level =" info " onMatch = "ACCEPT" onMismatch = "DENY" /> 
            < the PatternLayout pattern = "[% D {HH: mm: SS: the SSS}] [% P] -% m% n-" /> 
            < Policies > 
                < TimeBasedTriggeringPolicy /> 
                <SizeBasedTriggeringPolicy size= "100 MB" /> 
            </ Policies > 
        </ RollingFile >       
    </ the appenders in > 
    <-! Then define the logger, must define the logger and the introduction of appender, appender take effect -> 
    < Loggers > 
        <! - DEBUG filter out unwanted information and mybatis the spring -> 
        < Logger name = "org.springframework" Level = "the iNFO" > </ Logger > 
        < Logger name = "org.mybatis" Level = "the iNFO" > </ Logger >     
     
     
            <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
        </root>
    </loggers>
</configuration>

use

package com.my.sb.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@SpringBootApplication
@RequestMapping("/mvc")
public class TestController {
    
    private static final Logger logger = LogManager.getLogger(TestController.class);
    @RequestMapping("/test")
    public String outData(){
        logger.info("=================");
        return "index";
        
    }

}

 

Guess you like

Origin www.cnblogs.com/lukelook/p/11114433.html