jueves, marzo 05, 2015

Async loggin with log4j 1.x

If you are reading this surely you have found out that using AsyncAppender its not clearly documented and most of your google results lead you to some log4j 2.X.

Ok, in that case this is the right post for you.

Lets take an ordinary (synchronous) file logger from Log4j 1.x ( tested with 1.2.17) and wrap it up with a 'cool' AsyncAppender. :D.

Considering that you have

import org.slf4j.Logger
...
static Logger log = LoggerFactory.getLogger("MyLogger");

You turn your internal sync appender into an async with:

static {
  AsyncAppenderBuilder.wrapAsyncAppender(log, "MyAppndrName")
}

Then use just like a regular logger

The helping class looks like this:

Hope it helps!
Iván.