Spring boot 官网学习笔记 - logging
- commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择。log4j和logback则是具体的日志实现方案。
- 比较常用的搭配是commons-logging+log4j,slf4j+logback
为什么要用SLF4J+Logback 替换commons-logging+log4j?
- SLF4J是编译时绑定到具体的日志框架,性能优于采用运行时搜寻的方式的commons-logging
- 不需要使用logger.isDebugEnabled()来解决日志因为字符拼接产生的性能问题
logger.info("my name is {}", "medusar");
logger.info("my name is " + "medusar");-
在效率上,第一行比第二行更高,因为如果当前日志级别是ERROR,第一行不会进行字符串拼接,而第二行,无论日志级别是什么,都会先进行字符串拼接。
- 所以为了解决这个问题,commons-logging等框架提供了下面的方式:
if (log.isDebugEnabled()){
log.debug("dddd"+"eee");
}
- 基于commons-logging的日志使用
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class XXXService {
private static final Log log = LogFactory.getLog(XXXService.class);
public void doSomething(){
log.info("begin dosomething....");
}
}
基于slf4j的日志使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class XXXService {
private static final Logger logger = LoggerFactory.getLogger(XXXService.class);
public void doSomething() {
logger.info("begin dosomething...");
}
}
- SpringBoot底层也是使用slf4j+logback的方式进行日志记录;
- SpringBoot也把其他的日志都替换成了slf4j;
- Spring Boot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>- 以后开发的时候,日志记录方法的调用,不应该直接调用日志的实现类,而是调用日志抽象层里面的方法;给系统里面导入slf4j的jar和logback的实现jar
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(HelloWorld.class);
logger.info("Hello World"); }
}
指定文件中日志输出的格式# 在控制台输出的日志格式 logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{} - %msg%n
# 指定文件中日志输出的格式 logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{} === - %msg%n- <!-- 日志输出格式:
- %d表示日期时间,
- %thread表示线程名,
- %-5level:级别从左显示5个字符宽度
- %logger{50} 表示logger名字最长50个字符,否则按照句点分割。
- %msg:日志消息,
- %n是换行符-->
切换日志框架(无意义,slf4j+logback已经是最佳实现)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除转换包-->
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId> </exclusion>
</exclusions>
</dependency>
<!--添加slf4j依赖-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 调整日志级别
- Log Level:
ERROR,WARN,INFO,DEBUG, orTRACE. - Logback does not have a
FATALlevel. It is mapped toERROR. - The default log configuration echoes messages to the console as they are written. By default,
ERROR-level,WARN-level, andINFO-level messages are logged. You can also enable a “debug” mode by starting your application with a--debugflag. $ java -jar myapp.jar --debug
- Alternatively, you can enable a “trace” mode by starting your application with a
--traceflag (ortrace=truein yourapplication.properties). All the supported logging systems can have the logger levels set in the Spring
Environment(for example, inapplication.properties) by usinglogging.level.<logger-name>=<level>wherelevelis one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF. Therootlogger can be configured by usinglogging.level.root.The following example shows potential logging settings in
application.properties:logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
- Log Level:
- 日志文件
- By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a
logging.fileorlogging.pathproperty (for example, in yourapplication.properties). - Log files rotate when they reach 10 MB and, as with console output,
ERROR-level,WARN-level, andINFO-level messages are logged by default. Size limits can be changed using thelogging.file.max-sizeproperty. Previously rotated files are archived indefinitely unless thelogging.file.max-historyproperty has been set. - Logging properties are independent of the actual logging infrastructure. As a result, specific configuration keys (such as
logback.configurationFilefor Logback) are not managed by spring Boot. 
- By default, Spring Boot logs only to the console and does not write log files. If you want to write log files in addition to the console output, you need to set a
Log Groups
It’s often useful to be able to group related loggers together so that they can all be configured at the same time. For example, you might commonly change the logging levels for all Tomcat related loggers, but you can’t easily remember top level packages.
To help with this, Spring Boot allows you to define logging groups in your Spring
Environment. For example, here’s how you could define a “tomcat” group by adding it to yourapplication.properties:logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
Once defined, you can change the level for all the loggers in the group with a single line:
logging.level.tomcat=TRACE
Spring Boot includes the following pre-defined logging groups that can be used out-of-the-box:
Name Loggers web
org.springframework.core.codec,org.springframework.http,org.springframework.websql
org.springframework.jdbc.core,org.hibernate.SQL
Custom Log Configuration
- The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring
Environmentproperty:logging.config. - Since logging is initialized before the
ApplicationContextis created, it is not possible to control logging from@PropertySourcesin Spring@Configurationfiles. The only way to change the logging system or disable it entirely is via System properties. - Depending on your logging system, the following files are loaded:
Logging System Customization Logback
logback-spring.xml,logback-spring.groovy,logback.xml, orlogback.groovyLog4j2
log4j2-spring.xmlorlog4j2.xmlJDK (Java Util Logging)
logging.properties- When possible, we recommend that you use the
-springvariants for your logging configuration (for example,logback-spring.xmlrather thanlogback.xml). If you use standard configuration locations, Spring cannot completely control log initialization. To help with the customization, some other properties are transferred from the Spring
Environmentto System properties, as described in the following table:Spring Environment System Property Comments logging.exception-conversion-wordLOG_EXCEPTION_CONVERSION_WORDThe conversion word used when logging exceptions.
logging.fileLOG_FILEIf defined, it is used in the default log configuration.
logging.file.max-sizeLOG_FILE_MAX_SIZEMaximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)
logging.file.max-historyLOG_FILE_MAX_HISTORYMaximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)
logging.pathLOG_PATHIf defined, it is used in the default log configuration.
logging.pattern.consoleCONSOLE_LOG_PATTERNThe log pattern to use on the console (stdout). (Only supported with the default Logback setup.)
logging.pattern.dateformatLOG_DATEFORMAT_PATTERNAppender pattern for log date format. (Only supported with the default Logback setup.)
logging.pattern.fileFILE_LOG_PATTERNThe log pattern to use in a file (if
LOG_FILEis enabled). (Only supported with the default Logback setup.)logging.pattern.levelLOG_LEVEL_PATTERNThe format to use when rendering the log level (default
%5p). (Only supported with the default Logback setup.)PIDPIDThe current process ID (discovered if possible and when not already defined as an OS environment variable).
- The various logging systems can be activated by including the appropriate libraries on the classpath and can be further customized by providing a suitable configuration file in the root of the classpath or in a location specified by the following Spring
- xx
Spring boot 官网学习笔记 - logging的更多相关文章
- Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...
- Spring boot 官网学习笔记 - Spring DevTools 介绍
想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...
- Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)
Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...
- Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...
- Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...
- Spring boot 官网学习笔记 - Configuration Class(@import)
推荐使用 Java-based configuration ,也可以使用xml we generally recommend that your primary source be a single ...
- Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...
- (五)Spring Boot官网文档学习
文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...
随机推荐
- 设置ABP默认使用中文
ABP提供的启动模板, 默认使用是英文: 虽然可以通过右上角的菜单切换成中文, 但是对于国内项目来说, 默认使用中文是很正常的需求. 本文介绍了如何实现默认语言的几种方法, 希望能对ABP爱好者有所帮 ...
- Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles)
Leetcode之深度优先搜索(DFS)专题-547. 朋友圈(Friend Circles) 深度优先搜索的解题详细介绍,点击 班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递 ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- 基于ASP.Net Core开发的一套通用后台框架
基于ASP.Net Core开发一套通用后台框架 写在前面 这是本人在学习的过程中搭建学习的框架,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 知其然,知其所以然,并非重 ...
- 2019牛客暑期多校训练营(第二场) - J - Go on Strike! - 前缀和预处理
题目链接:https://ac.nowcoder.com/acm/contest/882/C 来自:山东大学FST_stay_night的的题解,加入一些注释帮助理解神仙代码. 好像题解被套了一次又一 ...
- HDU- 3605 - Escape 最大流 + 二进制压位
HDU - 3605 : acm.hdu.edu.cn/showproblem.php?pid=3605 题目: 有1e5的的人,小于10个的星球,每个星球都有容量,每个人也有适合的星球和不适合的星球 ...
- 集成学习方法Boosting和Bagging
集成学习是通过构架并结合多个学习器来处理学习任务的一种思想, 目前主要分为两大类:Boosting和Bagging. 对于任意一种集成方法, 我们都希望学习出来的基分类器具有较高的准确性和多样性, 基 ...
- Go语言获取系统性能数据gopsutil库
psutil是一个跨平台进程和系统监控的Python库,而gopsutil是其Go语言版本的实现.本文介绍了它的基本使用. Go语言部署简单.性能好的特点非常适合做一些诸如采集系统信息和监控的服务,本 ...
- Maven工程读取properties文件过程
1.创建需要读取的properties文件 2.在xml文件中加载配置文件 <!-- 加载配置文件 --> <context:property-placeholder locatio ...
- mybatis-generator:generate 生成代码配置踩坑详解
mybatis-generator:generate 生成代码配置踩坑不少,在此留下笔记以便后续填坑 一.mysql返回时间问题 错误信息: [ERROR] Failed to execute goa ...