Up_Router
Up_Router

Reputation: 103

p6spy log vs. Hibernate log

p6spy is very useful for debuging hibernate query, but, there is any way to format query with the same logic of Hibernate ? :

<property name="hibernate.format_sql" value="true" />

p6spy log example : p6spy - 1339663561390|15|0|statement|select personne0_.id as id5_,personne0_.dateNaissance as dateNais2_5_, personne0_.nom as nom5_, personne0_.prenom as prenom5_, personne0_.type as type5_ from Personne personne0_ where personne0_.nom=?|select personne0_.id as id5_, personne0_.dateNaissance as dateNais2_5_, personne0_.nom as nom5_, personne0_.prenom as prenom5_, personne0_.type as type5_ from Personne personne0_ where personne0_.nom='example'

Hibernate log example :

Hibernate: 
    insert 
    into
        TABLE
        (COLUMN_1, COLUMN_2) 
    values
        (?, ?)

I think the hibernate format is more readable, and i want something like this with p6spy.

Thank you.

Upvotes: 2

Views: 876

Answers (1)

quintonm
quintonm

Reputation: 848

P6Spy captures the SQL statement and logs it "as is". It does not try to do any sort of formatting at all. However, P6Spy can be easily extended to have the behavior that you are looking for. How this works varies between version 1.3 and 2.0 (easier in 2.0).

1.3 - Subclass whichever logger you are currently using and override the logSQL(...) method. For example, if you are using the FileLogger, your subclass might look something like the example below. Once you have the compiled class on the classpath, just update spy.properties to use the new logger implementation.

public class MyLogger extends FileLogger {
  public void logSQL(int connectionId, String now, long elapsed, String category, String prepared, String sql) {
    super.logSQL(connectionId,now,elapsed,category,reformat(prepared),reformat(sql));
  }
  private String reformat(final String sql) {
    // formatting logic goes here
  }
}

2.0 - In 2.0, we added a new strategy interface to handle the log message formatting. This is about the same amount of work as providing a new logger but it will be used by all loggers. You just need to provide a class that implements the MessageFormattingStrategy interface. To make p6spy use the new strategy, you just configure it in spy.properties or set a system property. See the configuration docs for specifics. For an example, take a look at MultiLineFormat.

BTW - If you do create a new formatting strategy and would like to share it, please send us a pull request. P6Spy is now being maintained on GitHub.

Upvotes: 2

Related Questions