Skip to content

Commit d8f5f38

Browse files
Elliot ForbesElliot Forbes
authored andcommitted
First pass at the ctxlog package
1 parent 8a24bbf commit d8f5f38

12 files changed

Lines changed: 202 additions & 0 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ctx-logger
2+
===========
3+
4+
5+
```go
6+
ctx := context.Background()
7+
log := ctxlog.New(
8+
ctxlog.WithJSONFormat(),
9+
)
10+
ctx = ctxlog.WithFields(ctx, ctxlog.Fields{
11+
"trace_id": "my-trace-id",
12+
})
13+
14+
log.Info(ctx, "hello world")
15+
```

Taskfile.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: '3'
2+
3+
tasks:
4+
test:
5+
cmds:
6+
- go test ./... -v

error.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ctxlog
2+
3+
import "context"
4+
5+
func (l *CtxLogger) Error(ctx context.Context, msg string) {
6+
fields := convertFieldsToLogrusFields(getFields(ctx))
7+
l.Log.WithFields(fields).Error(msg)
8+
}

error_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package ctxlog_test
2+
3+
import (
4+
"context"
5+
"github.com/TutorialEdge/ctxlog"
6+
"testing"
7+
)
8+
9+
func TestErrorLogs(t *testing.T) {
10+
t.Run("test error log output", func(t *testing.T) {
11+
ctx := context.Background()
12+
log := ctxlog.New()
13+
ctx = ctxlog.WithFields(ctx, ctxlog.Fields{
14+
"trace_id": "my-trace-id",
15+
})
16+
17+
log.Error(ctx, "some error message")
18+
})
19+
}

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/TutorialEdge/ctxlog
2+
3+
go 1.18
4+
5+
require (
6+
github.com/sirupsen/logrus v1.9.0 // indirect
7+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
8+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
5+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
8+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
9+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

info.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package ctxlog
2+
3+
import (
4+
"context"
5+
)
6+
7+
func (l *CtxLogger) Info(ctx context.Context, msg string) {
8+
fields := convertFieldsToLogrusFields(getFields(ctx))
9+
l.Log.WithFields(fields).Info(msg)
10+
}

info_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package ctxlog_test

logger.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package ctxlog
2+
3+
import (
4+
"context"
5+
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
type fieldsKey struct{}
10+
11+
type Fields map[string]any
12+
13+
type CtxLogger struct {
14+
Fields Fields
15+
Log *logrus.Logger
16+
}
17+
18+
func New(options ...func(*CtxLogger)) *CtxLogger {
19+
log := &CtxLogger{
20+
Log: logrus.New(),
21+
}
22+
23+
for _, o := range options {
24+
o(log)
25+
}
26+
27+
return log
28+
}
29+
30+
func WithJSONFormat() func(*CtxLogger) {
31+
return func(l *CtxLogger) {
32+
l.Log.SetFormatter(&logrus.JSONFormatter{})
33+
}
34+
}
35+
36+
func getFields(ctx context.Context) Fields {
37+
if val, ok := ctx.Value(fieldsKey{}).(Fields); ok {
38+
return val
39+
}
40+
return Fields{}
41+
}
42+
43+
func WithFields(ctx context.Context, newFields ...Fields) context.Context {
44+
fields := Fields{}
45+
46+
for k, v := range getFields(ctx) {
47+
fields[k] = v
48+
}
49+
50+
for _, fieldMap := range newFields {
51+
for k, v := range fieldMap {
52+
fields[k] = v
53+
}
54+
}
55+
56+
return context.WithValue(ctx, fieldsKey{}, fields)
57+
}
58+
59+
func convertFieldsToLogrusFields(fields Fields) logrus.Fields {
60+
logrusFields := make(logrus.Fields)
61+
for k, v := range fields {
62+
logrusFields[k] = v
63+
}
64+
return logrusFields
65+
}

logger_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ctxlog_test
2+
3+
import (
4+
"context"
5+
"github.com/TutorialEdge/ctxlog"
6+
"testing"
7+
)
8+
9+
func TestLogger(t *testing.T) {
10+
t.Run("test logger instantiates properly", func(t *testing.T) {
11+
ctx := context.Background()
12+
log := ctxlog.New()
13+
ctx = ctxlog.WithFields(ctx, ctxlog.Fields{
14+
"trace_id": "my-trace-id",
15+
})
16+
17+
log.Info(ctx, "hello world")
18+
})
19+
20+
t.Run("test logger WithJSONFormat", func(t *testing.T) {
21+
ctx := context.Background()
22+
log := ctxlog.New(
23+
ctxlog.WithJSONFormat(),
24+
)
25+
ctx = ctxlog.WithFields(ctx, ctxlog.Fields{
26+
"trace_id": "my-trace-id",
27+
})
28+
29+
log.Info(ctx, "hello world")
30+
log.Info(ctx, "something else")
31+
})
32+
}

0 commit comments

Comments
 (0)