How to configure p6spy with datasource in Spring + tomcat 7 +MySQL

帅比萌擦擦* 提交于 2019-12-11 07:29:48

问题


I have been trying to configure p3spy logging system on my maven based spring project.But each time when i try to configure p3spy differently. i get always errors.

Here is my following settings

I placed p6spy-2.3.0.jar, spy.properties and mysql-connector-java-5.1.17.jar inside the tomcat/lib directory.

tomcat/context.xml

<Resource auth="Container" 
            driverClassName="com.mysql.jdbc.Driver" 
            name="jdbc/myDataSource"
            type="javax.sql.DataSource"
            url="jdbc:p6spy:mysql://localhost:3306/coexi?useUnicode=true&amp;characterEncoding=utf-8" 
            username="root"
            password="" 
            maxActive="100" 
            maxIdle="50" />

while using this above settings.. it gives me this following error..

Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:p6spy:mysql://localhost:3306/coexi?useUnicode=true&characterEncoding=utf-8' java.sql.SQLException: No suitable driver

Here i also tried to replacce few settings inside this resources.

<Resource auth="Container" 
            driverClassName="com.p6spy.engine.spy.P6SpyDriver" 
            name="jdbc/myDataSource"
            type="javax.sql.DataSource"
            url="jdbc:p6spy:mysql://localhost:3306/coexi?useUnicode=true&amp;characterEncoding=utf-8" 
            username="root"
            password="" 
            maxActive="100" 
            maxIdle="50" />

And by using this code, getting this following error:

Cannot instantiate com.p6spy.engine.logging.appender.Log4jLogger, even on second attempt. java.lang.ClassNotFoundException: com.p6spy.engine.logging.appender.Log4jLogger

main/config/PersistanceConfiguration

@Bean
public DataSource dataSource() {
    Context ctx;
    try {
        ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:comp/env/jdbc/myDataSource");
    } catch (NamingException e) {
        LOGGER.info("Error to find the database : \"java:comp/env/jdbc/myDataSource\".");
    }
    return null;

tomcat/lib/spy.properties

driverlist=com.mysql.jdbc.Driver

deregisterdrivers=true

includecategories=info, statement
excludecategories=info,debug,result,batch
logfile     = /Users/coexi/Documents/spy.log

append=true

log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=p6spy - %m%n

log4j.logger.p6spy=INFO,STDOUT

/main/resources/logback.xml

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<appender name="GEN_FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${catalina.base}/logs/general.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- daily rollover -->
        <fileNamePattern>gen_%d{yyyy-MM-dd}.log</fileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
</appender>

<category name="com.p6spy">
    <priority value="INFO" />
</category>

<logger name="org.hibernate.SQL" additivity="false" >
    <level value="DEBUG" />    
    <appender-ref ref="SQLROLLINGFILE" />
</logger>

<logger name="org.hibernate.type" additivity="false" >
    <level value="TRACE" />
    <appender-ref ref="SQLROLLINGFILE" />
</logger>

<root level="info">
    <appender-ref ref="STDOUT" />
</root>
<root level="error">
    <appender-ref ref="ERRORS_FILE"/>
</root>

main/resources/log4j.xml.backend

<?xml version="1.0" encoding="UTF-8" ?>

http://jakarta.apache.org/log4j/'>

<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="INFO" />
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
    </layout>
</appender>

 <category name="com.p6spy">
    <priority value="INFO" />
  </category>


<logger name="org.hibernate.SQL" additivity="false" >   
 <level value="DEBUG" />    
 <appender-ref ref="SQLROLLINGFILE" />
</logger>

<logger name="org.hibernate.type" additivity="false" >
 <level value="TRACE" />
 <appender-ref ref="SQLROLLINGFILE" />
</logger>

<root>
    <level value="INFO" />
    <appender-ref ref="console" />
</root>

Fullstack error.

http://pastebin.com/3LpgtPtr

回答1:


You are using the wrong class name for the appender in spy.properties. The log4j logger was removed in 2.X, in favor of a slf4j logger. The correct class name is com.p6spy.engine.spy.appender.Slf4JLogger.

See the docs for further details.

BTW - The spy.properties that you are using is for P6Spy 1.3. You should discard that file and replace it with the 2.X version (after editing to include the appropriate changes for your requirements).




回答2:


I had similar problem with configuring p6spy on spring + hibernate + mysql + slf4j + log4j My aim was to log full sql with params to ide console instead hibernate query logging.

Inserting :p6spy: into db config url didn't help me(i got error during starting app with can't load db driver P6DataSource on the classpath) and i found workaround:

you can add something similar to this into your applicationContext.xml or create beans using @Profile(...) and @Configuration spring annotations:

in applicationContext.xml add this:

<beans profile="dev"> 
   <jee:jndi-lookup id="dataSourceReal" jndi-name="db_jndi_name" resource-ref="true"/>
    <bean name="dataSource" class="com.p6spy.engine.spy.P6DataSource">
        <constructor-arg name="delegate" ref="dataSourceReal"/>
    </bean>        
</bean>

<beans profile="!dev">
     <jee:jndi-lookup id="dataSource" jndi-name="db_jndi_name" resource-ref="true"/>  
 </beans>

all db urls and others properties are old, i just added maven dependency for p6spy (current last version for me is 3.0.0-alpha-1):

<dependency>
        <groupId>p6spy</groupId>
        <artifactId>p6spy</artifactId>
        <version>3.0.0-alpha-1</version>
</dependency>

added spy.properties with following properties:

realdriver=com.mysql.jdbc.Driver
appender=com.p6spy.engine.spy.appender.Slf4JLogger

and all works fine, but i had single line logging that i change with my formatter based on hibernate fomatter like this:

package com.my.package.logging;

import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.hibernate.engine.jdbc.internal.BasicFormatterImpl;
import org.hibernate.engine.jdbc.internal.Formatter;

public class P6spyHibernateSQLFormatter implements MessageFormattingStrategy {
    public static Formatter formatter = new BasicFormatterImpl();

    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql) {
        return "#" + now + " | took " + elapsed + "ms | " + category + " | connection " + connectionId
                + "\n" + "Hibernate query with params:" + formatter.format(sql)  +";";
    }
}

and added formatter to spy.properties:

logMessageFormat=com.my.package.logging.P6spyHibernateSQLFormatter

and also i disable hibernate logging... All works fine for me right now!



来源:https://stackoverflow.com/questions/37726599/how-to-configure-p6spy-with-datasource-in-spring-tomcat-7-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!