添加log4j2-spring.xml文件在src/main/resources文件下,配置文件代码如下:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <Appenders> <Console target="SYSTEM_OUT"> <PatternLayout charset="UTF-8" pattern="[%-5p] %d %c - %m%n" /> </Console> <File fileName="D:\\mylog.log"> <PatternLayout pattern="%m%n" /> </File> </Appenders> <Loggers> <root level="info"> <AppenderRef ref="CONSOLE" /> <AppenderRef ref="File" /> </root> </Loggers> </configuration>输入日志到控制台和D盘mylog.log文件中。
到此为止,已经完成了log4j2的集成,运行项目,查看日志。
示例源码:https://github.com/vipstone/springboot-example/tree/master/springboot-logging
参考资料
JAVA日志的前世今生:https://www.cnblogs.com/xiexj/p/9417128.html