当前位置:开发者网络 >> 技术教程 >> JSP教程 >> Java基础 >> 内容
精彩推荐
分类最新教程
分类热点教程
  
Java程序设计
作者:未知
日期:2004-08-31
人气:
投稿:xiaxia(转贴)
来源:未知
字体:
收藏:加入浏览器收藏
以下正文:
/*
* Created on 2004-8-15
* 目前Log最好的大概是log4j吧,不过在我的运用中,log4j比较难
* 设,设了半天还是不行,特别在tomcat运用中,不知如何可以定
* 义日志文件与项目的相对目录。
* 还有有时想用main做测试时log4j也不工作。
* 一怒之下自己写了个简单的logger
* 你可以自己改改用到你的项目中。
* 调用大概这个 private MyLogger log= MyLogger.getLogger(name);
* 或 private MyLogger log= MyLogger.getLogger(className.class);
* 其它的和log4j差不多了
* log.debug(message);
* log.info(message);
* log.warn(message);
* log.error(message);
* 注意:SystemPath是一个自己写的取得项目根目录路径的类。
*/
package net.ftiger.mis.tools.log;

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;

import net.ftiger.mis.tools.SystemPath;

/**
* @author Ftiger
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
* Date 2004-8-15
*/
public class MyLogger
{
private static HashMap instances = null;
private String name;
private static String logFilePath = "d:\\";
private static int level =0;
private static String[] sLever = {"debug","info ","warn ","error"};
private static String[] sColor = {"#eeeeee","green","yellow","red"};

private MyLogger(String name)
{
this.name = name;
}

public synchronized static MyLogger getLogger(Class cll)
{
String name = cll.getName();
return getLogger(name);
}

public synchronized static MyLogger getLogger(String name)
{
if (instances==null)
{
init();
}
Object obj = instances.get(name);
if (obj ==null)
{
MyLogger instance = new MyLogger(name);
instances.put(name,instance);
return instance;
}
else
{
return (MyLogger) obj;
}
}

private static void init()
{
instances= new HashMap();
logFilePath = SystemPath.getSystemPath("\\WEB-INF\\logs\\");
}

private void log(String message, int iLevel)
{
if (iLevel< level)
return;
Date now = new Date();
StringBuffer sLog = new StringBuffer("");

sLog.append(now.toLocaleString());
sLog.append(" ["+sLever][iLevel]+"] ");
sLog.append(message);
sLog.append("["+name+"]:");
System.out.println(sLog.toString());

//System.out.println(logFilePath);
if (logFilePath==null)
{
return;
}
String logFileName=logFilePath + now.getYear()+"_"+now.getMonth()+"_"+now.getDate()+"log.htm";
//System.out.println("logFileName="+logFileName);
File logFile = new File(logFileName);
if (!logFile.exists())
createNewFile(logFileName);
try
{
FileWriter writer = new FileWriter(logFileName,true);
PrintWriter out = new PrintWriter(writer);
out.println("<tr bgcolor=\""+sColor[iLevel]+"\">");
out.println("<td>"+now.toLocaleString());
out.println("<td>"+name);
out.println("<td>["+sLever][iLevel]+"]");
out.println("<td>"+message);
out.close();
writer.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace(System.out);
}
}

private void createNewFile(String fileName)
{
try
{
System.out.println(fileName);
FileWriter writer = new FileWriter(fileName,false);


PrintWriter out = new PrintWriter(writer);
out.println("<html>");
out.println("<head>");
out.println("<title>Logger</title>");
out.println("<style type=\"text/css\">");
out.println("body,table,td{font-size:12px}");
out.println("");
out.println("");
out.println("</style>");
out.println("</head>");
out.println("<table width=\"96%\" align=\"center\" bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"3\">");
out.println("<tr bgcolor=#006699>");
out.println("<th width=130>TIME</th>");
out.println("<th width=250>FROM</th>");
out.println("<th width=50>LEVEL</th>");
out.println("<th width=*>INFO</th>");
out.println("</tr>");
out.println("");
out.close();

writer.close();

}
catch (IOException e)
{
e.printStackTrace(System.out);
}

}

public void debug ( String message )
{
log(message,0);
}


public void info ( String message )
{
log(message,1);
}

public void warn ( String message )
{
log(message,2);
}

public void error (String message )
{
log(message,3);
}

public static void main(String[] args)
{
MyLogger log=MyLogger.getLogger(MyLogger.class);
log.debug("ok");
log.info("info ok");
log.warn("warn ok");
log.error("error ok");


}
}

相关文章: