log4j日志工具类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4j日志工具类相关的知识,希望对你有一定的参考价值。

/**
* log4j日志Util
*/
public class LogUtil {
private static Logger logger=null;

static{
StackTraceElement[] stackTraceElements=new Throwable().getStackTrace();
String callerClassName=stackTraceElements[1].getClassName();
logger=LoggerFactory.getLogger(callerClassName);
}

private LogUtil(){}

//---------------------error---------------------------------
public static void error(String param,String msg,Throwable e){
StackTraceElement se = Thread.currentThread().getStackTrace()[2];
String clazzMethod = " "+se.getClassName() +"." + se.getMethodName() + "("+se.getFileName()+":"+se.getLineNumber()+") ";

String log = clazzMethod+" PARAMS: " +param+" "+" MSG: "+ msg;
logger.error(log,e);
}

//---------------------warn----------------------------------
public static void warn(String param,String msg){
StackTraceElement se = Thread.currentThread().getStackTrace()[2];
String clazzMethod = " "+se.getClassName() +"." + se.getMethodName() + "("+se.getFileName()+":"+se.getLineNumber()+") ";

String log = clazzMethod+" PARAMS: " +param+" "+" MSG: "+ msg;
logger.warn(log);
}

//---------------------info----------------------------------
public static void info(String param,String msg){//
StackTraceElement se = Thread.currentThread().getStackTrace()[2];
String clazzMethod = " "+se.getClassName() +"." + se.getMethodName() + "("+se.getFileName()+":"+se.getLineNumber()+") ";

String log = clazzMethod + " PARAMS: " +param+" "+" MSG: "+ msg;
logger.info(log);
}

public static void debug(String msg) {
StackTraceElement se = Thread.currentThread().getStackTrace()[2];
String clazzMethod = " "+se.getClassName() +"." + se.getMethodName() + "("+se.getFileName()+":"+se.getLineNumber()+") ";

String log = clazzMethod + " MSG: "+ msg;
logger.debug(log);
}

}
















































以上是关于log4j日志工具类的主要内容,如果未能解决你的问题,请参考以下文章

开源日志工具log4j

日志工具 Log4j

Log4j

log4j日志工具

转 JDK自带日志工具Logger的研究使用

日志同步工具