zoukankan      html  css  js  c++  java
  • Logback将日志输出到Kafka配置示例

    1.maven依赖

       <dependency>
          <groupId>com.github.danielwegener</groupId>
          <artifactId>logback-kafka-appender</artifactId>
          <version>0.2.0-RC2</version>
        </dependency>
    
        <dependency>
          <groupId>net.logstash.logback</groupId>
          <artifactId>logstash-logback-encoder</artifactId>
          <version>6.4</version>
        </dependency>
    
        <dependency>
          <groupId>ch.qos.logback</groupId>
          <artifactId>logback-classic</artifactId>
          <version>1.2.3</version>
        </dependency> 

    2.logback-spring.xml中增加以下内容

        <springProperty scope="context" name="service" source="spring.application.name" defaultValue="UnknownService"/>
        <springProperty scope="context" name="env" source="elk.env" defaultValue="test"/>
        <springProperty scope="context" name="bootstrapServers" source="elk.kafka.bootstrap.servers" defaultValue="localhost:9092"/>
        <!--以上三行需要和yml对应-->
    
        <appender name="kafkaAppender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
            <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
                <providers class="net.logstash.logback.composite.loggingevent.LoggingEventJsonProviders">
                    <pattern>
                        <pattern>
                            {
                            "env": "${env}",
                            "service":"${service}",
                            "date":"%d{yyyy-MM-dd HH:mm:ss.SSS}",
                            "level":"%level",
                            "thread": "%thread",
                            "logger": "%logger{36}",
                            "msg":"%msg",
                            "exception":"%exception"
                            }
                        </pattern>
                    </pattern>
                </providers>
            </encoder>
            <topic>msa-log-${env}</topic>
            <keyingStrategy class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy"/>
            <deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy"/>
            <producerConfig>acks=0</producerConfig>
            <producerConfig>linger.ms=1000</producerConfig>
            <producerConfig>max.block.ms=0</producerConfig>
            <producerConfig>bootstrap.servers=${bootstrapServers}</producerConfig>
        </appender>
    
        <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
            <appender-ref ref="kafkaAppender"/>
        </appender>
    
        <root level="INFO">
            <appender-ref ref="ASYNC"/>
        </root>

     3.参考

    https://github.com/danielwegener/logback-kafka-appender

    https://github.com/danielwegener/logback-kafka-appender/blob/master/src/example/resources/logback.xml

  • 相关阅读:
    nginx 配置文件简介
    nginx 二进制安装
    nginx 简介  http://nginx.org
    全栈https
    运维工程师如果将web服务http专变为https
    12个JQuery小贴士
    AccessHelper 需修改
    MysqlHelper 需要重写
    Func<T,TResult>泛型委托
    DataConvertJson
  • 原文地址:https://www.cnblogs.com/dotqin/p/13648170.html
Copyright © 2011-2022 走看看