configurations { all*.exclude module: 'spring-boot-starter-logging' all*.exclude module: 'logback-classic' all*.exclude module: 'log4j-over-slf4j' }
compile ('org.springframework.boot:spring-boot-starter-log4j2') compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.2'
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
logging: level: root: warn
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]