반응형

Spring - Logging with Log4J


(원문 위치 : http://www.tutorialspoint.com/spring/logging_with_log4j.htm )

Spring 프로그램에서 Log4J 기능을 사용하는 것은 쉽다. 이번 강좌는 Log4J와 Spring사이의 간단한 통합을 설명하기 위해 간략한 몇단계를 거칠 것이다.

우선 Log4J가 설치되어 있다고 가정하겠다. 만약 설치되어 있지 않다면 'http://logging.apache.org/'에서 다운로드하고 압축을 푼다. 이번 강좌에서는 오로지 'log4j-x.y.z.jar'만을 사용할 것이다.
(** eclispe mar.1 - 오늘 업뎃햇다.. - 에서는 이미 설치되어 있는 것 같다.. 버전별로..)

[003] HelloWorld Example 강좌를 참조하여 프로젝트를 생성한다.

HelloWorld.java : 

package com.tutorialspoint;


public class HelloWorld {

private String message;


public void getMessage() {

System.out.println("Your Message : " + message);

}


public void setMessage(String message) {

this.message = message;

}

}



MainApp.java :

package com.tutorialspoint;


import org.jboss.logging.Logger;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainApp {

static Logger log = Logger.getLogger(MainApp.class.getName());

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("Beans.xml");

log.info("Going to create HelloWorld Obj");

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

obj.getMessage();

log.info("Exiting the program");

}

}


info message를 생성한 것과 유사하게 'debug'와 'error' 메시지도 생성할 수 있다.

Beans.xml :

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


<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">

       <property name="message" value="Hello World!"/>

   </bean>


</beans>


아래는 Log4j가 log message를 만들기 위해 필요한 표준 규칙을 정의한 log4j.properties의 내용이다.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
# Set the name of the file
log4j.appender.FILE.File=C:\\log.out

# Set the immediate flush to true (default)
log4j.appender.FILE.ImmediateFlush=true

# Set the threshold to debug mode
log4j.appender.FILE.Threshold=debug

# Set the append to false, overwrite
log4j.appender.FILE.Append=false

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

실행 결과는 아래와 같다.



Jakarta Commons Logging (JCL) API

다른것으로 Spring 프로그램에서 log를 생성하기 위해 Jakarta Commons Logging(JCL) API를 사용할 수 있다.

(이것 또한 이미 eclispe에 설치되어 잇는 상태이다..)


logging 기능을 사용하기 위해서는 org.apache.commons.logging.Log 객체가 필요하고 매 요청마다 아래 method중 하나를 호출할 수 있다.

  • fatal(Object message)

  • error(Object message)

  • warn(Object message)

  • info(Object message)

  • debug(Object message)

  • trace(Object message)

아래소스를 JCL API를 사용하는 MainApp2.java파일로 추가한다.

package com.tutorialspoint;


import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainApp2 {

static Log log = LogFactory.getLog(MainApp.class.getName());

public static void main(String[] args) {

ApplicationContext context =

new ClassPathXmlApplicationContext("Beans.xml");

log.info("Going to create HellowWorld Obj");

HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

obj.getMessage();

log.info("Exiting the program");;

}

}


실행 결과는 아래와 같다.



*** 이로써 Spring 강좌가 마무리 되었다. 내용이 빈약한 부분이 상당부분 있지만 차차 하나의 다른 강좌로 보완해 나갈 예정이다.

*** 다음 강좌는 iBATIS이다.




반응형

+ Recent posts