Spring Boot log4j2 configuration example
spring boot 默认log用的是 LogBack。
1. Log4j2 Maven Dependency
pom.xml
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.2.RELEASE</version>
</parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version>
</properties><dependencies><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency>
</dependencies>
2. Add log4j2.xml file in resources folder
如果存在log4j2 jar包,springboot会自动配置它。把 log4j2.xml
放在 src/main/resources
文件夹。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30"><Properties><Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property><Property name="APP_LOG_ROOT">c:/temp</Property></Properties><Appenders><Console name="Console" target="SYSTEM_OUT" follow="true"><PatternLayout pattern="${LOG_PATTERN}" /></Console><RollingFile name="appLog"fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log"filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log"><PatternLayout pattern="${LOG_PATTERN}" /><Policies><SizeBasedTriggeringPolicy size="19500KB" /></Policies><DefaultRolloverStrategy max="1" /></RollingFile></Appenders><Loggers><Logger name="com.howtodoinjava.app" additivity="false"><AppenderRef ref="appLog" /><AppenderRef ref="Console" /></Logger><Root level="debug"><AppenderRef ref="Console" /></Root></Loggers>
</Configuration>
3. Spring boot log4j2 demo
Application.java
package com.howtodoinjava.app;import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;@SpringBootApplication
public class Application extends SpringBootServletInitializer {private static final Logger LOGGER = LogManager.getLogger(Application.class);public static void main(String[] args){ApplicationContext ctx = SpringApplication.run(Application.class, args);LOGGER.info("Info level log message");LOGGER.debug("Debug level log message");LOGGER.error("Error level log message");}
}
Read More: Log4j2 properties file example
转载于:https://www.cnblogs.com/chenqr/p/11143572.html
Spring Boot log4j2 configuration example相关推荐
- spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法
spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法 参考文章: (1)spring boot log4j2与三方依赖库log4j冲突无法初始化问题解决方法 (2)ht ...
- Spring Boot Log4j2 日志学习
简介 Java 中比较常用的日志工具类,有: Log4j. SLF4j. Commons-logging(简称jcl). Logback. Log4j2(Log4j 升级版). Jdk Logging ...
- Spring Boot + Log4j2 日志框架配置 (Maven)
参考Spring Boot官方文档 日志部分 Spring Boot默认情况下,当使用"Starters" 使用Logback输出日志 , 还包括适当的Logback路由, 确保其 ...
- Spring Boot之 Configuration Annotation Proessor not found in classpath解决方法
出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationPropertie ...
- Spring Boot中Configuration problem: Cannot locate BeanDefinitionParser for element 的问题分析
环境信息 Spring Boot 2.0.2 JDK 8, OS: windows 7 问题的提出 今天在开发中,碰到一下类似的错误信息: 15:44:09.198 INFO com.jd.ai.cv ...
- Spring Boot 错误 Configuration Annotation Proessor not found in classpath
出现spring boot Configuration Annotation Proessor not found in classpath的提示是在用了@ConfigurationPropertie ...
- Spring Boot 使用 Log4j2
Java 中比较常用的日志工具类,有 Log4j.SLF4j.Commons-logging(简称jcl).Logback.Log4j2(Log4j 升级版).Jdk Logging. Spring ...
- Spring Boot 2.0.0参考手册_中英文对照_Part IV_26
文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 26. Logging Spring Boot uses Commons Logging for all inte ...
- Spring MVC 到 Spring BOOT 的简化之路
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 来源:juejin.im/post/5aa22d1f5188255 ...
最新文章
- 【C++】 66_C++ 中的类型识别
- 国产期刊崛起!上海大学、郑州大学主办期刊IF超15,2021版SCI期刊影响因子出炉...
- Importance Sampling in Arnold for Hair
- NLP技术路线详解:这是从数学到算法的艺术
- Microsoft Office Backstage(第 1 部分 – 幕后故事)
- 【LeetCode从零单排】No102 Binary Tree Level Order Traversal
- CmsEasy 漏洞挖掘
- initWithNibName与viewDidLoad的执行关系以及顺序
- 前端学习(1781):前端调试之security节点讲解
- Java学习指导————如何做到基础扎实
- Nestable 可移动拖拽的树型结构的使用(jQuery)
- Docker部署Angular应用
- 库克:5G iPhone目前还不是我们考虑的问题
- 多选框中的选中的值和未选中值的获取
- 编码的法则:c++程序员不可不知的101条经验
- Android EditText的常用技巧
- VALSE学习(十):图像去模糊-image deblurring
- androidstudio身高预测app
- 《深入理解计算机系统》实验二Bomb Lab下载和官方文档机翻
- Camera4 MTK camera驱动结构介绍
热门文章
- Mockito框架研究 - how is match any string implemented
- INTEROP tile cache
- java并发排序_Java并发(三):重排序
- unity 3d物体描边效果_从零开始的卡通渲染描边篇
- python requests库作用_python Requests库入门
- 复杂的c语言,c语言复杂数据类型
- mysql表定义外键语法_mysql设置外键的语法怎么写?
- c++排序函数_中式排名怎么做,你还在手动输入1、2、2、3?这组函数一秒搞定...
- 我的世界1.11服务器咋发信息,我的世界进服务器怎么注册怎么发信息
- hbase集群之间数据迁移_hbase数据迁移到另一集群上