既存のアプリケーションでのJDBCクエリとそのパラメーターのロギング





このパブリケーションでは、jdbc操作のログを既存のアプリケーションに再構築および再コンパイルせずに実装する方法について説明します。 これにより、プログラムが入力するクエリパラメータ、およびjdbcの操作に関する他の多くの側面を記録できます。



まあ、 log4jdbcをアプリケーションアセンブリに追加する機会があれば。



<dependency> <groupId>com.googlecode.log4jdbc</groupId> <artifactId>log4jdbc</artifactId> <version>1.2</version> </dependency>
      
      





また、接続を作成するときにnet.sf.log4jdbc.DriverSpyを使用します。



これが不可能な場合、aspectj-scriptingがjdbc操作のロギングに役立ちます。



hawt.io/h2およびCRaSH-sshに関する記事にあるように、SonarQubeは実験プログラムのままです。 これらの記事では、構成プロセスとaspectj-scriptingの原理についてより詳細に説明し、段階的な手順を示します。



jdbc操作をログに記録するには、jvmソナーの開始パラメーターを変更します。

sonar.web.javaAdditionalOpts = -javaagent: aspectj-scripting-1.0-agent.jar -Dorg.aspectj.weaver.loadtime.configuration = config:ファイル: h2_jdbc.xml



また、jvm aspectj-scripting agentとh2_jdbc.xml構成ファイルの可用性も必要です。



 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <configuration> <aspects> <name>com.github.igorsuhorukov.JdbcLog</name> <type>AROUND</type> <pointcut>execution(* org.apache.commons.dbcp.BasicDataSource.getConnection())</pointcut> <process> <expression> log4jdbcresultUrls = com.github.smreed.dropship.MavenClassLoader.forMavenCoordinates("com.googlecode.log4jdbc:log4jdbc:1.2").getURLs(); slf4jresultUrls = com.github.smreed.dropship.MavenClassLoader.forMavenCoordinates("org.slf4j:slf4j-simple:1.6.0").getURLs(); resultUrls = new java.net.URL[log4jdbcresultUrls.length + slf4jresultUrls.length]; System.arraycopy(log4jdbcresultUrls, 0, resultUrls, 0, log4jdbcresultUrls.length); System.arraycopy(slf4jresultUrls, 0, resultUrls, log4jdbcresultUrls.length, slf4jresultUrls.length); log4jdbcLoader = new java.net.URLClassLoader(resultUrls, Thread.currentThread().getContextClassLoader()); log4jdbcLoader.loadClass("net.sf.log4jdbc.ConnectionSpy"); currentLoader = java.lang.Thread.currentThread().getContextClassLoader(); java.lang.Thread.currentThread().setContextClassLoader(log4jdbcLoader); res = new net.sf.log4jdbc.ConnectionSpy(joinPoint.proceed()); java.lang.Thread.currentThread().setContextClassLoader(currentLoader); res; </expression> </process> </aspects> </configuration>
      
      





この側面により、common-dbcpライブラリからBasicDataSourceクラスのgetConnection()呼び出しをインターセプトし、ConnectionSpyラッパーを返してデータベースに接続できます。 同時に、ローカルリポジトリのMavenアーティファクトからクラスローダーを作成することにより、log4jdbcのクラスがアプリケーションで使用可能になります。 aspectj-scriptingは、上記で指定した構成に基づいて、com.googlecode.log4jdbc:log4jdbc:1.2およびorg.slf4j:slf4j-simple:1.6.0のアーティファクトをロードします。 これが機能するのは、jvmを起動するときに2つの追加パラメーターを渡したからです:「- javaagent 」でaspectj-scriptingエージェントを起動し、-Dorg.aspectj.weaver.loadtime.configurationで構成を渡します。 そして、Aspectjエージェントは、ロード時にアプリケーションクラスをインスツルメントしました。



log4jdbcライブラリでは、次のロガーを使用できます。





結論として、Web部分がrubyで記述され、jrubyで実行されるSonarQubeのログからいくつかの例を示します。



[jdbc.connection] 2.接続が開かれました

[jdbc.connection] 81.接続が閉じられました

[jdbc.sqltiming] select * from schema_migrations {4ミリ秒で実行}

[jdbc.audit] 7. PreparedStatement。 setString(1、 "sonar.core.id")が返されました

[jdbc.audit] 9. PreparedStatement .setTimestamp(1、2015-08-09 18:49:08.205)が返されました

[jdbc.audit] 9. PreparedStatement。 setFetchSize(200)が返されました

[jdbc.audit] 14.接続。 prepareStatement (更新メトリックセットbest_value =?、delete_historical_data =?、description =?、direction =?、domain =?、enabled =?、hidden =?、short_name =?、optimized_best_value =?、origin =?、qualitative =?、val_type =?、user_managed =?、worst_value =?where id =?)は、net.sf.log4jdbc.PreparedStatementSpy @ 6e22c0e5を返しました

[jdbc.resultset] 2. ResultSet。 getMetaData()rsMeta0を返しました:列= 1

[jdbc.resultset] 2. ResultSet。 getType()1003を返しました

[ jdbc.sqlonly ] ar.report_status = 'PENDING'であり、存在しないanalysis_reports arからar.idを選択します(arr.project_key = ar2.project_keyおよびar2.report_status = 'WORKING'であるanalysis_reports ar2から1を選択します)。 created_at asc、ar.id asc



記事で説明されている例は、スクリーンキャストで見ることができます:







そのため、jvm SonarQubeの構成を変更して、パラメーターを使用したJdbc APIアプリケーションへのすべての呼び出しがログファイルに記録されるようにすることができました。 この出版物がお役に立てば幸いです。また、既存のJavaプログラムにアスペクト指向プログラミングを適用する他の方法が見つかることを願っています



All Articles