Search code examples
emaillogginglogbackslf4jerror-logging

Logback SMTPAppender not working with gmail configuration


I am trying to setup my logger instances to send mails whenever an error log is generated by the application. I thought of using gmail for this purpose. My logger.xml is as below:

<configuration>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/path-to-application/application.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>/path-to-application/application-%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 250MB -->
                <maxFileSize>250MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date - %thread [%level] - %message%n%xException%n</pattern>
        </encoder>
    </appender>

     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date - %thread [%level] - %message%n%xException%n</pattern>
        </encoder>
    </appender>

     <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
        <smtpHost>smtp.gmail.com</smtpHost>
        <smtpPort>465</smtpPort>
        <ssl>true</ssl>
        <username>abc@gmail.com</username>
        <password>abc's password</password>
        <asynchronousSending>false</asynchronousSending>
        <to>xyz@gmail.com@gmail.com</to>
        <!--<to>ANOTHER_EMAIL_DESTINATION</to> &lt;!&ndash; additional destinations are possible &ndash;&gt;-->
        <from>abc@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>
        <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
            <!-- send just one log entry per email -->
            <bufferSize>1</bufferSize>
        </cyclicBufferTracker>
    </appender>

    <logger name="play" level="WARN"/>
    <logger name="application" level="INFO"/>

    <root level="ERROR">
        <!--  <appender-ref ref="STDOUT" /> -->
        <appender-ref ref="EMAIL"/>
    </root>
</configuration>

I added javax.mail and activation as dependencies in my SBT file. The Rolling file appender and console appender are working fine but SMTP appender seems not to work with these settings. Error logs are being logged in my rolling file but not being send as mail. There are no exceptions being logged so that I could investigate further.

Is there something wrong with these specifications or problem might be from gmail smtp server end?


Solution

  • I had the same problem, and finally found out via debugging.

    Logback cannot authenticate you to Gmail if you have setup 2-step authentication for instance. Try to generate an app password for logback and use it in place of your normal password in the config.

    You can follow these instructions: https://stackoverflow.com/a/25238515/1540818