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.

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:

Config.groovy:
----------------------------

import grails.util.Environment

if(Environment.current!=Environment.PRODUCTION){
  // log4j configuration
  log4j = {
    ...
  }
}


resources.groovy:
----------------------------

import grails.util.Environment

if(Environment.current==Environment.PRODUCTION){
  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.

miércoles, noviembre 26, 2014

Javascript: Haciendo herencia a través del prototype

Como en js no hay clases reales cuando se quiere obtener un funcionamiento de delegación similar hay que acudir a patrones más o menos liosos para ello.

La forma más efectiva de hacerlo (en términos de memoría) es haciendo uso de los prototypes de las funciones tal y como sigue:



PD: Sacado del curso de Udacity-Object Oriented Javascript (https://www.udacity.com/course/ud015), si puedes hazlo.

lunes, mayo 19, 2014

Grails, reusing domains with a binary plugin

How should I do in order to reuse my domains?
Use your own domain's plugin, this is how.

Just create your plugin:

grails create-plugin book-remote
...{work as usual with your domains}...

Change your groupId/version and set binary as default type of packaging:

BookRemoteGrailsPlugin.groovy:

String groupId = 'com.nortia.book'
String version = '0.1'
String packaging = 'binary'

Install into local maven repo (or wherever you want to):

grails  maven install --binary


Then, you have include your plugin as a dependency for your grails app:

BuildConfig.groovy:
    dependencies {
    //Since it is  binary plugin is goes here instead of in the 'plugins' section
compile 'com.nortia.book:book-remote:0.1'
    }


That's it! :D
PD: There is no need for the plugin to be 'binary' unless you're planning to use those domains in another groovy (no grails) project (which was my case).