Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions definitions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package log

import (
"github.com/logrusorgru/aurora"
"github.com/logrusorgru/aurora/v4"
)

type Level string
Expand All @@ -22,12 +22,12 @@ const (
)

var logColors = map[Level]string{
FATAL: aurora.BgRed(`[FATAL]`).String(),
ERROR: aurora.BgRed(`[ERROR]`).String(),
WARN: aurora.BgYellow(`[ WARN]`).String(),
INFO: aurora.BgBlue(`[ INFO]`).String(),
DEBUG: aurora.BgCyan(`[DEBUG]`).String(),
TRACE: aurora.BgMagenta(`[TRACE]`).String(),
FATAL: aurora.Red(`FATAL`).String(),
ERROR: aurora.Red(`ERROR`).String(),
WARN: aurora.Yellow(`WARN `).String(),
INFO: aurora.Blue(`INFO `).String(),
DEBUG: aurora.Cyan(`DEBUG`).String(),
TRACE: aurora.Magenta(`TRACE`).String(),
}

var logTypes = map[Level]int{
Expand Down
12 changes: 7 additions & 5 deletions example/json_logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,8 @@ func main() {

// enable context reading
// keys
type keyOne string
type keyTwo string

const k1 keyOne = "key1"
const k2 keyTwo = "key2"
const k1 string = "key1"
const k2 string = "key2"

// get details from context
lCtx := context.Background()
Expand Down Expand Up @@ -115,6 +112,11 @@ func main() {
fmt.Sprintf("%s: %+v", k2, ctx.Value(k2)),
}
}),
log.WithCtxMapExtractor(func(ctx context.Context) map[string]string {
return map[string]string{
k2: ctx.Value(k2).(string),
}
}),
)

ctxChildLogger.ErrorContext(lCtx, "message", "param1", "param2")
Expand Down
27 changes: 11 additions & 16 deletions example/text_logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,10 @@ func main() {
prefixedLogger.Error(`module.sub-module`, `message`)
prefixedLogger.Error(`module.sub-module`, `message`, `param1`, `param2`)

// custom logger
//customLogger := customLog.NewLogger()
//customLogger.Info(`info`)
//customLogger.Trace(`trace`)
//
//// create a logger instance derived from logger
//nestedLogger := logger.NewLog(log.WithLevel(log.TRACE), log.Prefixed(`level-2`))
//nestedLogger.Error(`error happened`, 22)

// enable context reading
// keys
type keyOne string
type keyTwo string

const k1 keyOne = "key1"
const k2 keyTwo = "key2"
const k1 = "key1"
const k2 = "key2"

// get details from context
lCtx := context.Background()
Expand All @@ -96,11 +84,16 @@ func main() {
fmt.Sprintf("%s: %+v", k1, ctx.Value(k1)),
}
}),
log.WithCtxMapExtractor(func(ctx context.Context) map[string]string {
return map[string]string{
k1 + `mp`: ctx.Value(k1).(string),
k2 + `mp`: ctx.Value(k2).(string),
}
}),
)

ctxLogger.ErrorContext(lCtx, `message`, `param1`, `param2`)
ctxLogger.ErrorContext(lCtx, `message`)
ctxLogger.ErrorContext(lCtx, `message`)
ctxLogger.ErrorContext(lCtx, `message`, `param1`, `param2`)
ctxLogger.ErrorContext(lCtx, log.WithPrefix(`prefix`, `message`))
ctxLogger.WarnContext(lCtx, log.WithPrefix(`prefix`, `message`), `param1`, `param2`)

