日志配置的一些思考

日志配置的一些思考

  • 背景
  • 说明
  • 基础配置抽取
  • 子服务扩展配置

背景

基本所有的系统都需要完善的日志配置, 这里是一些常用的配置. 但是没有本地验证, 只能提供一份配置思路.

说明

不可直接用, 会报错, 提供一种配置思路.
不可直接用, 会报错, 提供一种配置思路.
不可直接用, 会报错, 提供一种配置思路.

基础配置抽取

这里是抽取的基础日志: basic-logback.xml


<included>
    <property name="pattern" value="%date %level [%thread] %logger{10} [%file : %line] %msg%n"/>

    <appender name="spring" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/spring.logfile>
        <encoder>
            <pattern>${pattern}pattern>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/spring.%i.log.gzfileNamePattern>
            <minIndex>1minIndex>
            <maxIndex>20maxIndex>
        rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>300MmaxFileSize>
        triggeringPolicy>
    appender>

    <appender name="accessAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/access.logfile>
        <encoder>
            <pattern>%date --- %level --- [%X{X-B3-TraceId}, %X{X-B3-SpanId}, %X{X-Span-Export}] --- [%thread] --- %logger{10} --- [%file : %line] %msg%npattern>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/access.%i.log.gzfileNamePattern>
            <minIndex>1minIndex>
            <maxIndex>20maxIndex>
        rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>300MmaxFileSize>
        triggeringPolicy>
    appender>

    <appender name="warnError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/warn_error.logfile>
        <encoder>
            <pattern>${pattern}pattern>
        encoder>
        
        <filter class="com.xxx.xxx.xx.ExceptionThresholdFiler">
            <level>WARNlevel>
            <exceptionClazz>com.xxx.xxx.xxx.BusinessExceptionexceptionClazz>
            <onMatch>EDNYonMatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/warn_error.%i.log.gzfileNamePattern>
            <minIndex>1minIndex>
            <maxIndex>20maxIndex>
        rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>300MmaxFileSize>
        triggeringPolicy>
    appender>

    <appender name="businessError" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/business_error.logfile>
        <encoder>
            <pattern>${pattern}pattern>
        encoder>
        
        <filter class="com.xxx.xxx.xx.ExceptionThresholdFiler">
            <level>WARNlevel>
            <exceptionClazz>com.xxx.xxx.xxx.BusinessExceptionexceptionClazz>
            <onMatch>EDNYonMatch>
        filter>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/business_error.%i.log.gzfileNamePattern>
            <minIndex>1minIndex>
            <maxIndex>20maxIndex>
        rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>300MmaxFileSize>
        triggeringPolicy>
    appender>

    
    <logger name="com.xxx.xxx.xxx.AccessLogRecorder" level="info" additivity="false">
        <appender-ref ref="accessAppender"/>
    logger>

    <root level="info">
        <appender-ref ref="spring"/>
        <appender-ref ref="warnError"/>
        <appender-ref ref="businessError"/>
    root>
included>

子服务扩展配置

子服务配置: servie1-logback.xml


<configuration debug="false" scan="true" scanPeriod="3 seconds">
    
    <springProfile name="dev">
        <property name="LOG_HOME" value="./logs"/>
    springProfile>
    
    <springProfile name="!dev">
        <property name="LOG_HOME" value="${logging.path}"/>
    springProfile>
    
    <include resource="basic-logback.xml">include>

    <appender name="xxxxxService" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/service.logfile>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/service/service.%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>30maxHistory>
        rollingPolicy>
        <encoder>
            <pattern>${pattern}pattern>
        encoder>
    appender>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>${pattern}pattern>
        encoder>
    appender>

    <logger name="com.xxx.xxx" level="info" additivity="true">
        <appender-ref ref="xxxxxService">appender-ref>
    logger>

    <springProfile name="dev">
        <root>
            <appender-ref ref="console"/>
        root>
    springProfile>
configuration>

你可能感兴趣的:(java,开发语言)