https://logging.apache.org/log4j/2.x/
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
https://github.com/liuhongdi/tomcatlogs

ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF
<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>
<!--log4j2 begin-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<!--log4j2 end-->
说明1:spring-boot-starter-web默认包含了日志功能包,
包含了logback/slf4j两个日志包,
所以我们需要用excusion排除对此两个包的依赖
说明2:log4j的异步日志使用了Disruptor的队列技术,
我们需要使用异步日志,所以这里需要引入Disruptor,
附:Disruptor在mvn上的地址:可以从这里查看版本
https://mvnrepository.com/artifact/com.lmax/disruptor
#log4j2
logging.config=classpath:log4j2.xml
说明:指定log4j2配置文件的路径,放到resources目录下
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="INFO"> <Appenders> <Console name="STDOUT" target="SYSTEM_OUT"> <PatternLayout pattern=".%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level %logger{36} - %msg %n"/> </Console> <RollingFile immediateFlush="false" name="ErrorFile" fileName="/data/logs/tomcatlogs/error.log" filePattern="/data/logs/tomcatlogs/$${date:yyyy-MM}/error-%d{MM-dd-yyyy}-%i.log"> <Filters> <ThresholdFilter level="INFO" /> </Filters> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level %logger{35} - %msg %n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="102400KB"/> </Policies> </RollingFile> <RollingFile immediateFlush="false" name="BusinessFile" fileName="/data/logs/tomcatlogs/bussiness.log" filePattern="/data/logs/tomcatlogs/$${date:yyyy-MM}/bussiness-%d{MM-dd-yyyy}-%i.log"> <PatternLayout> <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] [%file:%line] %-5level %logger{35} - %msg %n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="102400KB"/> </Policies> </RollingFile> </Appenders> <Loggers> <AsyncLogger name="BusinessFile" level="info" additivity="false"> <appender-ref ref="BusinessFile"/> </AsyncLogger> <AsyncRoot level="info" includeLocation="true"> <AppenderRef ref="STDOUT"/> <AppenderRef ref="ErrorFile" /> </AsyncRoot> </Loggers> </Configuration>
说明:
ErrorFile放到asyncRoot下,用来记录系统的所有信息
@RestController @RequestMapping("/home") public class HomeController { @GetMapping("/list") @ResponseBody public String list() { Logger logger1 = LogManager.getLogger(this.getClass()); Logger logger2 = LogManager.getLogger("BusinessFile"); logger1.info("hello,this is in errorlog"); logger2.info("hello,this is in businesslog"); return "this is list"; } }
说明:
获取logger时,如果使用class,会保存到root下指定的日志
如果使用指定的日志AppenderRef名字,则会保存到名字对应的日志
http://127.0.0.1:8080/home/list
2,查看所写入的日志:
[liuhongdi@localhost tomcatlogs]$ tail -1 bussiness.log 2020-07-05 22:46:07.208 [http-nio-8080-exec-7] [:] INFO BusinessFile - hello,this is in businesslogge [liuhongdi@localhost tomcatlogs]$ tail -1 error.log 2020-07-05 22:46:07.208 [http-nio-8080-exec-7] [HomeController.java:20] INFO com.tomcatlogs.demo.controller.HomeController - hello,this is in errorlog
可以看到日志写入成功
. ____ _ __ _ _ /\\ / ___‘_ __ _ _(_)_ __ __ _ \ \ \ \ (( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ‘ |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.1.RELEASE)
spring boot:使用log4j2做日志打印(spring boot 2.3.1)
原文:https://www.cnblogs.com/architectforest/p/13251806.html