log4j2自定义kafka appender
@Plugin(name = "SelfKafkaAppender", category = "Core", elementType = "appender", printObject = true)public class SelfKafkaAppender extends AbstractAppender {private String topic;private String fileNam
·
@Plugin(name = "SelfKafkaAppender", category = "Core", elementType = "appender", printObject = true)
public class SelfKafkaAppender extends AbstractAppender {
private String topic;
private String fileName;
private String kafkaBroker;
private boolean append = true;
private Level level;
private Layout<? extends Serializable> layout;
public SelfKafkaAppender(String name, String topic, String fileName, String kafkaBroker, Filter filter, Layout<? extends Serializable> layout,
boolean append, String level) {
super(name, filter, layout);
this.topic = topic;
LOGGER.error("topic is:" + this.topic);
this.fileName = fileName;
LOGGER.error("fileName is:" + this.fileName);
this.kafkaBroker = kafkaBroker;
KafkaUtils.init(1, kafkaBroker, LOGGER);
LOGGER.error(("kafka broker is:" + kafkaBroker));
this.append = append;
if (level == null) {
level = "INFO";
}
if (layout == null) {
layout = PatternLayout.createDefaultLayout();
}
this.layout = layout;
this.level = Level.toLevel(level);
}
@Override
public void append(LogEvent logEvent) {
if (logEvent.getLevel().isMoreSpecificThan(this.level)) {
//将消息发送到kafka
}
}
@PluginFactory
public static SelfKafkaAppender createAppender(@PluginAttribute("topic") String topic,
@PluginAttribute("fileName") String fileName,
@PluginAttribute("kafkaBroker") String kafkaBroker,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginAttribute("name") String name,
@PluginAttribute("append") boolean append,
@PluginAttribute("level") String level
) {
return new SelfKafkaAppender(name, topic, fileName, kafkaBroker, null, layout, append, level);
}
依赖
<properties>
<log4j.version>2.12.1</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<!-- API bridge between log4j 1 and 2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
示例properties
rootLogger.level=INFO
rootLogger.appenderRef.kafka.ref=SelfKafkaAppender
appender.kafkaLog.name=SelfKafkaAppender
appender.kafkaLog.type=SelfKafkaAppender
appender.kafkaLog.append=true
appender.kafkaLog.fileName=文件名
appender.kafkaLog.kafkaBroker=
appender.kafkaLog.topic=
appender.kafkaLog.layout.type = PatternLayout
appender.kafkaLog.layout.pattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n
更多推荐
已为社区贡献1条内容
所有评论(0)