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