https://github.com/palantir/safe-logging
Interfaces and utilities for safe log messages
https://github.com/palantir/safe-logging
octo-correct-managed
Last synced: 2 months ago
JSON representation
Interfaces and utilities for safe log messages
- Host: GitHub
- URL: https://github.com/palantir/safe-logging
- Owner: palantir
- License: apache-2.0
- Created: 2017-05-15T16:17:34.000Z (about 8 years ago)
- Default Branch: develop
- Last Pushed: 2025-04-07T10:28:37.000Z (2 months ago)
- Last Synced: 2025-04-07T11:31:20.526Z (2 months ago)
- Topics: octo-correct-managed
- Language: Java
- Homepage:
- Size: 1.79 MB
- Stars: 16
- Watchers: 266
- Forks: 21
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: changelog/1.10.0/pr-176.v2.yml
- License: LICENSE
Awesome Lists containing this project
README
[  ](https://search.maven.org/search?q=g:com.palantir.safe-logging)
License
-------
This repository is made available under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0).Safe-Logging
=============
Interfaces and utilities for safe log messages.Usage
-----Add dependency to gradle:
```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:### _Previously_
```java
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);
```### _Now_
```java
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));
```Preconditions
=============
Guava Preconditions equivalent which produces exceptions conforming to the SafeLoggable standard.Usage
-----Add dependency to gradle:
```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:
```java
// 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));
```