1
0
mirror of https://github.com/go-kratos/kratos.git synced 2024-12-26 20:54:38 +02:00
kratos/log
kvii 311016862b
feat: use writer directly in stdLogger. (#3121)
* feat: use writer directly in stdLogger.

* fix: Add newlines for each Log

* fix: remove break changes.

---------

Co-authored-by: Daemon <haiyux@foxmail.com>
2024-03-16 14:25:37 +08:00
..
filter_test.go fix(log): WithContext() changed the ctx field of the parent log.Filter (#3069) 2023-11-22 11:35:58 +08:00
filter.go fix(log): WithContext() changed the ctx field of the parent log.Filter (#3069) 2023-11-22 11:35:58 +08:00
global_test.go fix: typo and grammatical errors (#2368) 2022-09-19 10:39:54 +08:00
global.go fix logger caller depth (#2283) 2022-08-16 22:54:55 +08:00
helper_test.go feat(log): Prevent log.Helper sprintf from running early #3209 (#3221) 2024-03-15 11:09:57 +08:00
helper_writer_test.go feat(log): Helper implemented io.Writer (#1927) 2022-05-06 15:59:50 +08:00
helper_writer.go fix: typo and grammatical errors (#2368) 2022-09-19 10:39:54 +08:00
helper.go feat(log): Prevent log.Helper sprintf from running early #3209 (#3221) 2024-03-15 11:09:57 +08:00
level_test.go fix(log): toString float32 precision loss and convert uint use FormatUint (#2461) 2022-10-19 15:07:58 +08:00
level.go fix(log): toString float32 precision loss and convert uint use FormatUint (#2461) 2022-10-19 15:07:58 +08:00
log_test.go fix(log): WithContext() changed the ctx field of the parent log.Filter (#3069) 2023-11-22 11:35:58 +08:00
log.go fix(log): WithContext() changed the ctx field of the parent log.Filter (#3069) 2023-11-22 11:35:58 +08:00
README.md feat: add logrus to contrib log (#2077) 2022-07-07 23:07:03 +08:00
std_test.go feat: use writer directly in stdLogger. (#3121) 2024-03-16 14:25:37 +08:00
std.go feat: use writer directly in stdLogger. (#3121) 2024-03-16 14:25:37 +08:00
value_test.go test: add log test (#1576) 2021-10-21 17:39:33 +08:00
value.go fix(log): DefaultCaller doesn't returns "pkg/file:line", it returns "file:line" now. For example, both biz/user.go and data/user.go are printed as user.go in logger. It's not clear. (#2274) 2022-08-13 13:00:02 +08:00

Logger

Usage

Structured logging

logger := log.NewStdLogger(os.Stdout)
// fields & valuer
logger = log.With(logger,
    "service.name", "hellworld",
    "service.version", "v1.0.0",
    "ts", log.DefaultTimestamp,
    "caller", log.DefaultCaller,
)
logger.Log(log.LevelInfo, "key", "value")

// helper
helper := log.NewHelper(logger)
helper.Log(log.LevelInfo, "key", "value")
helper.Info("info message")
helper.Infof("info %s", "message")
helper.Infow("key", "value")

// filter
log := log.NewHelper(log.NewFilter(logger,
	log.FilterLevel(log.LevelInfo),
	log.FilterKey("foo"),
	log.FilterValue("bar"),
	log.FilterFunc(customFilter),
))
log.Debug("debug log")
log.Info("info log")
log.Warn("warn log")
log.Error("warn log")

Third party log library

zap

go get -u github.com/go-kratos/kratos/contrib/log/zap/v2

logrus

go get -u github.com/go-kratos/kratos/contrib/log/logrus/v2

fluent

go get -u github.com/go-kratos/kratos/contrib/log/fluent/v2

aliyun

go get -u github.com/go-kratos/kratos/contrib/log/aliyun/v2