Maven项目之错误日志发邮件通知

1、主要通过logback.xml配置,该文件位于src/main/resource目录下


<configuration scan="false">

    <property name="fileName" value="xunming-scheduler"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{50} - %msg%npattern>
        encoder>
    appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{50} - %msg%npattern>
            <charset>UTF-8charset>
        encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs\\${fileName}.%d{yyyy-MM-dd}.logfileNamePattern>
            <maxHistory>365maxHistory>
        rollingPolicy>
    appender>

    
    
    <property name="smtpHost" value="smtp.exmail.qq.com"/>
    
    <property name="smtpPort" value="25"/>
    
    <property name="username" value="[email protected]"/>
    
    <property name="password" value="123456"/>
    
    <property name="SSL" value="false"/>
    
    <property name="email_to" value="[email protected]"/>
    
    <property name="email_from" value="[email protected]" />
    
    <property name="email_subject" value="【Error】: %logger - %msg " />
    
    <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
        <smtpHost>${smtpHost}smtpHost>
        <smtpPort>${smtpPort}smtpPort>
        <username>${username}username>
        <password>${password}password>
        <asynchronousSending>trueasynchronousSending>
        <SSL>${SSL}SSL>
        <to>${email_to}to>
        <from>${email_from}from>
        <subject>${email_subject}subject>
             
        <layout class="ch.qos.logback.classic.html.HTMLLayout">
            <Pattern>%date%level%thread%logger{0}%line%messagePattern>
        layout>
             
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
            
            <bufferSize>1bufferSize>
        cyclicBufferTracker>
    appender>

    
        
        
        
    

    

    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="EMAIL"/>
    root>

configuration>

2、pom.xml中添加如下依赖


        <dependency>
            <groupId>org.codehaus.janinogroupId>
            <artifactId>janinoartifactId>
            <version>2.7.8version>
        dependency>
        
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-coreartifactId>
            <version>1.1.7version>
        dependency>
        <dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.1.7version>
        dependency>
        
        <dependency>
            <groupId>javax.mailgroupId>
            <artifactId>mailartifactId>
            <version>1.4.7version>
        dependency>
        
         <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>jcl-over-slf4jartifactId>
            <version>${slf4j.version}version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>${slf4j.version}version>
        dependency>

3、正常版使用方法

Logger logger = LoggerFactory.getLogger(XmScheduler.class);
public void doAnything(){
    logger.error("邮件发送");
}

4、还有一种情况是根据指定的marker来发送邮件,那么logback.xml需要添加如下配置


        <evaluator class="ch.qos.logback.classic.boolex.OnMarkerEvaluator">
            <marker>Weimarker>
        evaluator>

5、标记版使用方法

 logger.info(MarkerFactory.getMarker("Wei"),"具体邮件内容");

6、实测,正常版效果图,标记版类似就不上图了

Maven项目之错误日志发邮件通知_第1张图片

参考博文:

logback 发送邮件和自定义发送邮件;java类发送邮件

你可能感兴趣的:(Java)