Skip to content

Commit

Permalink
Fix expensive folding only in debug level (#7042)
Browse files Browse the repository at this point in the history
foldExceptionStackTrace can be expensive and must be called only in
debug level log.
This happens when a field is not accessible (cross module since JDK16)
  • Loading branch information
jpbempel committed May 17, 2024
1 parent 3eb2828 commit f5e26a8
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public CapturedContext.CapturedValue fromJson(JsonReader reader) throws IOExcept
}

private static class JsonTokenWriter implements SerializerWithLimits.TokenWriter {
private static final Logger LOG = LoggerFactory.getLogger(JsonTokenWriter.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JsonTokenWriter.class);

private final JsonWriter jsonWriter;

Expand Down Expand Up @@ -424,10 +424,13 @@ public void objectFieldPrologue(String fieldName, Object value, int maxDepth)

@Override
public void handleFieldException(Exception ex, Field field) {
LOG.debug(
"Exception when extracting field={} exception={}",
field.getName(),
ExceptionHelper.foldExceptionStackTrace(ex));
if (LOGGER.isDebugEnabled()) {
// foldExceptionStackTrace can be expensive, only do it if debug is enabled
LOGGER.debug(
"Exception when extracting field={} exception={}",
field.getName(),
ExceptionHelper.foldExceptionStackTrace(ex));
}
String fieldName = field.getName();
try {
jsonWriter.name(fieldName);
Expand All @@ -438,7 +441,7 @@ public void handleFieldException(Exception ex, Field field) {
jsonWriter.value(ex.toString());
jsonWriter.endObject();
} catch (IOException e) {
LOG.debug("Serialization error: failed to extract field", e);
LOGGER.debug("Serialization error: failed to extract field", e);
}
}

Expand Down

0 comments on commit f5e26a8

Please sign in to comment.