欢迎光临
我们一直在努力

Java程序设计-JSP教程,Java基础

建站超值云服务器,限时71元/月

/*

* 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");

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Java程序设计-JSP教程,Java基础
分享到: 更多 (0)

相关推荐

  • 暂无文章