본문 바로가기

log4jdbc logback 설정.

java by 낼스 2015. 10. 2.
 
 

 

JAVA/Servlet 목록열기 (198)

목록열기

 

myBatis + log4j + log4jdbc (myBatis Query Logging) JAVA/Servlet

2015.03.12. 19:51

복사 http://blog.naver.com/pureb612b/220298184301

전용뷰어 보기

출처 : http://a.zany.kr:9003/board/bView.asp?bCode=52299899&aCode=7620


● pom.xml 에 dependency 추가

- Apache log4j : http://logging.apache.org/log4j/1.2
- SLF4J : http://www.slf4j.org
- log4jdbc : http://code.google.com/p/log4jdbc/
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
 
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.6</version>
</dependency>
 
<dependency>
    <groupId>org.lazyluke</groupId>
    <artifactId>log4jdbc-remix</artifactId>
    <version>0.2.7</version>
</dependency>



● spring context xml 파일에 log4j 환경 로드 Bean 설정

CLASSPATH 로 잡혀 있는 src/main/resources 디렉토리 안에 log4j.xml 또는 log4j.properties 파일을 넣어두면 되지만,
별도의 경로에 넣는 경우 아래와 같은 bean 설정을 통해 인식시켜줄 수 있다.
참고 : maven standard directory layout

1
2
3
4
5
6
7
8
9
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/>
    <property name="targetMethod" value="initLogging"/>
    <property name="arguments">
        <list>
            <value>src/main/resources/config/log4j.xml</value>
        </list>
    </property>
</bean>



● myBatis DataSource Bean 설정

일반적으로 아래와 같은 형태로 DataSource 를 설정할 것이지만,
 

1
2
3
4
5
6
<bean id="dataSourceMobilebaseDB" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://mydomain:port/instance" />
    <property name="username" value="myaccount" />
    <property name="password" value="mypassword" />
</bean>


log4jdbc 를 사용하려면 아래와 같이 위의 DataSouce 설정에서 driverClassName, url 만 변경해 준다.
 

1
2
3
4
5
6
<bean id="dataSourceMobilebaseDB" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="net.sf.log4jdbc.DriverSpy" />
    <property name="url" value="jdbc:log4jdbc:mysql://mydomain:port/instance" />
    <property name="username" value="myaccount" />
    <property name="password" value="mypassword" />
</bean>



● log4j.xml 파일 설정

- jdbc.sqlonly : logs executed sql with binding arguments replaced with bound data.
- jdbc.sqltiming : logs how long a sql took to execute.
- jdbc.audit : logs all jdbc calls except for ResultSets.
- jdbc.resultset : same as jdbc.audit plus ResultsSets.
- jdbc.connection: logs open and close connection events.
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?xml version="1.0" encoding="UTF-8"?>
 
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
 
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d{HHmmss.SSS} [%-5p] %c{1}.%M():%L %m%n"/>
        </layout>
    </appender>
 
    <!-- ===================================== -->
    <!-- log4jdbc settings                     -->
    <!-- ===================================== -->
    <!--                       development production -->
    <!-- jdbc.connection     :     WARN       WARN    -->
    <!-- jdbc.audit          :     WARN       WARN    -->
    <!-- jdbc.sqlonly        :     WARN       WARN    -->
    <!-- jdbc.sqltiming      :     INFO       WARN    -->
    <!-- jdbc.resultset      :     WARN       WARN    -->
    <!-- jdbc.resultsettable :     INFO       WARN    -->
 
    <logger name="jdbc.connection" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="console"/>
    </logger>
 
    <logger name="jdbc.audit" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="console"/>
    </logger>
 
    <logger name="jdbc.sqlonly" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="console"/>
    </logger>
 
    <logger name="jdbc.sqltiming" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="console"/>
    </logger>
 
    <logger name="jdbc.resultset" additivity="false">
        <level value="WARN"/>
        <appender-ref ref="console"/>
    </logger>
     
    <logger name="jdbc.resultsettable" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="console"/>
    </logger>
 
    <!-- ===================================== -->
    <!-- Possible Log Levels:                  -->
    <!-- ===================================== -->
    <!-- FATAL, ERROR, WARN, INFO, DEBUG, TRACE -->
 
    <root>
        <level value="INFO"/>
        <appender-ref ref="console"/>
    </root>
 
</log4j:configuration>



● 결과 확인
 

1
2
3
4
5
6
7
8
9
10
11
12
134552.226 [INFO ] LogManagerServiceTest.testGetMinMaxLogIds():36 ============================================
134552.226 [INFO ] LogManagerServiceTest.testGetMinMaxLogIds():37 TEST GET MIN-MAX LOG IDS
134552.226 [INFO ] LogManagerServiceTest.testGetMinMaxLogIds():38 ============================================
134552.343 [INFO ] sqltiming.sqlTimingOccured():357 select ifnull(min(log_id),-1) as logIdMin, ifnull(max(log_id),-1) as logIdMax,
count(log_id) as logCnt from app_logs_50659300 where logdate between str_to_date('20140101000000', '%Y%m%d%H%i%s')
and str_to_date('20140114235959', '%Y%m%d%H%i%s')  {executed in 25 msec}
134552.344 [INFO ] resultsettable.println():108 |---------|---------|-------|
134552.345 [INFO ] resultsettable.println():108 |logIdMin |logIdMax |logCnt |
134552.345 [INFO ] resultsettable.println():108 |---------|---------|-------|
134552.345 [INFO ] resultsettable.println():108 |2441749  |2466679  |24931  |
134552.346 [INFO ] resultsettable.println():108 |---------|---------|-------|
134552.346 [INFO ] LogManagerServiceTest.testGetMinMaxLogIds():51 RESULT OK.



● 참고

== log4j.xml ==
http://pupustory.tistory.com/153

== log4jdbc ==
https://code.google.com/p/log4jdbc/
http://www.javacodegeeks.com/2012/05/log-jdbc-operations-with-log4jdbc.html
http://www.mimul.com/pebble/default/2008/10/24/1224847200000.html
 





 

'java' 카테고리의 다른 글

POJO to String by java reflection  (0) 2015.12.28
cors enable 가이드.  (0) 2015.10.08
rowspan 계산.  (0) 2015.08.18
제우스(Jeus) 가상 호스트 등록 방법.  (0) 2015.04.24
Passing String Array Parameter  (0) 2014.12.05

댓글