组件开发中日志的引入。
# LogAdapter : log 适配器
##依赖
Spring中有一个依赖:spring-jcl.jar 用于适配日志框架。类名为:org.apache.commons.logging.LogAdapter
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jcl</artifactId> <version>5.3.24</version> </dependency>
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactoryService; import java.text.SimpleDateFormat; public class MyLog { private static class LogInfo { public String name; public String methodName; public long lineNumber; public String threadName; @Override public String toString() { //return " [" + threadName + "] " + name + " - " + methodName + "- " + lineNumber + ""; return name; } } private static String getCurDateTimeStr() { SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS"); return dateformat.format(System.currentTimeMillis()); } private static LogInfo getEnterClassName() { Thread cur = Thread.currentThread(); LogInfo logInfo = new LogInfo(); int iLevel = 4; if (cur != null) { StackTraceElement[] ary = cur.getStackTrace(); logInfo.threadName = cur.getName(); if ((ary != null) && (ary.length > iLevel)) { logInfo.name = ary[iLevel].getClassName(); logInfo.methodName = ary[iLevel].getMethodName(); logInfo.lineNumber = ary[iLevel].getLineNumber(); } } return logInfo; } private static void baseinfo(String msg) { Log logger =LogFactoryService.getLog(getEnterClassName().name); // logger.info(msg); logger.info(String.format("- ThreadId %s : %s", Thread.currentThread().getId(), msg)); } private static void basedebug(String msg) { Log logger =LogFactoryService.getLog(getEnterClassName().name); // logger.debug( msg ); logger.debug(String.format("- ThreadId %s : %s", Thread.currentThread().getId(), msg)); } private static void baseerror(String msg) { Log logger =LogFactoryService.getLog(getEnterClassName().name); // logger.error(msg); logger.error(String.format(" - ThreadId %s : %s", Thread.currentThread().getId(), msg)); } public static void info(String msg) { baseinfo(msg); } public static void info(String msg, Object... arguments) { String newMsg = msg.replaceAll("\\{\\}", "\\%s"); baseinfo(String.format(newMsg, arguments)); } public static void error(String msg) { baseerror(msg); } public static void error(String msg, Object... arguments) { String newMsg = msg.replaceAll("\\{\\}", "\\%s"); baseerror(String.format(newMsg, arguments)); } public static void error(String msg, Exception exp) { baseerror(msg); if (null != exp) { exp.printStackTrace(); } } public static void debug(String msg) { basedebug(msg); } public static void debug(String msg, Object... arguments) { String newMsg = msg.replaceAll("\\{\\}", "\\%s"); basedebug(String.format(newMsg, arguments)); } }
标签:logAdaper,java,String,适配器,static,void,msg,logger,public From: https://www.cnblogs.com/heshana/p/17108748.html
##包装使用
Mylog.debug("");
Mylog.info("");
Mylog.error("");