问题:我希望对线程内所有类的方法进行日志输出,在使用AOP编程时 发现我要输出日志的很多类都是静态方法,因此,自己写了一个类对日志进行处理。
package com.iss.util;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.ResourceBundle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 保存日志类
*
* @author gjp
*
*/
public class MessageHandleLogUtil {
final Logger logger = LoggerFactory.getLogger(getClass());
// 日期输出格式
private static final int TIMESTAMP = 1;
private static final int TIMESTAMP_DATE = 2;
private static final int DATE = 3;
private static final int TIME24 = 4;
private static MessageHandleLogUtil logInfo = null;
private static ThreadLocal<MessageHandleLogUtil> local = new ThreadLocal<MessageHandleLogUtil>();
private StringBuffer sb = new StringBuffer();
// false:不在控制台打印日志,true:在控制台打印日志
private static boolean flag = false;
// 日志是否在控制台输出标签
private static final String LOG_OUT_FLAG = "LOG_OUT_FLAG";
// 文件后准名称
private static final String FILE_SUFFIX = ".log";
// 日志输出路径标签
private static final String LOG_FILE_DIR = "LOG_FILE_DIR";
// 日志输出路径
private static String FilePath = null;
static {
ResourceBundle rb = ResourceBundle.getBundle("config");
FilePath = rb.getString(LOG_FILE_DIR);
flag = Boolean.parseBoolean(rb.getString(LOG_OUT_FLAG).trim());
}
public static MessageHandleLogUtil getInstance() {
logInfo = local.get();
if (logInfo == null) {
logInfo = new MessageHandleLogUtil();
local.set(logInfo);
}
return logInfo;
}
public void writeLog(String str) {
sb.append(dateInfo(TIMESTAMP));
sb.append("-");
sb.append(str);
sb.append("\n");
// 输出日志
if (flag) {
outConsole(str);
}
}
/**
* 向平台输入日志
*
* @param str
*/
public void outConsole(String str) {
StringBuffer sbf = new StringBuffer();
sbf.append(dateInfo(TIMESTAMP));
sbf.append("-");
sbf.append(str);
System.out.println(sbf.toString());
}
/**
* 保存报文过程
*
* @param fileLogName
*/
public void saveLogPress(String fileLogName) {
saveLog(fileLogName, null);
}
/**
* 保存报文体
*
* @param fileLogName
* @param content
*/
public void saveLogContent(String fileLogName, String content) {
saveLog(fileLogName, content);
}
/**
* 文件名称
*
* @param fileLogName
* @return
*/
public String filePath() {
StringBuffer sbFileName = new StringBuffer();
sbFileName.append(FilePath);
sbFileName.append(File.separator);
sbFileName.append(dateInfo(DATE));
sbFileName.append(File.separator);
return sbFileName.toString();
}
/**
* 保存实现方法
*
* @param fileLogName
* @param content
*/
private void saveLog(String fileLogName, String content) {
// 文件名
String fileName = dateInfo(TIMESTAMP_DATE) + "_" + fileLogName
+ FILE_SUFFIX;
File filePath = new File(filePath());
if (!filePath.exists()) {
filePath.mkdirs();
}
File file = new File(filePath, fileName);
OutputStream os = null;
try {
if (!file.exists()) {
file.createNewFile();
}
os = new BufferedOutputStream(new FileOutputStream(file));
if (null != content) {
os.write(content.getBytes());
} else {
os.write(sb.toString().getBytes());
}
os.flush();
} catch (Exception e) {
logger.error("error", e.getCause());
e.printStackTrace();
} finally {
if (null != os) {
try {
os.close();
// 清空sb字符串
if (null != sb) {
sb = new StringBuffer();
}
} catch (IOException e) {
logger.error("IOException", e.getCause());
e.printStackTrace();
}
}
}
}
/**
* 返回当前时间
*
* @return
*/
public String dateInfo(int type) {
String dateTime = null;
SimpleDateFormat sdf = null;
switch (type) {
case TIMESTAMP:
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:ms");
break;
case TIMESTAMP_DATE:
sdf = new SimpleDateFormat("yyyyMMddHHmmssms");
break;
case DATE:
sdf = new SimpleDateFormat("yyyyMMdd");
break;
case TIME24:
sdf = new SimpleDateFormat("HHmmssms");
break;
default:
break;
}
dateTime = sdf.format(Calendar.getInstance().getTime());
return dateTime;
}
}