Skip to content

Commit

Permalink
Rename package to slogdedup
Browse files Browse the repository at this point in the history
  • Loading branch information
veqryn committed Nov 10, 2023
1 parent 7e32782 commit effa1cf
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 25 deletions.
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ With this in mind, this repo was created with several different ways of deduplic
## Install
`go get github.com/veqryn/slog-dedup`

```go
import (
slogdedup "github.com/veqryn/slog-dedup"
)
```

## Usage
### Overwrite Older Duplicates Handler
```go
logger := slog.New(dedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger := slog.New(slogdedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger.Info("this is the dedup overwrite handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
Expand All @@ -62,7 +68,7 @@ Outputs:

### Ignore Newer Duplicates Handler
```go
logger := slog.New(dedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger := slog.New(slogdedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger.Info("this is the dedup ignore handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
Expand All @@ -81,7 +87,7 @@ Outputs:

### Increment Newer Duplicate Key Names Handler
```go
logger := slog.New(dedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger := slog.New(slogdedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger.Info("this is the dedup incrementer handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
Expand All @@ -102,7 +108,7 @@ Outputs:

### Append Duplicates Together in an Array Handler
```go
logger := slog.New(dedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger := slog.New(slogdedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil))
logger.Info("this is the dedup appender handler",
slog.String("duplicated", "zero"),
slog.String("duplicated", "one"),
Expand Down Expand Up @@ -131,12 +137,12 @@ import (
"log/slog"
"os"

dedup "github.com/veqryn/slog-dedup"
slogdedup "github.com/veqryn/slog-dedup"
)

func main() {
// OverwriteHandler
overwriter := dedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
overwriter := slogdedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(overwriter))

/*
Expand All @@ -154,7 +160,7 @@ func main() {
)

// IgnoreHandler
ignorer := dedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
ignorer := slogdedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(ignorer))

/*
Expand All @@ -172,7 +178,7 @@ func main() {
)

// IncrementHandler
incrementer := dedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
incrementer := slogdedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(incrementer))

/*
Expand All @@ -192,7 +198,7 @@ func main() {
)

// AppendHandler
appender := dedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
appender := slogdedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(appender))

/*
Expand All @@ -215,6 +221,21 @@ func main() {
}
```

## Breaking Changes
### O.1.0 -> 0.2.0
Package renamed from `dedup` to `slogdedup`.
To fix, change this:
```go
import "github.com/veqryn/slog-dedup"
var overwriter = dedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
```
To this:
```go
import "github.com/veqryn/slog-dedup"
var overwriter = slogdedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
```
Named imports are unaffected.

## Other Details
### Overwrite Handler
Using an overwrite handler allows a slightly different style of logging that is less verbose. As an application moves deeper into domain functions, it is common that additional details or knowledge is uncovered. By overwriting keys with better and more explanatory values as you go, the final log lines are often easier to read and more informative.
Expand Down
2 changes: 1 addition & 1 deletion append_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion append_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"log/slog"
Expand Down
12 changes: 6 additions & 6 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Package dedup provides structured logging (slog) deduplication for use with json logging
Package slogdedup 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.
Expand All @@ -8,7 +8,7 @@ Unfortunately the default behavior of the stdlib slog handlers is to allow dupli
Usage:
// OverwriteHandler
overwriter := dedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
overwriter := slogdedup.NewOverwriteHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(overwriter))
// {
Expand All @@ -24,7 +24,7 @@ Usage:
)
// IgnoreHandler
ignorer := dedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
ignorer := slogdedup.NewIgnoreHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(ignorer))
// {
Expand All @@ -40,7 +40,7 @@ Usage:
)
// IncrementHandler
incrementer := dedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
incrementer := slogdedup.NewIncrementHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(incrementer))
// {
Expand All @@ -58,7 +58,7 @@ Usage:
)
// AppendHandler
appender := dedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
appender := slogdedup.NewAppendHandler(slog.NewJSONHandler(os.Stdout, nil), nil)
slog.SetDefault(slog.New(appender))
// {
Expand All @@ -77,4 +77,4 @@ Usage:
slog.String("duplicated", "two"),
)
*/
package dedup
package slogdedup
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion ignore_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion ignore_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion increment_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion increment_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"strings"
Expand Down
2 changes: 1 addition & 1 deletion overwrite_handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion overwrite_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dedup
package slogdedup

import (
"log/slog"
Expand Down

0 comments on commit effa1cf

Please sign in to comment.