2025年Spring日志管理

Spring日志管理SpringBoot 关于日志的官方文档 1 简述 SpringBoot 官方文档关于日志的整体说明 本博客基于 SpringBoot 1 3 6 大家请先简单看下这篇英文的官方文档 文中有说 SpringBoot 内部日志系统使用的是 Commons Logging 并且 SpringBoot 给 JDKLogging Log4j2 Log4j 也是支持的 Logback

SpringBoot关于日志的官方文档

1、简述

SpringBoot官方文档关于日志的整体说明


本博客基于SpringBoot_1.3.6
大家请先简单看下这篇英文的官方文档,文中有说 SpringBoot 内部日志系统使用的是 Commons Logging 并且 SpringBoot 给 JDKLogging , Log4j2(Log4j也是支持的) , Logback 都提供了默认配置,并且如果使用了 Starters ,那么默认使用 Logback 。
那么什么是这个这个 Starters 呢?大家请看我的pom文件:

 4.0.0 com.lunqi springbootstart 0.0.1-SNAPSHOT boot SpringBootDemo  org.springframework.boot  spring-boot-starter-parent 1.3.6.RELEASE    UTF-8 UTF-8 1.7     org.springframework.boot spring-boot-starter-web   

简单的说,只要你的 pom 文件中使用了 spring-boot-starter 就代表着你使用了 SpringBoot 的 Starters 。相信各位玩 SpringBoot 的朋友肯定是看到了自己的 pom 文件中有这个了,因为,Starters(启动器)是 SpringBoot 最核心的部件之一,没有了启动器, SpringBoot 就几乎废掉了。

2、使用

SpringBoot 招人喜欢的一大特点就是配置方便,配置日志的相关参数也只需要写在 application.properties 中就可以了,当然,这仅仅是基本的配置,如果需要高级的配置,还是需要添加依赖所选择日志系统的配置文件。

SpringBoot官方文档关于配置Log级别的说明

官方文档中有提到, SpringBoot 的 Logging 配置的级别有7个:
TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF

配置格式:
logging.level.*=LEVEL

举例:

#root日志以WARN级别输出
logging.level.root=WARN #springframework.web日志以DEBUG级别输出 logging.level.org.springframework.web=DEBUG #hibernate日志以ERROR级别输出 logging.level.org.hibernate=ERROR

在进行了这样的配置后,就可以在控制台打印Log信息了!

但是很有人又会问了,在生产环境中,日志往往要以文件形式存放到本地,那么 SpringBoot 的默认配置文件能够实现吗?答案是可以的,我们继续看官方文档:

文档中对 SpringBoot 日志的文件输出有明确的说明,上面说到:
在默认情况下,SpringBoot 是仅仅在控制台打印log信息的,如果我们需要将log信息记录到文件,那么就需要在 application.properties 中配置 logging.file 或者 logging.path (注意啊,是或者,不是并且!)
而且官方文档有明确说明,配置 logging.file 的话是可以定位到自定义的文件的,使用 logging.path 的话,日志文件将使用 spring.log 来命名。
而且日志文件会在10Mb大小的时候被截断,产生新的日志文件,默认级别为:ERROR、WARN、INFO

这时候又会有很多同学问了,那如果我要自定义输出格式怎么办呢?其实在 application.properties 也是可以办到的。继续看官方文档:

SpringBoot官方文档关于日志配置的说明


但是要注意:这两个配置项是只对默认的日志系统Logback起作用的

举例(application.properties):

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.file=/log/log/my.log logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n

结果(Console部分):

2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'systemEnvironment': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'messageSource': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'servletContext': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextParameters': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextAttributes': no URL paths identified 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver- Looking for exception mappings: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@15ef3ab: startup date [Thu Sep 22 17:36:04 GMT+08:00 2016]; root of context hierarchy 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Looking for resource handler mappings 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/**/favicon.ico", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@b1df4d] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/webjars/**", locations=[class path resource [META-INF/resources/webjars/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@164e13b] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/**", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1fefcb1] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment- Adding [server.ports] PropertySource with highest search precedence

结果(File部分):

2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'messageSource': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'servletContext': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextParameters': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextAttributes': no URL paths identified 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver- Looking for exception mappings: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@15ef3ab: startup date [Thu Sep 22 17:36:04 GMT+08:00 2016]; root of context hierarchy 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Looking for resource handler mappings

好了,这时候大家已经对 application.properties 中配置Log有了了解,但是这时候又会发现一个问题, application.properties 中的配置有的时候不能满足我们的要求,或者我们要使用其他的集成进SpringBoot的日志系统,该怎么办呢?官方文档又给了我们

SpringBoot官方文档关于使用指定日志配置的说明


我们能从中知道:
1.通过将适当的库添加到classpath,可以激活各种日志系统。然后在 classpath 的根目录 (root) 或通过 Spring Environment( application.properties)的 logging.config 属性指定的位置提供一个合适的配置文件来达到进一步的定制(注意由于日志是在ApplicationContext被创建之前初始化的,所以不可能在Spring的@Configuration文件中,通过@PropertySources控制日志。系统属性和平常的Spring Boot外部配置文件能正常工作)。
2.如果我们使用指定日志系统的配置文件, application.properties 中相关的日志配置是可以不要的。
3.支持的三种日志系统( Logback , Log4j2 ( Log4j 也是支持的), JDKLogging )所识别的配置文件名。

好了,接下来我们就要学习如何在 SpringBoot 中玩这些指定的日志系统了。

3、扩展
#3.1使用Logback的指定配置文件实现更高级的日志配置:

如果我们的确要使用 Logback 的指定配置文件的话,那么说明 application.properties 中的配置功能已经不满足我们需求了,所以 application.properties 中关于日志记录的可以不要了,因为我们启用了 Logback 自己的配置文件,启用的方式很简单,在 classpath 的 resources 下新建 logback.xml 文件。这样就可以了。
具体配置和说明这里有个简单的介绍:

      %d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n     WARN  ACCEPT  DENY      /logs/error.log   %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n     ERROR  ACCEPT  DENY     error.%d{yyyy-MM-dd}.log  30        
#3.2使用Log4j的指定配置文件实现更高级的日志配置:

1.更改pom文件:
在创建 SpringBoot 工程时,我们引入了 spring-boot-starter,其中包含了 spring-boot-starter-logging ,该依赖内容就是 SpringBoot 默认的日志框架 Logback ,所以我们在引入 log4j 之前,需要先排除该包的依赖,再引入 log4j 的依赖。

 org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j 

之后我们可以使用 application.properties,其中的配置项之前有说,同样也是适用 Log4J 的。
如果需要更高级的配置选择,必须要添加 Log4j 的配置文件了。我们在 classpath 的 resources下新建 log4j.properties 文件,这里简单示例一下:

# 日志级别,日志追加程序列表... log4j.rootLogger=DEBUG,ServerDailyRollingFile,stdout #文件保存日志 log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender #文件保存日志日期格式 log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd_HH #文件保存日志文件路径 log4j.appender.ServerDailyRollingFile.File=/mnt/lunqi/demo/log4j.log #文件保存日志布局程序 log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout #文件保存日志布局格式 log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n #文件保存日志需要向后追加(false是测试的时候日志文件就清空,true的话就是在之前基础上往后写) log4j.appender.ServerDailyRollingFile.Append=false #控制台日志 log4j.appender.stdout=org.apache.log4j.ConsoleAppender #控制台日志布局程序 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #控制台日志布局格式 log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n
#3.3使用Log4j2的指定配置文件实现更高级的日志配置:

1.更改pom文件:
跟引入Log4j一样,我们也需要排除 spring-boot-starter-logging ,再引入Log4j2的依赖:

 org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j2 

2.跟Log4j一样,我们可以使用 application.properties ,如果需要更高级的配置选择,必须要添加 Log4j2 的配置文件了。我们在 classpath 的 resources下新建 log4j2.xml 文件:

                     

这里所有的配置信息和配置项在说Logback和Log4j的时候都有说过,所以Log4j2的配置文件肯定是能看懂的。

SpringBoot关于日志的官方文档

1、简述

SpringBoot官方文档关于日志的整体说明


本博客基于SpringBoot_1.3.6
大家请先简单看下这篇英文的官方文档,文中有说 SpringBoot 内部日志系统使用的是 Commons Logging 并且 SpringBoot 给 JDKLogging , Log4j2(Log4j也是支持的) , Logback 都提供了默认配置,并且如果使用了 Starters ,那么默认使用 Logback 。
那么什么是这个这个 Starters 呢?大家请看我的pom文件:

 4.0.0 com.lunqi springbootstart 0.0.1-SNAPSHOT boot SpringBootDemo  org.springframework.boot  spring-boot-starter-parent 1.3.6.RELEASE    UTF-8 UTF-8 1.7     org.springframework.boot spring-boot-starter-web   

简单的说,只要你的 pom 文件中使用了 spring-boot-starter 就代表着你使用了 SpringBoot 的 Starters 。相信各位玩 SpringBoot 的朋友肯定是看到了自己的 pom 文件中有这个了,因为,Starters(启动器)是 SpringBoot 最核心的部件之一,没有了启动器, SpringBoot 就几乎废掉了。

2、使用

SpringBoot 招人喜欢的一大特点就是配置方便,配置日志的相关参数也只需要写在 application.properties 中就可以了,当然,这仅仅是基本的配置,如果需要高级的配置,还是需要添加依赖所选择日志系统的配置文件。

SpringBoot官方文档关于配置Log级别的说明

官方文档中有提到, SpringBoot 的 Logging 配置的级别有7个:
TRACE , DEBUG , INFO , WARN , ERROR , FATAL , OFF

配置格式:
logging.level.*=LEVEL

举例:

#root日志以WARN级别输出
logging.level.root=WARN #springframework.web日志以DEBUG级别输出 logging.level.org.springframework.web=DEBUG #hibernate日志以ERROR级别输出 logging.level.org.hibernate=ERROR

在进行了这样的配置后,就可以在控制台打印Log信息了!

但是很有人又会问了,在生产环境中,日志往往要以文件形式存放到本地,那么 SpringBoot 的默认配置文件能够实现吗?答案是可以的,我们继续看官方文档:

文档中对 SpringBoot 日志的文件输出有明确的说明,上面说到:
在默认情况下,SpringBoot 是仅仅在控制台打印log信息的,如果我们需要将log信息记录到文件,那么就需要在 application.properties 中配置 logging.file 或者 logging.path (注意啊,是或者,不是并且!)
而且官方文档有明确说明,配置 logging.file 的话是可以定位到自定义的文件的,使用 logging.path 的话,日志文件将使用 spring.log 来命名。
而且日志文件会在10Mb大小的时候被截断,产生新的日志文件,默认级别为:ERROR、WARN、INFO

这时候又会有很多同学问了,那如果我要自定义输出格式怎么办呢?其实在 application.properties 也是可以办到的。继续看官方文档:

SpringBoot官方文档关于日志配置的说明


但是要注意:这两个配置项是只对默认的日志系统Logback起作用的

举例(application.properties):

logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.file=/log/log/my.log logging.pattern.console=%d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n logging.pattern.file=%d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n

结果(Console部分):

2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'systemEnvironment': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'messageSource': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'servletContext': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextParameters': no URL paths identified 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextAttributes': no URL paths identified 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver- Looking for exception mappings: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@15ef3ab: startup date [Thu Sep 22 17:36:04 GMT+08:00 2016]; root of context hierarchy 2016/09/22-17:36:06 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Looking for resource handler mappings 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/**/favicon.ico", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/], class path resource []], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@b1df4d] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/webjars/**", locations=[class path resource [META-INF/resources/webjars/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@164e13b] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Found resource handler mapping: URL pattern="/**", locations=[ServletContext resource [/], class path resource [META-INF/resources/], class path resource [resources/], class path resource [static/], class path resource [public/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@1fefcb1] 2016/09/22-17:36:06 [main] DEBUG org.springframework.web.context.support.StandardServletEnvironment- Adding [server.ports] PropertySource with highest search precedence

结果(File部分):

2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'messageSource': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'servletContext': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextParameters': no URL paths identified 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping- Rejected bean name 'contextAttributes': no URL paths identified 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver- Looking for exception mappings: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@15ef3ab: startup date [Thu Sep 22 17:36:04 GMT+08:00 2016]; root of context hierarchy 2016/09/22-17:36 [main] INFO org.springframework.web.servlet.handler.SimpleUrlHandlerMapping- Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016/09/22-17:36 [main] DEBUG org.springframework.web.servlet.resource.ResourceUrlProvider- Looking for resource handler mappings

好了,这时候大家已经对 application.properties 中配置Log有了了解,但是这时候又会发现一个问题, application.properties 中的配置有的时候不能满足我们的要求,或者我们要使用其他的集成进SpringBoot的日志系统,该怎么办呢?官方文档又给了我们

SpringBoot官方文档关于使用指定日志配置的说明


我们能从中知道:
1.通过将适当的库添加到classpath,可以激活各种日志系统。然后在 classpath 的根目录 (root) 或通过 Spring Environment( application.properties)的 logging.config 属性指定的位置提供一个合适的配置文件来达到进一步的定制(注意由于日志是在ApplicationContext被创建之前初始化的,所以不可能在Spring的@Configuration文件中,通过@PropertySources控制日志。系统属性和平常的Spring Boot外部配置文件能正常工作)。
2.如果我们使用指定日志系统的配置文件, application.properties 中相关的日志配置是可以不要的。
3.支持的三种日志系统( Logback , Log4j2 ( Log4j 也是支持的), JDKLogging )所识别的配置文件名。

好了,接下来我们就要学习如何在 SpringBoot 中玩这些指定的日志系统了。

3、扩展
#3.1使用Logback的指定配置文件实现更高级的日志配置:

如果我们的确要使用 Logback 的指定配置文件的话,那么说明 application.properties 中的配置功能已经不满足我们需求了,所以 application.properties 中关于日志记录的可以不要了,因为我们启用了 Logback 自己的配置文件,启用的方式很简单,在 classpath 的 resources 下新建 logback.xml 文件。这样就可以了。
具体配置和说明这里有个简单的介绍:

      %d{yyyy-MM-dd HH:mm:ss} [%level] - %m%n     WARN  ACCEPT  DENY      /logs/error.log   %d{yyyy-MM-dd HH:mm:ss} [%class:%line] - %m%n     ERROR  ACCEPT  DENY     error.%d{yyyy-MM-dd}.log  30        
#3.2使用Log4j的指定配置文件实现更高级的日志配置:

1.更改pom文件:
在创建 SpringBoot 工程时,我们引入了 spring-boot-starter,其中包含了 spring-boot-starter-logging ,该依赖内容就是 SpringBoot 默认的日志框架 Logback ,所以我们在引入 log4j 之前,需要先排除该包的依赖,再引入 log4j 的依赖。

 org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j 

之后我们可以使用 application.properties,其中的配置项之前有说,同样也是适用 Log4J 的。
如果需要更高级的配置选择,必须要添加 Log4j 的配置文件了。我们在 classpath 的 resources下新建 log4j.properties 文件,这里简单示例一下:

# 日志级别,日志追加程序列表... log4j.rootLogger=DEBUG,ServerDailyRollingFile,stdout #文件保存日志 log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender #文件保存日志日期格式 log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd_HH #文件保存日志文件路径 log4j.appender.ServerDailyRollingFile.File=/mnt/lunqi/demo/log4j.log #文件保存日志布局程序 log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout #文件保存日志布局格式 log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n #文件保存日志需要向后追加(false是测试的时候日志文件就清空,true的话就是在之前基础上往后写) log4j.appender.ServerDailyRollingFile.Append=false #控制台日志 log4j.appender.stdout=org.apache.log4j.ConsoleAppender #控制台日志布局程序 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout #控制台日志布局格式 log4j.appender.stdout.layout.ConversionPattern=%d yyyy-MM-dd HH:mm:ss %p [%c] %m%n
#3.3使用Log4j2的指定配置文件实现更高级的日志配置:

1.更改pom文件:
跟引入Log4j一样,我们也需要排除 spring-boot-starter-logging ,再引入Log4j2的依赖:

 org.springframework.boot spring-boot-starter   org.springframework.boot spring-boot-starter-logging     org.springframework.boot spring-boot-starter-log4j2 

2.跟Log4j一样,我们可以使用 application.properties ,如果需要更高级的配置选择,必须要添加 Log4j2 的配置文件了。我们在 classpath 的 resources下新建 log4j2.xml 文件:

                     

这里所有的配置信息和配置项在说Logback和Log4j的时候都有说过,所以Log4j2的配置文件肯定是能看懂的。

编程小号
上一篇 2025-03-18 09:11
下一篇 2025-03-23 14:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/112598.html