Errai now supports using the slf4j logging api on the server and client. This gives you the flexibility of choosing your own logging back-end for your server-side code, while still allowing a uniform logging interface that can be used in shared packages.
sl4j is logging abstraction. Using the slf4j api, you can add log statements to your code using a fixed api while maintaining the ability to switch the logging implementation at run-time. For example, the slf4j api can be used with java.util.logging (JUL) as the back-end.
The client-side slf4j code uses the GWT Logging as the back-end. Using slf4j in client-side code has two steps:
- Add the errai-common artifact as a maven dependency to your project
- Inherit the gwt module org.jboss.errai.common.ErraiCommon
In the ErraiCommon module, we have disabled the built-in GWT log handlers and provided four handlers of our own:
- ErraiSystemLogHandler : prints log statements to the terminal in Development Mode
- ErraiConsoleLogHandler : prints statements to the web console in the browser
- ErraiDevelopmentModeLogHandler : prints statements in the Development Mode window
- ErraiFirebugLogHandler : prints statements to the console in Firefox
These loggers are all enabled by default and set to handle all log levels.
The possible log levels correspond to those in java.util.logging.Level.
The Errai log handlers use ErraiSimpleFormatter to format log output. The format string is similar to that used in by java.util.SimpleFormatter (for precise differences please see the javadocs for ErraiSimpleFormatter and StringFormat).
On the server you are free to use any logging back-end that has slf4j bindings (or to make your own). Just make sure to add dependencies for the slf4j-api artifact and the slf4j binding you choose. Note: Some application servers provide their own slf4j bindings (such as JBoss AS), in which case you should add your binding dependency as provided scope.
To learn more about how to setup slf4j for your server-side code, see their website.
Here is sample usage of the slf4j code (which with the above setup can be run on the client or server):
By default, the above example with provide a logger with the fully qualified class name of the enclosing class. To inject a logger with an alternate name, use the NamedLogger annotation: