Grails GSP automatically injects the "log" service into every page. Now since Wicket replaces GSP as the default, we need a way to add the logger ourselves. The good news is I have a working solution, the bad news is it requires some additional typing. Hopefully someone can help improve this.
Wicket comes bundled with slf4j logging.
The default Grails log4j logging resulted in serialization errors from inside Wicket
So we are going to use slf4j in our code but still use Grails log4j config to control the debug output because there is a slf4j-log4j bridge (see the jars in the wicket plugin)
STEP 1:
In Config.groovy, specify log4j for the package so you can type : log.debug("blah")
As you can see it might be useful to name your packages starting with "com." like "com.home", "com.anotherpackage" etc so you can specify the parent "com" package in the log4j log config below instead of each individual package name. see below.
// log4j configuration
log4j = {
// Example of changing the log pattern for the default console
// appender:
//
//appenders {
// console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
//}
debug 'home'
//other stuff goes here
}
Thanks to the below link that helped me with this final piece of the puzzle.
http://buildchimp.com/wordpress/?p=290
STEP 2:
Parent class logging:
package home
import org.slf4j.LoggerFactory
import org.slf4j.Logger
public class HomePage extends WebPage {
public HomePage(final PageParameters parameters) {
transient Logger logme = LoggerFactory.getLogger(index.class) //Why "transient" ? see http://stackoverflow.com/a/82552
logme.info("SIMPLE INFO :-| ")
logme.debug("SIMPLE DEBUG :) ")
logme.error("SIMPLE ERROR :( ")
add(new Link<Void>("clickMeLink"){
@java.lang.Override
void onClick() {
logme.error("print this error to the logs") //WORKS!
setResponsePage(SigninPage.class)
}
})
}
}
Anonymous inner class logging:
package home
import org.slf4j.LoggerFactory
import org.slf4j.Logger
public class HomePage extends WebPage {
……parent code goes here
class EmailForm extends Form implements Serializable{
transient Logger logme = Logger.getLogger(EmailForm.class);
ValueMap properties = new ValueMap()
public EmailForm(id){
super(id)
TextField email = new TextField("emailText", new PropertyModel(properties, "email"))
email.setRequired(true)
email.add(new EmailAddressValidator())
add(email)
}
@Override
protected void onSubmit() {
logme.error( "EMAIL ENTERED : " + properties.getString("email") ) }
}
}