Skip to content
Michael O'Cleirigh edited this page Mar 26, 2011 · 9 revisions

Logback Project

About

The wicketstuff-logback module is the home for classes that can help with using wicket and logback together. The two important classes in this module are detailed below: LogbackConfigListener for logback config relocation and WicketWebPatternEncoder to enrich your log messages with request information.

Maven snippet

Add this to your pom.xml:

<dependency>
	<groupId>org.wicketstuff</groupId>
	<artifactId>wicketstuff-logback</artifactId>
	<version>some-version</version>
</dependency>

LogbackConfigListener

LogbackConfigListener is a ServletContextListener that can be used in web applications to define the location of the logback configuration file and optionally to make the webapp's context path available as a placeholder in the logback config. LogbackConfigListener was modelled after spring's Log4jConfigListener which is the same for log4j.

web.xml snippet

It has to be defined in the web.xml. The config location is specified in the "logbackConfigLocation" contex-param. The context path placeholder name can optionally be specified with "logbackConfigContextPathKey".

<context-param>
	<param-name>logbackConfigLocation</param-name>
	<param-value>/WEB-INF/log-sc.xml</param-value>
</context-param>

<!-- Optional: make context path available in logback config with ${contextPath} -->
<context-param>
	<param-name>logbackConfigContextPathKey</param-name>
	<param-value>contextPath</param-value>
</context-param>

<listener>
	<listener-class>org.wicketstuff.logback.LogbackConfigListener</listener-class>
</listener>

Where the configuration file is a web application resource. "/WEB-INF/log-sc.xml" is a standard logback config file.

logbackConfigLocation value

Placeholders (for example ${user.home}) in logbackConfigLocation are supported. Some example locations:

  • /WEB-INF/log-sc.xml (starts with '/') -> loaded from servlet context
  • classpath:foo/log-cp.xml (starts with "classpath:") -> loaded from classpath
  • file:/D:/log-absfile.xml (is a valid url) -> loaded as url
  • D:/log-absfile.xml (is an absolute file path) -> loaded as absolute file
  • log-relfile.xml (is a relative file path) -> loaded as file relative to the servlet container working directory

WicketWebPatternEncoder

WicketWebPatternEncoder can automatically add detailed information about the current HttpServletRequest to the log message. This class is extending its superclass (PatternLayoutEncoder from logback) with a new conversion word: "%web". If WicketWebPatternEncoder is configured as encoder for a logback appender the "%web" in pattern gets replaced with information about the current request. This involves the http method, url, query string, session id, etc. If no request is available (because for example not in a web request handling thread) the conversion word is replaced with an empty string.

logback.xml snippet

To use it you have to define it in your logback config (note org.wicketstuff.logback.WicketWebPatternEncoder and %web).

<appender name="appender" class="ch.qos.logback.core.ConsoleAppender">
	<encoder class="org.wicketstuff.logback.WicketWebPatternEncoder">
		<charset>UTF-8</charset>
		<pattern>%d|%p|%t|%c{36}|%r|%web%n\t%caller{1}\t%m%n%xEx</pattern>
	</encoder>
	<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
		<level>debug</level>
	</filter>
</appender>

Example output

Let us say that you have the above logback config and a wicket HomePage logging every construction like this:

public class HomePage extends WebPage {
	private static final long serialVersionUID = 2696517689763106924L;

	private static final Logger LOGGER = LoggerFactory
			.getLogger(HomePage.class);

	public HomePage() {
		LOGGER.info("Logging is good - said the lumberjack.");
	}
}

A log message would look like this (note the part in the first line after the last '|'):

2011-02-21 14:18:26,281|INFO|"http-nio-8080"-exec-2|o.w.logback.examples.HomePage|28066|GET http://localhost:8080/wicketstuff-logback-examples/?null null null 127.0.0.1:59363 127.0.0.1:8080 null Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.13 (KHTML, like Gecko) Chrome/9.0.597.98 Safari/534.13
	Caller+0	 at org.wicketstuff.logback.examples.HomePage.<init>(HomePage.java:19)
	Logging is good - said the lumberjack.

Dependencies

Wicketstuff-logback depends on wicket-core 1.5-RC1<= and logback-classic 0.9.28<=.

Further reading

For more info see the javadoc of the classes. For a complete example see the "wicketstuff-logback-examples" web application.

Clone this wiki locally