【弄nèng - Springboot】Springboot整合logback日志

文章目录

    • 1. logback介绍
    • 2. 添加依赖
    • 3. 配置YML
    • 4. 配置文件
    • 代码地址
    • 项目推荐

本片以使用为主,概念请移动百度

1. logback介绍

logback是一款开源的日志框架,内核重写了,是基于log4j基础进行改良的,在性能上有很大提升,拥有更多特性。
官网为https://logback.qos.ch/
logback-core:是其它两个模块的基础模块
logback-classic:log4j的改良版本,实现了Slf4j API
logback-access:集成Servle环境时就需要logback了,可以提供Http访问的接口

日志级别
日志级别:ERROR, WARN, INFO, DEBUG ,TRACE

2. 添加依赖

springboot 父类中自带,不用额外添加

	<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.1.6.RELEASEversion>
        <relativePath/>
    parent>

3. 配置YML

logging:
  path: E:\workspace\prod\tmp

4. 配置文件

在resources路径下创建logback-spring.xml


<configuration>

    <property name="server_name" value="community-website"/>
    //接收yml中的path参数
    <property name="log_dir" value="${LOG_PATH:-.}"/>
    <property name="maxFileSize" value="50MB"/>
    <property name="maxHistory" value="15"/>

    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            



            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
    appender>

    
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log_dir}/${server_name}-info-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log_dir}/${server_name}-warn-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>WARNlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log_dir}/${server_name}-error-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
    appender>

    
    <appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            
            <fileNamePattern>${log_dir}/${server_name}-all-%d{yyyy-MM-dd}-%i.logfileNamePattern>
            
            <maxHistory>${maxHistory}maxHistory>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                
                <maxFileSize>${maxFileSize}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>
                %d{yyyy-MM-dd HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{50} - %msg%n
            pattern>
        encoder>
        
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUGlevel>
        filter>
    appender>

    
    <springProfile name="dev">
        
        
        <logger name="com.baomidou.mybatisplus.*" level="DEBUG" additivity="false">logger>

        
        <root level="INFO" additivity="false">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="INFO"/>
            <appender-ref ref="WARN"/>
            <appender-ref ref="ERROR"/>
        root>
    springProfile>

    
    <springProfile name="test">
        
        
        <logger name="com.baomidou.mybatisplus.*" level="DEBUG" additivity="false">logger>

        
        <root level="INFO" additivity="false">
            <appender-ref ref="STDOUT"/>
            <appender-ref ref="INFO"/>
            <appender-ref ref="WARN"/>
            <appender-ref ref="ERROR"/>
            <appender-ref ref="ALL"/>
        root>
    springProfile>

    
    <springProfile name="pro">
        
        <root level="INFO">
            <appender-ref ref="ALL"/>
            <appender-ref ref="INFO"/>
            <appender-ref ref="WARN"/>
            <appender-ref ref="ERROR"/>

        root>
    springProfile>

configuration>

代码地址

传送门
开源项目,持续不断更新中,喜欢请 Star~

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等

你可能感兴趣的:(Spring,boot)