Search code examples
javalogginglogback

Logback thread pool


I use Logback for logging and I have a question. I use AsyncAppender with ConsoleAppender. When application starts it creates thread pool with "logback-" thread names. All logging work is completed by "AsyncAppender-Worker-" thread. For what purpose thread pool with "logback-" thread names was created and what work does it do?


Solution

  • The short answer

    These threads are used for all other work logback needs to do in the background - time-based rollovers, socket appenders, async SMTP appenders etc.

    A slightly longer answer

    By running a search on "logback-" over the logback codebase, I found only a single place where it's used: ExecutorServiceUtil.

    This helper class is used for creating executor services (accessed only by Contextbase.getScheduledExecutorService()), and by tracking its usages I found these usages:

    • Time-based rollover will asynchronously compress (if compression is enabled) and cleanup old archives. I assume this is because compressing old files on the application thread would be a bad thing.
    • Socket appenders have a connector thread that reconnects if the connection fails, and have an async message buffer that is being processed by the background thread.
    • A SMTP appender can be asynchronous if configured that way - then it will use the background executor, too.

    This is an exhaustive list. Note that all these are read from the source code. The time-based rollover, while it makes absolute sense to be asynchronous, is not documented to be, and could therefore change. The socket appender and the SMTP appender are documented to use a background thread.