aws / aws/aws-xray-sdk-java

LogContextMissingStrategy with customizable logging level

Open
#5 2 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Java
Stars
100
Forks
100
PR merge metrics
No merged PRs in 30d

Description

Currently `LogErrorContextMissingStrategy` uses commons-logging and always logs error:

https://github.com/aws/aws-xray-sdk-java/blob/01d52aee854baae36ad9224598ffa709290d7afa/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/strategy/LogErrorContextMissingStrategy.java#L18

My suggestion:

1. Use `slf4j-api` instead, which is de facto standard for modern Java apps and also more performant
2. Add `LogContextMissingStrategy` which can be used to log to user-specified level. `LogErrorContextMissingStrategy` will just become a very thin subclass (for backwards compatbility) that uses `Level.ERROR`

What I use:

```java
public static class LogContextMissingStrategy implements ContextMissingStrategy {
private static final Logger logger = LoggerFactory.getLogger(LogContextMissingStrategy.class);
private final Level level;
private static final Map map;

static {
map = new HashMap<>();
map.put(Level.TRACE, (logger, format, params) -> logger.trace(format, params));
map.put(Level.DEBUG, (logger, format, params) -> logger.debug(format, params));
map.put(Level.INFO, (logger, format, params) -> logger.info(format, params));
map.put(Level.WARN, (logger, format, params) -> logger.warn(format, params));
map.put(Level.ERROR, (logger, format, params) -> logger.error(format, params));
}

@FunctionalInterface
private interface LoggingFunction {
void log(Logger logger, String format, Object... params);
}

public LogContextMissingStrategy(Level level) {
this.level = level;
}

/**
* Logs {@code message} on the specified level.
* @param message the message to log
* @param exceptionClass the type of exception suppressed in favor of logging {@code message}
*/
@Override
public void contextMissing(String message, Class exceptionClass) {
map.get(level).log(logger, "Suppressing AWS X-Ray context missing exception ({}): {}",
exceptionClass.getSimpleName(), message);
}

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.