Log4j輸出日誌到kafka
阿新 • • 發佈:2018-12-18
一、新增需要的jar包
去掉springboot專案自帶的日誌jar包,新增kafka和log4j的jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> </dependency>
二、專案配置檔案(application.properties)新增log4j配置檔案
logging.config = classpath:log4j-spring-kafka.xml
三、log4j配置檔案
<?xml version="1.0" encoding="UTF-8"?> <!-- Configuration後面的status,這個用於設定log4j2自身內部的資訊輸出,可以不設定,當設定成trace時, 你會看到log4j2內部各種詳細輸出。可以設定成OFF(關閉)或Error(只輸出錯誤資訊) --> <Configuration status="OFF"> <properties> <property name="kafka_bootstrap_servers">0.0.0.0:9092</property> <property name="kafka_retries">3</property> <property name="kafka_linger_ms">1000</property> <!-- 如果你想減少請求的數量,你可以將linger.ms設定為大於某個值的東西 --> <property name="kafka_buffer_size">1048576</property> <!-- 緩衝區大小 --> <property name="kafka_buffer_memory">10485760</property> <!-- 控制生產者可用於緩衝的儲存器的總量 --> </properties> <Appenders> <!-- 輸出控制檯日誌的配置 --> <Console name="console" target="SYSTEM_OUT"> <!--控制檯只輸出level及以上級別的資訊(onMatch),其他的直接拒絕(onMismatch) --> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY" /> <!-- 輸出日誌的格式 --> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-5level %logger{36} - %msg%n" /> </Console> <!-- syncSend指是否同步等待,設為false表示傳送訊息後立即返回,true則會等待kafka響應後返回 --> <Kafka name="buried" topic="buried" syncSend="false"> <Filters> <MarkerFilter marker="buried" onMatch="ACCEPT" onMismatch="DENY"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" /> </Filters> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-5level %logger{36} %marker - %msg%n"/> <Property name="bootstrap.servers" value="${kafka_bootstrap_servers}"/> <Property name="retries" value="${kafka_retries}" /> <Property name="linger.ms" value="${kafka_linger_ms}" /> <Property name="buffer.size" value="${kafka_buffer_size}" /> <Property name="buffer.memory" value="${kafka_buffer_memory}" /> </Kafka> <Kafka name="monitor" topic="monitor" syncSend="false"> <Filters> <MarkerFilter marker="monitor" onMatch="ACCEPT" onMismatch="DENY"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY" /> </Filters> <PatternLayout charset="UTF-8" pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-5level %logger{36} %marker - %msg%n"/> <Property name="bootstrap.servers" value="${kafka_bootstrap_servers}"/> <Property name="retries" value="${kafka_retries}" /> <Property name="linger.ms" value="${kafka_linger_ms}" /> <Property name="buffer.size" value="${kafka_buffer_size}" /> <Property name="buffer.memory" value="${kafka_buffer_memory}" /> </Kafka> <Async name="async"> <AppenderRef ref="buried"/> <AppenderRef ref="monitor"/> </Async> </Appenders> <Loggers> <!-- name 必填且唯一 --> <!-- level 設定輸出最低級別 預設error --> <!-- additivity 是否在父Logger輸出, 預設 true --> <Logger name="com.start" level="trace" additivity="true"> <AppenderRef ref="async" /> </Logger> <!-- 必須包含一個 Root 元素 --> <Root level="debug"> <AppenderRef ref="console" /> </Root> </Loggers> </Configuration>
四、寫日誌
protected static final Logger LOGGER = LogManager.getLogger(FlumeStartApplication.class); public static final Marker BURIED = MarkerManager.getMarker("buried"); public static final Marker MONITOR = MarkerManager.getMarker("monitor"); public static void main(String[] args) { SpringApplication.run(FlumeStartApplication.class, args); int i = 0; while (true) { i++; if (i%3 == 0) { LOGGER.info(BURIED,"BURIED埋點訊息{}", System.currentTimeMillis()/1000); } else if(i%3 == 1) { LOGGER.debug(BURIED,"BURIED埋點訊息{}", System.currentTimeMillis()/1000); } else { LOGGER.info(MONITOR,"MONITOR監控訊息{}", System.currentTimeMillis()/1000); } try { Thread.sleep(2000); System.out.println("睡眠一會兒======="); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }