pom.xml:

<logback-encoder.version>4.10</logback-encoder.version>


<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>${logback-encoder.version}</version>
</dependency>

整合logstash后的logback:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <contextName>logback-spring</contextName>
    <property name="service.name" value="zlbp-api" />
    <property name="log.dir" value="/usr/local/app/logs/zlbp/" />
    <property name="log.path" value="{log.dir}/{service.name}.log" />
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <!-- 控制台输出-带颜色 -->
    <appender name="CONSOLE-WITH-COLOR" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>{CONSOLE_LOG_PATTERN}</pattern>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger{36} : [%X{traceId}] %msg%n</pattern>
        </encoder>
    </appender>

    <!-- 文件输出 -->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>{log.path}</file>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名%i指定索引-->
            <fileNamePattern>{log.dir}/{service.name}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
            <!--文件大小200M 超过创建新文件-->
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>200MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <!--程序启动时执行归档删除-->
            <cleanHistoryOnStart>true</cleanHistoryOnStart>
            <!--日志文件保留天数-->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
            <charset>UTF-8</charset>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level --- [%thread] %logger{36} : [%X{traceId}] %msg%n</pattern>
        </encoder>
    </appender>
    <!-- logstash -->
    <appender name="stash" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <!--  这是是logstash服务器地址 端口-->
        <destination>127.0.0.1:8888</destination>
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>TRACE</level>
        </filter>
        <!--输出的格式,推荐使用这个-->
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">

            <providers>
                <timestamp>UTC</timestamp>
                <pattern>
                    <pattern>
                        {
                        "level": "%level",
                        "name": "zlbp",
                        "traceId": "%X{traceId}",
                        "message": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>
    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="FILE"/>
            <appender-ref ref="CONSOLE-WITH-COLOR"/>
            <appender-ref ref="stash" />
        </root>
    </springProfile>

    <springProfile name="dev,test">
        <root level="DEBUG">
            <appender-ref ref="CONSOLE-WITH-COLOR"/>
            <appender-ref ref="FILE"/>
            <appender-ref ref="stash" />
        </root>
    </springProfile>
</configuration>
分类: java

0 条评论

发表评论

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注

站点统计

  • 文章总数:304 篇
  • 分类总数:19 个
  • 标签总数:189 个
  • 运行天数:864 天
  • 访问总数:475135 人次
ICP备案号: 辽ICP备20003309号