文档章节

logback打印错误日志到邮箱

zzuqiang
 zzuqiang
发布于 2016/07/04 17:40
字数 880
阅读 445
收藏 9

#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>  <!-- &amp;&amp; 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邮箱: qq邮箱显示效果 163邮箱: 163邮箱显示效果

4.参考网址

logback使用说明

5.线上异常

项目生产环境运营正常logback.xml文件的部分文件配置为:

<asynchronousSending>false</asynchronousSending>  

异步发送邮件=false 变更为同步发送。 问题:如果配置的发送邮箱服务器出现了问题无法发送时抛异常会把项目生产环境部署在tomcat的服务重启,就这样折腾了6个小时细心的同事注释了logger.error结果正常了。

© 著作权归作者所有

共有 人打赏支持
zzuqiang
粉丝 23
博文 61
码字总数 133582
作品 0
郑州
程序员
私信 提问
logback 配置详解

概览 简单地说,Logback 是一个 Java 领域的日志框架。它被认为是 Log4J 的继承人。 Logback 主要由三个模块组成: logback-core logback-classic logback-access logback-core 是其它模块的...

beanlam
2017/02/10
0
0
logback 配置详解(一)

一:根节点<configuration>包含的属性: scan: 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 scanPeriod: 设置监测配置文件是否有修改的时间间隔,如果没有...

有资本再款
2015/12/06
65
0
logback logback.xml常用配置详解(一) and <l

原创文章,转载请指明出处:http://aub.iteye.com/blog/1101260, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透彻的理解其配置 logb...

fish_yufeng
2015/12/29
28
0
Spring Boot -logback 使用

简单使用 依赖 配置 开启logback独立配置 logback配置 核心节点简介 节点 说明 root 控制项目整体输出日志级别,子节点为appender logger 控制指定包或者类的输出级别,和引用的appender,子节点...

MeiJM
02/26
0
0
spring-boot版本号1.4.1使用log4j日志,出错

因为使用的springboot版本号为1.4.1,springboot自带的日志,不能满足自己定义的日志需求,所以根据官网文档的描述,自定义配置日志. 官方文档是需求这么配置依赖的: <dependency><groupId>org....

贺小五
2016/12/18
485
0

没有更多内容

加载失败,请刷新页面

加载更多

租房软件隐私保护如同虚设

近日,苏州市民赵先生向江苏新闻广播新闻热线025-84658888反映,他在“安居客”手机应用软件上浏览二手房信息,并且使用该软件自动生成的虚拟号码向当地一家中介公司进行咨询。可电话刚挂不久...

linux-tao
今天
1
0
分布式项目(五)iot-pgsql

书接上回,在Mapping server中,我们已经把数据都整理好了,现在利用postgresql存储历史数据。 iot-pgsql 构建iot-pgsql模块,这里我们写数据库为了性能考虑不在使用mybatis,换成spring jd...

lelinked
今天
4
0
一文分析java基础面试题中易出错考点

前言 这篇文章主要针对的是笔试题中出现的通过查看代码执行结果选择正确答案题材。 正式进入题目内容: 1、(单选题)下面代码的输出结果是什么? public class Base { private Strin...

一看就喷亏的小猿
今天
2
0
cocoapods 用法

cocoapods install pod install 更新本地已经install的仓库 更新所有的仓库 pod update --verbose --no-repo-update 更新制定的仓库 pod update ** --verbose --no-repo-update...

HOrange
今天
3
0
linux下socket编程实现一个服务器连接多个客户端

使用socekt通信一般步骤 1)服务器端:socker()建立套接字,绑定(bind)并监听(listen),用accept()等待客户端连接。 2)客户端:socker()建立套接字,连接(connect)服务器,连接上后...

shzwork
昨天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部