logback 打印spring jdbcTemplate SQL日志

背景:

在spring框架里面配置logback打印出SQL日志包括参数值。

maven pom.xml配置

<!-- Logger -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.0.7</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.6.4</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.6.4</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>jcl-over-slf4j</artifactId>
			<version>1.6.4</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.16</version>
			<scope>runtime</scope>
		</dependency>

logback.xml

<!--JdbcTemplate的日志输出器 -->
	<logger name="org.springframework.jdbc.core.StatementCreatorUtils"
		additivity="false" level="TRACE">
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="org.springframework.jdbc.core" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="java.sql.Connection" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="java.sql.Statement" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="java.sql.PreparedStatement" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
	</logger>
	<logger name="java.sql.ResultSet" additivity="true">
		<level value="DEBUG" />
		<appender-ref ref="STDOUT" />
	</logger>

输出日志

2014-10-03 14:09:47.442 [1357862146@qtp-821556544-0] DEBUG o.s.jdbc.core.JdbcTemplate -
				Executing prepared SQL query
2014-10-03 14:09:47.442 [1357862146@qtp-821556544-0] DEBUG o.s.jdbc.core.JdbcTemplate -
				Executing prepared SQL statement [SELECT id, game_type, game_city, game_logo_name, game_logo_uri, game_grab_logo_distance, game_hide_time, game_hide_time_invalid, game_start_date, game_end_date, game_start_time, game_end_time, game_center_longitude, game_center_latitude, game_center_address, game_center_radius, game_logo_put_in_point_longitude, game_logo_put_in_point_latitude, game_logo_display_radius, game_rob_gold_times, game_max_move_speed, game_max_get_speed, game_share_text_template, created_time, last_mod_time, operator FROM hunting_game_info_test  where CONVERT(game_city USING utf8) COLLATE utf8_unicode_ci = ? and game_start_time <= ? and game_end_time >= ? order by game_start_time desc ]
2014-10-03 14:09:47.704 [1357862146@qtp-821556544-0] TRACE o.s.jdbc.core.StatementCreatorUtils -
				Setting SQL statement parameter value: column index 1, parameter value [北京], value class [java.lang.String], SQL type unknown
2014-10-03 14:09:47.705 [1357862146@qtp-821556544-0] TRACE o.s.jdbc.core.StatementCreatorUtils -
				Setting SQL statement parameter value: column index 2, parameter value [2014-10-03 14:09:47], value class [java.lang.String], SQL type unknown
2014-10-03 14:09:47.706 [1357862146@qtp-821556544-0] TRACE o.s.jdbc.core.StatementCreatorUtils -
				Setting SQL statement parameter value: column index 3, parameter value [2014-10-03 14:09:47], value class [java.lang.String], SQL type unknown
2014-10-03 14:09:47.713 [1357862146@qtp-821556544-0] DEBUG o.s.jdbc.core.JdbcTemplate -
				Executing prepared SQL query
2014-10-03 14:09:47.714 [1357862146@qtp-821556544-0] DEBUG o.s.jdbc.core.JdbcTemplate -
				Executing prepared SQL statement [SELECT id, game_type, game_city, game_logo_name, game_logo_uri, game_grab_logo_distance, game_hide_time, game_hide_time_invalid, game_start_date, game_end_date, game_start_time, game_end_time, game_center_longitude, game_center_latitude, game_center_address, game_center_radius, game_logo_put_in_point_longitude, game_logo_put_in_point_latitude, game_logo_display_radius, game_rob_gold_times, game_max_move_speed, game_max_get_speed, game_share_text_template, created_time, last_mod_time, operator FROM hunting_game_info_test  where CONVERT(game_city USING utf8) COLLATE utf8_unicode_ci = ? and created_time >= ? order by created_time asc limit 1; ]
2014-10-03 14:09:47.715 [1357862146@qtp-821556544-0] TRACE o.s.jdbc.core.StatementCreatorUtils -
				Setting SQL statement parameter value: column index 1, parameter value [北京], value class [java.lang.String], SQL type unknown
2014-10-03 14:09:47.715 [1357862146@qtp-821556544-0] TRACE o.s.jdbc.core.StatementCreatorUtils -
				Setting SQL statement parameter value: column index 2, parameter value [2014-10-03 14:09:47], value class [java.lang.String], SQL type unknown


你可能感兴趣的:(logback 打印spring jdbcTemplate SQL日志)