Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE and add logException config param #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/jdbcdslog/ConfigurationParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class ConfigurationParameters {
static boolean logDetailAfterStatement = true;
static boolean logAddBatchDetail = true;
static boolean logAddBatch = true;
static boolean logExecuteBatchDetail =true;
static boolean logExecuteBatchDetail = true;
static boolean logExceptions = true;

static {
ClassLoader loader = ConfigurationParameters.class.getClassLoader();
Expand All @@ -47,6 +48,7 @@ public class ConfigurationParameters {
initLogAddBatch();
initLogAddBatchDetail();
initLogExecuteBatchDetail();
initLogExceptions();

} catch (Exception e) {
logger.error(e.getMessage(), e);
Expand Down Expand Up @@ -143,5 +145,8 @@ private static void initLogExecuteBatchDetail() {
logExecuteBatchDetail = "true".equalsIgnoreCase(props.getProperty("jdbcdslog.logExecuteBatchDetail", "false"));
}

private static void initLogExceptions() {
logExceptions = "true".equalsIgnoreCase(props.getProperty("jdbcdslog.logExceptions", "false"));
}
/* init parameters end. */
}
18 changes: 6 additions & 12 deletions src/main/java/org/jdbcdslog/LogUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,10 @@
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 = ":";
Expand All @@ -27,16 +24,14 @@ public static void handleException(Throwable e, Logger l, StringBuilder msg) thr
e = ((InvocationTargetException) e).getTargetException();
}

l.error(msg.toString(), e);
if (ConfigurationParameters.logExceptions) {
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) {
Expand Down Expand Up @@ -80,7 +75,7 @@ public static StringBuilder appendStackTrace(StringBuilder sb) {
}

public static int firstNonJdbcDsLogStackIndex(StackTraceElement[] stackTraces) {
int i = 0;
int i;
for (i = 0; i < stackTraces.length; ++i) {
if ( ! stackTraces[i].getClassName().startsWith("org.jdbcdslog")) {
break;
Expand Down Expand Up @@ -270,7 +265,6 @@ protected static String replaceEach(String text, String[] searchList, String[] r
// index on index that the match was found
int textIndex = -1;
int replaceIndex = -1;
int tempIndex = -1;

// index of replace array that will replace the search string found
// NOTE: logic duplicated below START
Expand All @@ -279,7 +273,7 @@ protected static String replaceEach(String text, String[] searchList, String[] r
searchList[i].length() == 0 || replacementList[i] == null) {
continue;
}
tempIndex = text.indexOf(searchList[i]);
int tempIndex = text.indexOf(searchList[i]);

// see if we need to keep searching for this
if (tempIndex == -1) {
Expand Down Expand Up @@ -329,7 +323,7 @@ protected static String replaceEach(String text, String[] searchList, String[] r

textIndex = -1;
replaceIndex = -1;
tempIndex = -1;
int tempIndex;
// find the next earliest match
// NOTE: logic mostly duplicated above START
for (int i = 0; i < searchLength; i++) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jdbcdslog/ResultSetLoggingHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class ResultSetLoggingHandler extends LoggingHandlerSupport<ResultSet> {

public ResultSetLoggingHandler(LogMetaData logMetaData, ResultSet target) {
super(target);
this.logMetaData = logMetaData;
}

@Override
Expand Down