Skip to content

Commit

Permalink
Merge pull request #29 from gildas/release/1.7.6
Browse files Browse the repository at this point in the history
Merge release/1.7.6
gildas authored Dec 25, 2024

Verified

This commit was signed with the committer’s verified signature.
Mamaduka George Mamadashvili
2 parents f740901 + ff13de2 commit 59c0d6e
Showing 8 changed files with 136 additions and 187 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.22.x]
go-version: [1.23.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -372,7 +372,7 @@ main() {
}
```

When redacting a field, you can also call `logger.RedactWithHash` which will redact the value with a string like: "REDACTED-<hash>" where `<hash>` is a SHA256 hash of the original value. This is useful when you want to redact a value but still want to be able to identify it in the logs. You can also change the prefix with `logger.RedactWithPrefixedHash`.
When redacting a field, you can also call `logger.RedactWithHash` which will redact the value with a string like: "REDACTED-&lt;hash&gt;" where `<hash>` is a SHA256 hash of the original value. This is useful when you want to redact a value but still want to be able to identify it in the logs. You can also change the prefix with `logger.RedactWithPrefixedHash`.

For Complex objects, you can also implement the `logger.RedactableWithKeys` interface:

@@ -398,6 +398,13 @@ main() {
}
```

You can also redact slices and maps of objects:

```go
log.Record("users", logger.RedactSlice(users)).Infof("Got a list of users")
log.Record("users", logger.RedactMap(users)).Infof("Got a map of users")
```

You can also redact the log messages by providing regular expressions, called redactors. Whenever a redactor matches, its matched content is replaced with "REDACTED".

You can assign several redactors to a single logger:
57 changes: 28 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
module github.com/gildas/go-logger

go 1.22.7
go 1.23

toolchain go1.23.3

require (
cloud.google.com/go/logging v1.12.0
github.com/gildas/go-core v0.5.10
github.com/gildas/go-errors v0.3.7
github.com/gildas/go-core v0.6.0
github.com/gildas/go-errors v0.4.0
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.27.0
google.golang.org/api v0.206.0
github.com/stretchr/testify v1.10.0
golang.org/x/sys v0.28.0
google.golang.org/api v0.214.0
)

require (
cloud.google.com/go v0.116.0 // indirect
cloud.google.com/go/auth v0.10.2 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.5 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
cloud.google.com/go/longrunning v0.6.2 // indirect
cloud.google.com/go v0.117.0 // indirect
cloud.google.com/go/auth v0.13.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.6 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/longrunning v0.6.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/s2a-go v0.1.8 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.4 // indirect
github.com/googleapis/gax-go/v2 v2.14.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.57.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.57.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/metric v1.32.0 // indirect
go.opentelemetry.io/otel/trace v1.32.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/net v0.31.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
go.opentelemetry.io/otel/metric v1.33.0 // indirect
go.opentelemetry.io/otel/trace v1.33.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/genproto v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241118233622-e639e219e697 // indirect
google.golang.org/grpc v1.68.0 // indirect
google.golang.org/protobuf v1.35.2 // indirect
google.golang.org/genproto v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241223144023-3abc09e42ca8 // indirect
google.golang.org/grpc v1.69.2 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
213 changes: 66 additions & 147 deletions go.sum

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions logger_test.go
Original file line number Diff line number Diff line change
@@ -470,7 +470,6 @@ func (suite *LoggerSuite) TestLoggerHttpHandlerWithSuccess() {
lines = lines[:len(lines)-1] // remove the last empty line
suite.Require().Len(lines, 3, "Expected 3 lines of output")
suite.LogLineEqual(lines[0], map[string]string{
"agent": "",
"hostname": `[a-zA-Z_0-9\-\.]+`,
"level": "30",
"msg": `request start: GET /`,
@@ -548,7 +547,6 @@ func (suite *LoggerSuite) TestLoggerHttpHandlerWithFailure() {
lines = lines[:len(lines)-1] // remove the last empty line
suite.Require().Len(lines, 3, "Expected 3 lines of output")
suite.LogLineEqual(lines[0], map[string]string{
"agent": "",
"hostname": `[a-zA-Z_0-9\-\.]+`,
"level": strconv.Itoa(int(logger.INFO)),
"msg": `request start: GET /`,
@@ -828,7 +826,6 @@ func (suite *LoggerSuite) TestCanLogAtDifferentLevelsPerTopicAndEmptyScope() {
"msg": "message",
"name": "test",
"pid": "[0-9]+",
"scope": "",
"tid": "[0-9]+",
"time": `[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+Z`,
"topic": "hild",
@@ -850,10 +847,8 @@ func (suite *LoggerSuite) TestCanLogWithEmptyTopicAndEmptyScope() {
"msg": "message",
"name": "test",
"pid": "[0-9]+",
"scope": "",
"tid": "[0-9]+",
"time": `[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+Z`,
"topic": "",
"v": "0",
})
}
12 changes: 10 additions & 2 deletions redact.go
Original file line number Diff line number Diff line change
@@ -16,15 +16,23 @@ func Redact(value interface{}) string {
return ""
}

// RedactAll redacts all items in a slice of redactable items
func RedactAll[T Redactable](items []T) []any {
// RedactSlice redacts all items in a slice of redactable items
func RedactSlice[T Redactable](items []T) []any {
redacted := make([]any, len(items))
for i, item := range items {
redacted[i] = item.Redact()
}
return redacted
}

func RedactMap[S comparable, T Redactable](items map[S]T) map[S]any {
redacted := make(map[S]any, len(items))
for key, item := range items {
redacted[key] = item.Redact()
}
return redacted
}

// RedactWithHash redacts a value with a hash
func RedactWithHash(value interface{}) string {
return RedactWithPrefixedHash("REDACTED", value)
23 changes: 22 additions & 1 deletion redact_test.go
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ func (suite *RedactSuite) TestCanRedactSensitiveSliceOfStruct() {
customers := []User{{"12345678", "John Doe", nil}, {"87654321", "Jane Doe", nil}}
output := CaptureStdout(func() {
log := logger.Create("test", &logger.StdoutStream{Unbuffered: true})
log.Record("customers", logger.RedactAll(customers)).Infof("message")
log.Record("customers", logger.RedactSlice(customers)).Infof("message")
})
suite.LogLineEqual(output, map[string]string{
"customers": `\[map\[id:12345678 name:REDACTED\] map\[id:87654321 name:REDACTED\]\]`,
@@ -65,6 +65,27 @@ func (suite *RedactSuite) TestCanRedactSensitiveSliceOfStruct() {
})
}

func (suite *RedactSuite) TestCanRedactSensitiveMapOfStruct() {
customers := map[string]User{"12345678": {"12345678", "John Doe", nil}, "87654321": {"87654321", "Jane Doe", nil}}
output := CaptureStdout(func() {
log := logger.Create("test", &logger.StdoutStream{Unbuffered: true})
log.Record("customers", logger.RedactMap(customers)).Infof("message")
})
suite.LogLineEqual(output, map[string]string{
"customers": `map\[12345678:map\[id:12345678 name:REDACTED\] 87654321:map\[id:87654321 name:REDACTED\]\]`,
"hostname": `[a-zA-Z_0-9\-\.]+`,
"level": "30",
"msg": "message",
"name": "test",
"pid": "[0-9]+",
"scope": "main",
"tid": "[0-9]+",
"time": `[0-9]+-[0-9]+-[0-9]+T[0-9]+:[0-9]+:[0-9]+Z`,
"topic": "main",
"v": "0",
})
}

func (suite *RedactSuite) TestCanRedactMessage() {
redactor := core.Must(logger.NewRedactor(`\+[0-9]{11}`))
suite.Require().NotEmpty(redactor.String())
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -4,4 +4,4 @@ package logger
var commit string

// VERSION is the version of this application
var VERSION = "1.7.5" + commit
var VERSION = "1.7.6" + commit

0 comments on commit 59c0d6e

Please sign in to comment.