閒來沒事,看見當前的項目的日誌形式有點冗餘,每一個類都須要聲明確實有點繁瑣,java
所以從新將logback從新封裝一下,供整個工程共享使用,版本是1.0.9。spa
代碼以下:debug
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.slf4j.Marker; import ch.qos.logback.classic.Level; public final class Logger { private static ch.qos.logback.classic.Logger innerLogger; private static final String FQCN = Logger.class.getName(); private static Method innerMethod; public static int load(ch.qos.logback.classic.Logger logger) { innerLogger = logger; try { innerMethod = innerLogger.getClass().getDeclaredMethod("filterAndLog_0_Or3Plus", String.class, Marker.class, Level.class, String.class, Object[].class, Throwable.class); innerMethod.setAccessible(true); } catch (NoSuchMethodException | SecurityException e) { innerLogger.error("load failed, Exception:" + e); return -1; } return 0; } public static void debug(String msg) { innerLogMethod(FQCN, null, Level.DEBUG, msg, null, null); } public static void error(String msg) { innerLogMethod(FQCN, null, Level.ERROR, msg, null, null); } public static void info(String msg) { innerLogMethod(FQCN, null, Level.INFO, msg, null, null); } public static void warn(String msg) { innerLogMethod(FQCN, null, Level.WARN, msg, null, null); } private static void innerLogMethod(String localFQCN, Marker marker, Level level, String msg, Object[] params, Throwable t) { try { innerMethod.invoke(innerLogger, localFQCN, marker, level, msg, params, t); } catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { innerLogger.error("execute innerMethod failed, Exception:" + e); } } }
詳細原理隨後再解釋吧:日誌