Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultOtelSugaredLogger 输出的 Caller 似乎多跳了一跳? #1

Open
bigboss2063 opened this issue Aug 12, 2024 · 5 comments
Open
Assignees

Comments

@bigboss2063
Copy link

package main

import (
	"github.com/logerror/easylog"
)

func main() {
	easylog.InitGlobalLogger()
	easylog.DefaultSugaredLogger().Info("hello world")
	easylog.DefaultOtelSugaredLogger().Info("hello world")
}

输出结果如下:

{"level":"info","time":"2024-08-12 23:33:31.260","caller":"cmd/main.go:9","msg":"hello world"}
{"level":"info","time":"2024-08-12 23:33:31.266","caller":"runtime/proc.go:271","msg":"hello world"}
@bigboss2063
Copy link
Author

bigboss2063 commented Aug 12, 2024

将 option.go 里的默认 callskip 改为 0 之后 DefaultOtelSugaredLogger 正常了,DefaultSugaredLogger 就打印到里面去了。

{"level":"info","time":"2024-08-12 23:37:59.616","caller":"easylog/exported.go:182","msg":"hello world"}
{"level":"info","time":"2024-08-12 23:37:59.622","caller":"cmd/main.go:10","msg":"hello world"}

@bigboss2063
Copy link
Author

l.otelSugaredLogger = otelzap.NewSugaredLogger(l.sugaredLogger.WithOptions(zap.AddCallerSkip(-1)))

这样调整了一下就正常了,但没找出原因所在🤣

@logerror
Copy link
Owner

logerror commented Aug 20, 2024

可以通过 option.WithCallerSkip(2) 定义合适的值

easylog.InitGlobalLogger(option.WithLogLevel("info"),option.WithCallerSkip(2))

然后直接打印日志即可

easylog.Info("hello world")

full code

package main

import (
	"github.com/logerror/easylog"
	"github.com/logerror/easylog/pkg/option"
)

func main() {
	easylog.InitGlobalLogger(option.WithLogLevel("info"), option.WithCallerSkip(2))
	easylog.Info("hello world")
	easylog.Info("hello world")
}

output

{"level":"info","time":"2024-08-20 17:34:38.425","caller":"test/main.go:10","msg":"hello world"}
{"level":"info","time":"2024-08-20 17:34:38.425","caller":"test/main.go:11","msg":"hello world"}

@logerror logerror self-assigned this Aug 20, 2024
@logerror logerror pinned this issue Aug 20, 2024
@logerror logerror unpinned this issue Aug 20, 2024
@bigboss2063
Copy link
Author

如果多个协程同时使用的话,easylog 的初始化跟ReplaceLogger之类的会有竞态问题,可能需要上个读写锁啥的

@logerror
Copy link
Owner

嗯 确实有可能 考虑到 ReplaceLogger 的使用较少,后续考虑加上吧 或者您可以试着完善下?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants