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!

jueves, febrero 12, 2015

Grails in prod with an external log4j properties

Maybe you have tried to config your Grails app so you can:

  • Use the default logging configuration inside Config.groovy for test or dev.
  • Use an external log4j.properties file in production.

It's quite frustating and it is not so well documented.
After some digging I came up with a quite elegant solution:


import grails.util.Environment

  // log4j configuration
  log4j = {


import grails.util.Environment

  log4jConfigurer(org.springframework.beans.factory.config.MethodInvokingFactoryBean) {
    targetClass = "org.springframework.util.Log4jConfigurer"
    targetMethod = "initLogging"
    arguments = ["classpath:resources/log4j.properties"]

log4j.properties (src/resources/log4j.properties):
  //whatever make sense to you

Please remember NOT to forget your imports or otherwise it wont work.

Hope it helps!

PD: Grails 3.0 is about to see the light and ...well who kows!. This solution is right for Grails 2.x family.