updated logging of properties

Former-commit-id: f9e224a9c5ad0972e2f8ae0fc5850947b1e59c2f
This commit is contained in:
Jeremy Long
2014-03-03 07:01:19 -05:00
parent b48f83ff49
commit d95fa8a893

View File

@@ -22,8 +22,11 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -35,6 +38,8 @@ import java.util.logging.Logger;
*/
public final class Settings {
private static final Logger LOGGER = Logger.getLogger(Settings.class.getName());
/**
* The collection of keys used within the properties file.
*/
@@ -204,6 +209,43 @@ public final class Settings {
}
}
}
logProperties("Properties loaded", props);
}
/**
* Logs the properties. This will not log any properties that contain 'password' in the key.
*
* @param header the header to print with the log message
* @param properties the properties to log
*/
private static void logProperties(String header, Properties properties) {
if (LOGGER.isLoggable(Level.FINE)) {
final StringWriter sw = new StringWriter();
PrintWriter pw = null;
try {
pw = new PrintWriter(sw);
pw.format("%s:%n%n", header);
final Enumeration e = properties.propertyNames();
while (e.hasMoreElements()) {
final String key = (String) e.nextElement();
if (key.contains("password")) {
pw.format("%s='*****'%n", key);
} else {
final String value = properties.getProperty(key);
if (value != null) {
pw.format("%s='%s'%n", key, value);
}
}
}
pw.flush();
LOGGER.fine(sw.toString());
} finally {
if (pw != null) {
pw.close();
}
}
}
}
/**
@@ -214,6 +256,9 @@ public final class Settings {
*/
public static void setString(String key, String value) {
INSTANCE.props.setProperty(key, value);
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(String.format("Setting: %s='%s'", key, value));
}
}
/**
@@ -228,6 +273,9 @@ public final class Settings {
} else {
INSTANCE.props.setProperty(key, Boolean.FALSE.toString());
}
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.fine(String.format("Setting: %s='%b'", key, value));
}
}
/**
@@ -268,6 +316,7 @@ public final class Settings {
*/
public static void mergeProperties(InputStream stream) throws IOException {
INSTANCE.props.load(stream);
logProperties("Properties updated via merge", INSTANCE.props);
}
/**