上一篇說了一下《解決問題的通常套路》,裏面講到了日誌系統的重要性,日誌重要嗎?監控重要嗎?of course!日誌就是要能找到用戶作了什麼請求那個機器。html
上下游接口請求,請求參數和入參是否正確,咱們能夠統一寫一個面向切面方法去打印日誌,不用每一處去寫,切入點你們本身按照規則定義,AOP是Spring提供的關鍵特性之一。AOP即面向切面編程,是OOP編程的有效補充。使用AOP技術,能夠將一些系統性相關的編程工做,獨立提取出來,獨立實現,而後經過切面切入進系統。從而避免了在業務邏輯的代碼中混入不少的系統相關的邏輯——好比權限管理,事物管理,日誌記錄等等。這些系統性的編程工做均可以獨立編碼實現,而後經過AOP技術切入進系統便可。從而達到了 將不一樣的關注點分離出來的效果。java
@Aspect @Component public class ControllerLogAspect { private Logger logger = LoggerFactory.getLogger(getClass()); ThreadLocal<Long> startTime = new ThreadLocal<>(); @Pointcut("execution(* com.xxx.mobile.web.controller..*.*(..))") public void controllerLog() { } @Before("controllerLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { startTime.set(System.currentTimeMillis()); if (joinPoint == null) { return; } Signature signature = joinPoint.getSignature(); // 接收到請求,記錄請求內容 RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); ServletRequestAttributes attributes = null; HttpServletRequest request = null; String requestUrl = ""; String httpMethod = ""; String declaringTypeName = ""; String actionName = ""; String ip = ""; if (requestAttributes != null) { attributes = (ServletRequestAttributes) requestAttributes; } if (attributes != null) { request = attributes.getRequest(); } if (request != null) { requestUrl = request.getRequestURL().toString(); httpMethod = request.getMethod(); ip = IpUtils.getIpAddr(request); } if (signature != null) { declaringTypeName = joinPoint.getSignature().getDeclaringTypeName(); actionName = joinPoint.getSignature().getName(); } // 記錄下請求內容 logger.debug("URL:[{}] HTTP_METHOD:[{}] CLASS_METHOD:[{}.{}] ip:[{}] ARGS:{}", requestUrl, httpMethod, declaringTypeName, actionName, ip, Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "controllerLog()") public void doAfterReturning(Object ret) throws Throwable { // 處理完請求,返回內容 logger.debug("Execute Time:{}ms \n{}", (System.currentTimeMillis() - startTime.get()), ret); }
下面將使用@ExceptionHandler
處理全局異常,將異常信息更加人性化的輸出給用戶。固然咱們記錄日誌仍是會用log4j。至於log4j的用法你們能夠百度。程序員
@ControllerAdvice public class MobileWebExceptionHandler { private static Logger logger = LoggerFactory.getLogger(MobileWebExceptionHandler.class); @ExceptionHandler(value = Exception.class) @ResponseBody public Object exceptionHandler(HttpServletRequest request, Exception e) throws Exception { String message = String.format("Url:[%s]-%s", request.getRequestURL().toString(), e.getMessage()); logger.error(message, e); return MobileWebResponse.error(CODE_INVALID_PARAMETER, e.getMessage()); }
經過@ControllerAdvice
。咱們能夠將對於控制器的全局配置放置在同一個位置,註解了@ControllerAdvice
的類的方法可使用@ExceptionHandler
,@InitBinder
,@ModelAttribute
註解到方法上,這對全部註解了@RequestMapping
的控制器內的方法有:web
@ExceptionHandler
:用於全局處理控制器裏面的異常。@InitBinder
:用來設置WebDataBinder
,WebDataBinder
用來自動綁定前臺請求參數到Model
中。@ModelAttribute
:@ModelAttribute
原本的做用是綁定鍵值對到Model
裏,此處是讓全局的@RequestMapping
都能得到在此處設置的鍵值對。redis
何時該打印什麼樣的日誌級別,這個也很重要,通常狀況下咱們打印德日誌級別info,warn,error居多,日誌級別有:
編程
ALL:最低等級的,用於打開全部日誌記錄。
TRACE: designates finergrained informational events than the DEBUG.Since:1.2.12,很低的日誌級別,通常不會使用。
DEBUG: 指出細粒度信息事件對調試應用程序是很是有幫助的,主要用於開發過程當中打印一些運行信息。
INFO: 消息在粗粒度級別上突出強調應用程序的運行過程。打印一些你感興趣的或者重要的信息,這個能夠用於生產環境中輸出程序運行的一些重要信息,可是不能濫用,避免打印過多的日誌。
WARN: 代表會出現潛在錯誤的情形,有些信息不是錯誤信息,可是也要給程序員的一些提示。
ERROR: 指出雖然發生錯誤事件,但仍然不影響系統的繼續運行。打印錯誤和異常信息,若是不想輸出太多的日誌,可使用這個級別。
FATAL: 指出每一個嚴重的錯誤事件將會致使應用程序的退出。這個級別比較高了。重大錯誤,這種級別你能夠直接中止程序了。
OFF: 最高等級的,用於關閉全部日誌記錄 app
SpringBootAdmin顯示日誌監控級別,咱們能夠根據本身的需求控制打印什麼樣的日誌:編碼
咱們在打印日誌通常日誌頭會有時間,應用名,spanId,traceId,代碼行數,堆棧信息等,以下:spa
2018-08-05 11:52:58.470 WARN [xxx-web,e1ec017e8247b79e,e1ec017e8247b79e,true] 10652 --- [qtp1033348658-177] o.s.web.servlet.PageNotFound [1147 ] : No mapping found for HTTP request with URI [/flyway] in DispatcherServlet with name 'dispatcherServlet'debug
若是那個app報錯了,錯誤日誌怎樣讓你們看到,會選擇用RabbitMq+ELK(Elasticsearch , Logstash, Kibana), 這篇ELK原理與介紹(https://www.cnblogs.com/aresxin/p/8035137.html),這位小哥哥說的還不錯。Kafka能夠被redis和RabbitMq 所替換。最終錯誤日誌會顯示在kibana上,以下圖,除了時時監控錯誤的個數,還能夠DSL語言查詢某個時間段發生的錯誤日誌,幫助咱們分析問題。歡迎指正!