package log_util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
/**
* 我的日志记录器
* 日志是以xml格式输出日志信息
* 这样可以方便使用程序来解析日志文件
* @author Kevin.wangwei
* 2010-3-12
*/
public final class Log {
/**是的日志文件保存的父目录,默认是在D盘下log目录下*/
private String filePath="d:\\log";
/**日志文件名称*/
private String logName="Log.log";
/**是否打印到控制台*/
private boolean console=true;
/**日志对象实例*/
private static Log instance=null;
/**日志文件的完整路径*/
private String realPath="";
/**输出流*/
private PrintWriter out;
/**当前系统日期字符串*/
private String dateStr;
/**程序运行正常日志信息xml元素名称*/
private final static String INFO="INFO";
/**程序运行异常日志信息xml元素名称*/
private final static String EXCEPTION="exception";
/**阻止通过构造函数创建该对象*/
private Log(){
init();
}
/**获得当前对象实例静态方法*/
public static Log getInstance(){
if(instance==null){
synchronized(Log.class){
if(instance==null){//如果没有这个判断,当两个线程同时访问这个方法会出现创建两个对象
instance=new Log();
}
}
}
return instance;
}
/**初始化*/
private void init(){
Properties prop=new Properties();
try {
InputStream input=Log.class.getClassLoader().getResourceAsStream("log.properties");
prop.load(input);
String path=prop.getProperty("logFilePath");
if(path!=null&&!(path.trim().equals(""))){
filePath=path;
}
String name=prop.getProperty("logName");
if(name!=null&&!(name.trim().equals(""))){
logName=name;
}
String bStr=prop.getProperty("console");
if(bStr.equalsIgnoreCase("true")){
console=true;
}else{
console=false;
}
File f=new File(filePath);
if(!f.exists()){
f.mkdir();
}
realPath=filePath+"\\"+logName+".log";
SimpleDateFormat simpleDate=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateStr=simpleDate.format(new Date());
} catch (IOException e) {
e.printStackTrace();
}
}
/**打印提示信息*/
public void recordLog(String message){
if(message==null){
message="null";
}
if(console){
printMessageToConsole(INFO,dateStr,message);
}
writeLog(INFO,dateStr,message);
}
/**
* 将异常堆栈写到日志文件中
* @param e
*/
public void recordExceptionMsgLog(Exception e){
if(e==null){
return;
}
StackTraceElement[] stackTraceMsg=e.getStackTrace();
try {
out=new PrintWriter(new FileOutputStream(realPath,true));
out.println("<"+EXCEPTION+" time=\""+dateStr+"\" >");
StringBuffer sb=new StringBuffer();
for(int i=0;i<stackTraceMsg.length;i++){
String message =stackTraceMsg[i].toString();
if(i<stackTraceMsg.length-1){
sb.append(message+"\n");
}else{
sb.append(message);
}
out.println(message);
}
out.println("</"+EXCEPTION+">");
out.flush();
if(console){
printMessageToConsole(EXCEPTION,dateStr,sb.toString());
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
/**
* 记录日志
* @param type
* @param time
* @param message
*/
private void writeLog(String type,String time,String message){
try {
out=new PrintWriter(new FileOutputStream(realPath,true));
out.println("<"+type+" time=\""+time+"\" >");
out.println(message);
out.println("</"+type+">");
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
close();
}
}
/**
* 打印信息到控制台
* @param kind
* @param time
* @param message
*/
private void printMessageToConsole(String type,String time,String message){
System.out.println("<"+type+" time=\""+time+"\" >");
System.out.println(message);
System.out.println("</"+type+">");
}
/***
* 关闭文件流
*/
private void close(){
if(out!=null){
out.close();
}
}
}
#日志文件保存路径,在Linux路径为d:/log/ logFilePath=d:\\log #日志文件名称 logName=bperp #是否打印到控制台 console=true