Wicket + Log4j integration example

wicket log4j

In this tutorial, we will show you how to integrate the log4j framework with the Wicket web application. The Wicket framework is using SLF4j APIs, to make sure of Log4j, declares slf4j-log4j12 dependency.

Review the project environment :

  1. SLF4j 1.7.7
  2. Log4j 1.2.17
  3. Wicket 6.16.0
  4. Maven 3
  5. Tomcat 6
  6. Eclipse Kepler 4.3

1. Project Directory

Review the final project structure.

wicket-log4j-directory

2. Project Dependencies

Declares both Wicket and slf4j-log4j12 dependencies.

pom.xml

	<properties>
		<wicket.version>6.16.0</wicket.version>
		<slf4j.version>1.7.7</slf4j.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>org.apache.wicket</groupId>
			<artifactId>wicket-core</artifactId>
			<version>${wicket.version}</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>

	</dependencies>

Figure : pom.xml in Eclipse pom editor, dependency hierarchy

wicket-log4j-dependency

3. Log4j properties

Like usual, create a log4j.properties file and put it into the resources folder. Refer to step #1.

log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=${catalina.home}/logs/mywicketapp.log
log4j.appender.file.MaxFileSize=5KB
log4j.appender.file.MaxBackupIndex=5
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

4. Message Logging

Example to use log4j to log a debug and error message.

Hello.java

package com.mkyong.hello;

import org.apache.log4j.Logger;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;

public class Hello extends WebPage {

	// Get logger
	private static final Logger logger = Logger.getLogger(Hello.class);

	private static final long serialVersionUID = 1L;

	public Hello() {

		// logs debug
		if (logger.isDebugEnabled()) {
			logger.debug("Hello()");
		}

		// logs exception
		logger.error("Error message", new Exception("ABC"));

		add(new Label("message", "Wicket + Log4j"));

	}
}

5. Demo

Run the Wicket web application, for example : http://localhost:8080/Log4jAndWicket/. Both Wicket and application logging will be displayed on the console, and also output to a file.

wicket-log4j

Figure 5.1 : Eclipse console

wicket-log4j-file

Figure 5.2 : D:\apache-tomcat-6.0.37\logs\mywicketapp.log

Download Source Code

Download it – WicketAndLog4j.zip(10 KB)

References

  1. Apache Wicket
  2. log4j 1.2 official page
  3. log4j hello world example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Enable debug messages for org.apache.wicket.util.resource Recent comment authors
newest oldest most voted
trackback
Enable debug messages for org.apache.wicket.util.resource

[…] enable the logging in debug mode, and trace how Wicket find resources via log file. For example, integrate log4j with Wicket. Sample of log4j output… DEBUG MarkupCache:300 – Load markup: […]