·您的位置: 首页 » 资源教程 » 编程开发 » JAVA、JSP » Java程序设计

Java程序设计

类别: JAVA教程  评论数:0 总得分:0
/*
* 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");


}
}




-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1