程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java程序設計

Java程序設計

編輯:關於JAVA

/*

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

out.println(""+now.toLocaleString());

out.println(""+name);

out.println("["+sLever][iLevel]+"]");

out.println(""+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("");

out.println("");

out.println("");

out.println("");

out.println("");

out.println("

");

out.println("

");

out.println("

");

out.println("

");

out.println("

");

out.println("

");

out.println("

");

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

}

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved