日志接口

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;


public class Log8J {
private static final Object obj = new Object();
private static final String LOG_NAME_HEAD = "SALog_";
private static final String LOG_NAME_ASSES = ".txt";
private static final String ERROR = "[ERROR]\t";
private static final String INFO = "[INFO]\t";
private static File file = null;
private static FileOutputStream fos = null;
private static Properties prop = new Properties();
private static StackTraceElement[] ste = null;
private StringBuffer sb = new StringBuffer();
static {
try {
prop.load(Log8J.class.getClassLoader().getResourceAsStream("logInfo.properties"));
file = new File(prop.getProperty("logPath") + LOG_NAME_HEAD
+ new SimpleDateFormat("yyyyMMddHH").format(new Date())
+ LOG_NAME_ASSES);
file.createNewFile();
fos = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public static void error(String message) {
ste = Thread.currentThread().getStackTrace();
new Log8J().write(ERROR, message, ste);
}


public static void info(String message) {
ste = Thread.currentThread().getStackTrace();
new Log8J().write(INFO, message, ste);
}


private void write(String logType, String message, StackTraceElement[] ste) {
try {
checkFileSize();
String callerInfo = "[" + ste[2].getClassName() + "."
+ ste[2].getMethodName() + "()]\t";
synchronized (obj) {
if (file.length() != 0)
sb.append("\n");
sb.append(makeTimeStamp());
sb.append(logType);
sb.append(callerInfo);
sb.append(message);
}
fos.write(sb.toString().getBytes());


} catch (IOException e) {
System.out.println("The Log function was had a damn ERROR... ...");
e.printStackTrace();
}
}


private static String makeTimeStamp() {
return new SimpleDateFormat("HH:mm:ss").format(new Date()) + "\t";


}


/**
* 检查文件生成的个数,如果超过规定的个数则删除最开始的文件(时间在最前面)
*/
private static void checkFileNum() {
String path = prop.getProperty("logPath");
File file = new File(path);
int factNum = file.list().length;
int targetNum = Integer.valueOf((String) prop.getProperty("logNum"));
int cutNum = 0;
if (factNum > targetNum) {
cutNum = factNum - targetNum;
System.out.println(cutNum);
for (int tmp = 0; tmp < cutNum; tmp++) {
new File(path + file.list()[tmp]).delete();
}
}
}


/***
* 检查文件内容的大小,超过 5242880L 怎重新生成新的文件
*/

private static void checkFileSize() {
if (file.length() < 5242880L)
return;
file = new File(prop.getProperty("logPath") + LOG_NAME_HEAD
+ new SimpleDateFormat("yyMMddHHmm").format(new Date())
+ LOG_NAME_ASSES);
try {
fos = new FileOutputStream(file, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}


logInfo.properties (这里面放置的是一个从log4j.properties自动生成的文件路劲)
logpath = "C\:\\logs\\"
logPath = 15

我的项目是基于struts2和ibatis框架而来的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值