logback日志写入kafka

配置kafka

kafka:
    bootstrap-servers: 127.0.0.1:9092
    producer:
      retries: 0
      batch-size: 16384
      buffer-memory: 33554432
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

kafka配置类

@Configuration
@EnableKafka
public class KafkaProducerConfig {
    @Value("${spring.kafka.bootstrap-servers}")
    private String servers;
    @Value("${spring.kafka.producer.retries}")
    private int retries;
    @Value("${spring.kafka.producer.batch-size}")
    private int batchSize;
    @Value("${spring.kafka.producer.buffer-memory}")
    private int bufferMemory;

    public KafkaProducerConfig() {
    }

    public Map producerConfigs() {
        Map props = new HashMap();
        props.put("bootstrap.servers", this.servers);
        props.put("retries", this.retries);
        props.put("batch.size", this.batchSize);
        props.put("buffer.memory", this.bufferMemory);
        props.put("key.serializer", StringSerializer.class);
        props.put("value.serializer", StringSerializer.class);
        return props;
    }

    public ProducerFactory producerFactory() {
        return new DefaultKafkaProducerFactory(this.producerConfigs());
    }

    @Bean
    public KafkaTemplate kafkaTemplate() {
        return new KafkaTemplate(this.producerFactory());
    }
}

kafkaAppender

public class KafkaAppender extends ConsoleAppender {
    private static final String TOPIC = "LOG";

    public KafkaAppender() {
    }

    public void start() {
        super.start();
    }

    protected void append(ILoggingEvent iLoggingEvent) {
        if (!Objects.isNull(SpringContextUtils.getApplicationContext())) {
            KafkaTemplate kafkaTemplate = (KafkaTemplate)SpringContextUtils.getBean("kafkaTemplate");
            // 带时间格式日志
            String logStr = new String(this.encoder.encode(iLoggingEvent));
            if (logStr != null) {
                KafkaLogData kafkaLogData = new KafkaLogData();
                kafkaLogData.setKey(SpringContextUtils.getApplicationName());
                kafkaLogData.setLog(logStr);
                kafkaTemplate.send("LOG", JSON.toJSONString(kafkaLogData));
            }

        }
    }
}

logback.xml配置



    
    
    
    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{X-B3-TraceId:-} %-5level %logger{50} - %msg%n
        
    
    
    
        
            
            ${LOG_HOME}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.log
            
            30
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{X-B3-TraceId:-} %-5level %logger{50} - %msg%n
        
        
        
            10MB
        
    
    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %X{X-B3-TraceId:-} %-5level %logger{50} - %msg%n
        
    
    
    
	 
    
    
    
    
    
    

    
    
        
        
        
    
    // 关闭每次显示kafka设置信息
    
    
    
        
        
            
            
                
                
                
                
            
        
    

SpringContextUtils

package com.cbay.common.util.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringContextUtils implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Value("${spring.application.name}")
    private String applicationName0;
    private static String applicationName;

    public SpringContextUtils() {
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        applicationContext = applicationContext;
        applicationName = this.applicationName0;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    public static  T getBean(Class clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static  T getBean(String name, Class clazz) {
        return getApplicationContext().getBean(name, clazz);
    }

    public static String getApplicationName() {
        return applicationName;
    }
}

你可能感兴趣的:(笔记而已,java)