2012-04-06 122 views
3

我想将错误日志邮寄给Gmail。但是,logback不会发送任何邮件,我也没有收到任何错误,即使我将用户名更改为随机无效的邮件。我认为它不会发送邮件。logback-smtpAppender邮寄日志到Gmail

我在主函数中记录了500封邮件,但没有记录到文件和控制台appender。

for(int i=0;i<501;i++){ 
    logger.error("mail test"); 
} 

请参阅logback.xml,pom.xml和控制台输出。

问候,

**logback.xml** 

    <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender"> 
      <smtpHost>smtp.gmail.com</smtpHost> 
      <smtpPort>465</smtpPort> 
      <SSL>true</SSL> 
      <username>[email protected]</username> 
      <password>aaaaa</password> 

      <to> [email protected]</to> 
      <from> [email protected]</from> 
      <subject>TESTING: %logger{20} - %m</subject> 
      <layout class="ch.qos.logback.classic.PatternLayout"> 
       <pattern>%date %-5level %logger{35} - %message%n</pattern> 
      </layout> 
     </appender> 
..... 
    <root level="TRACE"> 
     <appender-ref ref="FILE"/> 
     <appender-ref ref="STDOUT"/> 
     <appender-ref ref="EMAIL" /> 
    </root> 

**pom.xml** 

     <dependency> 
      <groupId>ch.qos.logback</groupId> 
      <artifactId>logback-classic</artifactId> 
      <version>1.0.1</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.mail</groupId> 
      <artifactId>mail</artifactId> 
      <version>1.4.3</version> 
     </dependency> 

Connected to the target VM, address: '127.0.0.1:51152', transport: 'socket' 
23:25:31,588 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 
23:25:31,588 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 
23:25:31,588 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/projects/TmfCorbaTester/target/classes/logback.xml] 
23:25:31,686 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 
23:25:31,702 |-INFO in ch.qos.logback.core.joran.action.StatusListenerAction - Added status listener of type [ch.qos.logback.core.status.OnConsoleStatusListener] 
23:25:31,736 |-INFO in ch.qos.logback.classic.joran.action.LoggerContextListenerAction - Adding LoggerContextListener of type [ch.qos.logback.classic.jul.LevelChangePropagator] to the object stack 
23:25:31,737 |-INFO in [email protected] - Propagating DEBUG level on Logger[ROOT] onto the JUL framework 
23:25:31,742 |-INFO in ch.qos.logback.classic.joran.action.LoggerContextListenerAction - Starting LoggerContextListener 
23:25:31,746 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.classic.net.SMTPAppender] 
23:25:31,768 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [EMAIL] 
23:25:31,871 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender] 
23:25:31,875 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE] 
23:25:31,925 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use zip compression 
23:25:31,927 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern D:/projects/TmfCorbaTester/TmfCorbaTester-%d-%i.log for the active file 
23:25:31,931 |-INFO in [email protected] - The date pattern is 'yyyy-MM-dd' from file name pattern 'D:/projects/TmfCorbaTester/TmfCorbaTester-%d-%i.log.zip'. 
23:25:31,931 |-INFO in [email protected] - Roll-over at midnight. 
23:25:31,932 |-INFO in [email protected] - Setting initial period to Sat Apr 07 23:19:35 EEST 2012 
23:25:31,936 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Cleaning on start up 
23:25:31,936 |-INFO in ch[email protected]f8395f - first clean up after appender initialization 
23:25:31,936 |-INFO in ch[email protected]f8395f - periodsElapsed = 64 
23:25:31,979 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: D:\projects\TmfCorbaTester/TmfCorbaTester.log 
23:25:31,980 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - File property is set to [D:\projects\TmfCorbaTester/TmfCorbaTester.log] 
23:25:31,981 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 
23:25:31,983 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 
23:25:31,990 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 
23:25:31,990 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUTJACORB] 
23:25:31,991 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [jacorb] to DEBUG 
23:25:31,991 |-INFO in [email protected] - Propagating DEBUG level on Logger[jacorb] onto the JUL framework 
23:25:31,994 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [jacorb] to false 
23:25:31,994 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[jacorb] 
23:25:31,996 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUTJACORB] to Logger[jacorb] 
23:25:31,996 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to TRACE 
23:25:31,996 |-INFO in [email protected] - Propagating TRACE level on Logger[ROOT] onto the JUL framework 
23:25:31,996 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT] 
23:25:32,001 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 
23:25:32,001 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [EMAIL] to Logger[ROOT] 
23:25:32,001 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 
23:25:32,003 |-INFO in [email protected] - Registering current configuration as safe fallback point 
mail test 
**23:25:32,017 |-INFO in ch.qos.logback.classic.net.SMTPAppender[EMAIL] - SMTPAppender [EMAIL] is tracking [1] buffers** 
mail test 
mail test 
mail test 
mail test 
.. 
.. 
. 

回答

3

你有双重检查Gmail设置?一个示例是在logback文档中: http://logback.qos.ch/manual/appenders.html#gmailSTARTTLS

其他一切对我来说都很好。

+1

HI @Noremac,我也面临着同样的问题。我还仔细检查了我的设置,但仍无法发送电子邮件。你能告诉我一些原因吗? ? – Rishi 2015-02-06 13:26:56

+0

我所能建议的是双重检查一切,例如smtp服务器的凭证,以及确保appender使用适当的日志记录级别(即在配置的元素内)注册。 – Noremac 2015-02-06 15:20:45

+0

我得到了解决方案。实际上SMTPAppender,我们只能使用根级作为错误。我使用根级别作为信息。现在我可以发送电子邮件了。谢谢 – Rishi 2015-02-09 04:35:49

3

这为我工作:

<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender"> 
     <smtpHost>smtp.gmail.com</smtpHost> 
     <smtpPort>587</smtpPort> 
     <STARTTLS>true</STARTTLS> 
     <username></username> 
     <password></password> 
     <to></to> 
     <from></from> 
     <subject></subject> 
     <layout class="ch.qos.logback.classic.html.HTMLLayout"/> 
     <asynchronousSending>false</asynchronousSending> 
</appender> 

而且这也没有:

<asynchronousSending>true</asynchronousSending>