Skip to content
Open
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
19 changes: 19 additions & 0 deletions example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/carousell/go-logging/example

go 1.18

replace (
github.com/carousell/go-logging => ../
github.com/carousell/go-logging/gokit => ../gokit
)

require (
github.com/carousell/go-logging v0.0.0-20230322093349-63592a690170
github.com/carousell/go-logging/gokit v0.0.0-20230322093349-63592a690170
)

require (
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
)
10 changes: 10 additions & 0 deletions example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/carousell/go-logging v0.0.0-20230322093349-63592a690170 h1:2a4U/3KXreDg6xECAsY+/HnvubSDLMQzAYtKNBXUQOo=
github.com/carousell/go-logging v0.0.0-20230322093349-63592a690170/go.mod h1:rNIbSrWZ+PZyl3NJIC1g8UyFA1gjMJxHdBhqDFdNs1w=
github.com/carousell/go-logging/gokit v0.0.0-20230322093349-63592a690170 h1:/NMBf7No4l+VjPolLYSeobAYiniqoBpo+QgSdBvUxD4=
github.com/carousell/go-logging/gokit v0.0.0-20230322093349-63592a690170/go.mod h1:JQioIqeAAjr9n/7vWDeqKczZ7wqSMhrN+fKapvmTEsk=
github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4=
github.com/go-kit/kit v0.12.0/go.mod h1:lHd+EkCZPIwYItmGDDRdhinkzX2A1sj+M9biaEaizzs=
github.com/go-kit/log v0.2.0 h1:7i2K3eKTos3Vc0enKCfnVcgHh2olr/MyfboYq7cAcFw=
github.com/go-kit/log v0.2.0/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
60 changes: 60 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"context"

"github.com/carousell/go-logging"
"github.com/carousell/go-logging/gokit"
)

func main() {

ctx := context.Background()

logging.RegisterLogger(gokit.NewLogger())

ctx = logging.WithLogger(ctx,
logging.WithAttr("main1", "main1"),
logging.WithAttr("main2", 123),
Copy link
Contributor Author

@achichen achichen Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel this is a bit verbose, but cannot think of a succinct way.

One possible approach is to make shortcut function name might help, like logging.A(key,value). Similar to golang's testing package.

)

logging.InfoV2(ctx, "before calling foo()")
//{"@timestamp":"2023-04-27T00:39:45.573743+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"msg":"before calling foo()"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from current scope: "main1", "main2"


foo(ctx)
logging.InfoV2(ctx, "after called foo()")
//{"@timestamp":"2023-04-27T00:39:45.574079+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"msg":"after called foo()"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from current scope: "main1", "main2"

NOTE that this log appears after calling foo(ctx), but the attrs within foo(ctx) does not leak out


logging.InfoV3(ctx, "msg v3 without attributes", nil)
//{"@timestamp":"2023-04-27T00:01:39.701337+05:30","caller":"go-logging/new.go:35","level":"info","main1":"main1","main2":123,"msg":"msg v3 without attributes"}

logging.InfoV3(ctx, "msg v3", logging.AttrMap{"listing_id": "1000323749", "is_exp_enabled": true})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @bhadreswar-ghuku. I was considering taking map[string]interface{}. But this looks even better.

//{"@timestamp":"2023-04-27T00:04:01.998833+05:30","caller":"go-logging/new.go:35","is_exp_enabled":true,"level":"info","listing_id":"1000323749","main1":"main1","main2":123,"msg":"msg v3"}
}

func foo(ctx context.Context) {
ctx = logging.WithLogger(ctx,
logging.WithAttr("foo1", "foo1"),
logging.WithAttr("foo2", 456),
)

logging.InfoV2(ctx, "before calling bar()", logging.WithAttr("foo3", "foo3"))
//{"@timestamp":"2023-04-27T00:39:45.574018+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"foo1":"foo1","foo2":456,"foo3":"foo3","msg":"before calling bar()"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from parent scope: "main1", "main2"
  • attrs from current scope: "foo1", "foo2"
  • adhoc attrs : "foo3"


bar(ctx)

logging.InfoV2(ctx, "after called bar()", logging.WithAttr("result", "bar result"))
//{"@timestamp":"2023-04-27T00:39:45.574068+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"foo1":"foo1","foo2":456,"result":"bar result","msg":"after called bar()"}
Copy link
Contributor Author

@achichen achichen Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from parent scope: "main1", "main2"
  • attrs from current scope: "foo1", "foo2"
  • adhoc attrs : "result"

NOTE that this log appears after calling bar(ctx), but the attrs within bar(ctx) does not leak out

}

