• 格式化p6spy的输出日志


    众所周知, 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(", ", ",
    	");
    14             arrString[1] = arrString[1].replaceAll(" values ", ",
    values
    	");
    15             arrString[1] = arrString[1].replaceAll(" from ", "
    from
    	");
    16             arrString[1] = arrString[1].replaceAll(" where ", "
    where
    	");
    17             arrString[1] = arrString[1].replaceAll(" order by ", "
    order by
    	");
    18             arrString[1] = arrString[1].replaceAll(" group by ", "
    group by
    	");
    19             sb.append("
    ");
    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||

    怎么样, 这样的内容是不是容易看了呢?

  • 相关阅读:
    Python 文件批量改名
    解决 unity 生成 android apk read Resources
    IIS 重定向 自动追加 eurl.axd 后缀
    多线程
    zookeeper面试
    线程之间的通信(thread signal)
    软考高项之计算题成本类计算
    PowerDesigner PDM 复制comment到name
    软考高项之计算题进度类
    全面理解Java内存模型
  • 原文地址:https://www.cnblogs.com/plain-heart/p/5012728.html
Copyright © 2020-2023  润新知