本文记录一下log4j2的使用
其实就三步即可
第一步:添加log4j2依赖,排除spring-boot-starter-logging依赖
<!-- Import the log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Remove the internal dependency log spring-boot-starter-logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
第二步:编写log4j2的配置文件,文件名为log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="60">
<Properties>
<!-- 设置应用的英文名称 -->
<Property name="App">logs</Property>
<!-- 设置日志目录 -->
<Property name="logDir">logs</Property>
<Property name="splitSize">10 MB</Property>
<property name="pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %msg \t %logger{36} %F %M %L %n </property>
</Properties>
<Appenders>
<!-- 输出控制台日志的配置 -->
<Console name="console" target="SYSTEM_OUT">
<!-- 记录info和warn级别信息 -->
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<!-- 输出日志的格式 -->
<PatternLayout pattern="${pattern}"/>
</Console>
<!-- 打印出所有的信息 -->
<RollingFile name="info"
fileName="${logDir}/${App}-info.log"
filePattern="${logDir}/${App}-info-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${pattern}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="${splitSize}"/>
</Policies>
<Filters>
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
<!-- 存储所有error信息 -->
<RollingFile name="error"
fileName="${logDir}/${App}-error.log"
filePattern="${logDir}/${App}-error-%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="${pattern}" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="${splitSize}"/>
</Policies>
<Filters>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
</Filters>
<DefaultRolloverStrategy max="20"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="console"/>
<AppenderRef ref="info"/>
<AppenderRef ref="error"/>
</Root>
</Loggers>
</Configuration>
注意:log4j2.xml文件只需放在/src/main/resources文件夹下即可,不需要在application.yml文件中配置。
第三步:编写Log工具类,我就随便写一个仅供参考
package com.felix.firstdemo.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogUtil {
private static final Logger LOGGING = LoggerFactory.getLogger("fist_demo_log");
public static void debug(String message){
LOGGING.debug(message);
}
public static void info(String message){
LOGGING.info(message);
}
public static void warn(String message){
LOGGING.warn(message);
}
public static void error(String message){
LOGGING.error(message);
}
}
所有步骤已完成,可以使用了,贴一个使用的接口的demo
package com.felix.firstdemo.controller;
import com.felix.firstdemo.utils.LogUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
@GetMapping("/log")
public String log(@RequestParam(name = "logType", required = false) String logType) {
String result = "debug";
if (!logType.isEmpty()) {
result = logType;
}
String message="response is ; " + result;
switch (logType.toLowerCase()) {
case "debug":
LogUtil.debug(message);
break;
case "info":
LogUtil.info(message);
break;
case "warn":
LogUtil.warn(message);
break;
case "error":
LogUtil.error(message);
break;
}
return result;
}
}
运行程序,输入地址 /hello/log?logType=error
可以看到有日志输出: