-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
58 lines (46 loc) · 1.17 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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"
"github.com/cuizhaoyue/toolkit/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,
DisableCaller: true,
OutputPaths: []string{"test.log", "stdout"},
ErrorOutputPaths: []string{"error.log"},
}
// 初始化全局logger
log.Init(opts)
defer log.Flush()
// WithValues使用
lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
// Context使用
lv.Infof("Start to call pirntString function")
ctx := lv.WithContext(context.Background())
pirntString(ctx, "World")
}
func pirntString(ctx context.Context, str string) {
lc := log.FromContext(ctx)
lc.Infof("Hello %s", str)
}