log4jdbc之sql执行时间日志记录原理解析
2017-05-11 13:49
316 查看
log4jdbc可以用来记录sql执行日志,该文就我们常使用的sql执行信息日志(jdbc.sqltiming )原理进行分析:
<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<scope>runtime</scope>
</dependency>
我们经常看到的sql执行时间信息:
13:25:31.736 [1659326468@qtp-1849186564-196] INFO jdbc.sqltiming - select 1
{executed in 0 msec}
就是在StatementSpy类中打印出来的,现在我们看下该类的定义:
public class StatementSpy implements Statement, Spy
{
protected final SpyLogDelegator log;
/**
* 创建statement的connection类.
*/
protected ConnectionSpy connectionSpy;
/**
* The real statement that this StatementSpy wraps.
*/
protected Statement realStatement;
//执行sql的方法
public boolean execute(String sql, String[] columnNames) throws SQLException
{
String methodCall = "execute(" + sql + ", " + columnNames + ")";
reportStatementSql(sql, methodCall);
long tstart = System.currentTimeMillis();
try
{
//调用Statement类的execute方法执行sql语句
boolean result = realStatement.execute(sql, columnNames);
//打印sql执行时间信息
reportStatementSqlTiming(System.currentTimeMillis() - tstart, sql, methodCall);
return reportReturn(methodCall, result);
}
catch (SQLException s)
{
reportException(methodCall, s, sql, System.currentTimeMillis() - tstart);
throw s;
}
}
使用步骤:
1.需要在pom文件添加依赖:<dependency>
<groupId>com.googlecode.log4jdbc</groupId>
<artifactId>log4jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<!--log4jdbc使用slf4j用来记录日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency>
<!--底层使用logback记录日志--><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><scope>runtime</scope></dependency>
工作原理:
1.下面看下log4jdbc中的相关类:我们经常看到的sql执行时间信息:
13:25:31.736 [1659326468@qtp-1849186564-196] INFO jdbc.sqltiming - select 1
{executed in 0 msec}
就是在StatementSpy类中打印出来的,现在我们看下该类的定义:
public class StatementSpy implements Statement, Spy
{
protected final SpyLogDelegator log;
/**
* 创建statement的connection类.
*/
protected ConnectionSpy connectionSpy;
/**
* The real statement that this StatementSpy wraps.
*/
protected Statement realStatement;
//执行sql的方法
public boolean execute(String sql, String[] columnNames) throws SQLException
{
String methodCall = "execute(" + sql + ", " + columnNames + ")";
reportStatementSql(sql, methodCall);
long tstart = System.currentTimeMillis();
try
{
//调用Statement类的execute方法执行sql语句
boolean result = realStatement.execute(sql, columnNames);
//打印sql执行时间信息
reportStatementSqlTiming(System.currentTimeMillis() - tstart, sql, methodCall);
return reportReturn(methodCall, result);
}
catch (SQLException s)
{
reportException(methodCall, s, sql, System.currentTimeMillis() - tstart);
throw s;
}
}
相关文章推荐
- 启用mysql日志记录执行过的sql并且开启慢查询记录所有超过慢查询时间的SQL以及未使用索引SQL
- 解析Oracle数据扫描 Oracle SQL查询优化 提高局部范围数据扫描执行性能的原理
- 好记性不如烂笔头14-使用log4jdbc显示完整SQL语句和执行时间
- Linux history 命令记录加执行时间戳以及记录到日志
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- log4jdbc记录全部SQL日志
- EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
- log4jdbc记录全部SQL日志
- SQL语句执行流程与顺序原理解析
- SQL语句执行流程与顺序原理解析
- 步步为营VS 2008 + .NET 3.5(11) - DLINQ(LINQ to SQL)之大数据量分页、延迟执行和日志记录
- servlet 的filter,日志文件记录执行时间
- log4jdbc记录全部SQL日志
- mysql记录sql执行时间
- MySQL日志记录(2)SQL执行记录
- EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
- Postgres log_duration设置记录SQL执行时间