Grails HowTo: Log mybatis queries to separate file

Background

The grails application exposes JSON webservice API. The persistence is MyBatis.

Requirement

I want to have separate log file that only contains:

  • which API is being called
  • what SQL script being called
  • what’s the parameter passed

This will be very helpful for debugging the application.

Current solution

Update log4j configuration in config.groovy. First, I need to add new appender. In my case, I want it to be logged into ws-api.log

Next, is defining what’s going to be log into the file

Notice that ‘apiLog’ is the rollingFile appender’s name.

I use ‘trace’ level to print both mybatis’ SQL statement and the parameter passed in. Fyi, ‘com.hida.ctms.*’ is where all my mybatis gateways and mappers are.

One item left to solve: log which API to call.

To achive that, I just need to declare the following in the controller

Notice that the logger name is ‘api.ws’ which is one of the log pattern declared in log4j config.

That’s it.

Here is the example of log generated:

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">