Spring Boot 探索系列 - 自动化配置篇

26. Logging
Prev  Part IV. Spring Boot features  Next

26. Logging

Spring Boot uses Commons Logging for all internal logging, but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging,Log4JLog4J2 and Logback. In each case loggers are pre-configured to use console output with optional file output also available.

By default, If you use the ‘Starter POMs’, Logback will be used for logging. Appropriate Logback routing is also included to ensure that dependent libraries that use Java Util Logging, Commons Logging, Log4J or SLF4J will all work correctly.


There are a lot of logging frameworks available for Java. Don’t worry if the above list seems confusing. Generally you won’t need to change your logging dependencies and the Spring Boot defaults will work just fine.

26.1 Log format

The default log output from Spring Boot looks like this:

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: [/*]

The following items are output:

  • Date and Time — Millisecond precision and easily sortable.
  • Log Level — ERRORWARNINFODEBUG or TRACE.
  • Process ID.
  • --- separator to distinguish the start of actual log messages.
  • Thread name — Enclosed in square brackets (may be truncated for console output).
  • Logger name — This is usually the source class name (often abbreviated).
  • The log message.

Logback does not have a FATAL level (it is mapped to ERROR)

26.2 Console output

The default log configuration will echo messages to the console as they are written. By default ERRORWARN and INFO level messages are logged. You can also enable a “debug” mode by starting your application with a --debug flag.

$ java -jar myapp.jar --debug

you can also specify debug=true in your application.properties.

When the debug mode is enabled, a selection of core loggers (embedded container, Hibernate and Spring) are configured to output more information. Enabling the debug mode does not configure your application log all messages with DEBUG level.

26.2.1 Color-coded output

If your terminal supports ANSI, color output will be used to aid readability. You can set spring.output.ansi.enabled to a supported value to override the auto detection.

Color coding is configured using the %clr conversion word. In its simplest form the converter will color the output according to the log level, for example:

%clr(%5p)

The mapping of log level to a color is as follows:

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:

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

The following colors and styles are supported:

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

26.3 File output

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file orlogging.path property (for example in your application.properties).

The following table shows how the logging.* properties can be used together:

Table 26.1. Logging properties

logging.file logging.path Example Description

(none)


(none)

 
Console only logging.


Specific file


(none)


my.log


Writes to the specified log file. Names can be an exact location or relative to the current directory.


(none)


Specific directory


/var/log


Writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

Log files will rotate when they reach 10 Mb and as with console output, ERRORWARN and INFO level messages are logged by default.


The logging system is initialized early in the application lifecycle and as such logging properties will not be found in property files loaded via @PropertySourceannotations.


Logging properties are independent of the actual logging infrastructure. As a result, specific configuration keys (such as logback.configurationFile for Logback) are not managed by spring Boot.

26.4 Log Levels

All the supported logging systems can have the logger levels set in the Spring Environment (so for example in application.properties) using ‘logging.level.*=LEVEL’ where ‘LEVEL’ is one of TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. The root logger can be configured using logging.level.root. Exampleapplication.properties:

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

By default Spring Boot remaps Thymeleaf INFO messages so that they are logged at DEBUG level. This helps to reduce noise in the standard log output. SeeLevelRemappingAppender for details of how you can apply remapping in your own configuration.

26.5 Custom log configuration

The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config.


Since logging is initialized before the ApplicationContext is created, it isn’t possible to control logging from @PropertySources in Spring@Configuration files. System properties and the conventional Spring Boot external configuration files work just fine.)

Depending on your logging system, the following files will be loaded:

Logging System Customization

Logback


logback-spring.xmllogback-spring.groovylogback.xml or logback.groovy


Log4j


log4j-spring.propertieslog4j-spring.xmllog4j.properties or log4j.xml


Log4j2


log4j2-spring.xml or log4j2.xml


JDK (Java Util Logging)


logging.properties


When possible we recommend that you use the -spring variants for your logging configuration (for example logback-spring.xml rather thanlogback.xml). If you use standard configuration locations, Spring cannot completely control log initialization.


There are known classloading issues with Java Util Logging that cause problems when running from an ‘executable jar’. We recommend that you avoid it if at all possible.

To help with the customization some other properties are transferred from the Spring Environment to System properties:

Spring Environment System Property Comments

logging.exception-conversion-word


LOG_EXCEPTION_CONVERSION_WORD


The conversion word that’s used when logging exceptions.


logging.file


LOG_FILE


Used in default log configuration if defined.


logging.path


LOG_PATH


Used in default log configuration if defined.


logging.pattern.console


CONSOLE_LOG_PATTERN


The log pattern to use on the console (stdout). (Not supported with JDK logger.)


logging.pattern.file


FILE_LOG_PATTERN


The log pattern to use in a file (if LOG_FILE enabled). (Not supported with JDK logger.)


logging.pattern.level


LOG_LEVEL_PATTERN


The format to use to render the log level (default %5p). (Thelogging.pattern.level form is only supported by Logback.)


PID


PID


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

All the logging systems supported can consult System properties when parsing their configuration files. See the default configurations in spring-boot.jar for examples.


If you want to use a placeholder in a logging property, you should use Spring Boot’s syntax and not the syntax of the underlying framework. Notably, if you’re using Logback, you should use : as the delimiter between a property name and its default value and not :-.


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 will contain an MDC entry for "user" if it exists, e.g.

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

26.6 Logback extensions

Spring Boot includes a number of extensions to Logback which can help with advanced configuration. You can use these extensions in your logback-spring.xmlconfiguration file.


You cannot use extensions in the standard logback.xml configuration file since it’s loaded too early. You need to either use logback-spring.xml or define a logging.config property.

26.6.1 Profile-specific configuration

The <springProfile> tag allows you to optionally include or exclude sections of configuration based on the active Spring profiles. Profile sections are supported anywhere within the <configuration> element. Use the name attribute to specify which profile accepts the configuration. Multiple profiles can be specified using a comma-separated list.

<springProfile name="staging">
    <!-- configuration to be enabled when the "staging" profile is active -->
</springProfile>

<springProfile name="dev, staging">
    <!-- configuration to be enabled when the "dev" or "staging" profiles are active -->
</springProfile>

<springProfile name="!production">
    <!-- configuration to be enabled when the "production" profile is not active -->
</springProfile>

26.6.2 Environment properties

The <springProperty> tag allows you to surface properties from the Spring Environment for use within Logback. This can be useful if you want to access values from your application.properties file in your logback configuration. The tag works in a similar way to Logback’s standard <property> tag, but rather than specifying a direct value you specify the source of the property (from the Environment). You can use the scope attribute if you need to store the property somewhere other than inlocal scope.

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"/>
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
    <remoteHost>${fluentHost}</remoteHost>
    ...
</appender>

The RelaxedPropertyResolver is used to access Environment properties. If specify the source in dashed notation (my-property-name) all the relaxed variations will be tried (myPropertyNameMY_PROPERTY_NAME etc).

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

作者:孙立伟
链接:https://zhuanlan.zhihu.com/p/19958535
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

基于Spring Boot的应用,程序的入口是SpringApplication类。通过@EnableAutoConfiguration 开启Spring Boot的自动配置功能。这个是Spring Boot核心价值之一。我们首先要搞清楚的应该是Spring Boot如何自动的配置所有的特性以及如何定制自动化配置过程。这里列出几个在官方参考文档中和自动化配置相关的链接。

23. Externalized Configuration

36. Developing auto-configuration and using conditions

62.1. Troubleshoot auto-configuration

63. Properties & configuration

Appendix A. Common application properties

Appendix C. Auto-configuration classes

如何阅读自动化配置相关源代码

通过阅读以上参考资料,大致可以搞清楚Spring Boot自动化配置的过程需要遵循的方向。但是,阅读源代码是了解Spring Boot的自动配置机制的最佳选择。源代码链接:spring-projects/spring-boot · GitHub。整个项目使用Maven进行管理,直接导入即可。注意不要导入太多的Maven项目,我们的目的是通过阅读代码来加深Spring Boot的理解,只需要‘spring-boot‘, ‘spring-boot-actuator‘ 和 ‘spring-boot-autoconfigure‘。

Spring Boot 自动化配置相关源代码阅读起来并不困难,在官方文档中也有一些提示。在读完官方文档的基础上,才能开始阅读源码。我把目前了解到的关键类列一下。

另外,补充说明一点,本系列文章均基于 Spring Boot 1.2.1.RELEASE 版本。

  1. SpringApplication (Spring Boot Docs 1.2.1.RELEASE API)
  2. ConfigFileApplicationListener (Spring Boot Docs 1.2.1.RELEASE API)

Spring Boot通过实现各种ApplicationListener进行实际的配置工作,配置框架是使用Environment 和 PropertySource相关的类。几点注意事项:

  • Spring Boot支持从多个地方加载配置,比如命令行、系统环境变量、JNDI等,因此配置项的语法支持所谓的RelaxedEnvironment。在源码中,通过查找 Relaxed开头的类,比如RelaxedPropertyResolver,可以了解到相关的使用情况
  • SpringBoot同时支持YAML和Properties格式的配置文件,虽然并不禁止混合使用,但是加载顺序是不确定的。

日志框架配置文件位置‘logging.config‘的一个坑

在Spring Boot 1.2.1中,对于日志框架本身的配置文件位置是通过‘logging.config‘进行设置的。下面是LoggingApplicationListener的代码片段:

private void initializeSystem(ConfigurableEnvironment environment,
      LoggingSystem system) {
   LogFile logFile = LogFile.get(environment);
   String logConfig = environment.getProperty(CONFIG_PROPERTY);
   if (StringUtils.hasLength(logConfig)) {
try {
         //码农公社:校验logConfig是否可访问
         ResourceUtils.getURL(logConfig).openStream().close();
         system.initialize(logConfig, logFile);
      }
catch (Exception ex) {
this.logger.warn("Logging environment value ‘" + logConfig
               + "‘ cannot be opened and will be ignored "
               + "(using default location instead)");
         system.initialize(null, logFile);
      }
   }
else {
      system.initialize(null, logFile);
   }
}

(知乎专栏不支持对代码加行号,很不方便啊!)

关键是这一行代码:

ResourceUtils.getURL(logConfig).openStream().close();

得到‘logging.config‘(即常量 CONFIG_PROPERTY)所配置的值后,LoggingApplicationListener先调用ResourceUtils.geURL().openStream() 校验指定的位置是否可读。下面是 ResourceUtils.getURL的源码:

public static URL getURL(String resourceLocation) throws FileNotFoundException {
   Assert.notNull(resourceLocation, "Resource location must not be null");
   if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
      String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
      ClassLoader cl = ClassUtils.getDefaultClassLoader();
      URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
      if (url == null) {
         String description = "class path resource [" + path + "]";
         throw new FileNotFoundException(description +
" cannot be resolved to URL because it does not exist");
      }
return url;
   }
try {
// try URL
      // 码农笔记:logging.config必须使用真实的文件路径,不能是合法的URL语法
      return new URL(resourceLocation);
   }
catch (MalformedURLException ex) {
// no URL -> treat as file path
      try {
return new File(resourceLocation).toURI().toURL();
      }
catch (MalformedURLException ex2) {
throw new FileNotFoundException("Resource location [" + resourceLocation +
"] is neither a URL not a well-formed file path");
      }
   }
}

从上面的代码可得知,‘logging.config‘如果指定的是文件系统位置,必须直接写文件路径,不能加"file:/"前缀。 从而让 "new URL"抛出MalformedURLException,ResourceUtils.getURL才能返回一个正确的文件URL。

‘logging.config‘的写法文档中没有明确的说明,因此我一开始误以为都必须加前缀如classpath: 或者 file:。

https://zhuanlan.zhihu.com/p/19958535

时间: 2024-08-07 16:40:09

Spring Boot 探索系列 - 自动化配置篇的相关文章

Spring Boot干货系列:(一)优雅的入门篇

Spring Boot干货系列:(一)优雅的入门篇http://www.cnblogs.com/zheting/p/6707032.html  全篇参考:http://www.cnblogs.com/zheting/category/966890.html 前言 Spring一直是很火的一个开源框架,在过去的一段时间里,Spring Boot在社区中热度一直很高,所以决定花时间来了解和学习,为自己做技术储备.   正文 首先声明,Spring Boot不是一门新技术,所以不用紧张.从本质上来说,

Spring Boot干货系列:(四)Thymeleaf篇

Spring Boot干货系列:(四)Thymeleaf篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 Web开发是我们平时开发中至关重要的,这里就来介绍一下Spring Boot对Web开发的支持. 正文 Spring Boot提供了spring-boot-starter-web为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及Spring MVC的依赖. 项目结构推荐 一个好的项目结构会让你开发少一些问题,特别是Spr

Spring Boot干货系列:(五)开发Web应用JSP篇

Spring Boot干货系列:(五)开发Web应用JSP篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 上一篇介绍了Spring Boot中使用Thymeleaf模板引擎,今天来介绍一下如何使用SpringBoot官方不推荐的jsp,虽然难度有点大,但是玩起来还是蛮有意思的. 正文 先来看看整体的框架结构,跟前面介绍Thymeleaf的时候差不多,只是多了webapp这个用来存放jsp的目录,静态资源还是放在resources的static下面. 引入依赖 使用内嵌的to

Spring Boot实战系列(7)集成Consul配置中心

本篇主要介绍了 Spring Boot 如何与 Consul 进行集成,Consul 只是服务注册的一种实现,还有其它的例如 Zookeeper.Etcd 等,服务注册发现在微服务架构中扮演这一个重要的角色,伴随着服务的大量出现,服务与服务之间的配置管理.运维管理也变的难以维护,通过 Consul 可以解决这些问题,实现服务治理.服务监控. 关于 Consul 的更多知识点不在这里赘述,但是在学习本节之前还是希望您能先了解下,请移步我之前写的 微服务服务注册发现之 Consul 系列 快速导航

Spring Boot干货系列:(二)配置文件解析

Spring Boot:配置文件解析   前言 上一篇介绍了Spring Boot的入门,知道了Spring Boot使用"习惯优于配置"(项目中存在大量的配置,此外还内置了一个习惯性的配置,让你无需手动进行配置)的理念让你的项目快速运行起来.所以,我们要想把Spring Boot玩的溜,就要懂得如何开启各个功能模块的默认配置,这就需要了解Spring Boot的配置文件application.properties. 正文 Spring Boot使用了一个全局的配置文件applicat

Spring Boot干货系列:(三)启动原理解析

Spring Boot干货系列:(三)启动原理解析 2017-03-13 嘟嘟MD 嘟爷java超神学堂 前言 前面几章我们见识了SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面纱,让它不在神秘. 正文 我们开发任何一个Spring Boot项目,都会用到如下的启动类 从上面代码可以看出,Annotation定义(@SpringBootApplicat

Spring Boot干货系列:(六)静态资源和拦截器处理

Spring Boot干货系列:(六)静态资源和拦截器处理 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类WebMvcConfigurerAdapter. 正文 前面章节我们也有简单介绍过SpringBoot中对静态资源的默认支持,今天详细的来介绍下默认的支持,以及自定义扩展如何实现. 默认资源映射 Spring Boot 默认为我们提供了静态资源处理,使用 WebMvcAutoConfiguration

【转】Spring Boot干货系列:(三)启动原理解析

前言 前面几章我们见识了SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起一步步揭开SpringBoot的神秘面纱,让它不在神秘. 正文 我们开发任何一个Spring Boot项目,都会用到如下的启动类 @SpringBootApplication public class Application { public static void main(String[] args) { Spri

spring boot学习系列(二)

spring boot多环境配置以及yml配置文件 1.平时项目中,我们可能需要到配置生产环境,测试环境,以及开发环境 2.那么每次在项目发布的时候,可能都需要改一下配置文件,修改一些路径才可以. 3.接下来讲一下spring boot的多环境配置,以及yml配置文件. 4.基于上一个demo项目.我们打开一下application.properties文件 5.可以看到里面是空的,什么都没有 6.我们修改一下端口号,添加 server.port=8088 然后我们启动访问一下,端口修改成功.