Log类一般写成静态的,不用实例化,调用其静态方法就可以完成整个应用程序的日志操作.
下面是一个日志操作类Log,它支持两种运行日志记录的方式: 基于文件和基于控制台,另外定义了五个常量用来定义日志输出的级别,不同的级别用来指示消息的重要性,或者严重性.这使得应用程序能够选择接收的消息级别,级别越低,接收的消息越多。
public final static int LOG_LEVEL_DUMP = 1;//详细模式
public final static int LOG_LEVEL_TRACE = 2;//该模式输出程序流程信息
public final static int LOG_LEVEL_NORMAL= 3;//正常模式
public final static int LOG_LEVEL_ERROR = 4;//只输出真正的错误
public final static int LOG_LEVEL_NONE = 5;//不记录日志
import java.io.*;
import java.util.*;
/**
* The log class is used to write out log
* information.
* @author Jeff Heaton
* @version 1.2
*/
public class Log {
public final static int LOG_LEVEL_DUMP = 1;
public final static int LOG_LEVEL_TRACE = 2;
public final static int LOG_LEVEL_NORMAL= 3;
public final static int LOG_LEVEL_ERROR = 4;
public final static int LOG_LEVEL_NONE = 5;
/**
* 是否输出日志到控制台?
*/
protected static boolean log2console = true;
/**
* 是否输出日志到文件.
*/
protected static boolean log2file = false;
/**
* 日志文件的路径,默认为当前目录。
*/
protected static String path = "." +File.separator + "log.txt";
protected static int level = LOG_LEVEL_NONE;//默认的日志级别
private Log(){
}
static public void setLevel(int l)
{
if ( (l==LOG_LEVEL_TRACE) ||
(l==LOG_LEVEL_NORMAL) ||
(l==LOG_LEVEL_NONE) ||
(l==LOG_LEVEL_DUMP) ||
(l==LOG_LEVEL_ERROR) ) {
level = l;
} else {
level = LOG_LEVEL_NORMAL;
}
}
static public void setPath(String s)
{
path = s;
}
static public void setFile(boolean b)
{
log2file = b;
}
static public void setConsole(boolean b)
{
log2console = b;
}
static public int getLevel()
{
return level;
}
static public String getPath()
{
return path;
}
static public boolean getConsole()
{
return log2console;
}
static public boolean getFile()
{
return log2file;
}
/**
* 将异常加入日志.
*
* @param event The text to describe this log event.
* @param e The exception.
*/
static public void logException(String event,Exception e)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);
e.printStackTrace(ps);
ps.close();
log(LOG_LEVEL_ERROR,event + e + ":" + bos);
try {
bos.close();
} catch ( IOException f ) {
}
}
/**
* 实际在日志中记录一个事件.
*
* @param level The level of this event.
* @param event The text to be logged.
*/
synchronized static public void log(int level,String event)
{
if ( level == LOG_LEVEL_NONE )
return;
//if ( level< Log.level )
// return;
Date dt = new Date();
String log = "[" + dt.toString() + "] [";
switch ( level ) {
case LOG_LEVEL_TRACE:log+="TRACE";break;
case LOG_LEVEL_NORMAL:log+="NORMAL";break;
case LOG_LEVEL_ERROR:log+="ERROR";break;
case LOG_LEVEL_NONE:log+="NONE?";break;
case LOG_LEVEL_DUMP:log+="DUMP";break;
}
log+="][" + Thread.currentThread().getName() + "] " + event;
if ( log2console )
System.out.println( log );
if ( log2file ) {
try {
FileOutputStream fw = new FileOutputStream(path,true);
PrintStream ps = new PrintStream(fw);
ps.println(log);
ps.close();
fw.close();
} catch ( IOException e ) {
}
}
}
//测试
public static void main(String args[]){
Log.setConsole(true);
Log.setFile(true);
Log.log(Log.LOG_LEVEL_NORMAL,"HTTP GET "+"http://www.java3z.com");//你可以在应用中这样调用
}
}
运行结果:
C:\java>java Log
[Wed Nov 01 09:55:24 CST 2006] [NORMAL][main] HTTP GET http://www.java3z.com
C:\java>
</td> </tr> </table> </td> </tr> <tr>
↑返回目录
前一篇: 去掉DOS窗口
后一篇: GIF解码和编码操作库源码