众所周知, p6spy打印出来的日志是一行很长很长的内容, 很不容易查看, 牛B的p6spy为什么就不能想hibernate那样有format_sql的功能?
竟然没有, 我只好自己动手写一个日志输出类, 这里我只举出console日志输出的例子:
1 package com.techmango.higenericdao.utils; 2 3 import com.p6spy.engine.spy.appender.StdoutLogger; 4 5 public class P6SpyStdoutLogger extends StdoutLogger { 6 public void logText(String text) { 7 StringBuilder sb = new StringBuilder(); 8 //匹配到最后一个|作为分隔符 9 String[] arrString = text.split("\\|(?![^\\|]*\\|)"); 10 if(arrString.length > 1) { 11 sb.append(arrString[0]); 12 //去最后一段语句做替换进行格式化 13 arrString[1] = arrString[1].replaceAll(", ", ",\r\n\t"); 14 arrString[1] = arrString[1].replaceAll(" values ", ",\r\nvalues\r\n\t"); 15 arrString[1] = arrString[1].replaceAll(" from ", "\r\nfrom\r\n\t"); 16 arrString[1] = arrString[1].replaceAll(" where ", "\r\nwhere\r\n\t"); 17 arrString[1] = arrString[1].replaceAll(" order by ", "\r\norder by\r\n\t"); 18 arrString[1] = arrString[1].replaceAll(" group by ", "\r\ngroup by\r\n\t"); 19 sb.append("\r\n"); 20 sb.append(arrString[1]); 21 qlog.println(sb.toString()); 22 } 23 else { 24 qlog.println(text); 25 } 26 arrString = null; 27 } 28 }
把这个类P6SpyStdoutLogger配置到p6spy.properties文件里,
打印结果如下:
12-02 12:37:44,379|46|statement|connection 3|insert into gas.news (title, content, icon, img, time, type, subType, newsRange, keyword) values (?, ?, ?, ?, ?, ?, ?, ?, ?) insert into gas.news (title, content, icon, img, time, type, subType, newsRange, keyword), values ('实实在在地', 'hibernate 新闻', NULL, NULL, '02-十二月-15', '1', NULL, '1', NULL) 12-02 12:37:47,488|55|rollback|connection 3||
怎么样, 这样的内容是不是容易看了呢?