Logback Logging - Synchronous or Asynchronous
Asked Answered
S

3

17

Is the default file appended of Logback:

ch.qos.logback.core.FileAppender

synchronous or asynchronous? It seems to be synchronous as the logs are being shown as part of same thread.

Shaitan answered 4/5, 2015 at 23:38 Comment(0)
L
30

Yes, it's synchronous by default. You can see a config example on how to make it asynchronous in the documentation.

This way, you can make any Appender asynchronous much easier (by simply wrapping it in an AsyncAppender) than if all Appender implementations would have to manage the asynchronicity on their own.

Lavern answered 4/5, 2015 at 23:49 Comment(0)
S
8

Most appenders are synchronous, for example, RollingFileAppender. To enable async logging, you must wrap an appender with AsyncAppender to create an async appender based on the sync one, and it could be done easily in XML like below.

<appender name="ASYNC-VERSION-APPENDER" class="ch.qos.logback.classic.AsyncAppender">
    <appender-ref ref="DEFAULT-APPENDER"/>
</appender>
<logger name="ASYNC-LOGGER" level="INFO" additivity="false">
    <appender-ref ref="ASYNC-VERSION-APPENDER"/>
</logger>
Socket answered 29/9, 2018 at 11:44 Comment(0)
P
0

See this: here we are wrapping MyFileAppender appender with AsyncAppender to make an async version of MyFileAppender

<appender name="MyFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
            ...
</appender>

    <!-- this one wraps `MyFileAppender` appender with AsyncAppender  to make an async version of MyFileAppender->
<appender name="ASYNC-FileAppender" class="ch.qos.logback.classic.AsyncAppender">
            <queueSize>512</queueSize>
            <appender-ref ref="MyFileAppender"/>
</appender>


<root level="${logging.level.root}">
            <appender-ref ref="ASYNC-FileAppender"/>
</root>
Pounds answered 9/6 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.