logback 配置日志输出到 logstash

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defualts.xml"/>

    <springProperty scope="context" name="springAppName" source="spring.application.name"/>
    <springProperty scope="context" name="logFile" source="logging.file"/>

    <!--日志在工程中的输出位置-->
    <property name="LOG_FILE" value="${logFile}"/>
    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
    <!--控制台的日志输出样式-->
    <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}}"/>


    <!--控制台 Appender-->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>DEBUG</level>
        </filter>
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>


    <!--隐藏服务发现后输出的日志-->
    <logger name="com.netflix.discovery.shared.resolver.aws.ConfigClusterResolver" level="WARN"/>
    <logger name="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" level="WARN"/>


    <springProfile name="test,dev">
        <!--LOGSTASH-->
        <appender name="logstash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
            <destination>127.0.0.1:9250</destination>
            <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
            </encoder>
        </appender>
        <!--文件-->
        <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <Prudent>true</Prudent>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>
                    ${logFile}.%d{yyyy-MM-dd}.log
                </FileNamePattern>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss} -%msg%n
                </Pattern>
            </layout>
        </appender>
        <root level="INFO">
            <appender-ref ref="console"/>
            <appender-ref ref="logstash"/>
            <appender-ref ref="fileAppender"/>
        </root>
    </springProfile>
    <springProfile name="prod">
        <!--文件-->
        <appender name="fileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <Prudent>true</Prudent>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>
                    ${logFile}.%d{yyyy-MM-dd}.log
                </FileNamePattern>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>
                    %d{yyyy-MM-dd HH:mm:ss} -%msg%n
                </Pattern>
            </layout>
        </appender>
        <root level="WARN">
            <appender-ref ref="console"/>
            <appender-ref ref="fileAppender"/>
        </root>
    </springProfile>

</configuration>

 

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据