Springboot使用Logback实现日志配置与异常记录

 更新时间:2022年11月20日 13:48:47   作者:Buckletime  
默认情况下,SpringBoot内部使用logback作为系统日志实现的框架,将日志输出到控制台,不会写到日志文件。本篇文章主要讲解下如何自定义logabck.xml以及对logback文件中配置做一个详解,需要的可以参考一下

概述

默认情况下,SpringBoot内部使用logback作为系统日志实现的框架,将日志输出到控制台,不会写到日志文件。如果在application.properties或application.yml配置,这样只能配置简单的场景,保存路径、日志格式等。复杂的场景(区分 info 和 error 的日志、每天产生一个日志文件等)满足不了,只能自定义配置文件logback-spring.xml或者logback.xml。本篇文章主要讲解下如何自定义logabck.xml以及对logback文件中配置做一个详解。

logback配置详解

首先我们先了解下logback。

logback 主要分为三个模块:

  • logback-core:是其他两个模块的基础模块
  • logback-classic:是对 core 模块的扩展,相当于 log4j 的改良版。classic 模块实现了 Slf4j 的 API 因此可以便于和其他日志框架直接切换
  • logback-access:与Servlet容器集成,以提供http访问日志功能。

官网配置文档地址:https://logback.qos.ch/manual/configuration.html

logback配置

1.application .properties

# 日志打印信息配置
logging.config=classpath:logback-spring.xml

2.logback-spring.xml

<?mybatis version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <contextName>logback</contextName>
    <!--输出到控制台-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!-- <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
             <level>ERROR</level>
         </filter>-->
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 日志文件会滚动 -->
        <!-- 文件路径 -->
        <file>./logs/gf_scanning.log</file><!-- 当前的日志文件存放路径 -->
        <!-- 日志滚动策略 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 历史日志文件的存放路径和名称 -->
            <fileNamePattern>./logs/gf_scanning.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!-- 日志文件最大的保存历史 数量-->
            <maxHistory>30</maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n</pattern> <!-- 日志文件中日志的格式 -->
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
    </root>
</configuration>

异常情况

遇到个奇怪的问题,按上述配置完成logback的配置后,大多项目可以正常运行并保存日志到文件,但有一个项目启动时报错。java.lang.IllegalStateException: Could not initialize Logback logging from classpath:logback-spring.xml ... Caused by: ch.qos.logback.core.joran.spi.JoranException: Parser configuration error occurred

Logging system failed to initialize using configuration from 'classpath:logback-spring.xml'
java.lang.IllegalStateException: Could not initialize Logback logging from classpath:logback-spring.xml
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:168)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:66)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:57)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:132)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:316)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:282)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:240)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
    at java.util.ArrayList.forEach(ArrayList.java:1257)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:339)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:297)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301)
    at cn.piesat.ScanningApplication.main(ScanningApplication.java:18)
Caused by: ch.qos.logback.core.joran.spi.JoranException: Parser configuration error occurred
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:89)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.recordEvents(SaxEventRecorder.java:57)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:151)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.configureByResourceUrl(LogbackLoggingSystem.java:188)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:165)
    ... 22 more
Caused by: org.xml.sax.SAXNotSupportedException: Feature: http://xml.org/sax/features/external-general-entities
    at org.apache.crimson.parser.XMLReaderImpl.setFeature(XMLReaderImpl.java:211)
    at org.apache.crimson.jaxp.SAXParserImpl.setFeatures(SAXParserImpl.java:143)
    at org.apache.crimson.jaxp.SAXParserImpl.<init>(SAXParserImpl.java:126)
    at org.apache.crimson.jaxp.SAXParserFactoryImpl.newSAXParserImpl(SAXParserFactoryImpl.java:113)
    at org.apache.crimson.jaxp.SAXParserFactoryImpl.setFeature(SAXParserFactoryImpl.java:141)
    at ch.qos.logback.core.joran.event.SaxEventRecorder.buildSaxParser(SaxEventRecorder.java:82)
    ... 28 more
Disconnected from the target VM, address: '127.0.0.1:64222', transport: 'socket'

Process finished with exit code 1

可能是由于jar包冲突,在pom文件中单独引入logback-core的依赖后,问题解决。

<!-- logback 日志 -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-core</artifactId>
    <version>1.1.7</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.7</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

到此这篇关于Springboot使用Logback实现日志配置与异常记录的文章就介绍到这了,更多相关Springboot Logback内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 基于SpringBoot和Vue的动态语音播放实现

    基于SpringBoot和Vue的动态语音播放实现

    本文介绍如何使用SpringBoot和Vue实现音频文件的动态播放,包括前端页面设计、后端接口开发、音频文件存储和调用等方面。通过该实现,用户可以在网页上直接播放音频,增强用户体验,提高网站互动性
    2023-04-04
  • JAVA中的日期LocalDate类详细用法讲解

    JAVA中的日期LocalDate类详细用法讲解

    Java中存在一个日历表示法的类库,LocalDate类,如果只想要指定的年月日就可以用这个LocalDate类,下面这篇文章主要给大家介绍了关于JAVA中日期LocalDate类详细用法讲解的相关资料,需要的朋友可以参考下
    2024-01-01
  • springboot集成redis lettuce

    springboot集成redis lettuce

    目前java操作redis的客户端有jedis跟Lettuce。本文主要介绍了springboot集成redis lettuce,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • Apache Commons CLI构建命令行应用利器教程

    Apache Commons CLI构建命令行应用利器教程

    这篇文章主要为大家介绍了构建命令行应用利器Apache Commons CLI的使用教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • Java代码实现对properties文件有序的读写的示例

    Java代码实现对properties文件有序的读写的示例

    本篇文章主要介绍了Java代码实现对properties文件有序的读写的示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • MyBatisPlus防全表更新与删除的实现示例

    MyBatisPlus防全表更新与删除的实现示例

    本文主要介绍了MyBatisPlus防全表更新与删除的实现示例,针对 update 和 delete 语句,阻止恶意的全表更新和全表删除,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • 利用IDEA社区版创建SpringBoot项目的详细图文教程

    利用IDEA社区版创建SpringBoot项目的详细图文教程

    大家应该都知道Idea社区版本,默认是不能创建SpringBoot项目的,下面这篇文章主要给大家介绍了关于利用IDEA社区版创建SpringBoot项目的详细图文教程,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • Druid连接池的自定义过滤功能实现方法

    Druid连接池的自定义过滤功能实现方法

    在数据密集型应用中,监控和分析数据库操作对于确保性能和稳定性至关重要,本文将探讨如何实现一个自定义的Druid过滤器来捕获数据库请求并进行日志记录,以辅助开发和维护工作,需要的朋友可以参考下
    2023-11-11
  • Spring Retry重试框架的使用讲解

    Spring Retry重试框架的使用讲解

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次。用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有,感兴趣的可以了解一下
    2022-10-10
  • Java reservedcodecachesize虚拟机参数案例详解

    Java reservedcodecachesize虚拟机参数案例详解

    这篇文章主要介绍了Java reservedcodecachesize虚拟机参数案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08

最新评论