|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
/* * 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"); } } 返回类别: 教程 上一教程: Java Reflection (JAVA反射) 下一教程: Java多线程中的两个问题 您可以阅读与"Java程序设计"相关的教程: · 如何优化JAVA程序设计和编码,提高JAVA性能 · 《Java 手机/PDA 程序设计入门》读书笔记8--LCDAUI低级API之 线程 时间控制 · Java程序设计的基本结构 · 漫谈Java程序设计中的接口应用 · JAVA的网络程序设计 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |