廢話不多說,直接上代碼
/**
* Java實現類似C/C++中的__FILE__、__FUNC__、__LINE__等,主要用于日志等功能中。
*
* @version 1.0
*
*/
public abstract class CommonFunction {
/**
* 打印日志時獲取當前的程序文件名、行號、方法名 輸出格式為:[FileName | LineNumber | MethodName]
*
* @return
*/
public static String getFileLineMethod() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
StringBuffer toStringBuffer = new StringBuffer("[").append(
traceElement.getFileName()).append(" | ").append(
traceElement.getLineNumber()).append(" | ").append(
traceElement.getMethodName()).append("]");
return toStringBuffer.toString();
}
// 當前文件名
public static String _FILE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getFileName();
}
// 當前方法名
public static String _FUNC_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getMethodName();
}
// 當前行號
public static int _LINE_() {
StackTraceElement traceElement = ((new Exception()).getStackTrace())[1];
return traceElement.getLineNumber();
}
// 當前時間
public static String _TIME_() {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
return sdf.format(now);
}
}
新聞熱點
疑難解答