转:spring boot + log4j2 配置
以前的log一直都是组长进行配置,自己甚至连log的级别都分不太清楚,为了避免这种尴尬的情况再次发生,自己尝试了一下在spring boot中进行log4j2的配置。
首先自定义一个log4j2.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout> <pattern>%d %p %C{1.} [%t] %m%n</pattern> </PatternLayout> </Console> <File name="File" fileName="/Users/zk_chs/logtest/zk_chs.log"> <PatternLayout> <pattern>%d %p %C{1.} [%t] %m%n</pattern> </PatternLayout> </File> </Appenders> <Loggers> <Logger name="org.apache.catalina.util.LifecycleBase" level="error" /> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" /> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" /> <Logger name="org.hibernate.validator.internal.util.Version" level="warn" /> <Logger name="org.springframework" level="warn" /> <Logger name="com.github" level="debug" /> <Root level="info"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
这个log文件相信大家都能看懂,不太明白的话可以参考下面的地址:
http://www.it165.net/pro/html/201409/21592.html
接下来在pom.xml按照spring boot的文档进行定义
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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>
最后在application.properties文件中,指定加载的log文件位置:
logging.config=classpath:log4j2.xml
这样,我们便能在程序中使用log了,目前我们的日志是在控制台进行输出,如果需要输出到文件中的话,添加<AppenderRef ref="File" />即可
相关推荐
chw0 2020-11-04
大唐帝国前营 2020-08-18
sdaq 2020-07-26
MrLiar 2020-07-07
sdaq 2020-06-16
CXC0 2020-06-14
丨Fanny丨Cri 2020-06-13
CXC0 2020-06-08
dongxurr 2020-06-07
sdaq 2020-06-06
MrLiar 2020-06-04
丨Fanny丨Cri 2020-06-03
MrLiar 2020-05-25
丨Fanny丨Cri 2020-05-17
MrLiar 2020-05-14
MrLiar 2020-05-12
sdaq 2020-05-11