首页 > 其他 > 详细

logging

时间:2021-01-04 22:59:08      阅读:26      评论:0      收藏:0      [点我收藏+]

  本篇文章主要介绍SpringBoot日志配置相关内容。方便大家更好的使用日志,为问题的排查和日志的维护提供便利。如果文章中有错误或不明确的地方,请大家望指正,谢谢!

代码:

Application.java

package com.wm8.logging;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wm8</groupId>
    <artifactId>logging</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <name>logging</name>
    <url>http://maven.apache.org</url>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

 

代码启动后,日志输入如下

  .   ____          _            __ _ _
 /\\ / ___‘_ __ _ _(_)_ __  __ _ \ \ \ ( ( )\___ | ‘_ | ‘_| | ‘_ \/ _` | \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  ‘  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.2.RELEASE)

2021-01-04 15:27:22.313  INFO 40548 --- [           main] com.wm8.logging.Application              : Starting Application on XXX with PID 40548 (C:\tool\workspace\logging\target\classes started by XXX in C:\tool\workspace\logging)
2021-01-04 15:27:22.313  INFO 40548 --- [           main] com.wm8.logging.Application              : No active profile set, falling back to default profiles: default
2021-01-04 15:27:24.426  INFO 40548 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-01-04 15:27:24.458  INFO 40548 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-01-04 15:27:24.458  INFO 40548 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2021-01-04 15:27:24.614  INFO 40548 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-01-04 15:27:24.614  INFO 40548 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2053 ms
2021-01-04 15:27:24.973  INFO 40548 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService ‘applicationTaskExecutor‘
2021-01-04 15:27:25.618  INFO 40548 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ‘‘
2021-01-04 15:27:25.642  INFO 40548 --- [           main] com.wm8.logging.Application              : Started Application in 4.217 seconds (JVM running for 5.007)

Spring Boot 内部使用Commons-logging来记录日志,方便于集成 Java Util LoggingLog4J2, 和Logback。默认使用logback。

日志格式介绍

  • 日期和时间:毫秒精度

  • 日志级别:ERROR WARN INFO DEBUG TRACE

  • 进程ID

  • 一个---分割器来区分实际日志消息的开始

  • 线程名称:用方括号括起来

  • 记录器名称: 源类名称

  • 日志消息

 

日志输出到文件

  Spring Boot 默认情况下只输出到console中,且日志级别为info级别。如果想要输出到文件中可以配置logging.file.name或者logging.flie.path属性

属性配置说明

logging.file.namelogging.file.pathExampleDescription

(none)

(none)

 

只控制台输出

指定

(none)

my.log

日志记录my.log文件. 值可以指定到精确位置比如:c:/logs/my.log。 如果没有路径,则输出到当前目录下

(none)

指定的目录

/var/log

写spring.log文件到指定的目录. 可以指定绝对路径或相对路径

当两个属性都配置时默认:logging.file.name优先. 

添加:application.yml

logging:
  file:
    name: c:/logs/my.log

当日志文件达到10MB时,会自动压缩为gz包。效果如下:

技术分享图片

 

文件更迭

  该特性在2.4.1上支持 ,当前版本2.3.1不支持

  如果使用logback,可以使用应用程序属性或者应用程序的yaml文件,来配置自己的更迭设置

  支持的更迭策略属性

属性名说明

logging.logback.rollingpolicy.file-name-pattern

日志存档的文件名格式

logging.logback.rollingpolicy.clean-history-on-start

是否在应用启动时进行日志存档清理

logging.logback.rollingpolicy.max-file-size

日志文件大小上限,达到后自动进行存档

logging.logback.rollingpolicy.total-size-cap

存档的日志文件累计最大容量

logging.logback.rollingpolicy.max-history

保存多久的归档日志文件(默认7天)

 

日志级别

logging.level.root=warn
logging.level.org.springframework.web=debug
logging.level.org.hibernate=error

包级别的日志配置

 

日志组

logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
logging.level.tomcat=TRACE

对于多个包且日志级别相同的日志级别定义,此种方式更简洁。

 

Spring Boot 默认预定义了一些组

NameLoggers

web

org.springframework.core.codecorg.springframework.httporg.springframework.weborg.springframework.boot.actuate.endpoint.weborg.springframework.boot.web.servlet.ServletContextInitializerBeans

sql

org.springframework.jdbc.coreorg.hibernate.SQLorg.jooq.tools.LoggerListener

 

自定义日志配置

 

Logging SystemCustomization

Logback

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

Log4j2

log4j2-spring.xml or log4j2.xml

JDK (Java Util Logging)

logging.properties  

根据不同的日志配置文件,会选用不同的日志系统。推荐使用-spring的日志配置文件

 

为了方便于自定义,一些属性从Spring 环境中转义到系统属性中

Spring EnvironmentSystem PropertyComments

logging.exception-conversion-word

LOG_EXCEPTION_CONVERSION_WORD

The conversion word used when logging exceptions.

logging.file.clean-history-on-start

LOG_FILE_CLEAN_HISTORY_ON_START

Whether to clean the archive log files on startup (if LOG_FILE enabled). (Only supported with the default Logback setup.)

logging.file.name

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.file.path

LOG_PATH

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

logging.file.total-size-cap

LOG_FILE_TOTAL_SIZE_CAP

Total size of log backups to be kept (if LOG_FILE enabled). (Only supported with the default Logback setup.)

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.)

logging.pattern.rolling-file-name

ROLLING_FILE_NAME_PATTERN

Pattern for rolled-over log file names (default ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz). (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).

 

 

Logback扩展

 

可以在不同profile下 定义属性

<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>

 

声明一些系统属性,可以在Logback中使用,便于对日志输出格式进行扩展

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

 

logging

原文:https://www.cnblogs.com/9wan/p/14230195.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!