Language/Java

Log4j 설정 (file vs xml)

아르비스 2013. 3. 28. 10:52

Java Program에서 System.out.println("").. 외에 자주 사용하는것이 log4J 이다


실행해주는 luncher의 특성에 따라서 xml형태의 log4j를 load 못하는 형태가 있다.

이때 jar 에 정의된 log4j는 따로 log를 남기기 어렵다

이때 다음과 같이 file 형태로 log4j를 설정하면 application과 달리 log남기는것이 가능하다


xml형태의 log4j를 file형태로 변환 한 예이다.

<log4j.xml>


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

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">


<log4j:configuration debug="true" xmlns:log4j="http://jakarta.apache.org/log4j/">


<appender name="SAEMUL" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="/LOGS/smb/sa/sa_${saiId}_${num}.log" />

<param name="Encoding" value="UTF-8" />

<param name="DatePattern" value="'.'yyyy-MM-dd" />

<param name="ImmediateFlush" value="true" />

<param name="Append" value="true" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d{ABSOLUTE} %5p [%c{1}:%L] - %m%n" />

</layout>

</appender>

<appender name="SASTATISTICS" class="org.apache.log4j.DailyRollingFileAppender">

<param name="File" value="/LOGS/smb/statistics_sa/statistics_sa_${saiId}_${num}.log" />

<param name="Encoding" value="UTF-8" />

<param name="DatePattern" value="'.'yyyy-MM-dd" />

<param name="ImmediateFlush" value="true" />

<param name="Append" value="true" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d{ABSOLUTE} %5p [%c{1}:%L] - %m%n" />

</layout>

</appender>


<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">

<param name="Encoding" value="UTF-8" />

<param name="ImmediateFlush" value="true" />

<layout class="org.apache.log4j.PatternLayout">

<param name="ConversionPattern" value="%d{ABSOLUTE} %5p [%c{1}:%L] - %m%n" />

</layout>

</appender>

<logger name="com.sds.smb.component">

<level value="DEBUG" />

<appender-ref ref="SAEMUL" />

</logger>

<logger name="com.sds.smb.net">

<level value="INFO" />

<appender-ref ref="SAEMUL" />

</logger>

<logger name="com.sds.smb.log.statistics">

<level value="DEBUG" />

<appender-ref ref="SASTATISTICS" />

</logger>


<logger name="org.springframework">

<level value="INFO" />

<appender-ref ref="SAEMUL" />

</logger>

<root>

<level value="INFO" />

<appender-ref ref="CONSOLE" />

</root>

</log4j:configuration> 



<SaLog.java>

import java.io.IOException;


import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

import org.apache.log4j.DailyRollingFileAppender;

import org.apache.log4j.Logger;

import org.apache.log4j.PatternLayout;


/**

 * @author juseok.yun

 * @version 2.0

 * @date 2013. 3. 28.

 */

public class SaLog

{

      Log log = LogFactory.getLog(getClass());

      

      /**

       * SA Log file location setting method.

       */

      public SaLog(String SaiId)

      {

            loadSaLogger(SaiId);

            

            loadStatisticsLogger(SaiId);

      }


      private void loadStatisticsLogger(String SaiId)

      {

            Logger logger = null;

            String filename = null;

            

            logger = Logger.getLogger("com.sds.smb.log.statistics");

            filename = addStatisticsLogger(SaiId, logger);

           

            log.info("sa Statistics log Load : " + filename);

      }


      private void loadSaLogger(String SaiId)

      {

            Logger logger = null;

            String filename = null;

            

            logger = Logger.getLogger("com.sds.smb.component");

            filename = addLogger(SaiId, logger);

            

            logger = Logger.getLogger("com.sds.smb.net");

            filename = addLogger(SaiId, logger);

            

            logger = Logger.getLogger("org.springframework");

            filename = addLogger(SaiId, logger);

            

            log.info("sa log Load : " + filename);

      }


      private String addLogger(String SaiId, Logger logger)

      {

            String filename = "/LOGS/smb/sa/sa_"+ SaiId + ".log";

            

            return makeLogger(logger, filename);

      }

      

      private String addStatisticsLogger(String SaiId, Logger logger)

      {

            String filename = "/LOGS/smb/statistics_sa/statistics_sa_"+ SaiId + ".log";

            

            return makeLogger(logger, filename);

      }


      private String makeLogger(Logger logger, String filename)

      {

            String pattern = "%d{ABSOLUTE} %5p [%c{1}:%L] - %m%n";

            PatternLayout layout = new PatternLayout(pattern);

            

            String datePattern = "'.'yyyy-MM-dd";

            

            DailyRollingFileAppender appender = null;

            try

            {

                  appender = new DailyRollingFileAppender(layout, filename, datePattern);

            }

            catch (IOException e)

            {

                  e.printStackTrace();

            }

            

            logger.addAppender(appender);

            return filename;

      }

}


별내용 아니었는데.. 참 애를 먹였다..

google에서 찾아도 잘 안나와서 남긴다..