博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
服务器报错邮件发送错误日志
阅读量:6950 次
发布时间:2019-06-27

本文共 4311 字,大约阅读时间需要 14 分钟。

hot3.png

类:ResourceLoggingAspect    /**     * 对于方法抛出异常后的操作     * BaseAppException的 400和200不会打印日志文件     * @param exception     */    @AfterThrowing(pointcut = "resourceLogging()",throwing = "exception")    public void afterThrowing(JoinPoint joinPoint,Exception exception) {        try{            if(exception instanceof BaseAppException){                BaseAppException baseAppException=(BaseAppException) exception;                if(baseAppException.getErrorCode() == null                    ||baseAppException.getErrorCode().equals(BaseDTO.CODE_PARAM)                    ||baseAppException.getErrorCode().equals(BaseDTO.CODE_SUCCESS)){                    return;                }            }else{                String msg = joinPointToMsgForHtml(joinPoint);                StringWriter stringWriter = new StringWriter();                PrintWriter printWriter = new PrintWriter(stringWriter);                exception.printStackTrace(printWriter);                sendMailService.sendErrorLogMail(msg+"
"+stringWriter.toString()); } String msg = joinPointToMsg(joinPoint); Logback.error( msg, exception, logger); }catch(Exception e){ Logback.error("操作错误日志(ERROR)记录失败[com.公司.项目名.gateway.aop.webLog.WebRequestLogAspect.afterThrowing()]", e,logger); } } /** * 根据切点获取请求,返回打印信息 */ private String joinPointToMsgForHtml(JoinPoint joinPoint) { String beanName = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); Object[] paramsArray = joinPoint.getArgs(); String params = argsArrayToString(paramsArray); StringBuffer info=new StringBuffer(); info.append("
USERID["+ SecurityUtils.getCurrentUserId()+"]"); info.append("
ClassName=["+beanName+"."+methodName+"()]"); info.append("
Params=["+params+"]"); info.append("
CurrentDate=["+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS").format(new Date())+"]"); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(attributes != null){ HttpServletRequest request = attributes.getRequest(); String uri = request.getRequestURI(); String remoteAddr = getIpAddr(request); String method = request.getMethod(); info.append("
Url=["+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+uri+"("+method+")]"); info.append("
RemoteAddr=["+remoteAddr+"]
"); } return info.toString(); } /** * 根据切点获取请求,返回打印信息 */ private String joinPointToMsg(JoinPoint joinPoint) { // 接收到请求,记录请求内容 String beanName = joinPoint.getSignature().getDeclaringTypeName(); String methodName = joinPoint.getSignature().getName(); Object[] paramsArray = joinPoint.getArgs(); String params = argsArrayToString(paramsArray); StringBuffer info=new StringBuffer(); info.append("\nUSERID["+ SecurityUtils.getCurrentUserId()+"]"); info.append("\nClassName=["+beanName+"."+methodName+"()]"); info.append("\nParams=["+params+"]"); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if(attributes != null){ HttpServletRequest request = attributes.getRequest(); String uri = request.getRequestURI(); String remoteAddr = getIpAddr(request); String method = request.getMethod(); info.append("\nUrl=["+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+uri+"("+method+")]"); info.append("\nRemoteAddr=["+remoteAddr+"]"); } return info.toString(); } 类:SendMailService /** * 发送邮件 * @param mailContent 邮件内容 */ public void sendErrorLogMail(String mailContent) { try{ SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd_HHmmss"); String timeStamp = simpleDateFormat.format(new Date()); final String mailSubject = "[Process"+timeStamp+"]ERROR日志报告"; String webServerAttribute = WebServerAttributeUtil.attributeToStringForHtml(webServerUrl); for(int i=0; i

转载于:https://my.oschina.net/zhangshsURL/blog/1788419

你可能感兴趣的文章
搜狐笔试 最大连续递增子段和 关键词连续递增
查看>>
.NET URL 301转向方法的实现
查看>>
新浪微博开放平台链接耗尽的情况分析
查看>>
极限编程的12个实践原则
查看>>
javascript--返回顶部效果
查看>>
C# NamePipe使用小结
查看>>
ZooKeeper Watcher注意事项
查看>>
Linux下清理内存和Cache方法
查看>>
表单元素的外观改变(webkit and IE10)
查看>>
帆软报表学习笔记②——行转列
查看>>
redis应用场景:实现简单计数器-防止刷单
查看>>
两款开发辅助工具介绍
查看>>
python 文件的打开与读取
查看>>
基于ROS的运动识别
查看>>
python 之selectors 实现文件上传下载
查看>>
【hdu - 2568】ACM程序设计期末考试081230
查看>>
C语言基础(一)
查看>>
python处理xml中非法字符的一种思路
查看>>
itextSharp 附pdf文件解析
查看>>
solr6.0.0 + tomcat8 配置问题
查看>>