All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sghd.logging.PatternFileAppender Maven / Gradle / Ivy

The newest version!
package com.sghd.logging;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.WeakHashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.FileAppender;
import org.apache.log4j.spi.LoggingEvent;

import com.sghd.common.utils.json.JsonUtils;
import com.sghd.logging.csv.CsvUtils;

/**
 * 抽象日期格式文件记录器
 */
public class PatternFileAppender extends FileAppender {
	protected final static Field MESSAGE_FIELD;
	static {
		try {
			MESSAGE_FIELD = LoggingEvent.class.getDeclaredField("renderedMessage");
			MESSAGE_FIELD.setAccessible(true);
		} catch (Exception exception) {
			throw new RuntimeException(exception);
		}
	}
	private String directoryName;
	private String filePattern;
	private String format;
	/** 记录器缓存 */
	private WeakHashMap appenders = new WeakHashMap<>();

	@Override
	public void activateOptions() {
	}
	
	@Override
	public synchronized void doAppend(LoggingEvent event) {
		Object[] params = (Object[]) event.getMessage();
		String name = (String) params[0];
		String cron = (String) params[1];
		Record message = (Record) params[2];
		if (StringUtils.isBlank(name)) {// 未指定日志名称, 使用类名
			name = message.getClass().getSimpleName();
		}
		String content = getContent(message);
		if(StringUtils.isBlank(content)){
			return;
		}
		FileAppender appender = touch(PathUtils.getPath(directoryName, filePattern, cron, name, message));
		try {
			MESSAGE_FIELD.set(event, content);
		} catch (Exception e) {
			throw new RuntimeException("设置日志内容异常", e);
		}
		appender.doAppend(event);
	}
	
	public String getContent(Record message){
		if (message instanceof EmptyRecord ) {
			return null;
		}
		try {
			if (format != null && format.equalsIgnoreCase("csv")) {
				return CsvUtils.object2String(message);
			} else {
				return JsonUtils.object2String(message);
			}
		} catch (Exception e) {
			throw new RuntimeException("设置日志内容异常", e);
		}
	}
	
	protected FileAppender touch(String fullPath) {
		FileAppender appender = appenders.get(fullPath);
		if (appender == null) {
			try {
				appender = new FileAppender(layout, fullPath);
			} catch (IOException e) {
				throw new RuntimeException("创建记录器异常", e);
			}
			appender.activateOptions();
			appenders.put(fullPath, appender);
		} else {
			String file = appender.getFile();
			if (file == null || !file.equals(fullPath)) {
				appender.setFile(fullPath);
				appender.activateOptions();
			}
		}
		return appender;
	}
	
	public String getDirectoryName() {
		return directoryName;
	}
	
	public String getFilePattern() {
		return filePattern;
	}

	public String getFormat() {
		return format;
	}

	public void setDirectoryName(String directoryName) {
		this.directoryName = directoryName;
	}

	public void setFilePattern(String filePattern) {
		this.filePattern = filePattern;
	}

	public void setFormat(String format) {
		this.format = format;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy