首页 > 代码库 > log4j日志工具类

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日志工具类