#logback打印错误日志到邮箱 需要依赖的jar文件:janino
<log.janino.version>2.7.8</log.janino.version>
pom.xml配置如下:
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>${log.janino.version}</version>
</dependency>
<!-- email -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<!-- email END-->
如果没有添加 mail 依赖会出现以下错误:
09:46:21,222 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds
09:46:21,223 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:\zyq\as\target\classes\logs\logback.xml]] every 30 seconds.
09:46:21,223 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
09:46:21,232 |-INFO in ch.qos.logback.classic.joran.action.ContextNameAction - Setting logger context name as [as]
09:46:21,233 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender]
09:46:21,239 |-ERROR in ch.qos.logback.core.joran.action.AppenderAction - Could not create an Appender of type [ch.qos.logback.classic.net.SMTPAppender]. ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
at ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:73)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:48)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:35)
at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:54)
at at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:275)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:147)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:129)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
at at com.xdja.as.common.logback.LogbackConfigurer.initLogging(LogbackConfigurer.java:43)
at at com.xdja.as.common.logback.LogbackWebConfigurer.initLogging(LogbackWebConfigurer.java:52)
at at com.xdja.as.common.logback.LogbackConfigListener.contextInitialized(LogbackConfigListener.java:18)
at at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoClassDefFoundError: javax/mail/Authenticator
at at java.lang.Class.getDeclaredConstructors0(Native Method)
at at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)
at at java.lang.Class.getConstructor0(Class.java:2842)
at at java.lang.Class.newInstance(Class.java:345)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:65)
at ... 23 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
at ... 28 common frames omitted
09:46:21,242 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@15:73 - ActionException in Action for tag [appender] ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
at ch.qos.logback.core.joran.spi.ActionException: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:82)
at at ch.qos.logback.core.joran.spi.Interpreter.callBeginAction(Interpreter.java:275)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:147)
at at ch.qos.logback.core.joran.spi.Interpreter.startElement(Interpreter.java:129)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:50)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
at at com.xdja.as.common.logback.LogbackConfigurer.initLogging(LogbackConfigurer.java:43)
at at com.xdja.as.common.logback.LogbackWebConfigurer.initLogging(LogbackWebConfigurer.java:52)
at at com.xdja.as.common.logback.LogbackConfigListener.contextInitialized(LogbackConfigListener.java:18)
at at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at at java.lang.Thread.run(Thread.java:745)
Caused by: ch.qos.logback.core.util.DynamicClassLoadingException: Failed to instantiate type ch.qos.logback.classic.net.SMTPAppender
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:73)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:48)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassName(OptionHelper.java:35)
at at ch.qos.logback.core.joran.action.AppenderAction.begin(AppenderAction.java:54)
at ... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/mail/Authenticator
at at java.lang.Class.getDeclaredConstructors0(Native Method)
at at java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)
at at java.lang.Class.getConstructor0(Class.java:2842)
at at java.lang.Class.newInstance(Class.java:345)
at at ch.qos.logback.core.util.OptionHelper.instantiateByClassNameAndParameter(OptionHelper.java:65)
at ... 23 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.mail.Authenticator
at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
1.准备邮件配置项
定义文件名称:logback.properties
LOG_PATH=D\:/logs/mall/mall-soonew
smtpHost=smtp.163.com
smtpPort=25
username=xxxxx@163.com
password=xxxxxx
SSL=false
email_to=xxx@qq.com,zyq@xxx.com
email_from=xx@163.com
email_subject=\u3010Error\u3011\: %logger
localhost=www.huaqinwang.com
2.logback.xml文件中引入配置项
logback.xml配置文件添加如下内容:
<property file="建议绝对路径/logback.properties" />
<property resource="类路径下/logback.properties" />
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>${smtpHost}</smtpHost>
<smtpPort>${smtpPort}</smtpPort>
<username>${username}</username>
<password>${password}</password>
<localhost>${localhost}</localhost>
<SSL>${SSL}</SSL>
<asynchronousSending>true</asynchronousSending>
<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%message</pattern>
</layout>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
<expression> <!-- && null != throwable -->
if(level >= WARN ) {
return true;
}
return false;
</expression>
</evaluator>
<!-- <level>error</level> -->
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
............
<root level="INFO">
<appender-ref ref="EMAIL"/>
</root>
3.效果截图
QQ邮箱: 163邮箱:
4.参考网址
5.线上异常
项目生产环境运营正常logback.xml文件的部分文件配置为:
<asynchronousSending>false</asynchronousSending>
异步发送邮件=false 变更为同步发送。 问题:如果配置的发送邮箱服务器出现了问题无法发送时抛异常会把项目生产环境部署在tomcat的服务重启,就这样折腾了6个小时细心的同事注释了logger.error结果正常了。