#logback打印錯誤日誌到郵箱 須要依賴的jar文件:janinohtml
<log.janino.version>2.7.8</log.janino.version>
pom.xml配置以下:java
<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 依賴會出現如下錯誤:express
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)
定義文件名稱:logback.propertiesapache
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
logback.xml配置文件添加以下內容:tomcat
<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>
QQ郵箱: 163郵箱: 服務器
logback使用說明app
項目生產環境運營正常logback.xml文件的部分文件配置爲:異步
<asynchronousSending>false</asynchronousSending>
異步發送郵件=false 變動爲同步發送。 問題:若是配置的發送郵箱服務器出現了問題沒法發送時拋異常會把項目生產環境部署在tomcat的服務重啓,就這樣折騰了6個小時細心的同事註釋了logger.error結果正常了。async