Search code examples
androidasynchronousslf4jlogbacksmtpappender

Android Logback SMTP appender - unable to configure smtp appender with with asynchronousSending set to false


I'm using logback-android-11.1-2.jar with slf4j-api-1.7.6.jar and android javamail libs (activation, additional and mail jars).

I am using a SMTP appender that works fine when asynchronousSending is set to true or is omitted (as by default is true as well), but if I set it explicitly to false there is no email sent, I don't receive anything.

What I am trying to achieve is the re-attempt of email sending when the device gains connectivity, as if the SMTP appender is triggered while the device has no connection then the email will never be sent. I thought that maybe configuring the SMTP appender as synchronous would work?

Am I doing something wrong or is this an issue?

Thank you,

This is my logback.xml:

<configuration>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>dnzakex.com</smtpHost>
<smtpPort>25</smtpPort>
<to>[email protected]</to>
<from>[email protected]</from>
<subject>Mobile LOG - %logger{35}</subject>

    <layout class="ch.qos.logback.classic.html.HTMLLayout">
        <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </layout>

<!--Asynchronous by default is true, but we want to wait confirmation in case there is no connectivity. THIS DOES NOT WORK-->
<asynchronousSending>false</asynchronousSending>
</appender>

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/mnt/sdcard/MobileLog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <!-- rollover daily. The %i will iterate file names as file1, file2, etc for the same date-->
    <FileNamePattern>/mnt/sdcard/MobileLog.%d{dd-MM-yyyy}.%i.log.gz</FileNamePattern>
    <MaxHistory>30</MaxHistory>
    <timeBasedFileNamingAndTriggeringPolicy
        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>5MB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<append>true</append>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <charset>UTF-8</charset>
    <pattern>%d{dd-MM-yyyy HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="EMAIL" />
<appender-ref ref="FILE" />
</root>
</configuration> 

PS: I have tried with logback-android-11.1-3.jar with same result. Surprinsingly configured as asynchronousSending false it works ONLY sometimes. For example, if I start the android application and I start logging something it will append it to files, if I log an ERROR I will not get the email that the error should have triggered (and when asynchronousSending is true I receive it properly), but.. the email is triggered if some other error is logged later on LOG.error(..). It does not make any sense to me what could suddenly be making the email sending work, but it seems like a bug in the framework?


Solution

  • As per android logback's owner this is not a bug but an expected behavior. The restriction is that a LOG trace cannot be logged in the main thread if we are expecting the SMTP appender to send the email when asynchronousSending option is set to false.

    More details on this and on why SMTP appender does not keep track of the emails successfully or unsuccessfully sent at:

    https://github.com/tony19/logback-android/issues/71