Logback 기본 설정

■ Maven + Spring 기반 웹프로젝트에서 LogBack을 기반으로 한 로깅을 최대한 간단하게 구현해 보았다.
■ 기본 파일 구조는 전형적인 Maven 웹 프로젝트 구조인데 logback설정시 필요한 파일은 maven의 pom.xml, /src/main/resource/logback.xml파일이 필요하다.
logback 구조
■ maven의 pom.xml에서 logback-classic, slf4j-api 디펜던시를 추가한다. 전체 pom.xml 소스는 아래와 같다.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.iotinfra.pilot</groupId>
  <artifactId>logbackpilot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
    <properties>

		<!-- Generic properties -->
		<java.version>1.6</java.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		
		<!-- Web -->
		<jsp.version>2.2</jsp.version>
		<jstl.version>1.2</jstl.version>
		<servlet.version>2.5</servlet.version>
		
		<!-- Spring -->
		<spring-framework.version>3.2.3.RELEASE</spring-framework.version>

		<!-- Logging -->
		<logback.version>1.0.13</logback.version>
		<slf4j.version>1.7.5</slf4j.version>

		<!-- Tomcat Plugin -->
		<tomcat.plugin.version>2.2</tomcat.plugin.version>
		
	</properties>
	
	<dependencies>
	
		<!-- Spring MVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring-framework.version}</version>
		</dependency>
		
		<!-- Other Web dependencies -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>${servlet.version}</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>${jsp.version}</version>
			<scope>provided</scope>
		</dependency>

		<!-- Logging with SLF4J & LogBack -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>${logback.version}</version>
			<scope>runtime</scope>
		</dependency>

	</dependencies>	
	
	<build>
  		<plugins>
    		<plugin>
      			<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
			    <version>${tomcat.plugin.version}</version>
			    <configuration>
			    	<url>http://localhost</url>
			        <path>/</path>
			        <port>9999</port>
			        <contextReloadable>true</contextReloadable>
				</configuration>
			</plugin>
  		</plugins>
	</build>
	
</project>

 

■ logback.xml파일을 /src/main/resources 파일에 생성한다. 이 파일은 배포시에 /WEB-INF/classes폴더로 복사된다. 전체 소스는 아래와 같으며 로깅 메세지는 콘솔(표준출력)로만 나오도록 구성되어 있고 로깅 레벨은 debug, 그리고 30초마다 이 파일을 체크하여 변경내용(로그 레벨 변경등…)을 확인하여 실시간으로 서버에 반영되도록 설정되어 있다.

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>false</resetJUL>
    </contextListener>

    <jmxConfigurator/>

    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%-5level] %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>
         
    <root level="debug">
        <appender-ref ref="console"/>
    </root>
    
</configuration>

 

■ web.xml 파일에는 spring 기본 설정만 반영되어 있다.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>logbackpilot</display-name>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:/spring/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

■ spring-config.xml에서도 역시 ViewResolver 하나만 정의되어 있다.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="net.iotinfra.pilot.logback"/>
    <mvc:annotation-driven />

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="prefix" value="/WEB-INF/view/"/>
	        <property name="suffix" value=".jsp"/>
	</bean>

</beans>

 

■ LogbackPilotController.java

package net.iotinfra.pilot.logback;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value = "/logback")
public class LogbackPilotController {

	private final static Logger logger = LoggerFactory.getLogger(LogbackPilotController.class);
	
	@RequestMapping(value = "list")
	public ModelAndView list(@RequestParam (required=false) String dummy) {
		
		logger.debug( "[mirinae.maru] /logback/list logging..." );
		logger.trace("aaa");
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("logback_list");
		
		return mav;
	}
}

 

■ /WEB-INF/view/logback_list.jsp

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
<html>
	<head>
		<meta charset="utf-8">
		<title>Welcome</title>
	</head> 
	<body>

<h2>logback_list</h2>

	</body>
</html>

 

■ pom.xml 파일이 있는 디렉토리에서 도스창을 열어 mvn tomcat7:run 명령어를 입력하면 tomcat 서버가 실행되고 브라우저에서 http://localhost:9999/logback/list를 호출하면 tomcat서버를 실행한 도스창(콘솔)에서 logback 로그가 로깅되는 것을 확인할 수 있다.
 
 
 

댓글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다