5.7. Logging

For application logging you should use one of the well established logging libraries such as Java Util Logging (JUL) or Log4J.

The library you use will largely depend upon the application server you are targeting. For Apache Tomcat or RedHat JBoss the Log4j library is a good choice. While for the IBM WebSphere or Oracle WebLogic application servers Java Util Logging is better choice as this library is better supported.

If you have to target multiple application servers you should consider using the SLF4J library which uses compile time bindings to an underlying logging implementation.

As a general principle you should probably avoid Commons Logging because of the class loading issues associated with it. If you are using Commons Logging please make sure you have the latest version.

It is a best place to define a logger method in a common base page, for example:

public class BasePage extends Page {

    protected Logger logger;

    public Logger getLogger() {
        if (logger == null) {
            logger = Logger.getLogger(getClass());
        }
        return logger;
    }
}

Using this pattern all your application bases should extend BasePage so they can use the getLogger() method.

public class CustomerListPage extends BasePage {

    public void onGet() {
        try {
            ..
        } catch (Exception e) {
            getLogger().error(e);
        }
    }
}

If you have some very heavy debug statement you should possibly use an isDebugEnabled switch so it is not invoked if debug is not required.

public class CustomerListPage extends BasePage {

    public void onGet() {
        if (getLogger().isDebugEnabled()) {
            String msg = ..

            getLogger().debug(msg);
        }

        ..
    }
}

Please note the Click logging facility is not designed for application use, and is for Click internal use only. When Click is running in production mode it will not produce any logging output. By default Click logs to the console using ConsoleLogService.

If you need to configure Click to log to an alternative destination please configure a LogService such as JdkLogService, Log4JLogService or Slf4jLogService.