SpringBoot logback 配置文件自定義屬性


 

添加自定義屬性類

 

package com.cus.config;


import ch.qos.logback.core.PropertyDefinerBase;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class CustomProperty extends PropertyDefinerBase {

    @Override
    public String getPropertyValue() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String str = sdf.format(new Date());
        try{
            InetAddress address = InetAddress.getLocalHost();
            str = address.getHostAddress() + "_" + str;
        }
        catch (Exception e){
            e.printStackTrace();
        }

        return str;
    }
}

 

在 logback.xml 中使用自定義屬性

 

定義屬性:

<define name="port" class="com.cus.config.CustomProperty" />

使用定義的屬性:

${port}
<file>${LOG_HOME}/info_${port}.log</file>

 

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <jmxConfigurator/>
    <define name="port" class="com.cus.config.CustomProperty" />
    <!--<property name="LOG_FILE" value="/opt/logs/connectivity"/>-->
    <property name="LOG_FILE" value="D:/logs/connectivity"/>
    <!--啟動File Loader時需要修改Log文件的目錄為/aplog/fileLoader -->
    <!--<property name="LOG_FILE" value="/aplog/fileLoader"/>-->
    <!--定義日志文件的存儲地址 勿在 LogBack 的配置中使用相對路徑 -->
    <property name="LOG_HOME" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-./tmp}}/}}"/>
    <!-- 最大保存歷史日志天數 -->
    <property name="LOG_MAX_HISTORY" value="30"/>
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    <property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!--讀取配置中心的屬性-->
    <springProperty scope="context" name="dataSourceClass" source="portal-manager.datasource.type"/>
    <springProperty scope="context" name="driverClassName" source="portal-manager.datasource.driver-class-name"/>
    <springProperty scope="context" name="url" source="portal-manager.datasource.url"/>
    <springProperty scope="context" name="username" source="portal-manager.datasource.username"/>
    <springProperty scope="context" name="password" source="portal-manager.datasource.password"/>

    <!-- 控制台輸出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <FileNamePattern>${LOG_HOME}/job_stdout_${port}.log.%d{yyyy-MM-dd}.log</FileNamePattern>
        <!--<withJansi>true</withJansi>-->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 -->
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- 消息日志,記錄項目所有消息記錄 -->
    <appender name="infoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 如果指定了file屬性,當天的文件名為file屬性值 -->
        <file>${LOG_HOME}/info_${port}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件輸出的文件名 -->
            <FileNamePattern>${LOG_HOME}/info_${port}.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <maxFileSize>200MB</maxFileSize>
            <totalSizeCap>8GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 -->
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>INFO</level>
        </filter>
    </appender>

    <!-- 消息日志,記錄項目所有消息記錄 -->
    <appender name="warnLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 如果指定了file屬性,當天的文件名為file屬性值 -->
        <file>${LOG_HOME}/warn_${port}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件輸出的文件名 -->
            <FileNamePattern>${LOG_HOME}/warn_${port}.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <maxFileSize>200MB</maxFileSize>
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 -->
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>WARN</level><!-- 只接收錯誤級別的日志 -->
        </filter>
    </appender>

    <!-- 錯誤日志,記錄項目標識的錯誤級別信息 -->
    <appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/error_${port}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/error_${port}.log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <maxFileSize>100MB</maxFileSize>
            <totalSizeCap>2GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>

        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
    </appender>

    <appender name="DBAppender" class="ch.qos.logback.classic.db.DBAppender">
        <connectionSource class="ch.qos.logback.core.db.DataSourceConnectionSource">
            <dataSource class="${dataSourceClass}">
                <driverClassName>${driverClassName}</driverClassName>
                <url>${url}</url>
                <username>${username}</username>
                <password>${password}</password>
            </dataSource>
        </connectionSource>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 消息日志,記錄項目所有消息記錄 -->
    <appender name="debugLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 如果指定了file屬性,當天的文件名為file屬性值 -->
        <file>${LOG_HOME}/debug_${port}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <!--日志文件輸出的文件名 -->
            <FileNamePattern>${LOG_HOME}/debug_${port}.log.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
            <maxHistory>${LOG_MAX_HISTORY}</maxHistory>
            <maxFileSize>400MB</maxFileSize>
            <totalSizeCap>5GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <!--格式化輸出:%d表示日期,%thread表示線程名,%-5level:級別從左顯示5個字符寬度%msg:日志消息,%n是換行符 -->
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>


</included>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM