Java開発

Java開発に役立つ技術メモやロジックのサンプル

2017/11 | 123456789101112131415161718192021222324252627282930

ブログ内検索

まずは、log4jdbc から JDK のバージョンにあった jar ファイルを入手します。

  • log4jdbc3-{バージョン}.jar (JDK 1.4 or 5.0)
  • log4jdbc4-{バージョン}.jar (JDK 6.0 or 7.0)

log4jdbc は SLF4J を使用しているので、これも入手します。

  • slf4j-api-{バージョン}.jar
  • slf4j-log4j12-{バージョン}.jar

最初に JDBC のドライバ名に net.sf.log4jdbc.DriverSpy を指定します。

次に JDBC の接続 URL に jdbc:log4 を追加します。

Oracle の 接続 URL の場合
jdbc:oracle:thin:@localhost:11521:java6
    ↓
jdbc:log4jdbc:oracle:thin:@localhost:11521:java6

最後に log4j.xml に Appender を追加します。

  • jdbc.log => JDBC の実行されたメソッドのログ
  • sql.log => JDBC で実行された SQL のログ
  • sqltiming.log => SQL の 実行時間ログ

log4.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' debug="false">

	<!-- appender log4jdbc -->
	<appender name="stdout-appender" class="org.apache.log4j.ConsoleAppender">
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS}\t[%-5p]\t%c{1}:\t%m%n"/>
		</layout>
	</appender>

	<!-- appender log4jdbc -->
	<appender name="sql-appender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${LOG_HOME}/sql.log"/>
		<param name="Append" value="true"/>
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />
		<param name="Encoding" value="UTF-8" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS}\t[%-5p]\t[%t]\t%m%n"/>
		</layout>
	</appender>

	<!-- appender log4jdbc -->
	<appender name="sql-timing-appender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${LOG_HOME}/sqltiming.log"/>
		<param name="Append" value="true"/>
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />
		<param name="Encoding" value="UTF-8" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS}\t[%-5p]\t[%t]\t%m%n"/>
		</layout>
	</appender>

	<!-- appender log4jdbc -->
	<appender name="jdbc-appender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${LOG_HOME}/jdbc.log"/>
		<param name="Append" value="true"/>
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />
		<param name="Encoding" value="UTF-8" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS}\t[%-5p]\t[%t]\t%m%n"/>
		</layout>
	</appender>

	<!-- appender log4jdbc -->
	<appender name="connection-appender" class="org.apache.log4j.RollingFileAppender">
		<param name="File" value="${LOG_HOME}/connection.log"/>
		<param name="Append" value="true"/>
		<param name="MaxFileSize" value="10MB" />
		<param name="MaxBackupIndex" value="10" />
		<param name="Encoding" value="UTF-8" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS}\t[%-5p]\t[%t]\t%m%n"/>
		</layout>
	</appender>

	<!-- logger SQL (pre-execution) plus exceptions caused by SQL -->
	<logger name="jdbc.sqlonly" additivity="false">
		<level value="INFO"/>
		<appender-ref ref="sql-appender"/>
	</logger>

	<!-- logger SQL with timing information, post execution -->
	<logger name="jdbc.sqltiming" additivity="false">
		<level value="INFO"/>
		<appender-ref ref="sql-timing-appender"/>
	</logger>

	<!-- logger all jdbc calls except ResultSet calls -->
	<logger name="jdbc.audit" additivity="false">
		<level value="INFO"/>
		<appender-ref ref="jdbc-appender"/>
	</logger>

	<!-- logger the jdbc ResultSet calls -->
	<logger name="jdbc.resultset" additivity="false">
		<level value="INFO"/>
		<appender-ref ref="jdbc-appender"/>
	</logger>

	<!-- log connection open/close events and dump of all open connection numbers -->
	<logger name="jdbc.connection" additivity="false">
		<level value="INFO"/>
		<appender-ref ref="connection-appender"/>
	</logger>

	<!-- this log is for internal debugging of log4jdbc, itself -->
	<!-- debug logging for log4jdbc itself -->
	<logger name="log4jdbc.debug" additivity="false">
		<level value="WARN"/>
		<appender-ref ref="stdout-appender"/>
	</logger>

	<root>
		<level value ="off" />
	</root>
	
</log4j:configuration>

connection.log
2011/01/23 08:45:56.328	[INFO ]	[main]	1. Connection opened
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection opened
2011/01/23 08:46:03.375	[INFO ]	[main]	1. Connection opened
2011/01/23 08:46:03.453	[INFO ]	[main]	2. Connection opened

jdbc.log
2011/01/23 08:45:56.328	[INFO ]	[main]	1. Connection.new Connection returned 
2011/01/23 08:45:56.328	[INFO ]	[main]	1. Connection.isClosed() returned false
2011/01/23 08:45:56.343	[INFO ]	[main]	1. Connection.getAutoCommit() returned true
2011/01/23 08:45:56.343	[INFO ]	[main]	1. Connection.setAutoCommit(false) returned 
2011/01/23 08:45:56.343	[INFO ]	[main]	1. Connection.isReadOnly() returned false
2011/01/23 08:45:56.343	[INFO ]	[main]	1. Connection.isClosed() returned false
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.new Connection returned 
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.isClosed() returned false
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.getAutoCommit() returned true
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.setAutoCommit(false) returned 
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.isReadOnly() returned false
2011/01/23 08:45:56.406	[INFO ]	[main]	2. Connection.isClosed() returned false
2011/01/23 08:45:56.562	[INFO ]	[main]	2. PreparedStatement.new PreparedStatement returned 
2011/01/23 08:45:56.562	[INFO ]	[main]	2. Connection.prepareStatement(SELECT * FROM SYSTEM_ID WHERE SYSTEM_ID = ? AND DELETE_FLAG = 0, 1003, 1007) returned net.sf.log4jdbc.PreparedStatementSpy@23e5d1
2011/01/23 08:45:56.562	[INFO ]	[main]	2. PreparedStatement.setString(1, "FAMEBL3") returned 
2011/01/23 08:45:56.625	[INFO ]	[main]	2. ResultSet.new ResultSet returned 
2011/01/23 08:45:56.625	[INFO ]	[main]	2. PreparedStatement.executeQuery() returned net.sf.log4jdbc.ResultSetSpy@c980c9
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getMetaData() returned oracle.jdbc.driver.OracleResultSetMetaData@7808b9
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.next() returned true
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getObject(SYSTEM_ID) returned FAMEBL3
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getObject(HOST) returned 192.168.1.11
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getObject(PORT) returned 59003
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getObject(REMARKS) returned テスト
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getObject(DELETE_FLAG) returned 0
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.getTimestamp(TIME_STAMP) returned 2011-01-23 08:44:04.0
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.next() returned false
2011/01/23 08:45:56.640	[INFO ]	[main]	2. ResultSet.close() returned 

sql.log
2011/01/23 08:45:57.250	[INFO ]	[main]	SELECT * FROM SERVICE WHERE DELETE_FLAG = 0 
2011/01/23 08:45:57.281	[INFO ]	[main]	SELECT * FROM SERVICE_ELEM WHERE DELETE_FLAG = 0 

sqltiming.log
2011/01/23 08:46:04.468	[INFO ]	[main]	SELECT EQUIPMENT_ID FROM EQUIPMENT WHERE PROCESS_NAME = 'SERVER' AND DELETE_FLAG = 0  {executed in 15 msec}
2011/01/23 08:46:04.484	[INFO ]	[main]	SELECT * FROM NAME_CONVERSION WHERE NAME_CONVERSION_TYPE = 'ABC' AND DELETE_FLAG = 0  {executed in 16 msec}

トラックバック
この記事のトラックバックURL
http://java6.blog117.fc2.com/tb.php/45-bb73a5a3
この記事にトラックバックする(FC2ブログユーザー)
この記事へのトラックバック