Posts

Showing posts from November, 2012

Log4jdbc+Grails+Tomcat7

As your Grails application grows in size and popularity you will find yourself with performance and concurrency problems to overcome. As part of this process you will likely find yourself debugging the raw sql sent to the database. Hibernate comes with functionality to do this, but it is not very most user friendly. The most popular tool for sql debugging is P6spy, but an up and coming alternative is Log4jdbc. Log4jdbc works similar to P6Spy in that you replace your usual sql driver with one provided by the tool. //driverClassName = "org.h2.Driver" driverClassName = "net.sf.log4jdbc.DriverSpy" You then have to make a change to the database connection url. You must add log4jdbc to the protocol component. Log4jdbc should then be able to detect for which driver it is proxying for. jdbc:log4jdbc:mysql://localhost/mydatabase Next you need to add appropriately to the logging configuration of your Grails app. Log4jdbc provides lots of options. I like to use j…

How to include session id and username in Grails logs

Introduction It can be very useful in debugging problems to include the session id and the logged in user's username in your logs. Of course you can do it manually in every applicable log, but there is another way, a more Spring/Grails like way. There is a feature in log4J called Mapped Diagnostic Context (MDC). This enables the storing of thread/request/context specific values in a map, that can be included in log output. To store something in this map you would write code like the following: import org.apache.log4j.MDC ... MDC.put("username", username) This entry in the MDC map can then be referenced in an application's log4J conversion pattern. To add the username defined above you would have a pattern like: %c{2} %X{username} %m%n Grails Example Let's now move to an example of how once could take advantage of MDC in Grails. Firstly, create a filter which will populate the MDC map. Such a filter could look like if you are using Spring Security: class MyFi…

Getting started with Grails functional tests using Geb + Spock

Introduction There are a number of different functional test frameworks that can be used with Grails. Each of which can be incorporated into the overall test infrastructure of your Grails app by including the relevant plugin:
Canoo WebtestG-FuncGebSelenium-RCWebDriver
Grails doesn't hold any opinion on which one to choose. This is fair, and gives developers choice, but it can also be a headache; one has to make the choice of which framework, and also do the necessary setup and configuration. The functional test framework of the day is currently Geb, which builds on top of another functional framework in WebDriver. Geb can be used with the test runners JUnit, TestNG, or Spock. Spock, like Geb is the tool dejour and they are commonly used together. The best thing about Spock tests is that they are easy to read.
New Dependencies in your BuildConfig To run your functional tests you need to include a number of libraries and plugins in your BuildConfig.groovy. Here is what you typica…