> Hello World !!!

     

@syaku

Slfj4 & Logback 설정. Log4j

Slfj4 & Logback 설정. Log4j

일반적으로 아래와 같이 설정하면 Slf4j & Logback을 사용할 수 있다.

Gradle

def version = [
        slf4j: '1.7.12'
]

configurations.all {
    exclude group: "commons-logging", module: "commons-logging"
}

dependencies {

    runtime "org.slf4j:jcl-over-slf4j:${version.slf4j}"
    compile "ch.qos.logback:logback-classic:1.1.7"

}

Maven


...

<org.slf4j-version>1.7.15</org.slf4j-version>

...

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${org.slf4j-version}</version>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.7</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
    <scope>runtime</scope>
</dependency>

더 정확한 설정은 메이븐에 설정된 거라고 한다... 확실하지 않음~

이제 로그를 설정한다. 

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="LOGS_ABSOLUTE_PATH" value="로그파일경로" />

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOGS_ABSOLUTE_PATH}/mei.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOGS_ABSOLUTE_PATH}/mei.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>


    <logger name="org.springframework" level="info" />
    <logger name="org.springframework.security" level="info" />

    <root level="info">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

scan: 설정이 변경됬을 때 자동으로 리로드한다. 가견은 30초로 설정했다.
파일과 콘솔에 로그를 출력할 수 있게 설정하였다. 자세한건 http://logback.qos.ch 확인한다.

Spring Profiles설정에 따라 로그설정 변경하기

의존성 설정에 추가한다.

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>2.7.7</version>
</dependency>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <if condition='isDefined("spring.profiles.active")'>
        <then>
            <if condition='"${spring.profiles.active}".contains("test")'>
                <then>
                    <include resource="logback-mei.test.xml" />
                </then>
            </if>
            <if condition='"${spring.profiles.active}".contains("prd")'>
                <then>
                    <include resource="logback-mei.prd.xml" />
                </then>
            </if>
        </then>

        <else>
            <include resource="logback-mei.xml" />
        </else>
    </if>
</configuration>

if 문을 사용하여 설정파일을 include 하는 방법이다. include될 설정파일은 다음과 같이 시작하면 된다.

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

... TODO ...

</included>

WAS 구동할때 꼭 옵션을 -Dspring.profiles.active= 해줘야한다. 직접 로그설정파일을 지정하려면 -Dlogback.configurationFile= 절대경로를 포함한 설정파일 경로를 입력한다.

JDBC 로그 남기기

의존성 설정에 추가해준다.

<dependency>
    <groupId>com.googlecode.log4jdbc</groupId>
    <artifactId>log4jdbc</artifactId>
    <version>1.2</version>
</dependency>

로그 레벨을 추가한다.

  • jdbc.sqlonly : SQL문만을 로그로 남기며, PreparedStatement일 경우 관련된 argument 값으로 대체된 SQL문이 보여진다. 
  • jdbc.sqltiming : SQL문과 해당 SQL을 실행시키는데 수행된 시간 정보(milliseconds)를 포함한다. 
  • jdbc.audit : ResultSet을 제외한 모든 JDBC 호출 정보를 로그로 남긴다. 많은 양의 로그가 생성되므로 특별히 JDBC 문제를 추적해야 할 필요가 있는 경우를 제외하고는 사용을 권장하지 않는다. 
  • jdbc.resultset : ResultSet을 포함한 모든 JDBC 호출 정보를 로그로 남기므로 매우 방대한 양의 로그가 생성된다.

[출처] http://www.mimul.com/pebble/default/2008/10/24/1224847200000.html

<logger name="java.sql" level="error" />
<logger name="jdbc.sqlonly" level="debug" />
<logger name="jdbc.sqltiming" level="debug" />
<logger name="jdbc.audit" level="error" />
<logger name="jdbc.resultset" level="error" />

전자정부프레임워크 Slf4j & Logback 사용하기

pom.xml 에서 아래의 설정을 추가하거나 변경한다.


... 

<egovframework.rte.version>3.5.0</egovframework.rte.version>

... 

<dependency>
    <groupId>egovframework.rte</groupId>
    <artifactId>egovframework.rte.bat.core</artifactId>
    <version>${egovframework.rte.version}</version>
    <exclusions>
        <exclusion>
            <artifactId>log4j-slf4j-impl</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
        <exclusion>
            <artifactId>log4j-core</artifactId>
            <groupId>org.apache.logging.log4j</groupId>
        </exclusion>
    </exclusions>
</dependency>



posted syaku blog

Syaku Blog by Seok Kyun. Choi. 최석균.

http://syaku.tistory.com