public static DataSource createLoggingDS(DataSource ds, Log log) { return (DataSource)Proxy.newProxyInstance(ds.getClass().getClassLoader(), new Class[] {DataSource.class}, new LoggingInterceptor<DataSource>(ds,log)); } }
public DataSource getDataSource() { //return __logSql.isDebugEnabled() ? new LoggingDataSourceWrapper(_datasource, __logSql) : _datasource; return __logSql.isDebugEnabled() ? LoggingInterceptor.createLoggingDS(_datasource, __logSql) : _datasource; }
private void print(Method method, Object[] args) { if (shouldPrint()) { print("prepareStatement: " + args[0]); } else if ("prepareCall".equals(method.getName())) { print("prepareCall: " + args[0]); } else if ("close".equals(method.getName())) { print("close()"); } else if ("commit".equals(method.getName())) { print("commit()"); } else if ("rollback".equals(method.getName())) { print("rollback()"); } else if ("setTransactionIsolation".equals(method.getName())) { print("Set isolation level to " + args[0]); print(method.getName() + "(" + args[0] + "), " + getParams()); } else if (method.getName().startsWith("execute")) { print(method.getName() + ", " + getParams()); } else if ("clearParameters".equals(method.getName())) { _paramsByIdx.clear();
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (method.getDeclaringClass() == DataSource.class && "getConnection".equals(method.getName())) { Connection conn = (Connection)method.invoke(_delegate, args); print("getConnection (tx=" + conn.getTransactionIsolation() + ")"); return Proxy.newProxyInstance(_delegate.getClass().getClassLoader(), new Class[] {Connection.class}, new LoggingInterceptor<Connection>(conn, _log)); } else if (method.getDeclaringClass() == Connection.class && Statement.class.isAssignableFrom(method.getReturnType())) { Statement stmt = (Statement)method.invoke(_delegate, args); print(method, args); return Proxy.newProxyInstance(_delegate.getClass().getClassLoader(), new Class[] {method.getReturnType()}, new LoggingInterceptor<Statement>(stmt, _log)); } else { print(method, args); return method.invoke(_delegate, args); } } catch (InvocationTargetException e) { throw e.getTargetException(); } }