log4j2自定义kafka appender

@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 layout;

    public SelfKafkaAppender(String name, String topic, String fileName, String kafkaBroker, Filter filter, Layout 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 layout,
                                                   @PluginAttribute("name") String name,
                                                   @PluginAttribute("append") boolean append,
                                                   @PluginAttribute("level") String level
    ) {
        return new SelfKafkaAppender(name, topic, fileName, kafkaBroker, null, layout, append, level);
    }

依赖

  


        2.12.1
    

    
        
            org.apache.logging.log4j
            log4j-api
            ${log4j.version}
        

        
            org.apache.logging.log4j
            log4j-core
            ${log4j.version}
        

        
            
            org.apache.logging.log4j
            log4j-1.2-api
            ${log4j.version}
        

        
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.12.1
        


        
            org.apache.kafka
            kafka-clients
            1.1.0
        
    

 示例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

 

你可能感兴趣的:(java,web,基础)