记得刚开始学习Java的时候,为了跟踪程序的进程及异常信息,到处添加System.out.println()语句,到后来虽然也学会了配置并使用Log4j,但一直都只是用用而已,没有去深入地了解,近期在做一个小项目的时候又用到了Log4j,在捕捉一些异常的时候希望直接记录在日志文件里,于是在网上找了些资料完成了这个小小的工具类。
package
com.hing.tools;
import org.apache.log4j.Logger;
public class LogTool ... {
public static void info(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.info(getException((Exception)e));
}else ...{
logger.info(e.toString());
}
}
public static void warn(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.warn(getException((Exception)e));
}else ...{
logger.warn(e.toString());
}
}
public static void debug(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.debug(getException((Exception)e));
}else ...{
logger.debug(e.toString());
}
}
public static void error(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.error(getException((Exception)e));
}else ...{
logger.error(e.toString());
}
}
public static void fatal(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.fatal(getException((Exception)e));
}else ...{
logger.fatal(e.toString());
}
}
public static String getException(Exception e)...{
StackTraceElement[] ste = e.getStackTrace();
StringBuffer sb = new StringBuffer();
sb.append(e.getMessage() + " ");
for (int i = 0; i < ste.length; i++) ...{
sb.append(ste[i].toString() + " ");
}
return sb.toString();
}
}
import org.apache.log4j.Logger;
public class LogTool ... {
public static void info(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.info(getException((Exception)e));
}else ...{
logger.info(e.toString());
}
}
public static void warn(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.warn(getException((Exception)e));
}else ...{
logger.warn(e.toString());
}
}
public static void debug(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.debug(getException((Exception)e));
}else ...{
logger.debug(e.toString());
}
}
public static void error(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.error(getException((Exception)e));
}else ...{
logger.error(e.toString());
}
}
public static void fatal(Class logClass,Object e)...{
Logger logger=Logger.getLogger(logClass);
if(e instanceof Exception)...{
logger.fatal(getException((Exception)e));
}else ...{
logger.fatal(e.toString());
}
}
public static String getException(Exception e)...{
StackTraceElement[] ste = e.getStackTrace();
StringBuffer sb = new StringBuffer();
sb.append(e.getMessage() + " ");
for (int i = 0; i < ste.length; i++) ...{
sb.append(ste[i].toString() + " ");
}
return sb.toString();
}
}
测试类:
package
com.sms.test;
import com.hing.tools.LogTool;
public class TestLogTool ... {
public static void main(String[] args)...{
LogTool.info(TestLogTool.class, "this is info");
try...{
throw new Exception("my exception");
}catch(Exception e)...{
LogTool.error(TestLogTool.class, e);
}
}
}
import com.hing.tools.LogTool;
public class TestLogTool ... {
public static void main(String[] args)...{
LogTool.info(TestLogTool.class, "this is info");
try...{
throw new Exception("my exception");
}catch(Exception e)...{
LogTool.error(TestLogTool.class, e);
}
}
}
测试结果:
2007
-
02
-
13
15
:
22
:
26
[com.sms.test.TestLogTool]
-
[INFO]
this
is info
2007 - 02 - 13 15 : 22 : 26 [com.sms.test.TestLogTool] - [ERROR] my exception
com.sms.test.TestLogTool.main(TestLogTool.java: 8 )
2007 - 02 - 13 15 : 22 : 26 [com.sms.test.TestLogTool] - [ERROR] my exception
com.sms.test.TestLogTool.main(TestLogTool.java: 8 )