org.jdbcdslog.LogUtils Maven / Gradle / Ivy
package org.jdbcdslog;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
public class LogUtils {
static Logger logger = LoggerFactory.getLogger(LogUtils.class);
public final static String CONNECTION_ID_MDC_KEY = "jdbcdslog.connectionId";
private final static String NAMED_PARAMETERS_PREFIX = ":";
public static void handleException(Throwable e, Logger l, StringBuilder msg) throws Throwable {
if (e instanceof InvocationTargetException) {
e = ((InvocationTargetException) e).getTargetException();
}
l.error(msg.toString(), e);
throw e;
}
/**
* Append Elapsed Time to log message if it is configured to be included.
*
* @param sb
* @param elapsedTimeInNano
* @return
*/
public static StringBuilder appendElapsedTime(StringBuilder sb, long elapsedTimeInNano) {
if (ConfigurationParameters.showTime) {
sb.append("\nElapsed Time: ").append(String.format("%.9f", elapsedTimeInNano/1000000000.0)).append(" s.");
}
return sb;
}
public static String appendStackTrace(String message) {
if (ConfigurationParameters.printStackTrace) {
return appendStackTrace(new StringBuilder(message)).toString();
} else {
return message;
}
}
public static StringBuilder appendStackTrace(StringBuilder sb) {
if (ConfigurationParameters.printStackTrace) {
StackTraceElement[] stackTraces = new Throwable().getStackTrace();
int firstNonJdbcDsLogStackIndex = firstNonJdbcDsLogStackIndex(stackTraces);
if (ConfigurationParameters.printFullStackTrace) {
for (int i = firstNonJdbcDsLogStackIndex; i < stackTraces.length; ++i) {
sb.append("\nat ").append(stackTraces[i]);
}
} else if (ConfigurationParameters.printStackTracePattern.length() == 0) {
sb.append("\nat ").append(stackTraces[firstNonJdbcDsLogStackIndex]);
} else { // pattern provided
String matchPattern = ConfigurationParameters.printStackTracePattern;
for (StackTraceElement stackTraceElement : stackTraces) {
if ( stackTraceElement.getClassName().matches(matchPattern)){
sb.append("\nat ").append(stackTraceElement);
break;
}
}
}
}
return sb;
}
public static int firstNonJdbcDsLogStackIndex(StackTraceElement[] stackTraces) {
int i = 0;
for (i = 0; i < stackTraces.length; ++i) {
if ( ! stackTraces[i].getClassName().startsWith("org.jdbcdslog")) {
break;
}
}
try {
if (i > 0 && Proxy.isProxyClass(Class.forName(stackTraces[i].getClassName()))) {
++i; // skip one more level for the proxy
}
} catch (ClassNotFoundException ignore) {
}
return i;
}
public static StringBuilder createLogEntry(Method method, String sql, Map parameters, Map namedParameters) {
StringBuilder s = new StringBuilder();
if (method != null) {
s.append(method.getDeclaringClass().getName()).append(".").append(method.getName()).append(": ");
}
appendSql(s, sql, parameters, namedParameters);
return s;
}
public static void appendSql(StringBuilder s,
String sql,
Map parameters,
Map namedParameters) {
if (ConfigurationParameters.inlineQueryParams) {
if (parameters != null && !parameters.isEmpty()) {
appendSqlWithInlineIndexedParams(s, sql, parameters);
} else {
appendSqlWithInlineNamedParams(s, sql, namedParameters);
}
} else { // display separate query parameters
appendSqlWithSeparateParams(s, sql, parameters, namedParameters);
}
}
protected static void appendSqlWithSeparateParams(StringBuilder s,
String sql,
Map parameters,
Map namedParameters) {
if (sql != null) {
s.append(sql);
}
if (parameters != null && !parameters.isEmpty()) {
s.append(" parameters: ")
.append(parameters);
} else if (namedParameters != null && !namedParameters.isEmpty()){
s.append(" named parameters: ")
.append(namedParameters);
}
}
public static void appendBatchSqls(StringBuilder s,
String sql,
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy