목차 >> Logging 
+- GlueLog  
+- logback  
+- logback  
----+- appender

10장 Logging

log4j

자바 로깅 구현체(Logger)로 log4j 을 사용할 경우, 다음 2개의 dependency가 필요합니다. 그리고 log4j configuration이 필요합니다.log4j configuration과 관련된 자세한 것은 log4j manual을 참고합니다.

제공되는 예제 'sample-hello-log4j' 을 통해 응용해 보시기 바랍니다.

pom.xml 일부
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${slf4j-jcl-version}</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>${slf4j-log4j-version}</version>
</dependency>
log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value = "[%t] %-5p - %c||%m%n"/>
        </layout>
    </appender>

    <logger name="com.poscoict">
        <level value="debug" />
    </logger>
    <logger name="org.springframework">
        <level value="info" />
    </logger>
    <logger name="sample">
        <level value="debug" />
    </logger>
    <root>
        <level value="warn"/>
        <appender-ref ref="CONSOLE"/>
    </root>
</log4j:configuration>

appender

위의 log4j.xml에는 ConsoleAppender가 사용되었습니다. log4j 역시 ConsoleAppender외에도 RollingFileAppender, DailyRollingFileAppender, AsyncAppender 등이 있습니다. 다음은 ConsoleAppender와 RollingFileAppender를 함께 사용한 예입니다.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value = "[%t] %-5p - %c||%m%n"/>
        </layout>
    </appender>
    <appender name="FILEAPPENDER" class="org.apache.log4j.RollingFileAppender">
        <param name="append" value="true"/>
        <param name="file" value="${CONFIG_PATH}/log/glue_app_debug_log.log"/>
        <param name="maxFileSize" value="5MB"/>
        <param name="maxBackupIndex" value="100"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value = "%d %-5p - [%t]%c||%m%n"/>
        </layout>
    </appender>

    <logger name="com.poscoict">
        <level value="debug" />
    </logger>
    <logger name="org.springframework">
        <level value="info" />
    </logger>
    <logger name="sample">
        <level value="debug" />
    </logger>
    <root>
        <level value="warn"/>
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILEAPPENDER"/>
    </root>
</log4j:configuration>