This repository is made available under the Apache 2.0 License.
Interfaces and utilities for safe log messages.
Add dependency to gradle:
dependencies {
implementation 'com.palantir.safe-logging:logger'
}
Update logger instances to use SafeLogger
and annotate log parameters with
named SafeArg
and UnsafeArg
as appropriate. For example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger log = LoggerFactory.getLogger(MyClass.class);
...
log.info("Twisted the {} knob {} times", knobName, count);
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
private static final SafeLogger log = SafeLoggerFactory.get(MyClass.class);
...
log.info("Twisted the {} knob {} times", UnsafeArg.of("knobName", knobName), SafeArg.of("count", count));
// Even cleaner without slf4j-style interpolation markers:
log.info("Twisted the knob", UnsafeArg.of("knob", knobName), SafeArg.of("twists", count));
Guava Preconditions equivalent which produces exceptions conforming to the SafeLoggable standard.
Add dependency to gradle:
dependencies {
implementation 'com.palantir.safe-logging:preconditions'
// optional test utilities
testImplementation 'com.palantir.safe-logging:preconditions-assertj'
}
Annotate Preconditions error messages with named SafeArg
and UnsafeArg
as appropriate. For example:
// previously
import com.google.common.base.Preconditions;
...
Preconditions.checkArgument(uname.size() > MAX_LEN, "%s username longer than max %s", uname, MAX_LEN);
// now
import com.palantir.logsafe.Preconditions;
...
Preconditions.checkArgument(uname.size() > MAX_LEN, "username longer than max",
UnsafeArg.of("uname", uname), SafeArg.of("max", MAX_LEN));