java logback 向 EFK 写入日志

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gsying1474/article/details/52970881

此篇文章主要讲述EFK与logback的整合,主要解决docker的log_driver的日志输出的问题:

  1. 首先,处理错误栈信息的时候,docker会将错误栈分解成多行,而使用此插件,则可以将错误栈完整的保存在elasticsearch的一条记录中
  2. docker内部的服务可以不用写日志文件了(或者有限循环),如果服务还活着,那么日志一定会被写入到elasticsearch

部署一套EFK

详情参考部署文档CentOS7 安装EFK(elasticsearch、fluent、kibana)进行Docker下日志搜集

打包日志插件

源码地址:logback-more-appenders

关于此插件的打包,这里不多做说明,相应jar包可以在刘迎光的nexus中找到,搜索“logback-more-appenders”即可

项目引用

pom.xml文件添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.firewarm</groupId>
	<artifactId>testLogback4Fluentd</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>testLogback4Fluentd Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.1.7</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.7</version>
		</dependency>
		<dependency>
			<groupId>com.sndyuk</groupId>
			<artifactId>logback-more-appenders</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>org.fluentd</groupId>
			<artifactId>fluent-logger</artifactId>
			<version>0.3.2</version>
			<optional>true</optional>
		</dependency>
	</dependencies>
	<build>
		<finalName>testLogback4Fluentd</finalName>
	</build>
</project>

配置logback-test.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appender name="FLUENT_TEXT"
		class="ch.qos.logback.more.appenders.FluentLogbackAppender">
		<!-- Tag for Fluentd. Farther information: http://docs.fluentd.org/articles/config-file -->
		<tag>testlogback</tag>
		<!-- [Optional] Label for Fluentd. Farther information: http://docs.fluentd.org/articles/config-file -->
		<label>logback</label>
		<!-- Host name/address and port number which Flentd placed -->
		<remoteHost>192.168.10.143</remoteHost>
		<port>24224</port>
		<!-- Max queue size of logs which is waiting to be sent (When it reach
			to the max size, the log will be disappeared). -->
		<maxQueueSize>999</maxQueueSize>
		<layout class="ch.qos.logback.classic.PatternLayout">
			<pattern><![CDATA[%date{HH:mm:ss.SSS} [%thread] %-5level %logger{15}#%line %msg]]></pattern>
		</layout>
	</appender>

	<logger name="com.firewarm" level="trace">
		<appender-ref ref="FLUENT_TEXT" />
	</logger>
	<root level="trace">
		<appender-ref ref="trace" />
	</root>
</configuration>

测试的java文件

TestLog.java

package com.firewarm.test;

import com.firewarm.test.utils.LogUtil;

/** 
*
* @author Liuyg 
* @mail liuyg@liuyingguang.cn 
* @version  
* @time 2016年10月15日
*
*/
public class TestLog {
	public static void main(String[] args) {
		TestLog log=new TestLog();
		log.log();
	}
	
	private void log(){
		LogUtil.trace(this, "aaaaaaaaaaaaa");
		try{
			String s=null;
			s.trim();
		}catch (Exception e){
			LogUtil.error(this,e, "Exception");
		}
		
	}
}

LogUtil.java

package com.firewarm.test.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** 
 *
 * @author Liuyg 
 * @mail lyg210@msn.cn 
 * @version  
 * @time May 20, 2015
 *
 */
public class LogUtil {

private static Logger logger =null; 
	
	public static void trace(Object obj,String msg,Object...params) {
		if(obj==null){
			logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
		}else{
			logger = LoggerFactory.getLogger(obj.getClass());
		}
		logger.trace(String.format("-->> %s",String.format(msg, params)));
	}

	public static void error(Object obj,Throwable e,String msg,Object...params) {
		if(obj==null){
			logger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
		}else{
			logger = LoggerFactory.getLogger(obj.getClass());
		}
		logger.error(String.format("-->> %s",String.format(msg, params)),e);
	}
}

以上测试源码地址:testLogback4Fluentd

测试结果如下图 image

展开阅读全文

没有更多推荐了,返回首页