【Spring Boot 官方文档】26、Log日志

简介:

Spring Boot所有内部日志使用Apache的Commons Logging组件,同时也开放了底层的日志实现。

Spring Boot为3种日志组件Java Util Logging,Log4J2,Logback提供了默认配置,而且为每一种预设了控制台输出,并提供文件输出可选。

如果使用Spring Boot的starters组件,默认使用Logback组件。

Spring Boot提供了适当的Logback路由,以保证依赖库使用Java Util Logging, Commons Logging, Log4J, or SLF4J的时候,能正常运作。

1、日志格式

默认日志格式如下

2014-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
2014-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: ‘dispatcherServlet‘ to [/]
2014-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: ‘hiddenHttpMethodFilter‘ to: [/*]

其中内容包含:

日期和时间

日志等级:ERROR, WARN, INFO, DEBUG, or TRACE.

进程ID

---:分隔符

线程名称: 包含在方括号中

日志器名:通常是来源类

日志信息

2、控制台输出

默认日志配置会在写入消息时将消息回传给控制台。

默认情况下,将记录ERROR级别,WARN级别和INFO级别的消息。您还可以通过使用--debug标志启动应用程序来启用“调试”模式。

运行时:

$ java -jar myapp.jar --debug

也可以在application.properties文件中设置

debug=true

当启用调试模式,是指让一些内部日志器(如嵌入式容器,Hibernate和Spring Boot)把Debug日志也输出出来。

是否启动调试模式,并不会影响用户自定义的Debug日志是否显示。

同样,可以通过--trace或者trace=true的形式,让系统中一些内部日志器输出跟踪信息。

控制颜色输出

如果终端支持ANSI,则可以通过输出带颜色的文字提高可读性。

增加设置

#输出彩色日志
spring.output.ansi.enabled=ALWAYS

[这部分未细看]

You can set spring.output.ansi.enabled to a supported value to override the auto detection.

Color coding is configured by using the %clr conversion word. In its simplest form, the converter colors the output according to the log level, as shown in the following example:

%clr(%5p)

The following table describes the mapping of log levels to colors:

Level Color

FATAL


Red


ERROR


Red


WARN


Yellow


INFO


Green


DEBUG


Green


TRACE


Green

Alternatively, you can specify the color or style that should be used by providing it as an option to the conversion. For example, to make the text yellow, use the following setting:

%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){yellow}

The following colors and styles are supported:

  • blue
  • cyan
  • faint
  • green
  • magenta
  • red
  • yellow

3、文件输出

默认情况下,Spring Boot只会在控制台输出日志信息。我们可以设置让Spring Boot把日志信息输出到文件中。

可以在application.properties文件中设置logging.filelogging.path属性

  • 2个属性都不设置时,则只在控制台输出日志
  • logging.file=my.log:写到特定文件中,可以是相对路径或者绝对路径
  • logging.path=/var/log:写到特定文件夹中,可以是相对路径或者绝对路径

默认情况下当文件达到10MB,会重新创建一个文件继续记录,默认记录ERRORWARN, INFO等级的日志。

  • 设置logging.file.max-size可以改变文件的大小限制
  • 默认下,之前的日志文件会一直保存,可以通过logging.file.max-history属性来设置保存多少的历史日志

注意2点:

  • 日志记录系统在应用程序生命周期的很早期就进行了初始化。因此,不能在属性文件中通过@PropertySource注释设置日志属性。
  • Spring Boot的日志属性是独立于基础组件的(如logback),所以组件的属性(如logback的logback.configurationFile)不被Spring Boot所管理,Spring Boot只管理自己的日志属性(上限讨论的属性)

4、日志等级

可以在Spring环境(例如application.properties)中为所有支持的日志系统设置日志记录等级。

通过logging.level.<logger-name>=<level> 进行设置,其中logger-name是调用logger-name时传入的名称(下面实例中了解),level是日志等级

level日志等级包括:TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF

可以通过logging.level.root=<level> 来进行统一配置

一个使用实例

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

5、自定义日志配置

各种日志系统,可以通过以下方式进行自定义配置:

  • 将适当的依赖类放置到类路径下
  • 然后创建一个对应的配置文件放在类路径根目录下;或使用Spring环境属性logging.config指明对应配置文件

可以通过org.springframework.boot.logging.LoggingSystem属性,强制Spring Boot使用特定的一个日志系统;

这个值必须是一个日志系统实现的全限定类名;

也可以设置为none,完全禁止Spring Boot的日志配置。

注意!因为日志系统在ApplicationContext之前创建,所以日志属性只能通过properties文件进行设置,无法通过Sprint@Configuration文件中使用@PropertySources设置。

根据指定的系统,回加载对应的配置文件

  • Logback:logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
  • Log4j2:log4j2-spring.xml or log4j2.xml
  • JDK (Java Util Logging):logging.properties

建议使用-spring的命名方式

当使用Java Util Logging时,如果运行一个外部可执行jar,会产生类加载问题;所以推荐使用外部可执行jar时,禁止这个日志系统

为了实现自定义,Spring Boot将一些配置从Spring环境中转移到系统properties中去

Spring Environment System Property Comments

logging.exception-conversion-word


LOG_EXCEPTION_CONVERSION_WORD


The conversion word used when logging exceptions.


logging.file


LOG_FILE


If defined, it is used in the default log configuration.


logging.file.max-size


LOG_FILE_MAX_SIZE


Maximum log file size (if LOG_FILE enabled). (Only supported with the default Logback setup.)


logging.file.max-history


LOG_FILE_MAX_HISTORY


Maximum number of archive log files to keep (if LOG_FILE enabled). (Only supported with the default Logback setup.)


logging.path


LOG_PATH


If defined, it is used in the default log configuration.


logging.pattern.console


CONSOLE_LOG_PATTERN


The log pattern to use on the console (stdout). (Only supported with the default Logback setup.)


logging.pattern.dateformat


LOG_DATEFORMAT_PATTERN


Appender pattern for log date format. (Only supported with the default Logback setup.)


logging.pattern.file


FILE_LOG_PATTERN


The log pattern to use in a file (if LOG_FILE is enabled). (Only supported with the default Logback setup.)


logging.pattern.level


LOG_LEVEL_PATTERN


The format to use when rendering the log level (default %5p). (Only supported with the default Logback setup.)


PID


PID


The current process ID (discovered if possible and when not already defined as an OS environment variable).

如果想在日志记录属性中使用占位符,则应该使用Spring Boot的语法,而不是基础框架的语法。值得注意的是,如果你使用Logback,你应该使用:作为属性名和默认值之间的分隔符,而不是使用:- 。

[这段不太明白]

You can add MDC and other ad-hoc content to log lines by overriding only the LOG_LEVEL_PATTERN (or logging.pattern.level with Logback). For example, if you use logging.pattern.level=user:%X{user} %5p, then the default log format contains an MDC entry for "user", if it exists, as shown in the following example.

2015-09-30 12:30:04.031 user:someone INFO 22174 --- [  nio-8080-exec-0] demo.Controller
Handling authenticated request

fdsa

6、Logback扩展

这部分略,需要的自行查看Spring Boot官方文档

7、实际应用

原文地址:https://www.cnblogs.com/LiveYourLife/p/9221032.html

时间: 2024-08-12 19:01:57

【Spring Boot 官方文档】26、Log日志的相关文章

Spring Boot 官方文档入门及使用

个人说明:本文内容都是从为知笔记上复制过来的,样式难免走样,以后再修改吧.另外,本文可以看作官方文档的选择性的翻译(大部分),以及个人使用经验及问题. 其他说明:如果对Spring Boot没有概念,请先移步上一篇文章 Spring Boot 学习.本篇原本是为了深入了解下Spring Boot而出现的. 另外,Spring Boot 仍然是基于Spring的,建议在赶完工之后深入学习下Spring,有兴趣可以看看我的 Spring 4 官方文档学习(十一)Web MVC 框架 .欢迎探讨,笑~

20191114 Spring Boot官方文档学习(4.7)

4.7.开发Web应用程序 Spring Boot非常适合于Web应用程序开发.您可以使用嵌入式Tomcat,Jetty,Undertow或Netty创建独立的HTTP服务器.大多数Web应用程序都使用该spring-boot-starter-web模块来快速启动和运行.您还可以选择使用spring-boot-starter-webflux模块来构建反应式Web应用程序. 4.7.1.Spring Web MVC框架 在Spring Web MVC框架(通常简称为"Spring MVC"

Spring Boot 官方文档学习(二)特点

一.SpringApplication banner,就是启动时输出的信息,可以在classpath下添加 banner.txt,或者设置 banner.location 来指向特定的文件.(默认编码utf-8,或者通过banner.charset指定) 除了txt,你还可以使用 banner.gif (jpg / png),或者设定 banner.imgage.location. 下面是默认的banner(忽略吧,没意义的东西): banner变量,只有应用相关的信息,略,见pdf. 还可以使

20191112 Spring Boot官方文档学习(4.5-4.6)

4.5.国际化 Spring Boot支持本地化消息,因此您的应用程序可以迎合不同语言首选项的用户.默认情况下,Spring Boot messages在类路径的根目录下查找message resource bundle的存在. 当配置的resource bundle的默认属性文件可用时(即默认为messages.properties),将应用自动配置.如果您的resource bundle仅包含特定于语言的属性文件,则需要添加默认文件.如果找不到与任何配置的基本名称匹配的属性文件,则不会自动配

Spring Framework 官方文档学习(四)之Validation、Data Binding、Type Conversion

前言 在Spring Framework官方文档中,这三者是放到一起讲的,但没有解释为什么放到一起.大概是默认了读者都是有相关经验的人,但事实并非如此,例如我.好在闷着头看了一遍,又查资料又敲代码,总算明白了. 其实说穿了一文不值,我们用一个例子来解释: 假定,现有一个app,功能是接收你输入的生日,然后显示你的年龄.看起来app只要用当前日期减去你输入的日期就是年龄,应该很简单对吧?可惜事实不是这样的. 这里面有三个问题: 问题一:我们输入的永远是字符串,字符串需要转成日期格式才能被我们的ap

Spring 4 官方文档学习(十二)View技术

1.介绍 Spring 有很多优越的地方,其中一个就是将view技术与MVC框架的其他部分相隔离.例如,在JSP存在的情况下使用Groovy Markup Templates 还是使用Thymeleaf,仅仅是一个配置问题. 本章覆盖了主要的view技术,嗯嗯,可以与Spring结合的那些,并简明的说明了如何增加新的view技术. 本章假定你已经熟悉了Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图 -- 它覆盖了views如何耦合到MVC框架

Spring 4 官方文档学习(十一)Web MVC 框架之resolving views 解析视图

接前面的Spring 4 官方文档学习(十一)Web MVC 框架,那篇太长,故另起一篇. 针对web应用的所有的MVC框架,都会提供一种呈现views的方式.Spring提供了view resolvers,可以让你在浏览器中render model,而不必绑定到某种特定的view技术上.开箱即用,例如,Spring可以让你使用JSPs.Velocity目标和XSLT views.See Chapter 23, View technologies for a discussion of how

Spring MVC 官方文档

Spring MVC 官方文档地址:https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc Spring MVC 是基于Servlet API构建的Web框架. 原文地址:https://www.cnblogs.com/smailjunk/p/11174845.html

Spring Cloud官方文档中文版-声明式Rest客户端:Feign

官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#spring-cloud-feign 文中例子我做了一些测试在:http://git.oschina.net/dreamingodd/spring-cloud-preparation Declarative REST Client: Feign 声明式Rest客户端:Feign Feign is a declarative web service client. It