Archiv für den Monat: August 2017

Logging in Griffon 2
using Log4j

I’ve done quite some Java programming for the Desktop with JavaFX lately. For that I’ve jumped into using the Griffon Framework and I really love it.

For logging, it comes with SLF4J (the Simple Logging Facade for Java), so you can plug in the actual framework of your chioce – and I gave it a try with Log4J 2 . Since it took me a little while to get it all working the way I wanted it to, here are the basic steps that worked for me:

first, in build.gradle, you have to put

    runtime('log4j:log4j:1.2.17') {
        exclude group: 'ant',         module: 'ant-nodeps'
        exclude group: 'ant',         module: 'ant-junit'
        exclude group: 'ant-contrib', module: 'ant-contrib'
    }
    runtime "org.slf4j:slf4j-log4j12:${slf4jVersion}"

then, create griffon-app/resources/log4j.properties:

log4j.rootLogger=TRACE,myapp

log4j.appender.myapp=org.apache.log4j.ConsoleAppender
log4j.appender.myapp.Target=System.out
log4j.appender.myapp.layout=org.apache.log4j.PatternLayout
# log4j.appender.myapp.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{8}:%L - %m%n
log4j.appender.myapp.layout.ConversionPattern=%-5p %c{8}:%L - %m%n

# show tool versions at startup:
log4j.logger.org.codehaus.griffon.runtime=INFO

# turn down noise on griffon's internals:
log4j.logger.org.codehaus=WARN
log4j.logger.griffon=WARN
# you might need those when searching for a problem
#log4j.logger.griffon.core=WARN
#log4j.logger.griffon.util=WARN
#log4j.logger.griffon.javafx=WARN
#log4j.logger.org.codehaus.griffon=WARN

# logs the app itself:
log4j.logger.griffon.app=TRACE

That is it! This of corse is just a very basic configuration but it is exactly what I need and use during development – for deployment, you just need to change the ConsoleAppender to e.g. a RollingFileAppender or whatever you prefer. And you may change the app’s logging level to something less noisy.

Now  just like in Grails, you get a log variable injected into your MVC-members
and you can  for example…

log.info "hello world!"

Quite simple, IMHO.

HTH! cheers, weHe