阿里云日志服务 sdk集成

背景描述:

原来使用log4j 进行日志打印,一天下来,一个文件要跑到200M+,每次都要先下载下来才能查看,很费时间,看了阿里的log服务,可以在线查看,感觉很牛叉,就拿过来用了

集成方法有很多种,我是采用的Log4j Appender 方式,(因为可以在原来的log4j的基础上 引入几个包就可以了,这样还可以自由打印日志,很方便)

1,首先开通日志服务,在日志后台创建项目

2,创建日志库

https://help.aliyun.com/document_detail/29005.html?spm=5176.doc43757.6.554.ZXKKNm


3 引入所需jar包


需要的jar包 获取方法

https://help.aliyun.com/document_detail/43757.html?spm=5176.doc28981.2.27.neLd89


jar包下载下来之后重点看下有没有这个loghub-log4j-appender.jar,我就是没有这个包导致报错,最后找到了客服,要的这个包,客服很给力,服务到位

4,配置log4j.property


基本看这里的配置就好了

https://help.aliyun.com/document_detail/43757.html?spm=5176.doc28981.2.27.neLd89

这个过程中有个坑,明明包引入了,配置里却找不到LoghubAppender 类,于是客服提供了LoghubAppender源码,自己再继承封装了一下

下面是 LoghubAppender源码

package com.xc.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;

import com.aliyun.openservices.log.common.LogItem;
import com.aliyun.openservices.log.producer.LogProducer;
import com.aliyun.openservices.log.producer.ProducerConfig;
import com.aliyun.openservices.log.producer.ProjectConfig;

public class LoghubAppender extends AppenderSkeleton {
	private ProducerConfig config = new ProducerConfig();
	private LogProducer producer;
	private ProjectConfig projectConfig = new ProjectConfig();
	private String logstore;
	private String topic = "";
	private String timeZone = "UTC";
	private String timeFormat = "yyyy-MM-dd'T'HH:mmZ";
	private SimpleDateFormat formatter;

	@Override
	public void activateOptions() {
		super.activateOptions();
		formatter = new SimpleDateFormat(timeFormat);
		formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
		producer = new LogProducer(config);
		producer.setProjectConfig(projectConfig);
	}

	public String getTimeFormat() {
		return timeFormat;
	}

	public void setTimeFormat(String timeFormat) {
		this.timeFormat = timeFormat;
		formatter = new SimpleDateFormat(timeFormat);
		formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
	}

	public void close() {
		producer.flush();
		producer.close();
	}

	public boolean requiresLayout() {
		return false;
	}

	public String getLogstore() {
		return logstore;
	}

	public void setLogstore(String logstore) {
		this.logstore = logstore;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(String timeZone) {
		this.timeZone = timeZone;
		formatter = new SimpleDateFormat(timeFormat);
		formatter.setTimeZone(TimeZone.getTimeZone(timeZone));
	}

	@Override
	protected void append(LoggingEvent event) {
		List logItems = new ArrayList();
		LogItem item = new LogItem();
		logItems.add(item);
		item.SetTime((int) (event.getTimeStamp() / 1000));
		item.PushBack("time", formatter.format(new Date(event.getTimeStamp())));
		item.PushBack("level", event.getLevel().toString());
		item.PushBack("thread", event.getThreadName());
		item.PushBack("location",
				event.getLocationInformation().fullInfo.toString());
		String message = event.getMessage().toString();
		ThrowableInformation throwable = event.getThrowableInformation();
		if(throwable != null){
			for(String s: throwable.getThrowableStrRep()){
				message += System.lineSeparator() + s;
			}
		}
		item.PushBack("message", message);
		Map properties = event.getProperties();
		if (properties.size() > 0) {
			Object[] keys = properties.keySet().toArray();
			Arrays.sort(keys);
			for (int i = 0; i < keys.length; i++) {
				item.PushBack(keys[i].toString(), properties.get(keys[i])
						.toString());
			}
		}
		producer.send(projectConfig.projectName, logstore, topic, null,
				logItems);
	}

	public String getProjectName() {
		return projectConfig.projectName;
	}

	public void setProjectName(String projectName) {
		projectConfig.projectName = projectName;
	}

	public String getEndpoint() {
		return projectConfig.endpoint;
	}

	public void setEndpoint(String endpoint) {
		projectConfig.endpoint = endpoint;
	}

	public String getAccessKeyId() {
		return projectConfig.accessKeyId;
	}

	public void setAccessKeyId(String accessKeyId) {
		projectConfig.accessKeyId = accessKeyId;
	}

	public String getAccessKey() {
		return projectConfig.accessKey;
	}

	public void setAccessKey(String accessKey) {
		projectConfig.accessKey = accessKey;
	}

	public String getStsToken() {
		return projectConfig.stsToken;
	}

	public void setStsToken(String stsToken) {
		projectConfig.stsToken = stsToken;
	}

	public int getPackageTimeoutInMS() {
		return config.packageTimeoutInMS;
	}

	public void setPackageTimeoutInMS(int packageTimeoutInMS) {
		config.packageTimeoutInMS = packageTimeoutInMS;
	}

	public int getLogsCountPerPackage() {
		return config.logsCountPerPackage;
	}

	public void setLogsCountPerPackage(int logsCountPerPackage) {
		config.logsCountPerPackage = logsCountPerPackage;
	}

	public int getLogsBytesPerPackage() {
		return config.logsBytesPerPackage;
	}

	public void setLogsBytesPerPackage(int logsBytesPerPackage) {
		config.logsBytesPerPackage = logsBytesPerPackage;
	}

	public int getMemPoolSizeInByte() {
		return config.memPoolSizeInByte;
	}

	public void setMemPoolSizeInByte(int memPoolSizeInByte) {
		config.memPoolSizeInByte = memPoolSizeInByte;
	}

	public int getIoThreadsCount() {
		return config.maxIOThreadSizeInPool;
	}

	public void setIoThreadsCount(int ioThreadsCount) {
		config.maxIOThreadSizeInPool = ioThreadsCount;
	}

	public int getShardHashUpdateIntervalInMS() {
		return config.shardHashUpdateIntervalInMS;
	}

	public void setShardHashUpdateIntervalInMS(int shardHashUpdateIntervalInMS) {
		config.shardHashUpdateIntervalInMS = shardHashUpdateIntervalInMS;
	}

	public int getRetryTimes() {
		return config.retryTimes;
	}

	public void setRetryTimes(int retryTimes) {
		config.retryTimes = retryTimes;
	}
}


然后 写了个LogUTils工具类(其实配置文件中可以直接使用 LoghubAppender的 ,只是项目中使用了日志唯一入口——自己原来封装的logUtils继承自DaliRolling,做了分级别打印,为了偷懒就直接改了继承LoghubAppender)


就这么简单,最后到阿里日志控制台查看,有了


程序员常用网址收藏管理——666导航网


你可能感兴趣的:(网站问题)