获取从中触发SQL查询的程序包的名称? [英] Get the name of the package from which a SQL query was fired?

查看:100
本文介绍了获取从中触发SQL查询的程序包的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将jdbcdslog与webapp中的log4j日志记录引擎一起使用.我能够记录执行的SQL查询. 我想知道如何获取触发查询的程序包的名称.这样我就可以分离出Log信息,并仅通过查看日志而不是通过代码来知道查询凸轮的位置.

I am using jdbcdslog with log4j logging engine in a webapp. I am able to log SQL queries that are executed. I want to know how to get the name of the package from which the query was fired. So that I can separate out the Log information, and know where a query cam from just by looking at the log, rather than going through the code.

我试图在Log4j的PatternLayout中使用%C参数来获取包名称,如所述

I tried to use the %C parameter in PatternLayout of Log4j to get the package name as mentioned here

但是我得到的输出是

2013-07-30-main--INFO -org.jdbcdslog.StatementLogger:java.sql.Statement.executeQuery: SELECT id, first, last, age FROM Employees;

如您所见,SQL查询已记录下来,但是它给出的类是org.jdbcdslog.StatementLogger.这不是所需要的,因为我想要从中触发查询的类的名称.

As you can see, the SQL query is logged, but the class it gave out is org.jdbcdslog.StatementLogger. Which is not what is desired, Since I want the name of the class the query was fired from.

我正在Log4j.properties文件中使用以下设置

I am using the following setting in Log4j.properties file

log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd}-%t-%x-%-5p-%C:%m%n

发生这种情况是因为jdbcdslog将连接包装到JDBC驱动程序. 我想知道如何获取触发查询的类的名称.

It happens because jdbcdslog wraps the connection to the JDBC driver. I want to know how to get the name of the class where the query was fired from.

推荐答案

来自 http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html 我看不到任何可以为原始方法指定变量的地方.同样查看log4j传递的事件看起来也不像是沿着事件链传递信息.(

From the http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html I cant see any place where you can specify a variable for originating method. Also looking through the events log4j passes around it doesnt look like that information gets passed down the chain of events.(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/spi/LoggingEvent.html).

但是,如果您确实需要此功能,则可以采用一些解决方法,包括实现自己的DataSourceProxyBase.我从其下载页面下载了jdbcdslog源,看起来您可以扩展和覆盖DataSourceProxyBase类中的某些方法,并添加自己的记录器.然后指向您的设置以使用该数据源代理.

However if you really need this functionality you could probably pull off some workaround involving implementing your own DataSourceProxyBase. I downloaded the jdbcdslog source from their download page, It looks like you can extend and override some methods from the DataSourceProxyBase class and add in your own logger. Then point your setup to use that data source proxy.

<bean id="dataSourceActual" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/jdbc/MyDatasource</value>
    </property>
</bean>

<bean id="dataSource" class="com.me.MySoureConnectionPoolDataSourceProxy">
    <property name="targetDSDirect" ref="dataSourceActual" />
</bean>

如果您下载源代码,则可以开始遵循他们的程序,然后查看要实现所需的内容而需要覆盖的内容.一个好的起点可能是类PooledConnectionLoggingProxy

If you download the source you can start following their program around and see what you would need to override to implement what you need. A good starting place might be the class PooledConnectionLoggingProxy

这篇关于获取从中触发SQL查询的程序包的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