Author Archive

logback configuration for gradle/springboot type projects …

yet another painful trawl to set up logging, and have it behave like spring boot default.

So to basics.

  1. create your gradle project, i’m using Intellij Idea for that.  You need to include the latest springboot-gradle-plugin.  Then ensure you apply the plugin.  Then fill your dependencies, but include latest log back from maven central and Slf4j-api.  This will provide default mapping for logger directly to logback.
  2. group 'org.softwood'
    version '1.0-SNAPSHOT'
    buildscript {
        repositories {
        dependencies {
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'groovy'
    apply plugin: 'java'
    sourceCompatibility = 1.8
    repositories {
    dependencies {
        compile 'org.apache.camel:camel-spring-boot-starter:2.18.4'
        compile 'org.apache.camel:camel-groovy:2.18.4'
        compile 'org.apache.camel:camel-stream:2.18.4'
        compile 'org.codehaus.groovy:groovy-all:2.4.11'
        //add Logback
        compile 'ch.qos.logback:logback-classic:1.2.3'
        compile 'ch.qos.logback:logback-core:1.2.3'
        compile 'org.slf4j:slf4j-api:1.7.25'
        testCompile group: 'junit', name: 'junit', version: '4.11'
        testCompile group: 'junit', name: 'junit', version: '4.12'
  3. Then put either a logback.groovy or a logback.xml file into the project classpath. By default i’ve used logback.groovy for this groovy project and put it under the resources directory (along with application.yml etc you may want to tailor the spring application)

    I did some digging and if you want coloured output as the spring boot default you need a logback.groovy a bit like this. This does a scan for changes which you can disable, and calls two closures to configure the appenders, and the loggers

    import org.springframework.boot.ApplicationPid
    import java.nio.charset.Charset
    import static ch.qos.logback.classic.Level.*
    import ch.qos.logback.core.ConsoleAppender
    import ch.qos.logback.classic.encoder.PatternLayoutEncoder
    import ch.qos.logback.core.status.OnConsoleStatusListener
    import ch.qos.logback.core.FileAppender
    scan('5 minutes')  // Scan for changes every 5 minutes.
    def displayStatusOnConsole() {
        statusListener OnConsoleStatusListener
    def setupAppenders() {
        //add to converters to handle colour and whitespace
        conversionRule 'clr', org.springframework.boot.logging.logback.ColorConverter
        conversionRule 'wex', org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter
        def consolePatternFormat = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr([%property{PID} - %thread]){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(>){faint} %m%n%wex"
        def filePatternFormat = "%d{yyyy-MM-dd HH:mm:ss.SSS} [%property{PID} - %thread] %-5level %-12logger{12}:[.%M] > %msg%n%wex"
        if (!System.getProperty("PID")) {
            System.setProperty("PID", (new ApplicationPid()).toString())
        //def logfileDate = timestamp('yyyy-MM-dd') // Formatted current date.
        // hostname is a binding variable injected by Logback.
        //def filePatternFormat = "%d{HH:mm:ss.SSS} %-5level [${hostname}] %logger - %msg%n"
        //appender('logfile', FileAppender) {
        //    file = "simple.${logfileDate}.log"
        //    encoder(PatternLayoutEncoder) {
        //        pattern = filePatternFormat
        //    }
        // Add custom converter for %smiley pattern.
        //conversionRule 'smiley', SmileyConverter
        appender('STDOUT', ConsoleAppender) {
            encoder(PatternLayoutEncoder) {
                charset = Charset.forName('UTF-8')
                pattern = consolePatternFormat
                //pattern =
    def setupLoggers() {
        //logger '', getLogLevel(), ['logfile']
        logger 'application.Application', DEBUG
        root DEBUG, ['STDOUT']
    def getLogLevel() {
        (isDevelopmentEnv() ? DEBUG : INFO)
    def isDevelopmentEnv() {
        def env =['app.env'] ?: 'DEV'
        env == 'DEV'
  4. As this is a groovy project you can just use @SLF4J annotation to your class and it will inject the log variable automatically – less noise that creating the logger. Now use the logh variable in a method as you need it
    @Slf4j //inject logger
    public class Application {
        ApplicationContext ctx
    public someMethod () {
                log.debug "log something "

That will give you a basic config that you can extend as you see fit, and add new classes to log as you go.

Categories: Uncategorized

A little bit of housework – MOPping up

December 31, 2016 Leave a comment

in doing some basics you keep relearning some stuff so here’s a quick recap on Groovy MOP.

Each groovy class has a metaClass, who’s default class is groovy.lang.MetaClassImpl.  If you set a property on the metaClass groovy changes this to be ExpandoMetaClass.  Normally calls to methods on your class are normally redirected to the metaClass to call the methods on your class using the invokeMethod call on the metaClass

Using the metaClass of your class you can lookup the methods on your class  like this

class MyIntercepted {

    def myMethods () {
        println "methods fom meta class"
        int total = 0
        def list = this.getMetaClass().getMethods().each {total++; println "$"}
        println "total methods found : $total \n"

    def myMetaMethods () {
        println "list of meta methods from meta class"
        int total = 0
        def list = this.getMetaClass().getMetaMethods().each {total++; println "$"}
        println "total of metaMethods found : $total \n"

    //has higher priority than invokeMethod
    /*def methodMissing (String name, args) {
        println "missing method: method $name ($args) was called by methodMissing"

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        println "metaclass is of type " + this.getMetaClass().getClass()
        println "method $name ($args) was called intercepted by invokeMethod"

// and use it 
MyIntercepted mi = new MyIntercepted()

mi.myMethods()       //class method exists
mi.myMetaMethods()   //class method exists
mi.smoke()           //class method doesn't exist 


calling myMethods() lists out the basic class methods found via the metaClass including those of GroovyObject, and my two class methods.

calling the myMetaMethods() will list out the broader list of metaMethods that Groovy embellishes each class with with via the GDK, which includes the “println” method.

if you implement def invokeMethod (String name, args), as shown (and you have to put the String type to name to get right dynamic dispatch), then when you call the nonexistent smoke(), method then Groovy calls the invokeMethod call where you can see the trace wriiten to your console.

if you uncomment the def methodMissing() this has higher priority and so this methodMissing() is called when the mi.smoke() method is called.

if you change your class to implement the marker interface GroovyInterceptable like this, then groovy intercepts all calls on the class and directs them to the invokeMethod call, including metaMethods such as println. Because of this you can’t use println as this is intercepted as wll and go into stackoverflow loop. you have to use the System.out.println to get trace output.

class MyIntercepted implements GroovyInterceptable {

    def myMethods () {
        println "methods from meta class"
        int total = 0
        def list = this.getMetaClass().getMethods().each {total++; println "$"}
        println "total methods found : $total \n"

    def myMetaMethods () {
        println "list of meta methods fom meta class"
        int total = 0
        def list = this.getMetaClass().getMetaMethods().each {total++; println "$"}
        println "total of metaMethods found : $total \n"

    //has higher priority than invokeMethod
    def methodMissing (String name, args) {
        println "missing method: method $name ($args) was called by methodMissing"

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        System.out.println "method $name ($args) was called intercepted by invokeMethod"

So how do call method if you have intercepted all the calls? You lookup the method and if it exists you can use the method.invoke(this, args) on it

    //will be called if no method can be matched
    def invokeMethod (String name, args) {
        //System.out.println "metaclass is of type " + this.getMetaClass().getClass()
        System.out.println "method $name ($args) was called intercepted by invokeMethod"
        MyIntercepted.metaClass.invokeMethod(this, 'println', "invoked println from metaClass.invokeMethod ")
        MetaMethod method = MyIntercepted.metaClass.getMetaMethod("hello")
        if (method) {
            System.out.println "looked up hello metamethod and tried to call it "
            method.invoke(this, "\tmethod.invoke: invoked hello metamethod")
            method.invokeMethod('println', "\tmethod.invokeMethod: invoked println directly from a meta method")

however you have to be careful like with println as this will again go into stack overflow

if you override the invokeMethod on the metaClass like this

MyIntercepted mi = new MyIntercepted()
mi.metaClass.invokeMethod = {String name, args -> System.out.println "used overriden metaClass invokeMethod"}

it has lower priority than a class method of same the name implemented in the class. whereas if you have a methodMissing on a class and one on the metaClass, the resolution path uses the metaClass implementation first!

Categories: Groovy Tags: ,

Eclipse high display scale (DPI) – ‘tactical’ fix for Windows

March 26, 2016 Leave a comment

I Eventually found a fix that seems to work for eclipse Mars (2) and Neon builds. It doesn’t seem to have any impact on Eclipse Luna (great shame i was trying to get IBM RSA 9.5 which is based on Luna to scale – failed back to IBM for an upgrade path).  But if you are doing general development work with eclipse on windows with high DPI screen this is massive improvement – it makes it all usable again.

My laptop is running Windows 10, but i suspect this works for Windows 8/8.1 just the same, but i no longer have such a build to try it on.

Essentially this is just a re-post of the answer i found here referenced fix

To get this to work you have to first make an edit to your registry  as follows.  You have to add a new key, and set its value to 1 (true), as follows. The registry structure up to the SideBySide entry should already be in existence when you navigate in the registry browser.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\SideBySide\PreferExternalManifest (DWORD) to 1

The next thing you have to do is to create a new text file in the same directory as your eclipse.exe installation.
The file should be called eclipse.exe.manifest and have the following ‘magic’ text cut and past and then saved.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
                <requestedExecutionLevel xmlns:ms_asmv3="urn:schemas-microsoft-com:asm.v3"
        <asmv3:windowsSettings xmlns="">
            <ms_windowsSettings:dpiAware xmlns:ms_windowsSettings="">false</ms_windowsSettings:dpiAware>

Then stand back and pray. Double click your eclipse.exe file and it should come up
looking as it would in the ‘good’ old days, and the icons will to be legible.

I am sure Neon is supposed to have proper SWT full DPI scaling and detection on release, but i tried Neon-M5 today, and it was still
illegible. This tactical fix makes it work for now – hoorah.

Quick note on groovy testing a spock

February 29, 2016 Leave a comment

Been getting to grips with gradle, groovy and spock, on a little project and couldnt figure out how to get the GroovyTestCase power assertions available to spock tests.

So eventually found it you have to add the following import

import static groovy.test.GroovyAssert.*
import static org.assertj.core.api.Assertions.*

this enables you to use the the groovy power asserts (shouldfail, assertFalse, etc ), the other is a fluent assertion library that has some interesting features – you need to update your compile dependencies to include ‘org.assertj:assertj-core:3.0.0’

I have not played with the Hamcrest matchers at this point but probably worth a look as these with Spock too

If you want Gradle to find and run your tests i’ve found its easiest to end your class file nameTest.groovy then you can run single shot tests for example like this

gradle -Dtest.single=DynamicRuleSpecification test

which will run the DynamicRuleSpecificationTest.groovy in your ‘src/test/groovy’ folder

Happy testing

Categories: gradle, Groovy, Testing Tags: ,

silly problems with Spring @Value ‘${}’ with IoC injection

February 23, 2016 Leave a comment

hit some basic problems with Spring injection today worth noting for others

I was trying out @Value injection today in spring and hit some basic issues

1. when you use @Value in groovy and spring – despite the fact that you might want to use a ${} expression, you cant wrap this in “” which you might expect. The Groovy compiler gets in a huff and declares the the value is not a String (technically its a GString in groovy).

to make this work in groovy you have write something like (note single ”)

@Value ('${}')

This is counter intuitive if you are used to the normal “” Gstring interpolation in Groovy. To make it work with “” you have to try

@Value ("${}".toString())

or just use the ” as previously shown.

2. second issue: if injecting values from in root of your classpath , then note that the properties values in that file default to string, so `x.y.z=”hi”` will return a string including the quotes, which is not what you expect. just leave the strings unadorned to inject correctly.

3. then, if you use param injection alone like this

void setHost ( @Value ('${}') String host)

then you get a null injection (not sure why). To fix this, either

a) move the

@Value ('${}'

to method level, or

b) use @autowired at method level and leave the

@Value ('${}')

at parameter level as well,  to get the injection to occur.

It’s these small nuances that take so much time, hopefully someone else will benefit and save a couple of hours.

4. Lastly, you can lookup properties using the Spring Environment like this. Just use the @Autowired on method or constructor, and do the lookup /type conversion using the env.getRequiredProperty call like this

class Props {

    private Map props = [:]
    boolean jmxEnabled
    String host

    @Autowired  //test inject the env
    Props (Environment env) {
        jmxEnabled = env.getRequiredProperty(&quot;org.softwood.jmx&quot;, Boolean.class)


    //format either use Autowired for method and @value on param
    // or just @value as method injection will work
    void setHost ( @Value ('${}') String host)  {

        assert host
        println &quot;set host by injection&quot; = host
        props[host] = host


accessing Google geocode api with groovy

February 23, 2016 Leave a comment

This is just a quick post on how you can get started with the google geocode API services from groovy

you start by visiting Google set to get an authentication key

use the ‘get a key’ button in the second section. this runs a wizard for you and you complete your details. I set up a server key for dev pc as i’m playing and that’s the registered server name i used.

At the end of this process google will generate an API key for you which you should record safely and securely. when coding read this value from a config file, etc so that you don’t encode it directly in your code.

Once setup with this you try the API out. There is a interactive javascript version of this api which i havent used – i’m using the json webservice, and there is an XML version of the service also.

The return result type has been encoded into the base URL, and not using accept headers.

first lets start with location class, that you’ll provide instance of with the address you want to get lat/long for. (you can do this in reverse as well)

class Location {
String houseNumber
String street
String city
String state
String zip
String country

double latitude
double longitude

String toString () {"$street, $city, $zip, $state, $country : latitude:$latitude, longitude:$longitude"}

i’m not persisting this put you do so readily as local cache etc.

The web service is located with a url base of

 def base = ""

what the geocoder service will do is take a location (with populated record details) and build an address parameter, by URLencoding an array of address data from the location, using the join(‘,’) as the separator.

You then creat a params array for the web service, by taking a map of the address and your server API key (there is an optional components attribute that i have not used here – see google api defn for details). you then collect and join the key=value pairs you build to get the resultant param format for the call.

you then use JsonSlurper to parse the result of the call, (assert it all went ok), and then read the lat, lng values back (google api shows you the structure of the result set) and store these back in the location instance, converting the result to Double as you do so.

class Geocoder {
    def base = ""

    void completeLatLong (Location loc){
        def addressFields =  loc.street ? [loc.street,,,loc.state,] : [,]
        def address = addressFields.collect {URLEncoder.encode (it, 'UTF-8')}.join (',')
        def params = [address:address,
                      key:<your api key here>]
        def url = base +params.collect {k,v -> "$k=$v"}.join ('&')

        def response = new JsonSlurper().parse (url.toURL())

        assert response.status == "OK"
        loc.latitude = response.results[0]
        loc.longitude = response.results[0].geometry.location.lng.toDouble ()


you can now write tests, or simple driver script to use the service (fill in your own street, city, etc.

def loc = [street: "<your street>", city: "<your city>", zip: "<your zip>", state:"<your state>", country:"<your country>"] as Location

def geo = new Geocoder()
geo.completeLatLong (loc)

println loc  //should return completed lat/long from the successful service 

This should return you the loc record with the added geocoding details, which you can use elsewhere

Categories: Groovy

Groovy DSL and example notes to myself

February 22, 2016 Leave a comment

most of this stuff is already out there but i wanted to try and make sure i’d understood command chaining and other tricks

so first off we note that groovy allows your to drop ‘()’ off methods where there are multiple attributes (its still required for no arg method though)

def method1 ()
def method2 (arg1, arg2=null)
def method3 (Map map, arg1, arg2=null, ...)

method2 (someArg) //is equivalent to
method2 someArg

//also with mixed named/unamed attributes groovy collects all named
// attributes as into a 1st map attribute of the method

//these two are equivalent
method3 (address: "the house", 0143 222222, street: "south close")
method3 address: "the house", street: "south close", 0143 222222

Also if your function returns itself or another function (or a closure) then the dot invocation is optional, for the compiler can read this when we drop the ‘.’

def method4 () {
    println "hello"
return this
method4().method4 () //is equivalent to 
method4() method4()

so this gives rise to some interesting examples of using groovy to work to build
DSL facilities.

so we’ll build an example up as we go of a groovy script to show some of the options, using a simple robot like DSL which can go in some direction, travel some distance, at some speed.

first i added a couple of static imports for enums that come later in the script

import static Direction.*
import static DistanceUnit.*
import groovy.transform.TupleConstructor

i then defined a couple of enums for a DistanceUnit and Duration as we are going
to embellish Numbers with some additional method facilities

//using static import to get 'left' and 'right' direction  enums -
//see static imports above
enum Direction {
    forward, backward, left, right

enum DistanceUnit {
 centimeters ('cm', 0.01),
 meters ('m', 1),
 kilometers ('km', 1000)

 String abbreviation
 double multiplier

 DistanceUnit (String abbr, double mult) {
 this.abbreviation = abbr
 this.multiplier = mult}

enum Duration {
    millisecond ('ms', 0.001),
    second ('s', 1),
    hour ('h', 60),
    day ('d', 60*24)

    String abbreviation
    double multiplier

    Duration (String abbr, double mult) {
        this.abbreviation = abbr
        this.multiplier = mult}

//define a symbol for hour in script, used when getting a speed = dist/dur
def h = Duration.hour

We the want to create a couple of classes for Speed and Distance, but use the @TupleConstructor annotation so that we can avoid having to type ‘new’ when we wish to create some instances of these classes. Also as we wish to have something like ‘’ we need to overload the div operator for the Distance class and return a Speed from this operation.

//avoid having to use new in declaration
class Distance {
    Number amount
    DistanceUnit unit

    //added div overload so that we can have distance over unit of time
    Speed div (Duration dur) {
        new Speed (amount, unit, Duration.hour)

    String toString () {"$amount $unit"}
class Speed {
    Number amount
    DistanceUnit unit
    Duration duration

    String toString () {"$amount $unit/$duration.abbreviation"}

Now through metaprogramming we can embellish Numbers with some methods to get a Distance like this

//add new meta method to Number- return the number (delegate) as instance of Distance
//global change to Number though - probably want to limit this using categories but forces us to inject
//and use groovy shell evaluate to do the assessment
Number.metaClass.getMeters = {new Distance (delegate, DistanceUnit.meters)}

However this is a global change to Number, and we probably wish to limit the scope of these methods, so lets define a Category instead like this

//limit the scope of added methods to use closure
class DistanceCategory {
    static Distance getCentimetres (Number num) {
        new Distance (num, DistanceUnit.centimeters)

    static Distance getMeters (Number num) {
        new Distance (num, DistanceUnit.meters)

    static Distance getKilometers (Number num) {
        new Distance (num, DistanceUnit.kilometers)

    //short form
    static getCm (Number num) {

    static getM (Number num) {

    static getKm (Number num) {
        getKilometers (num)

So using the above our first go for a DSL might be to be to define a function like this

def move1 (Direction dir, Distance d) {
        println "move1: moving $dir by $d"

//works because 3.meters calls the new metamethod to return instance 
//of Distance.  
//wrap in 'use' closure if using category class
move1 left, 3.meters

thats not too bad, we have removed the parentheses, and invoked the getMeters()
metamethod to return a Distance.

This will print ‘move1: moving left by 3 meters’ on the console

we can get a bit more readable by defining our move method like this. Ive used
the category class this time, not the global addition of meta method to Number.

//when a method call is made with a mix of named and no named params, then grooy puts all
//the named params into a map - as first param, and the rest as unnamed param in the order seen
//so define a new move like this

def move2 (Map m, Direction dir) {
    println "move2: moving $dir by $ at $"

//use the move DSL and Category to add metamethods
use (DistanceCategory) {
move2 right, by: 3.meters, at: / h

This works as groovy collects the named parameters and drops them into the map,
and provides the remaining attributes, after that.

This writes ‘move2: moving right by 3 meters at 5 kilometers/h’ to the console.

However it would be nice to drop the ‘,’ and the : in the maps to make this more
readable. so here is the last trick using command chains (return a callable class – here a closure for next call)

//using command chains now
def move3 (Direction dir) {
    [by: {Distance dist ->
        [at: {Speed speed ->
            println "move3: moving $dir by $dist, at $speed"

use (DistanceCategory) {
    move3 right by 3.meters at / h
    //is the same as
    move3 (right).by (3.meters).at(

now when you provide the direction to the move3 method, it returns a map with key ‘by’ and a single closure as its value which is invoked with 3.meters. This
immediately returns another map with key ‘at’, and another closure which is
invoked with the 5km/h (using the overloaded Distance class to pass in a Speed as the attribute to the closure, the ‘h’ property was defined previously in the script binding above.

This can make your head explode walking through the tautology, but if you step through what the compiler sees you should be able to ‘see’ what this does.

Lastly I tried this

def func (arg = null) {
    println "got args as " + arg ?: null
    if (!arg) {
        println "setting up no arg closure in map"
        [by : { -> println "no args closure: hello william "}()]
    else {
        println "setting up with arg closure in map"
        [by: { mapArg -> println "with args closure : $mapArg" }]

//println "calling no args on func : "
func() by

//println "calling with args on func : "
println "calling args on func : "
func("someArg") by 3

In the first case we call ‘func’ with no args, and the function definition defines a single map with key ‘by’ that has a closure with no args. as no args as passed you have to add the ‘()’ in the first map to execute the returned closure. In the second case you provide an attribute for func, which sets up a map with key ‘by’ that takes an argument. so you can trigger this with the by 3 and call that closure when it returns.

All this best done with a clear head and step each bit through at a time.

You can also provide the DSL boilerplating in one class and separate the language script in another through using groovy shell evaluate and injecting the compiler customisers, static imports and extended base script to that the evaluate method inherits all the DSL
definition parts like this

def binding = new Binding (//add some variables etc ) 

def importCustomizer = new ImportCustomizer()
importCustomizer.addStaticStars //etc 

def conf = new CompilerConfiguration()
conf.addCompilationCustomers (importCustomizer)
conf.scriptBaseClass =  //specify the script base class

def shell = new GroovyShell (this.class.classLoader, binding, config)
shell.evaluate '''
      move left...
Categories: Groovy, Uncategorized Tags:
%d bloggers like this: