log日志记录

package util;

import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 日志操作类
*/
public class CDcwLogFile {
private static Log logger=LogFactory.getLog(CDcwLogFile.class);
/**
* 日志名称
*/
private String logName;
/**
* 最后一次删除日志时间,0为1970年起始
*/
private Date lastDelOldLogFileTime = new Date(0);
/**
* 日志文件名日期格式化
*/
private SimpleDateFormat dataformat = new SimpleDateFormat(
"yyyyMMdd", Locale.US);
/**
* 日志记录标题头日期格式
*/
private SimpleDateFormat strtitleformat = new SimpleDateFormat(
"HH:mm:ss->", Locale.US);

/**
* 构造函数
* 
* @param logname
* 日志名称
*/
public CDcwLogFile(String logname) {
super();
this.logName = logname;
}

/**
* 取得日志文件全名
* 
* @param date
* 当前日期
* @return 返回日志文件全名
*/
private String getLogFileName(Date date) {
return Config.getLogSavePath() + File.separator + this.logName
+ dataformat.format(date) + ".txt";
}

/**
* 删除旧的日志
* 
* @param t
* 当前日期
* @param bForce
* 强行删除
*/
private void deleteOldLogFile(Date t) {
if ((t.getTime() - lastDelOldLogFileTime.getTime()) > 1000 * 60 * 60 * 24) {
// 删除相隔了3天的日志(防止跳过)deleteOnExit()是jvm结束后删除
for (int i = 1; i <= 3; i++) {
Date d = new Date(t.getTime() - 1000 * 60 * 60 * 24 * i
* Config.getLogValidityDay());
File f = new File(getLogFileName(d));
f.deleteOnExit();
}
lastDelOldLogFileTime = t;
}
}

/**
* 记录日志写入
* 
* @param level
* 日志级别,0->不记录 1->起动失败错误 2->警告错误 3->详细记录
* @param format
* 内容格式化
* @param args
* 格式参数
*/
public synchronized void writeLog(int level, String format) {
if (Config.getLogLevel() >= level) {
FileWriter fw=null;
try {
Date d = new Date();
logger.info(format+"\n");
deleteOldLogFile(d);
fw = new FileWriter(this.getLogFileName(d), true);
PrintWriter out = new PrintWriter(fw);
out.write(strtitleformat.format(d));
/**out.format(format);用于printf()格式,如:
* out.printf("姓名:%s;年龄:%d;性别:%c;分数:%5.2f;", name,age,sex,score);*/
out.println(format);
out.flush();
out.close();
} catch (Exception e) {
logger.info(e);
}finally{
try {
if(fw!=null){
fw.close();
}
} catch (IOException e) {
logger.info(e);
}
}
}
}

/**
* 记录日志,十六进制格式
* 
* @param level
* 日志级别,0->不记录 1->起动失败错误 2->警告错误 3->详细记录
* @param name
* 项名称
* @param buf
* 字节内容缓冲
* @param len
* 缓冲长度

public synchronized void writeByteTo0x16Format(int level, String name,
byte[] buf, int len) {
if (Config.getLogLevel() >= level) {
try {
Date d = new Date();
StringBuffer str = new StringBuffer();
for (int i = 0; i < len; i++) {
str.append(String.format("%2X ", buf[i]));
}
System.out.printf("%s%s(%d) %s", strtitleformat.format(d),
name, len, str.toString());
System.out.println();
deleteOldLogFile(d);
FileWriter fw = new FileWriter(this.getLogFileName(d), true);
PrintWriter out = new PrintWriter(fw);
out.write(strtitleformat.format(d));
out.format("%s %s", name, str.toString());
out.println();
out.flush();
out.close();
fw.close();
} catch (Exception e) {
}
}
} */
}

  

posted @ 2017-12-15 14:35  杯子茶壶  阅读(57)  评论(0)    收藏  举报