func bar(ctx context.Context) {
ctx = logging.WithLogger(ctx,
logging.WithAttr("bar1", "bar1"),
logging.WithAttr("bar2", 789),
)
logging.InfoV2(ctx, "before do something", logging.WithAttr("bar3", 9876))
//{"@timestamp":"2023-04-27T00:39:45.574037+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"foo1":"foo1","foo2":456,"bar1":"bar1","bar2":789,"bar3":9876,"msg":"before do something"}
Copy link
Contributor Author

@achichen achichen Apr 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from grantparent scope: "main1", "main2"
  • attrs from parent scope: "foo1", "foo2"
  • attrs from current scope: "bar1", "bar2"
  • adhoc attrs : "bar3"


logging.InfoV2(ctx, "after do something", logging.WithAttr("result", "success"))
//{"@timestamp":"2023-04-27T00:39:45.574055+08:00","caller":"go-logging/new.go:33","level":"info","main1":"main1","main2":123,"foo1":"foo1","foo2":456,"bar1":"bar1","bar2":789,"result":"success","msg":"after do something"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • attrs from grantparent scope: "main1", "main2"
  • attrs from parent scope: "foo1", "foo2"
  • attrs from current scope: "bar1", "bar2"
  • adhoc attrs : "result"

}
89 changes: 89 additions & 0 deletions new.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package logging

import (
"context"
)

type Attr [2]interface{}

type AttrMap map[string]interface{}

type LoggerV2 interface {
Info(ctx context.Context, msg string, attrs ...Attr)
}

func NewLoggerV2(baseLogger BaseLogger) LoggerV2 {
return loggerV2{
baseLogger: baseLogger,
}
}

type loggerV2 struct {
baseLogger BaseLogger

attrs []Attr
}

func (l loggerV2) log(ctx context.Context, level Level, msg string, attrs ...Attr) {
args := attrsToArgs(l.attrs)
args = append(args, attrsToArgs(attrs)...)
args = append(args, "msg", msg)
l.baseLogger.Log(ctx, level, 1, args...)
}

func (l loggerV2) Info(ctx context.Context, msg string, attrs ...Attr) {
l.log(ctx, InfoLevel, msg, attrs...)
}

type loggerKeyType struct{}

var loggerKey loggerKeyType = struct{}{}

func WithLogger(ctx context.Context, attrs ...Attr) context.Context {
if parentLogger, ok := GetLoggerV2(ctx).(loggerV2); ok {
attrs = append(attrs, parentLogger.attrs...)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inherit parent scope logger attrs


logger := loggerV2{
baseLogger: globalLogger,
attrs: attrs,
}

ctx = context.WithValue(ctx, loggerKey, logger)

return ctx
}

func GetLoggerV2(ctx context.Context) LoggerV2 {
logger, ok := ctx.Value(loggerKey).(LoggerV2)
if !ok {
return NewLoggerV2(globalLogger)
}
return logger
}

func InfoV2(ctx context.Context, msg string, attrs ...Attr) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using verbose function name InfoV2 just to avoid conflict with existing Info() in old interface.

We will have to deal with function conflict if we are adding new interfaces. But I am open for other options like
a) completely remove old interface from this library (as this library is not yet published, backward compatibility is not the mandatory consideration IMO)
b) create v2 version of this library, and fully switching to new interface
c) adding new interface in a sub-package (but i have no idea a good name atm)

logger := GetLoggerV2(ctx)
logger.Info(ctx, msg, attrs...)
}

func InfoV3(ctx context.Context, msg string, attrs AttrMap) {
logger := GetLoggerV2(ctx)
var args []Attr
for k, v := range attrs {
args = append(args, WithAttr(k, v))
}
logger.Info(ctx, msg, args...)
}

func WithAttr(key string, value interface{}) Attr {
return [2]interface{}{key, value}
}

func attrsToArgs(attrs []Attr) []interface{} {
args := make([]interface{}, 0, len(attrs)*2)
for _, attr := range attrs {
args = append(args, attr[0], attr[1])
}
return args
}