基于 `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.

59 lines
1.2 KiB

2 years ago
  1. // Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "context"
  7. "flag"
  8. "cynking.com/pkg/log"
  9. )
  10. var (
  11. h bool
  12. level int
  13. format string
  14. )
  15. func main() {
  16. flag.BoolVar(&h, "h", false, "Print this help.")
  17. flag.IntVar(&level, "l", 0, "Log level.")
  18. flag.StringVar(&format, "f", "console", "log output format.")
  19. flag.Parse()
  20. if h {
  21. flag.Usage()
  22. return
  23. }
  24. // logger配置
  25. opts := &log.Options{
  26. Level: "debug",
  27. Format: "console",
  28. EnableColor: true,
  29. DisableCaller: true,
  30. OutputPaths: []string{"test.log", "stdout"},
  31. ErrorOutputPaths: []string{"error.log"},
  32. }
  33. // 初始化全局logger
  34. log.Init(opts)
  35. defer log.Flush()
  36. // WithValues使用
  37. lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
  38. // Context使用
  39. lv.Infof("Start to call pirntString function")
  40. ctx := lv.WithContext(context.Background())
  41. pirntString(ctx, "World")
  42. }
  43. func pirntString(ctx context.Context, str string) {
  44. lc := log.FromContext(ctx)
  45. lc.Infof("Hello %s", str)
  46. }