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

feat: add fields with logx methods, support using third party logging libs. #1847

Merged
merged 14 commits into from May 3, 2022

Conversation

kevwan
Copy link
Contributor

@kevwan kevwan commented Apr 30, 2022

Examples:

logx.Infow("fetch URL",
	logx.Field("url", "http://localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)
logx.Errorw("failed to fetch URL",
	logx.Field("url", "http://localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)
logx.Sloww("slow to fetch URL",
	logx.Field("url", "http://localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)
logx.Error("hello")
logx.Infov(map[string]interface{}{
	"url":     "localhost:8080/hello",
	"attempt": 3,
	"backoff": time.Second,
})
logx.WithDuration(1100*time.Microsecond).Infow("slow to fetch URL",
	logx.Field("url", "localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)
logx.WithContext(context.Background()).WithDuration(1100*time.Microsecond).Errorw(
	"failed to fetch URL",
	logx.Field("url", "localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)
logx.WithDuration(1100*time.Microsecond).WithContext(context.Background()).Errorw(
	"failed to fetch URL",
	logx.Field("url", "localhost:8080/hello"),
	logx.Field("attempt", 3),
	logx.Field("backoff", time.Second),
)

Outputs like:

{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:63","content":"failed to fetch URL","level":"error","url":"http://localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:68","content":"slow to fetch URL","level":"slow","url":"http://localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","caller":"main.go:73","content":"hello","level":"error"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:58","content":"fetch URL","level":"info","url":"http://localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","caller":"main.go:74","content":{"attempt":3,"backoff":1000000000,"url":"localhost:8080/hello"},"level":"info"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:79","content":"slow to fetch URL","duration":"1.1ms","level":"info","url":"localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:84","content":"failed to fetch URL","duration":"1.1ms","level":"error","url":"localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:00:23.692+08:00","attempt":3,"backoff":"1s","caller":"main.go:90","content":"failed to fetch URL","duration":"1.1ms","level":"error","url":"localhost:8080/hello"}
{"@timestamp":"2022-04-30T13:01:23.693+08:00","caller":"usage.go:61","content":"CPU: 0m, MEMORY: Alloc=2.1Mi, TotalAlloc=2.1Mi, Sys=14.5Mi, NumGC=0","level":"stat"}

Using third party libs for logging:

https://github.com/zeromicro/zero-contrib/tree/main/logx/zapx

https://github.com/zeromicro/zero-contrib/tree/main/logx/logrusx

@kevwan kevwan marked this pull request as draft April 30, 2022 05:03
@kevwan kevwan added this to the v1.3.4 milestone Apr 30, 2022
@kevwan kevwan added the feature Marks an issue or PR as related to a new feature. label Apr 30, 2022
@smithyj
Copy link
Contributor

smithyj commented May 1, 2022

This feature is quite useful.

@zeromicro zeromicro deleted a comment from Issues-translate-bot May 1, 2022
@kevwan kevwan marked this pull request as ready for review May 1, 2022 07:25
@kevwan kevwan self-assigned this May 1, 2022
Copy link
Collaborator

@zhoushuguang zhoushuguang left a comment

Choose a reason for hiding this comment

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

+LGTM

Copy link
Contributor

@fynxiu fynxiu left a comment

Choose a reason for hiding this comment

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

LGTM

@nchtw164
Copy link

nchtw164 commented May 2, 2022

LGTM

@kevwan kevwan requested review from zhoushuguang and fynxiu May 2, 2022 11:35
@kevwan kevwan changed the title feat: add fields with logx methods. feat: add fields with logx methods, support using third party logging libs. May 2, 2022
@kevwan kevwan requested a review from re-dylan May 2, 2022 11:56
@kevwan kevwan merged commit ac321fc into zeromicro:master May 3, 2022
@kevwan kevwan deleted the feat/logx branch May 3, 2022 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Marks an issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

recommend logx add feature in the future logx add support logx.SetLogger
6 participants