Logback in Spring-boot application, inits the same appender twice

1.3k Views Asked by At

My logback has two appenders a console and an email appender, which extends from my custom SMPTAppender... My logger logger.error("My error"); triggers an email to be sent after x - minutes in my custom SMTPAppender, the problem is that, the scheduler doesn't wait for the delay, as two are initialized.

This happens twice:

Attaching appender named [EMAIL] to Logger[ROOT]

That's why IMO also two schedulers are created (startmethod is invoked twice)

Logback.xml (Where the problem should be):

<configuration debug="true">

 Logging per console and per email
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            sets the format of the output
            %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="EMAIL" class="com.konverto.phonebillasaj.appenders.ScheduledSMTPAppender">
    <subject>TESTING: %logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout" />

    <smtpHost>smtp.xxx.net</smtpHost>
    <smtpPort>587</smtpPort>
    <STARTTLS>true</STARTTLS>
    <username>[email protected]</username>
    <password>xxxx</password>
    <to>[email protected]</to>
    <from>[email protected]</from>
    <maxMessages>10</maxMessages>

    for testing , comment in production, default 256
    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
        Send just one log entry per email, ready for a lot of emails if you put one.
        <bufferSize>1</bufferSize>
    </cyclicBufferTracker>

    for testing , comment in production, default asynchronousSending = true
    <asynchronousSending>false</asynchronousSending>
</appender>


<logger name="com.konverto.phonebillasaj" level="error" additivity="false">
    <appender-ref ref="EMAIL"/>
    <appender-ref ref="CONSOLE" />
</logger>

<root level="error">
    <appender-ref ref="EMAIL" />
    <appender-ref ref="CONSOLE" />
</root>

1

There are 1 best solutions below

1
Ajanthan On

I also faced the same issue when adding two log appenders. But was able to resolve it by removing the console appender.

My initial root config was like below

<root level="info">
    <appender-ref ref="CONSOLE" />
    <appender-ref ref="ROLLING_FILE" />
</root>

In this case I got duplicated logs in the console, but not in the file logs.It seems like whatever the appender you set in logger they write to console no mater what (While running in IDE or run as .jar). So I removed the Console appender and just kept my FILE appender. like below.

<root level="info">
    <!-- <appender-ref ref="CONSOLE" /> -->
    <appender-ref ref="ROLLING_FILE" />
</root>

New log out like below Log out with one log appender.

I believe same would solve your problem too.