logback.xml的使用,将日志异步保存到数据库中

想要把日志异步保存到数据库中,首先需要创建一个数据库,然后创建三张固定的表:

https://github.com/xiaorenwu-dashijie/logback.git

xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    
    <property name="LOG_HOME" value="/home/logs" />
    
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            
            <pattern>%X{ip} %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{150} - %msg%npattern>
        encoder>
    appender>
      
    <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
              
            <fileNamePattern>${LOG_HOME}//security-info-%d{yyyy-MM-dd}.logfileNamePattern>  
              
            <maxHistory>60maxHistory>  
        rollingPolicy>  
        <encoder>  
            <pattern>%X{ip} %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
        encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
            <level>INFOlevel>  
            <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_HOME}//security-error-%d{yyyy-MM-dd}.logfileNamePattern>  
              
            <maxHistory>60maxHistory>  
        rollingPolicy>  
        <encoder>  
            <pattern>%X{ip} %d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern>  
        encoder>  
        <filter class="ch.qos.logback.classic.filter.LevelFilter">  
            <level>ERRORlevel>  
            <onMatch>ACCEPTonMatch>  
            <onMismatch>DENYonMismatch>  
        filter>  
    appender>

    
    <appender name="DB" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="org.apache.tomcat.jdbc.pool.DataSource">
                <driverClassName>com.mysql.jdbc.DriverdriverClassName>
                <url>jdbc:mysql://**.***.*.**:3306/log_repository?characterEncoding=UTF-8url>
                <username>*****username>
                <password>*****password>
            dataSource>
        connectionSource>
    appender>

    
    
    <logger name="com.logback.test.dao" level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT" />
    logger>
    
    <logger name="com.logback.test" level="INFO" additivity="true">
        <appender-ref ref="DB"/>
    logger>
    
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="INFO" />
        <appender-ref ref="ERROR"/>
    root>
configuration>

在记录日志的时候,我们可能有这种需求,对于每个记录除了知道日志信息外,也要记录访问该系统的IP。

MDC便提供了这个功能,只需要在代码中添加下面的方法即可

MDC.put("ip", ip);

在配置文件中用下面的方式获取即可

%X{ip} 

转载于:https://www.cnblogs.com/java-spring/p/8360109.html

你可能感兴趣的:(logback.xml的使用,将日志异步保存到数据库中)