-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Package dedup provides structured logging (slog) deduplication for use with json logging | ||
(or any other format where duplicates are not appreciated). | ||
The main impetus behind this package is because most JSON tools do not like duplicate keys for their member | ||
properties/fields. Some of them will give errors or fail to parse the log line, and some may even crash. | ||
Unfortunately the default behavior of the stdlib slog handlers is to allow duplicate keys. | ||
Usage: | ||
// OverwriteHandler | ||
overwriter := dedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil) | ||
slog.SetDefault(slog.New(overwriter)) | ||
// { | ||
// "time": "2023-10-03T01:30:00Z", | ||
// "level": "INFO", | ||
// "msg": "this is the dedup overwrite handler", | ||
// "duplicated": "two" | ||
// } | ||
slog.Info("this is the dedup overwrite handler", | ||
slog.String("duplicated", "zero"), | ||
slog.String("duplicated", "one"), | ||
slog.String("duplicated", "two"), | ||
) | ||
// IgnoreHandler | ||
ignorer := dedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil) | ||
slog.SetDefault(slog.New(ignorer)) | ||
// { | ||
// "time": "2023-10-03T01:30:00Z", | ||
// "level": "INFO", | ||
// "msg": "this is the dedup ignore handler", | ||
// "duplicated": "zero" | ||
// } | ||
slog.Info("this is the dedup ignore handler", | ||
slog.String("duplicated", "zero"), | ||
slog.String("duplicated", "one"), | ||
slog.String("duplicated", "two"), | ||
) | ||
// IncrementHandler | ||
incrementer := dedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil) | ||
slog.SetDefault(slog.New(incrementer)) | ||
// { | ||
// "time": "2023-10-03T01:30:00Z", | ||
// "level": "INFO", | ||
// "msg": "this is the dedup incrementer handler", | ||
// "duplicated": "zero", | ||
// "duplicated#01": "one", | ||
// "duplicated#02": "two" | ||
// } | ||
slog.Info("this is the dedup incrementer handler", | ||
slog.String("duplicated", "zero"), | ||
slog.String("duplicated", "one"), | ||
slog.String("duplicated", "two"), | ||
) | ||
// AppendHandler | ||
appender := dedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil) | ||
slog.SetDefault(slog.New(appender)) | ||
// { | ||
// "time": "2023-10-03T01:30:00Z", | ||
// "level": "INFO", | ||
// "msg": "this is the dedup appender handler", | ||
// "duplicated": [ | ||
// "zero", | ||
// "one", | ||
// "two" | ||
// ] | ||
// } | ||
slog.Info("this is the dedup appender handler", | ||
slog.String("duplicated", "zero"), | ||
slog.String("duplicated", "one"), | ||
slog.String("duplicated", "two"), | ||
) | ||
*/ | ||
package dedup |