Spring MVC slf4j + Logback Logging Example
Almost all web application need some sort of logging. Either for an audit trail, legal aspects or for debugging purposes during development. In this tutorial we integrate logging in a web application configured with Spring MVC slf4j and logback.
Project Structure
Make sure your project looks similar to the following structure. Note we configure the application using java configuration so we don’t need a web.xml
src
|--main
| +--java
| +--com
| +--memorynotfound
| +--config
| |--ServletInitializer.java
| |--WebConfig.java
| +--controller
| |--HomeController.java
| +--resources
| |--logback.xml
| +--webapp
pom.xml
Maven Dependencies
If you manage your application using maven, add the following dependencies to your project and Maven will automatically resolve them.
<!-- spring libraries -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.20</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
Logback Configuration
Add the logback.xml configuration file to the src/main/resources folder. This logback configuration file creates a ConsoleAppender
that’ll print all logging to the console. We can enable different logging levels for different packages. We set all loggers org.springframework
, com.memorynotfound
and root
loggers to debug level.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<logger name="org.springframework" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<logger name="com.memorynotfound" level="debug" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Slf4j + Logback Logging Example
We created a simple rest-service that’ll be executed when a HTTP HEAD request method is executed to the correct URL. This service simply prints all logging levels.
package com.memorynotfound.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeController {
private final Logger logger = LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value = "/", method = RequestMethod.HEAD)
public void index(){
logger.trace("trace logging");
logger.debug("debug logging");
logger.info("info logging");
logger.warn("warning logging");
logger.error("error logging", new RuntimeException("help"));
}
}
Demo
When we produce a HEAD request to the following URL: http://localhost:8081/spring-mvc-logback/ the controller defined above is executed and the logging statements will be triggerd.
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - DispatcherServlet with name 'dispatcher' processing GET request for [/spring-mvc-logback/]
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Looking up handler method for path /
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.w.s.m.m.a.RequestMappingHandlerMapping - Returning handler method [public java.lang.String com.memorynotfound.controller.HomeController.index(org.springframework.ui.ModelMap)]
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'homeController'
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Last-Modified value for [/spring-mvc-logback/] is: -1
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG c.m.controller.HomeController - debug logging
2016-05-11 15:31:13 [http-nio-8081-exec-7] INFO c.m.controller.HomeController - info logging
2016-05-11 15:31:13 [http-nio-8081-exec-7] WARN c.m.controller.HomeController - warning logging
2016-05-11 15:31:13 [http-nio-8081-exec-7] ERROR c.m.controller.HomeController - error logging
java.lang.RuntimeException: help
at com.memorynotfound.controller.HomeController.index(HomeController.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
[...]
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'index'
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Rendering view [org.springframework.web.servlet.view.JstlView: name 'index'; URL [/WEB-INF/views/index.jsp]] in DispatcherServlet with name 'dispatcher'
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.web.servlet.view.JstlView - Forwarding to resource [/WEB-INF/views/index.jsp] in InternalResourceView 'index'
2016-05-11 15:31:13 [http-nio-8081-exec-7] DEBUG o.s.web.servlet.DispatcherServlet - Successfully completed request