/***************************************
* 作用: 程序写日志
* 作者:
***************************************/
package util;
import java.io.*;
import java.util.*;
public class Log {
public static void main(String[] args) {
try {
int i = 1 / 0;
} catch(Exception e) {
Log.writeComLog(e.toString());
}
}
/**
* 记录日志内容
* */
public static void writeComLog(String str) {
Calendar c = GregorianCalendar.getInstance();
String filename = "comm"+c.get(c.YEAR)+ fillZero(1+c.get(c.MONTH)+"", 2)
+ fillZero(""+c.get(c.DAY_OF_MONTH), 2) + ".log";
System.out.print(filename);
try {
BufferedWriter bufOut;
File f = new File(filename);
if(f.exists()==true){
bufOut = new BufferedWriter(new FileWriter(f,true));
}else {
bufOut = new BufferedWriter(new FileWriter(f));
}
String datetime = "" + c.get(c.YEAR) + "-"
+ fillZero(1+c.get(c.MONTH)+"", 2) + "-"
+ fillZero(""+c.get(c.DAY_OF_MONTH), 2) + " "
+ fillZero(""+c.get(c.HOUR), 2) + ":"
+ fillZero(""+c.get(c.MINUTE), 2) + ":"
+ fillZero(""+c.get(c.SECOND), 2);
bufOut.write("----------------------------------------------------------------------\n");
bufOut.write("\t" + datetime + "\n");
bufOut.write(str + "\n");
bufOut.write("----------------------------------------------------------------------\n");
bufOut.close();
} catch(Exception e) {
System.out.println("Error");
}
}
/*右对齐左补零*/
static String fillZero(String str, int len) {
int tmp = str.length();
int t;
String str1 = str;
if(tmp >= len)
return str1;
t = len - tmp;
for(int i = 0; i < t; i++ )
str1 = "0" + str1;
return str1;
}
}
/*****************************************************
**将异常消息写入DB
*******************************************************/
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
public static String renderException(Throwable t) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
t.printStackTrace(printWriter);
return result.toString();
}
阅读(1999) | 评论(2) | 转发(0) |