Configuring LogBack Logging in Spring & Spring Roo

LogBack is the more versatile logging strategy created by the same person that created log4j. Use the following steps to configure your Roo 1.1 project to use LogBack.

See Also: Reasons to Switch to LogBack

1. Create a logback.xml file in src/main/resources

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                logbak: %d{HH:mm:ss.SSS} %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>
    <logger name="com.gordondickens" level="DEBUG"/>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

2. Configure your Maven pom.xml

<-- Properties Settings -->
    <properties>
        <roo.version>1.1.0.RELEASE</roo.version>
        <spring.version>3.0.5.RELEASE</spring.version>
        <aspectj.version>1.6.10</aspectj.version>
        <slf4j.version>1.6.1</slf4j.version>
        <logback.version>0.9.26</logback.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

<-- Dependencies Settings -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>${slf4j.version}</version>
</dependency>
<!-- NOTE COMMENT OUT SLF4J-LOG4J, SLF4J WILL USE LOGBACK INSTEAD
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>${slf4j.version}</version>
</dependency>
-->
<dependency>
	<groupId>log4j</groupId>
	<artifactId>log4j</artifactId>
	<version>1.2.16</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>jcl-over-slf4j</artifactId>
	<version>${slf4j.version}</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-classic</artifactId>
	<version>${logback.version}</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-core</artifactId>
	<version>${logback.version}</version>
</dependency>
<dependency>
	<groupId>ch.qos.logback</groupId>
	<artifactId>logback-access</artifactId>
	<version>${logback.version}</version>
</dependency>

3. Logging in Roo (ALWAYS use SLF4J Logging)

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...

public class ItemController {
	Logger logger = LoggerFactory.getLogger(ItemController.class);
...

	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid Item item, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("item", item);
            logger.debug("CREATING AN ITEM!");
            return "items/create";
        }
        item.persist();
        return "redirect:/items/" + encodeUrlPathSegment(item.getId().toString(), request);
    }

}

About Gordon

Technology enthusiast primarily focused on Java and Open Source projects. Spring Certified Professional and Trainer. http://twitter.com/gdickens http://linkedin.com/in/gordondickens http://github.com/gordonad
This entry was posted in LogBack, Logging, Maven, Roo, SLF4J, Spring Roo and tagged , , , , , , , . Bookmark the permalink.

One Response to Configuring LogBack Logging in Spring & Spring Roo

  1. Pingback: Tweets that mention Technophile Blog ยป Configuring LogBack in Roo -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>