基于 `zap` 包封装。除了实现 `Go` 日志包的基本功能外,还实现了很多高级功能 本包基于`github.com/tkestack/tke/pkg/util/log`裁剪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.8 KiB

// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"cynking.com/pkg/log"
)
var (
h bool
level int
format string
)
func main() {
flag.BoolVar(&h, "h", false, "Print this help.")
flag.IntVar(&level, "l", 0, "Log level.")
flag.StringVar(&format, "f", "console", "log output format.")
flag.Parse()
if h {
flag.Usage()
return
}
// logger配置
opts := &log.Options{
Level: "debug",
Format: "console",
EnableColor: true, // if you need output to local path, with EnableColor must be false.
DisableCaller: true,
OutputPaths: []string{"test.log", "stdout"},
ErrorOutputPaths: []string{"error.log"},
}
// 初始化全局logger
log.Init(opts)
defer log.Flush()
// Debug、Info(with field)、Warnf、Errorw使用
log.Debug("This is a debug message")
log.Info("This is a info message", log.Int32("int_key", 10))
log.Warnf("This is a formatted %s message", "warn")
log.Errorw("Message printed with Errorw", "X-Request-ID", "fbf54504-64da-4088-9b86-67824a7fb508")
// WithValues使用
lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
lv.Infow("Info message printed with [WithValues] logger")
lv.Infow("Debug message printed with [WithValues] logger")
// Context使用
ctx := lv.WithContext(context.Background())
lc := log.FromContext(ctx)
lc.Info("Message printed with [WithContext] logger")
ln := lv.WithName("test")
ln.Info("Message printed with [WithName] logger")
// V level使用
log.V(log.InfoLevel).Info("This is a V level message")
log.V(log.ErrorLevel).
Infow("This is a V level message with fields", "X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
}