Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package kzht.gm.p6spy.extension.logging; import com.p6spy.engine.common.P6SpyProperties; import com.p6spy.engine.logging.appender.Log4jLogger; import java.util.ArrayList; import java.util.List; import java.util.Properties; import lombok.Data; import org.apache.commons.lang3.StringUtils; /** * P6spy ? Log4j <br /> * * {@link com.p6spy.engine.logging.appender.Log4jLogger} ?? * ??????????????????? * * @author kazuhito * */ @Data public class Log4jLoggerEx extends Log4jLogger { /** * */ private static final String DEFAULT_FORMAT = "%e[ms] %s"; /** * ? */ private List<String> excludeCategories; /** * */ private List<String> includeCategories; /** * PreparedStatement ??? */ private boolean preparedEnabled = true; /** * Statement ??? */ private boolean sqlEnabled = true; /** * DbUnit ??????? <br /> * ????????? */ private boolean dbunitEnabled = true; /** * <br /> * * ?????????????? * <ul> * <li>%cat : </li> * <li>%cid : ? ID</li> * <li>%e : ? (msec)</li> * <li>%s : </li> * </ul> */ private String format; /** * */ public Log4jLoggerEx() { P6SpyProperties p6SpyProperties = new P6SpyProperties(); Properties properties = p6SpyProperties.forceReadProperties(); excludeCategories = extractCategory(properties.getProperty("excludecategories")); includeCategories = extractCategory(properties.getProperty("includecategories")); preparedEnabled = extractBooleanValue(properties.getProperty("commons.enable.preparedstatement")); sqlEnabled = extractBooleanValue(properties.getProperty("commons.enable.sqlstatement")); format = extractFormat(properties.getProperty("commons.format")); dbunitEnabled = extractBooleanValue(properties.getProperty("commons.enable.dbunitstatement")); } /** * ????<br /> * * @param connectionId ? ID * @param now ? (???) * @param elapsed ? (msec) * @param category * @param prepared * @param sql SQL */ @Override public void logSQL(int connectionId, String now, long elapsed, String category, String prepared, String sql) { if (shouldWrite(category)) { if (preparedEnabled && StringUtils.isNotBlank(prepared)) { super.logText(format(connectionId, now, elapsed, category, prepared)); } if (sqlEnabled && StringUtils.isNotBlank(sql)) { super.logText(format(connectionId, now, elapsed, category, sql)); } } } /** * ???? * * ?????... * * @param connectionId ? ID * @param now ? (???) * @param elapsed ? (msec) * @param category * @param statement * @return ?? */ private String format(int connectionId, String now, long elapsed, String category, String statement) { return format.replace("%cid", String.valueOf(connectionId)).replace("%e", String.valueOf(elapsed)) .replace("%cat", category).replace("%s", statement); } /** * ??? (DbUnit or ) ??????<br /> * * @param category * @return ??? <code>true</code>, ???????? <code>false</code> ?? */ private boolean shouldWrite(String category) { // P6spy ??? exclude ??????? if (excludeCategories.contains(category)) { return false; } if (includeCategories.isEmpty() || includeCategories.contains(category)) { if (dbunitEnabled) { return true; } else { return isApplicationQuery(); } } else { return false; } } /** * (Hibernate) ??????????<br /> * ???? : "SessionImpl" ???????????? * * @return ????? <code>true</code>, ???????? <code>false</code> * ?? */ private boolean isApplicationQuery() { for (StackTraceElement ste : new Throwable().getStackTrace()) { if (ste.toString().indexOf("SessionImpl") >= 0) { return true; } } return false; } private List<String> extractCategory(String categories) { List<String> categoryList = new ArrayList<String>(); if (StringUtils.isNotBlank(categories)) { for (String category : categories.trim().split(",")) { categoryList.add(category.trim()); } } return categoryList; } private boolean extractBooleanValue(String value) { if (StringUtils.isBlank(value)) { return true; } else { return Boolean.valueOf(value).booleanValue(); } } private String extractFormat(String format) { if (StringUtils.isBlank(format)) { return DEFAULT_FORMAT; } else { return format; } } }