一、引入依赖
<!--kafka-logback-appender-->
<dependency>
<groupId>io.github.rahulsinghai</groupId>
<artifactId>kafka-logback-appender</artifactId>
<version>1.0.1</version>
</dependency>
二、配置?log4j-spring-kafka.xml
<configuration>
<appender name="kafka-appender" class="io.github.rahulsinghai.KafkaLogbackAppender">
<BrokerList>localhost:9092</BrokerList>
<Topic>song-admin-log</Topic>
<SyncSend>false</SyncSend>
<encoder>
<pattern>%date{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', Asia/Shanghai} %-5p [%c{1}] %m</pattern>
<charset>UTF-8</charset>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="kafka-appender"/>
</root>
</configuration>
三、在application.properties中配置logging
logging.config=classpath:log4j-spring-kafka.xml
logging.level.root=info
logging.level.com.alibaba=error
logging.level.com.song=info
logging.pattern.console=%date{yyyy-MM-dd'T'HH:mm:ss.SSS'Z', Asia/Shanghai} %-5p [%c{1}] %m
四、启动应用并查看日志是否写入
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic song-admin-log --from-beginning
参阅:kafka-logback-appender
|