Java/SpringBoot LoggerUtil方法
记录一个LoggerUitl方法
SpringWebUtil.getCurrentUserId()方式是自己项目里的实现,用来获取当前登陆到系统的userId.
import org.slf4j.LoggerFactory; public class LoggerUtil { public static void error(String msg) { LoggerFactory.getLogger(getClassName()).error("staffId " + staffId() + ", " + msg); } public static void error(String msg, Object... obj) { LoggerFactory.getLogger(getClassName()).error("staffId " + staffId() + ", " + msg, obj); } public static void warn(String msg) { LoggerFactory.getLogger(getClassName()).error("staffId " + staffId() + ", " + msg); } public static void warn(String msg, Object... obj) { LoggerFactory.getLogger(getClassName()).error("staffId " + staffId() + ", " + msg, obj); } public static void info(String msg) { LoggerFactory.getLogger(getClassName()).info("staffId " + staffId() + ", " + msg); } public static void info(String msg, Object... obj) { LoggerFactory.getLogger(getClassName()).info("staffId " + staffId() + ", " + msg, obj); } public static void debug(String msg) { LoggerFactory.getLogger(getClassName()).debug("staffId " + staffId() + ", " + msg); } public static void debug(String msg, Object... obj) { LoggerFactory.getLogger(getClassName()).debug("staffId " + staffId() + ", " + msg, obj); } // 获取调用 error,info,debug静态类的类名 private static String getClassName() { return new SecurityManager() { public String getClassName() { return getClassContext()[3].getName(); } }.getClassName(); } private static String staffId() { String staffId = ""; try { staffId = SpringWebUtil.getCurrentUserId(SpringWebUtil.getCurrentSession()); } catch (IllegalStateException e) { staffId = "SYSTEM"; } return staffId; } }