pom.xml

增加依赖

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>com.github.danielwegener</groupId>
            <artifactId>logback-kafka-appender</artifactId>
            <version>0.2.0-RC1</version>
            <scope>runtime</scope>
        </dependency>

添加依赖,不然会有解析错误

        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.11</version>
        </dependency>

application.yaml

log:
  topic: test-log-topic
  address: 147.94.126.233:9093,147.97.22.206:9093,147.98.156.201:9093

logback-spring.xml

增加这两行配置

<springProperty scope="context" name="topic" source="log.topic"/>
    <springProperty scope="context" name="address" source="log.address"/>

增加appender

    <appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
        <!--输出的格式,推荐使用这个-->
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">

            <providers>
                <timestamp>UTC</timestamp>
                <pattern>
                    <pattern>
                        {
                        "level": "%level",
                        "name": "这里填写原logstash的name",
                        "traceId": "%X{traceId}",
                        "topic": "{topic}",
                        "message": "%message"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
        <topic>{topic}</topic>
        <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/>
        <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>


        <producerConfig>bootstrap.servers=${address}</producerConfig>

        <appender-ref ref="CONSOLE-WITH-COLOR"/>
    </appender>
    <appender name="async" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="kafkaAppender"/>
    </appender

修改spring profile,日志级别改成info,如果是debug会报错,(这个是bug

    <springProfile name="dev,test">
        <root level="INFO">
            <appender-ref ref="CONSOLE-WITH-COLOR"/>
            <appender-ref ref="FILE"/>
            <appender-ref ref="kafkaAppender"/>

        </root>

    </springProfile>

停掉原本采集的logstash

分类: java

0 条评论

发表评论

Avatar placeholder

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

ICP备案号: 辽ICP备20003309号