Expand All @@ -118,4 +111,6 @@ func main() {
ctxChildLogger.ErrorContext(lCtx, `message`)
ctxChildLogger.ErrorContext(lCtx, log.WithPrefix(`prefix`, `message`))
ctxChildLogger.WarnContext(lCtx, log.WithPrefix(`prefix`, `message`), `param1`, `param2`)
ctxChildLogger.Println(`param1`, `param2`)
ctxChildLogger.Print(`param1`, `param2`)
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/tryfix/log
go 1.14

require (
github.com/google/uuid v1.1.1
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/rs/zerolog v1.22.0
github.com/google/uuid v1.6.0
github.com/logrusorgru/aurora/v4 v4.0.0
github.com/rs/zerolog v1.33.0
github.com/tryfix/traceable-context v1.0.1
)
62 changes: 33 additions & 29 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381 h1:bqDmpDG49ZRnB5PcgP0RXtQvnMSgIF14M7CBd2shtXs=
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA=
github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.22.0 h1:XrVUjV4K+izZpKXZHlPrYQiDtmdGiCylnT4i43AAWxg=
github.com/rs/zerolog v1.22.0/go.mod h1:ZPhntP/xmq1nnND05hhpAh2QMhSsA4UN3MGZ6O2J3hM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tryfix/traceable-context v1.0.1 h1:BNAx5NzCi2oEhvLXR4WxtUc5bCIv7PClLCWgGh33kDg=
github.com/tryfix/traceable-context v1.0.1/go.mod h1:yXNt6rINIlKZDYQuZnVFfZhjTDSQXryhC8KM5vuP6Vw=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
67 changes: 63 additions & 4 deletions json_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log_test

import (
"context"
"fmt"
"io/ioutil"
"testing"

Expand All @@ -18,6 +19,7 @@ func BenchmarkZLBaseline(b *testing.B) {
Logger()
var msg interface{} = "message"
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Error().Msgf("%s", msg)
Expand All @@ -35,6 +37,7 @@ func BenchmarkZLBaselineWithCaller(b *testing.B) {
Logger()
var msg interface{} = "message"
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Error().Msgf("%s", msg)
Expand All @@ -51,6 +54,7 @@ func BenchmarkJsonPrint(b *testing.B) {
log.WithLevel(log.DEBUG),
)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Print("message")
Expand Down Expand Up @@ -85,6 +89,7 @@ func BenchmarkJsonLoggers(b *testing.B) {
b.Run(c.name, func(b *testing.B) {
logger := baseLogger.Log(c.cfs...)
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Error("message")
Expand All @@ -102,6 +107,7 @@ func BenchmarkJsonLogInfo(b *testing.B) {
log.WithFilePath(false),
log.WithColors(false)).Log()
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.Info(testLog)
Expand All @@ -117,6 +123,7 @@ func BenchmarkJsonLogInfoFilePath(b *testing.B) {
log.WithFilePath(true),
log.WithColors(false)).Log()
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.Info(testLog)
Expand All @@ -133,6 +140,7 @@ func BenchmarkJsonInfoContext(b *testing.B) {
log.WithColors(false)).Log()
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.InfoContext(ctx, testLog)
Expand All @@ -141,22 +149,72 @@ func BenchmarkJsonInfoContext(b *testing.B) {
}

func BenchmarkJsonInfoContextExt(b *testing.B) {
ctx1 := context.WithValue(context.Background(), `ctx1`, `ctx one value`)
ctx2 := context.WithValue(ctx1, `ctx2`, `ctx two value`)
ctx1 := context.WithValue(context.Background(), `ctx1`, `ctx 1 value`)
for i := 2; i <= 10; i++ {
ctx1 = context.WithValue(ctx1, fmt.Sprintf(`ctx%d`, i), fmt.Sprintf(`ctx %d value`, i))
}
lg := log.NewLog(
log.WithLevel(log.INFO),
log.WithStdOut(ioutil.Discard),
log.WithFilePath(false),
log.WithOutput(log.OutJson),
log.WithCtxExtractor(func(ctx context.Context) []interface{} {
return []interface{}{ctx.Value(`ctx1`), ctx.Value(`ctx2`)}
return []interface{}{
"ctx1: " + ctx.Value(`ctx1`).(string),
"ctx2: " + ctx.Value(`ctx2`).(string),
"ctx3: " + ctx.Value(`ctx3`).(string),
"ctx4: " + ctx.Value(`ctx4`).(string),
"ctx5: " + ctx.Value(`ctx5`).(string),
"ctx6: " + ctx.Value(`ctx6`).(string),
"ctx7: " + ctx.Value(`ctx7`).(string),
"ctx8: " + ctx.Value(`ctx8`).(string),
"ctx9: " + ctx.Value(`ctx9`).(string),
"ctx10: " + ctx.Value(`ctx10`).(string),
}
}),
log.WithColors(false)).Log()

b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.InfoContext(ctx1, testLog)
}
})
}

func BenchmarkJsonInfoContextMapExt(b *testing.B) {
ctx1 := context.WithValue(context.Background(), `ctx1`, `ctx 1 value`)
for i := 2; i <= 10; i++ {
ctx1 = context.WithValue(ctx1, fmt.Sprintf(`ctx%d`, i), fmt.Sprintf(`ctx %d value`, i))
}

lg := log.NewLog(
log.WithLevel(log.INFO),
log.WithStdOut(ioutil.Discard),
log.WithFilePath(false),
log.WithOutput(log.OutJson),
log.WithCtxMapExtractor(func(ctx context.Context) map[string]string {
return map[string]string{
`ctx1`: ctx.Value(`ctx1`).(string),
`ctx2`: ctx.Value(`ctx2`).(string),
`ctx3`: ctx.Value(`ctx3`).(string),
`ctx4`: ctx.Value(`ctx4`).(string),
`ctx5`: ctx.Value(`ctx5`).(string),
`ctx6`: ctx.Value(`ctx6`).(string),
`ctx7`: ctx.Value(`ctx7`).(string),
`ctx8`: ctx.Value(`ctx8`).(string),
`ctx9`: ctx.Value(`ctx9`).(string),
`ctx10`: ctx.Value(`ctx10`).(string),
}
}),
log.WithColors(false)).Log()

b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.InfoContext(ctx2, testLog)
lg.InfoContext(ctx1, testLog)
}
})
}
Expand All @@ -170,6 +228,7 @@ func BenchmarkJsonInfoParams(b *testing.B) {
log.WithColors(false)).Log()
ctx := context.Background()
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
lg.InfoContext(ctx, testLog, `parm1`, `parm2`, `parm3`, `parm4`)
Expand Down
7 changes: 6 additions & 1 deletion json_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ func (l *jsonLogParser) withExtractedTrace(ctx context.Context, event *zerolog.E
func (l *jsonLogParser) withExtractedCtx(ctx context.Context, event *zerolog.Event) *zerolog.Event {
if l.ctxExt != nil {
if ctxData := l.ctxExt(ctx); len(ctxData) > 0 {
return event.Interface("context", ctxData)
event.Interface("context", ctxData)
}
}
if l.ctxMapExt != nil {
if ctxData := l.ctxMapExt(ctx); len(ctxData) > 0 {
event.Interface("contextMap", ctxData)
}
}

Expand Down
25 changes: 23 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type logOptions struct {
writer io.Writer
output Output
ctxExt func(ctx context.Context) []interface{}
ctxMapExt func(ctx context.Context) map[string]string
ctxTraceExt func(ctx context.Context) string
}

Expand Down Expand Up @@ -98,7 +99,7 @@ func WithFilePath(enabled bool) Option {
}
}

// WithFilePath sets whether the file path is logged or not.
// WithFuncPath sets whether the func path is logged or not.
func WithFuncPath(enabled bool) Option {
return func(opts *logOptions) {
opts.funcPath = enabled
Expand Down Expand Up @@ -132,7 +133,9 @@ func WithLevel(level Level) Option {
}
}

// WithCtxExtractor allows setting up of a function to extract values from the context.
// Deprecated: use WithCtxMapExtractor instead.
//
// WithCtxExtractor allows setting up a function to extract values from the context as an array.
func WithCtxExtractor(fn func(ctx context.Context) []interface{}) Option {
return func(opts *logOptions) {
parent := opts.ctxExt
Expand All @@ -146,6 +149,24 @@ func WithCtxExtractor(fn func(ctx context.Context) []interface{}) Option {
}
}

// WithCtxMapExtractor allows setting up a function to extract values from the context as a key:value map.
func WithCtxMapExtractor(fn func(ctx context.Context) map[string]string) Option {
return func(opts *logOptions) {
parentExt := opts.ctxMapExt
opts.ctxMapExt = func(ctx context.Context) map[string]string {
if parentExt != nil {
parentMap := parentExt(ctx)
for key, val := range fn(ctx) {
parentMap[key] = val
}
return parentMap
}

return fn(ctx)
}
}
}

// WithCtxTraceExtractor allows setting up of a function to extract trace from the context.
// Default value func(_ context.Context) string{return ""}
func WithCtxTraceExtractor(fn func(ctx context.Context) string) Option {
Expand Down
Loading