格式化列印日誌Java實現
阿新 • • 發佈:2018-11-01
日誌按所在主體分為
系統
子系統
模組
子模組
日誌按嚴重型別分為
資訊
警告
錯誤
致命錯誤
該類被呼叫示例如下
LogManager logManager = new LogManager("SystemName","SubSystemName","ModuleName","SubModuleName"); logManager.info("this is info message"); logManager.warn("this is warn message"); logManager.error("this is error message"); logManager.fatal("this is fatal message");
該類原始碼如下
package common; import java.io.IOException; import java.sql.Timestamp; import java.text.SimpleDateFormat; public class LogManager { // m_szSystemName private String m_szSystemName = ""; public void setSystemName(String strSystemName){ m_szSystemName = strSystemName; } public String getSystemName(){ return m_szSystemName; } // m_szSubSystemName private String m_szSubSystemName = ""; public void setSubSystemName(String strSubSystemName){ m_szSubSystemName = strSubSystemName; } public String getSubSystemName(){ return m_szSubSystemName; } // m_szMoudleName private String m_szMoudleName = ""; public void setMoudleName(String strMoudleName){ m_szMoudleName = strMoudleName; } public String getMoudleName(){ return m_szMoudleName; } // m_szSubMoudleName private String m_szSubMoudleName = ""; public void setSubMoudleName(String strSubMoudleName){ m_szSubMoudleName = strSubMoudleName; } public String getSubMoudleName(){ return m_szSubMoudleName; } // m_szLogContent private String m_szLogContent = ""; public void setLogContent(String strLogContent){ m_szLogContent = strLogContent; } public String getLogContent(){ return m_szLogContent; } // 建構函式 LogManager(){ } LogManager(String strSubMoudleName){ m_szSubMoudleName = strSubMoudleName; } LogManager(String strMoudleName,String strSubMoudleName){ m_szMoudleName = strMoudleName; m_szSubMoudleName = strSubMoudleName; } LogManager(String strSubSystemName, String strMoudleName,String strSubMoudleName){ m_szSubSystemName = strSubSystemName; m_szMoudleName = strMoudleName; m_szSubMoudleName = strSubMoudleName; } LogManager(String strSystemName, String strSubSystemName, String strMoudleName,String strSubMoudleName){ m_szSystemName = strSystemName; m_szSubSystemName = strSubSystemName; m_szMoudleName = strMoudleName; m_szSubMoudleName = strSubMoudleName; } /** * print函式 * 1.獲取當前時間 * 2.根據時間及內容組成列印字串 * 3.列印字串 * * nFailLevel: * 0 info * 1 warn * 2 error * 3 fatal * * */ private void print(int nFailLevel){ // 日期字串 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); String currentTime = sdf.format(System.currentTimeMillis()); StringBuffer sb = new StringBuffer(currentTime); // 新增錯誤等級 sb.append(" "); switch(nFailLevel){ // 0 info case 0: sb.append("info ").append(" "); break; // 1 warn case 1: sb.append("warn ").append(" "); break; // 2 error case 2: sb.append("error").append(" "); break; // 3 fatal case 3: sb.append("fatal").append(" "); break; // default info default: sb.append("info ").append(" "); } // 組織模組名組合 if(m_szSystemName.length() > 0){ sb.append("[").append(m_szSystemName).append("]"); } if(m_szSubSystemName.length() > 0){ sb.append("[").append(m_szSubSystemName).append("]"); } if(m_szMoudleName.length() > 0){ sb.append("[").append(m_szMoudleName).append("]"); } if(m_szSubMoudleName.length() > 0){ sb.append("[").append(m_szSubMoudleName).append("]"); } // 組織列印內容 if(m_szLogContent.length() > 0){ sb.append(" ").append(m_szLogContent); } // 系統視窗列印log; System.out.print(sb); // log檔案列印log LogFileManager logFileManager = LogFileManager.getInstance(); logFileManager.print(sb); // Debug視窗列印log DeugFrame deugFrame = DeugFrame.getInstance(); deugFrame.print(sb); } // info function public void info(String strInfo){ m_szLogContent = strInfo + "/n"; print(0); } public void info(String strInfo, Exception eInfo){ m_szLogContent = strInfo + "/n"; m_szLogContent = m_szLogContent + getExceptionString(eInfo).toString(); print(0); } // warn function public void warn(String strInfo){ m_szLogContent = strInfo + "/n"; print(1); } public void warn(String strInfo, Exception eInfo){ m_szLogContent = strInfo + "/n"; m_szLogContent = m_szLogContent + getExceptionString(eInfo).toString(); print(1); } // error function public void error(String strInfo){ m_szLogContent = strInfo + "/n"; print(2); } public void error(String strInfo, Exception eInfo){ m_szLogContent = strInfo + "/n"; m_szLogContent = m_szLogContent + getExceptionString(eInfo).toString(); print(2); } // fatal function public void fatal(String strInfo){ m_szLogContent = strInfo + "/n"; print(3); } public void fatal(String strInfo, Exception eInfo){ m_szLogContent = strInfo + "/n"; m_szLogContent = m_szLogContent + getExceptionString(eInfo).toString(); print(3); } /* * 根據Exception獲取棧中內容 * * */ private String getExceptionString(Exception eInfo){ StringBuffer sb = new StringBuffer(eInfo.toString() + "/n"); StackTraceElement[] steTmp = eInfo.getStackTrace(); for(int i = 0; i < steTmp.length; i++){ sb.append(steTmp[i].toString()).append("/n"); } return sb.toString(); } public static void main(String[] args) { try { LogManager logManager = new LogManager("SystemName","SubSystemName","ModuleName","SubModuleName"); logManager.info("this is info message"); logManager.warn("this is warn message"); logManager.error("this is error message"); logManager.fatal("this is fatal message"); try{ String sTmp = null; int n = sTmp.length(); } catch(Exception e1){ logManager.info("abc",e1); logManager.warn("def",e1); logManager.error("def",e1); logManager.fatal("def",e1); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }