Log日志整理
1.全局异常处理器
将dao/service等异常抛出,统一在controller中捕获
POM
1 2 3 4 5 6 7 8
| <!-- 日志处理 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- 邮箱另导 -->
|
1-1.log4j.properties
slf4j-api | slf4j-log4j :支持各种日志工具的平台(一般用log4j)
log4j:log4j自己的日志工具
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #DEBUG 模式 A3写出日志 STDOUT控制台输出 MAIL 输出到邮箱 log4j.rootLogger=DEBUG,A3,STDOUT
#控制台输出 log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout #日志格式 log4j.appender.STDOUT.layout.ConversionPattern=[%p] [%l] %10.10c - %m%n
#日志写出 RollingFileAppender 持续写出 log4j.appender.A3=org.apache.log4j.RollingFileAppender #日志名 server.log log4j.appender.A3.file=logs/server.log #最大单个日志1024KB,超过则新建一个日志文件 log4j.appender.A3.MaxFileSize=1024KB log4j.appender.A3.MaxBackupIndex=10 log4j.appender.A3.layout=org.apache.log4j.PatternLayout #日志格式 log4j.appender.A3.layout.ConversionPattern=\n\n[%-5p] %d{yyyy-MM-dd HH\:mm\:ss,SSS} method\:%l%n%m%n
|
1-2.GlobalExceptionResolver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import com.e3mall.search.mail.ErrorLogMail; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class GlobalExceptionResolver implements HandlerExceptionResolver {
private static Logger logger=LoggerFactory.getLogger(GlobalExceptionResolver.class);
@Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { ex.printStackTrace(); logger.debug("测试日志:==================[debug]======================="); logger.info("系统发生异常了:==============[info]========================"); logger.error("系统发生异常:===============[error]=======================",ex);
ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("error/exception");
return modelAndView; } }
|
1-3.ErrorLogMail(未生效)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
import org.springframework.mail.MailException; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.UnsupportedEncodingException; import java.util.Properties;
public class ErrorLogMail { private static final String HOST = "smtp.163.com"; private static final Integer PORT = 25; private static final String USERNAME = "625432639@163.com"; private static final String PASSWORD = "68835230ZZ"; private static final String EMAILFORM = "625432639@163.com"; private static JavaMailSenderImpl mailSender = createMailSender();
private static JavaMailSenderImpl createMailSender() { JavaMailSenderImpl sender = new JavaMailSenderImpl(); sender.setHost(HOST); sender.setPort(PORT); sender.setUsername(USERNAME); sender.setPassword(PASSWORD); sender.setDefaultEncoding("Utf-8"); Properties p = new Properties(); p.setProperty("mail.smtp.timeout", "25000"); p.setProperty("mail.smtp.auth", "false"); sender.setJavaMailProperties(p); return sender; }
public static void sendHtmlMail(String html) { try { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8"); messageHelper.setFrom(EMAILFORM, "DonY15"); messageHelper.setTo("625432639@qq.com"); messageHelper.setSubject("ErrorLog"); messageHelper.setText(html, true); mailSender.send(mimeMessage); } catch (MessagingException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MailException e) { e.printStackTrace(); } } }
|