About log4j-gwt
log4j-gwt enables GWT client applications to use the log4j framework. It provides a translatable implementation of the classes used for logging.
The output is forwarded to the normal GWT logging facility. So log4j-gwt supports the appenders and configuration provided by GWT.
It provides the Logger features of log4j. Most notable are:
- Log methods accept any object as the message parameter, not just strings
- Support for NDC (nested diagnostic context) and MDC (mapped diagnostic context)
- Full compatibility with logging code written for log4j
How to use log4j-gwt?
In order to use log4j on the client side of a GWT application, you need to:
- add log4j-gwt.jar to the classpath right next to log4j.jar. Usually this is done by copying it to war/WEB-INF/lib
- add the following line to your module.gwt.xml file:
<inherits name="org.apache.log4j.Log4j" />
log4j-gwt makes use of the normal GWT logging configuration. log4j-gwt supports the GWT appenders including the widget appender (see screenshot) and the Firebug appender.
import org.apache.log4j.Logger; import org.apache.log4j.MDC; import org.apache.log4j.NDC; public class Demogwt implements EntryPoint { private static Logger logger = Logger.getLogger(Demogwt.class); public void onModuleLoad() { // Simple messages logger.trace("A message at trace level."); logger.debug("A message at debug level."); logger.info("A message at info level."); logger.warn("A message at warn level."); // Logging exceptions try { Integer.parseInt("Hallo"); } catch (NumberFormatException e) { logger.error("Parsing the number failed", e); } // nested diagnostic context NDC.push("ndc1"); NDC.push("ndc2"); logger.info("Test for the NDC."); NDC.clear(); // mapped diagnostic context MDC.put("key1", "value1"); MDC.put("key2", "value2"); logger.info("Test for the MDC."); MDC.clear(); } }
Change log
- Release 1.0 (2012-08-04)
- stable release
- Release 0.2 (2012-07-12)
- added support for NDC and MDC (without support for lazy cleanup)
- added support for stacktraces of exceptions
- silently ignore attempts to configure log4j using the PropertiesConfigurator ot the DOMConfigurator
- decreased code size by providing stubs that are deleted by the GWT compiler
- Release 0.1 (2012-07-11)
- Initial release: Logger, Level, LogManager and more are working