做推文网站,长沙信息网,网络服务合同法律规定,商贸有限公司注销流程本章从第4章开始
4. Logging
Spring Boot使用Commons Logging进行所有内部日志记录#xff0c;但保留底层日志实现开放。为Java Util Logging、Log4J2和Logback提供了默认配置。在每种情况下#xff0c;记录器都预先配置为使用控制台输出和可选的文件输出。 默认情况下但保留底层日志实现开放。为Java Util Logging、Log4J2和Logback提供了默认配置。在每种情况下记录器都预先配置为使用控制台输出和可选的文件输出。 默认情况下如果您使用“starter”则使用Logback进行日志记录。还包括适当的Logback路由以确保使用Java Util Logging、Commons Logging、Log4J或SLF4J的依赖库都能正确工作。
Java有很多可用的日志框架。如果上面的列表看起来令人困惑不要担心。一般来说您不需要更改日志依赖项Spring Boot默认值也可以正常工作。
当将应用程序部署到servlet容器或应用程序服务器时使用Java Util logging API执行的日志记录不会路由到应用程序的日志中。这可以防止容器或已部署到容器的其他应用程序执行的日志记录出现在应用程序的日志中。
4.1. Log Format
Spring Boot的默认日志输出类似如下示例:
2023-10-28 18:53:33.562 INFO 21592 --- [nio-3344-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet dispatcherServlet
2023-10-28 18:53:33.562 INFO 21592 --- [nio-3344-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet dispatcherServlet
2023-10-28 18:53:33.563 INFO 21592 --- [nio-3344-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Logback没有致命级别。它被映射到ERROR。
4.2. Console Output
默认日志配置在写入消息时将消息回显到控制台。缺省情况下日志记录级别为ERROR-level、WARN-level和INFO-level。您还可以通过使用——debug标志启动应用程序来启用“调试”模式。
类似于
$ java -jar myapp.jar --debug又或者在配置文件中写入
debugtrue
当启用调试模式时将配置一些核心日志记录器(嵌入式容器、Hibernate和Spring Boot)以输出更多信息。启用调试模式不会将应用程序配置为以debug级别记录所有消息。 或者您可以通过使用——trace标志启动应用程序来启用“跟踪”模式(或在application.properties中使用tracetrue)。这样做可以对选定的核心记录器(嵌入式容器、Hibernate模式生成和整个Spring组合)进行跟踪日志记录。
4.2.1. Color-coded Output
如果您的终端支持ANSI则使用颜色输出来提高可读性。您可以将spring.output.ansi.enabled设置为支持的值以覆盖自动检测。 使用%clr转换字配置颜色编码。在其最简单的形式中转换器根据日志级别为输出着色如下例所示:
%clr(%5p)日志级别与颜色的对应关系如下表所示:
LevelColor FATAL Red ERROR Red WARN Yellow INFO Green DEBUG Green TRACE Green
4.3. File Output
默认情况下Spring Boot只记录到控制台不写日志文件。如果除了控制台输出之外还想写入日志文件则需要设置logging.file.name或logging.file.path属性(例如在application.properties中)。 下表显示了如何进行日志记录。*属性可以一起使用:
logging.file.namelogging.file.pathExampleDescription (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.
日志文件在达到10 MB时轮换并且与控制台输出一样默认情况下记录错误级别、警告级别和info级别的消息。
日志记录属性独立于实际的日志记录基础设施。因此特定的配置键(如logback。配置文件Logback)不是由spring Boot管理的。
4.4. File Rotation
如果您正在使用Logback则可以使用您的应用程序对日志轮换设置进行微调。属性或应用程序。yaml文件。对于所有其他日志系统您需要自己直接配置旋转设置(例如如果您使用Log4J2那么您可以添加一个Log4J2 .xml或Log4J2 -spring.xml文件)。 支持以下旋转策略属性:
NameDescription logging.logback.rollingpolicy.file-name-pattern The filename pattern used to create log archives. logging.logback.rollingpolicy.clean-history-on-start If log archive cleanup should occur when the application starts. logging.logback.rollingpolicy.max-file-size The maximum size of log file before it is archived. logging.logback.rollingpolicy.total-size-cap The maximum amount of size log archives can take before being deleted. logging.logback.rollingpolicy.max-history The maximum number of archive log files to keep (defaults to 7).
4.5. Log Levels
所有支持的日志系统都可以通过使用logging.level在Spring环境中(例如在application.properties中)设置日志记录器级别。logger-namelevel其中level是TRACE、DEBUG、INFO、WARN、ERROR、FATAL或OFF中的一个。可以使用logging.level.root配置根记录器。
下面的例子显示了application.properties中可能的日志设置:
logging.level.rootwarn
logging.level.org.springframework.webdebug
logging.level.org.hibernateerror也可以使用环境变量设置日志记录级别。例如LOGGING_LEVEL_ORG_SPRINGFRAMEWORK_WEBDEBUG将org.springframework.web设置为DEBUG。
4.6. Log Groups
能够将相关的记录器分组在一起以便可以同时配置它们这通常是很有用的。例如您可能经常更改所有与Tomcat相关的日志记录器的日志级别但是您不容易记住顶级包。 为了帮助实现这一点Spring Boot允许您在Spring环境中定义日志组。例如下面是你如何通过将“tomcat”组添加到application.properties中来定义它:
logging.group.tomcatorg.apache.catalina,org.apache.coyote,org.apache.tomcat
一旦定义你可以用一行改变组中所有记录器的级别: PropertiesYaml
logging.level.tomcattraceSpring Boot包括以下可以开箱即用的预定义日志组:
NameLoggers web org.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans sql org.springframework.jdbc.core, org.hibernate.SQL, org.jooq.tools.LoggerListener
4.7. Using a Log Shutdown Hook
为了在应用程序终止时释放日志资源提供了一个关机钩子该钩子将在JVM退出时触发日志系统清理。这个关闭钩子是自动注册的除非您的应用程序作为war文件部署。如果您的应用程序具有复杂的上下文层次结构那么shutdown钩子可能无法满足您的需求。如果没有禁用shutdown钩子并研究底层日志系统直接提供的选项。例如Logback提供上下文选择器允许在自己的上下文中创建每个Logger。您可以使用日志记录。
logging.register-shutdown-hookfalse4.8. Custom Log Configuration
可以通过在类路径中包含适当的库来激活各种日志记录系统还可以通过在类路径的根目录或由以下Spring Environment属性指定的位置提供合适的配置文件来进一步定制日志记录系统:logging.config。 你可以通过使用org.springframework.boot.logging.LoggingSystem系统属性强制Spring Boot使用特定的日志系统。该值应该是LoggingSystem实现的完全限定类名。您还可以通过使用一个值完全禁用Spring Boot的日志配置
因为日志记录是在ApplicationContext创建之前初始化的所以不可能从Spring Configuration文件中的PropertySources控制日志记录。更改日志系统或完全禁用它的唯一方法是通过系统属性。
根据您的日志系统将加载以下文件:
Logging SystemCustomization 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变量(例如logback-spring.xml而不是logback.xml)。如果使用标准配置位置Spring不能完全控制日志初始化。
Java Util Logging有一些已知的类加载问题在从“可执行jar”运行时会导致问题。如果可能的话我们建议您在从“可执行jar”运行时避免使用它。
为了帮助进行定制将一些其他属性从Spring环境转移到System属性。这允许记录系统配置使用属性。例如在应用程序中设置logging.file.name。属性或LOGGING_FILE_NAME作为环境变量将导致设置LOG_FILE System属性。传输的属性如下表所示:
Spring EnvironmentSystem PropertyComments logging.exception-conversion-word LOG_EXCEPTION_CONVERSION_WORD The conversion word used when logging exceptions. logging.file.name LOG_FILE If defined, it is used in the default log configuration. logging.file.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). logging.pattern.dateformat LOG_DATEFORMAT_PATTERN Appender pattern for log date format. logging.charset.console CONSOLE_LOG_CHARSET The charset to use for console logging. logging.pattern.file FILE_LOG_PATTERN The log pattern to use in a file (if LOG_FILE is enabled). logging.charset.file FILE_LOG_CHARSET The charset to use for file logging (if LOG_FILE is enabled). logging.pattern.level LOG_LEVEL_PATTERN The format to use when rendering the log level (default %5p). PID PID The current process ID (discovered if possible and when not already defined as an OS environment variable).
如果使用Logback还会传输以下属性:
Spring EnvironmentSystem PropertyComments logging.logback.rollingpolicy.file-name-pattern LOGBACK_ROLLINGPOLICY_FILE_NAME_PATTERN Pattern for rolled-over log file names (default ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz). logging.logback.rollingpolicy.clean-history-on-start LOGBACK_ROLLINGPOLICY_CLEAN_HISTORY_ON_START Whether to clean the archive log files on startup. logging.logback.rollingpolicy.max-file-size LOGBACK_ROLLINGPOLICY_MAX_FILE_SIZE Maximum log file size. logging.logback.rollingpolicy.total-size-cap LOGBACK_ROLLINGPOLICY_TOTAL_SIZE_CAP Total size of log backups to be kept. logging.logback.rollingpolicy.max-history LOGBACK_ROLLINGPOLICY_MAX_HISTORY Maximum number of archive log files to keep.
所有支持的日志系统在解析配置文件时都可以参考System属性。参见spring-boot.jar中的默认配置示例: Logback Log4j 2 Java Util logging
如果你想在日志属性中使用占位符你应该使用Spring Boot的语法而不是底层框架的语法。值得注意的是如果使用Logback应该使用:作为属性名与其默认值之间的分隔符而不是使用:-。 您可以通过仅覆盖LOG_LEVEL_PATTERN(或使用Logback覆盖logging.pattern.level)将MDC和其他特别内容添加到日志行。例如如果您使用logging.pattern。leveluser:%X{user} %5p则默认日志格式中包含“user”的MDC条目如果存在如下例所示。
2019-08-30 12:30:04.031 user:someone INFO 22174 --- [ nio-8080-exec-0] demo.Controller
Handling authenticated request
4.9. Logback Extensions
Spring Boot包括许多Logback扩展可以帮助进行高级配置。您可以在logback-spring.xml配置文件中使用这些扩展名。
因为标准的logback.xml配置文件加载得太早所以不能在其中使用扩展。您需要使用logback-spring.xml或定义一个日志记录。配置属性。
扩展不能与Logback的配置扫描一起使用。如果您尝试这样做对配置文件进行更改会导致类似于以下错误之一的错误记录:
ERROR in ch.qos.logback.core.joran.spi.Interpreter4:71 - no applicable action for [springProperty], current ElementPath is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter4:71 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
4.9.1. Profile-specific Configuration
springProfile标签允许您选择性地包含或排除基于活动Spring配置文件的配置部分。在configuration元素中的任何地方都支持配置文件节。使用name属性指定哪个概要文件接受配置。springProfile标记可以包含一个概要名称(例如staging)或一个概要表达式。概要表达式允许表达更复杂的概要逻辑例如production (eu-central | eu-west)。查看参考指南了解更多细节。下面的清单显示了三个样例配置文件:
springProfile namestaging!-- configuration to be enabled when the staging profile is active --
/springProfilespringProfile namedev | staging!-- configuration to be enabled when the dev or staging profiles are active --
/springProfilespringProfile name!production!-- configuration to be enabled when the production profile is not active --
/springProfile4.9.2. Environment Properties
springProperty标记允许您公开Spring环境中的属性以便在Logback中使用。如果希望访问应用程序中的值这样做可能很有用。属性文件中的Logback配置。该标记的工作方式类似于Logback的标准property标记。但是不是直接指定值而是指定属性的来源(来自环境)。如果需要将属性存储在局部作用域以外的地方可以使用scope属性。如果需要一个回退值(如果没有在环境中设置属性)可以这样做。
springProperty scopecontext namefluentHost sourcemyapp.fluentd.hostdefaultValuelocalhost/
appender nameFLUENT classch.qos.logback.more.appenders.DataFluentAppenderremoteHost${fluentHost}/remoteHost...
/appender源必须以kebab的形式指定(例如my.property-name)。但是可以通过使用宽松的规则将属性添加到环境中。