首页>代码>Java版FastDFS文件服务器,springmvc做的集成>/fastdfs-fileserver/src/main/java/hello/world/fileserver/tools/Logit.java
package hello.world.fileserver.tools; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.event.Level; /***************************************************** * * @author 宋健 * 改为了slf4j 桥接 log4j2 *****************************************************/ public class Logit { //调试 private static final Logger DEBUG_LOG = LoggerFactory.getLogger("sys.debug"); //警告 private static final Logger WARN_LOG = LoggerFactory.getLogger("sys.warn"); //数据记录 private static final Logger RECORD_LOG = LoggerFactory.getLogger("sys.record"); //错误 private static final Logger ERROR_LOG = LoggerFactory.getLogger("sys.error"); //应用分析日志 private static final Logger ANALYSYS_LOG = LoggerFactory.getLogger("sys.analysys"); //定时任务日志 private static final Logger RUNNABLE_LOG = LoggerFactory.getLogger("sys.runnable"); //输出返回JSON private static final Logger ACCESS_LOG = LoggerFactory.getLogger("sys.access"); /********************/ private static final Logger DB_LOG = LoggerFactory.getLogger("db.sql"); public static void record(String format,Object ...args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); RECORD_LOG.info(sb.toString(),args); } public static void runnable(String format,Object ...args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); RUNNABLE_LOG.info(sb.toString(),args); } public static void runnableError(String format,Object ...args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); RUNNABLE_LOG.error(sb.toString(),args); } public static void analysys(String format,Object ...args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); ANALYSYS_LOG.info(sb.toString(),args); } public static void analysysError(String log) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); ANALYSYS_LOG.error(sb.toString()); } public static void analysysError(String log,Throwable throwable) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); ANALYSYS_LOG.error(sb.toString(),throwable); } /** * * @param log */ public static void access(String log) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); ACCESS_LOG.info(sb.toString()); } public static void access(String format,Object ...args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); ACCESS_LOG.info(sb.toString(),args); } /** * 用户调试日志 * @param log */ public static void debug(String log) { debug(log,""); } /** * 用户调试日志 */ public static void debug(String format,Object ... args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); debugPlog(Level.DEBUG,sb.toString(),args); } private static void debugPlog(Level level,String format,Object ... log) { if (level==Level.DEBUG) { DEBUG_LOG.debug(format,log); } else if (level==Level.INFO) { DEBUG_LOG.info(format,log); } else if (level==Level.WARN) { DEBUG_LOG.warn(format,log); } else if (level==Level.ERROR) { DEBUG_LOG.error(format,log); } else { DEBUG_LOG.debug(format,log); } } /** * 用户告警日志 * @param log */ public static void warn(String log) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); WARN_LOG.error(sb.toString()); } public static void warn(String format,Object ... args) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(format); WARN_LOG.error(sb.toString(),args); } /** * 错误日志 * @param log */ public static void error(String log) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); errorPLog(sb.toString(), null); } public static void error(String log, Throwable throwable) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); errorPLog(sb.toString(), throwable); } /** * db日志 * * @param log */ public static void sql(String log, Object... objects) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); if (objects != null && objects.length > 0) { sb.append("; parameter["); for (Object obj : objects) { sb.append(obj); sb.append(","); } sb.delete(sb.length() - 1, sb.length()); sb.append("]"); } DB_LOG.debug(sb.toString()); } /** * 错误日志 * @param log * @param throwable */ private static void errorPLog(String log, Throwable throwable) { if (throwable == null) { ERROR_LOG.error(log); } else { ERROR_LOG.error(log, throwable); } } /** * 自定义日志名称 * @param name * @param log */ public static void log(String name, String log) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); logByName(name, sb.toString(), "debug", null); } /** * 自定义日志名称 * @param name * @param log * @param loglevel */ public static void log(String name, String log, String loglevel) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); logByName(name, sb.toString(), loglevel, null); } public static void log(String name, String log, String loglevel, Throwable throwable) { StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); logByName(name, sb.toString(), loglevel, throwable); } /** * * @param name * @param log * @param loglevel * @param throwable */ private static void logByName(String name, String log, String loglevel, Throwable throwable) { if (name == null) { return; } StackTraceElement[] stacks = new Throwable().getStackTrace(); StringBuffer sb = new StringBuffer(stacks[1].getClassName()); sb.append("."); sb.append(stacks[1].getMethodName()); sb.append("---"); sb.append(log); Logger logger = LoggerFactory.getLogger(name); try { if (throwable == null) { if ("debug".equalsIgnoreCase(loglevel)) { logger.debug(log); } else if ("info".equalsIgnoreCase(loglevel)) { logger.info(log); } else if ("warn".equalsIgnoreCase(loglevel)) { logger.warn(log); } else if ("error".equalsIgnoreCase(loglevel)) { logger.error(log); } else { logger.debug(log); } } else { if ("debug".equalsIgnoreCase(loglevel)) { logger.debug(log, throwable); } else if ("info".equalsIgnoreCase(loglevel)) { logger.info(log, throwable); } else if ("warn".equalsIgnoreCase(loglevel)) { logger.warn(log, throwable); } else if ("error".equalsIgnoreCase(loglevel)) { logger.error(log, throwable); } else { logger.debug(log, throwable); } } } catch (Exception ex) { // 不做处理 } } }
最近下载更多
1529860026 LV24
2023年6月1日
lris_luanling LV11
2021年1月22日
simple丶余心 LV21
2020年9月26日
lwp011 LV27
2020年7月7日
王可以 LV10
2020年4月22日
442529474 LV6
2020年3月11日
whtfj LV2
2019年9月6日
huangxing11111 LV1
2019年8月6日
yanghanqiang LV3
2019年7月5日
liujiaweijv LV7
2019年7月5日