logback-smtpAppender将日志邮寄到gmail

时间:2012-04-06 21:11:53

标签: logback

我正在尝试将错误日志邮寄到gmail。但是logback不发送任何邮件,我没有收到任何错误, 甚至我将用户名更改为随机无效的邮件。我认为它不会尝试发送邮件。

我在main函数中记录了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>myMail@gmail.com</username>
             <password>aaaaa</password>

            <to> myMail@gmail.com</to>
            <from> myMail@gmail.com</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 ch.qos.logback.classic.jul.LevelChangePropagator@8a548b - 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 ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1d87b85 - 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 ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1d87b85 - Roll-over at midnight.
23:25:31,932 |-INFO in ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP@1d87b85 - 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.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover@f8395f - first clean up after appender initialization
23:25:31,936 |-INFO in ch.qos.logback.core.rolling.helper.SizeAndTimeBasedArchiveRemover@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 ch.qos.logback.classic.jul.LevelChangePropagator@8a548b - 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 ch.qos.logback.classic.jul.LevelChangePropagator@8a548b - 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 ch.qos.logback.classic.joran.JoranConfigurator@14b6bed - 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 个答案:

答案 0 :(得分:3)

您是否仔细检查了Gmail设置?一个例子是在logback文档中: http://logback.qos.ch/manual/appenders.html#gmailSTARTTLS

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

答案 1 :(得分: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>

答案 2 :(得分:0)