Skip to content

Commit

Permalink
#426: Make the format for boolean parameters configurable (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
OBreidenbach authored and typekpb committed Nov 7, 2017
1 parent ea4a668 commit fa6bcde
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/assembly/individualFiles/spy.properties
Expand Up @@ -115,6 +115,10 @@
# (default is dd-MMM-yy)
#databaseDialectDateFormat=dd-MMM-yy

# format that is used for logging booleans, possible values: boolean, numeric
# (default is boolean)
# databaseDialectBooleanFormat=boolean

# whether to expose options via JMX or not
# (default is true)
#jmx=true
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/p6spy/engine/common/Value.java
Expand Up @@ -27,7 +27,7 @@
* Value holder of the data passed to DB as well as of those retrieved capable
* of binary data logging depending on the configuration property
* {@code excludebinary}.
*
*
* @author Peter Butkovic
*
*/
Expand All @@ -45,7 +45,7 @@ public Value(Object valueToSet) {
this();
this.value = valueToSet;
}

public Value() {
}

Expand Down Expand Up @@ -73,7 +73,7 @@ public String toString() {
* set.</li>
* <li>for other types string representation is simply returned.</li>
* </ul>
*
*
* @param value
* @return
*/
Expand All @@ -85,6 +85,12 @@ public String convertToString(Object value) {

if (value instanceof java.util.Date) {
result = new SimpleDateFormat(P6SpyOptions.getActiveInstance().getDatabaseDialectDateFormat()).format(value);
} else if (value instanceof Boolean) {
if ("numeric".equals(P6SpyOptions.getActiveInstance().getDatabaseDialectBooleanFormat())) {
result = Boolean.FALSE.equals(value) ? "0" : "1";
} else {
result = value.toString();
}
} else if (value instanceof byte[]) {
if (P6LogOptions.getActiveInstance().getExcludebinary()) {
result = "[binary]";
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/p6spy/engine/spy/P6SpyOptions.java
Expand Up @@ -51,6 +51,7 @@ public class P6SpyOptions extends StandardMBean implements P6SpyLoadableOptions
public static final String REALDATASOURCEPROPERTIES = "realdatasourceproperties";
public static final String CUSTOM_LOG_MESSAGE_FORMAT = "customLogMessageFormat";
public static final String DATABASE_DIALECT_DATE_FORMAT = "databaseDialectDateFormat";
public static final String DATABASE_DIALECT_BOOLEAN_FORMAT = "databaseDialectBooleanFormat";
public static final String JMX = "jmx";
public static final String JMX_PREFIX = "jmxPrefix";

Expand All @@ -75,6 +76,7 @@ public class P6SpyOptions extends StandardMBean implements P6SpyLoadableOptions
defaults.put(RELOADPROPERTIES, Boolean.FALSE.toString());
defaults.put(RELOADPROPERTIESINTERVAL, Long.toString(60));
defaults.put(DATABASE_DIALECT_DATE_FORMAT, "dd-MMM-yy");
defaults.put(DATABASE_DIALECT_BOOLEAN_FORMAT, "boolean");
defaults.put(CUSTOM_LOG_MESSAGE_FORMAT, String.format("%s|%s|%s|connection%s|%s",
CustomLineFormat.CURRENT_TIME, CustomLineFormat.EXECUTION_TIME, CustomLineFormat.CATEGORY,
CustomLineFormat.CONNECTION_ID, CustomLineFormat.SQL_SINGLE_LINE));
Expand Down Expand Up @@ -109,6 +111,7 @@ public void load(Map<String, String> options) {
setRealDataSourceClass(options.get(REALDATASOURCECLASS));
setRealDataSourceProperties(options.get(REALDATASOURCEPROPERTIES));
setDatabaseDialectDateFormat(options.get(DATABASE_DIALECT_DATE_FORMAT));
setDatabaseDialectBooleanFormat(options.get(DATABASE_DIALECT_BOOLEAN_FORMAT));
setCustomLogMessageFormat(options.get(CUSTOM_LOG_MESSAGE_FORMAT));
setJmx(options.get(JMX));
setJmxPrefix(options.get(JMX_PREFIX));
Expand Down Expand Up @@ -305,6 +308,26 @@ public void setDatabaseDialectDateFormat(String databaseDialectDateFormat) {
optionsRepository.set(String.class, DATABASE_DIALECT_DATE_FORMAT, databaseDialectDateFormat);
}

/**
* Returns the databaseDialectBooleanFormat.
*
* @return String
*/
@Override
public String getDatabaseDialectBooleanFormat() {
return optionsRepository.get(String.class, DATABASE_DIALECT_BOOLEAN_FORMAT);
}

/**
* Sets the databaseDialectDateFormat.
*
* @param databaseDialectBooleanFormat The databaseDialectBooleanFormat to set
*/
@Override
public void setDatabaseDialectBooleanFormat(String databaseDialectBooleanFormat) {
optionsRepository.set(String.class, DATABASE_DIALECT_BOOLEAN_FORMAT, databaseDialectBooleanFormat);
}

/**
* Returns the customLogMessageFormat.
*
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/p6spy/engine/spy/P6SpyOptionsMBean.java
Expand Up @@ -91,6 +91,10 @@ public interface P6SpyOptionsMBean {

void setDatabaseDialectDateFormat(String databaseDialectDateFormat);

String getDatabaseDialectBooleanFormat();

void setDatabaseDialectBooleanFormat(String databaseDialectBooleanFormat);

String getCustomLogMessageFormat();

void setCustomLogMessageFormat(String customLogMessageFormat);
Expand Down
Expand Up @@ -143,6 +143,7 @@ public void testP6SpyOptionDefaults() {
Assert.assertNull(opts.getRealDataSource());
Assert.assertNull(opts.getRealDataSourceClass());
Assert.assertEquals("dd-MMM-yy", opts.getDatabaseDialectDateFormat());
Assert.assertEquals("boolean", opts.getDatabaseDialectBooleanFormat());
Assert.assertEquals(String.format("%s|%s|%s|connection%s|%s",
CustomLineFormat.CURRENT_TIME, CustomLineFormat.EXECUTION_TIME, CustomLineFormat.CATEGORY,
CustomLineFormat.CONNECTION_ID, CustomLineFormat.SQL_SINGLE_LINE),
Expand Down

0 comments on commit fa6bcde

Please sign in to comment.