-
Notifications
You must be signed in to change notification settings - Fork 4
/
root.go
180 lines (148 loc) · 3.93 KB
/
root.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
// Package logger wrapped zap as a basic logging implement.
package logger
import (
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/robfig/cron/v3"
"go.uber.org/zap"
"gopkg.in/natefinch/lumberjack.v2"
)
var (
root *zap.Logger
defaultOption = &Option{
Level: DEBUG,
Flags: OPT_DEFAULT,
}
SchemeTCP = "tcp"
SchemeUDP = "udp"
)
const (
NameKeyMod = "mod"
NameKeyMsg = "msg"
NameKeyLevel = "lev"
NameKeyTime = "ts"
NameKeyPos = "pos"
)
func doSetGlobalRootLogger(fpath, level string, options int) error {
if fpath == "" {
return fmt.Errorf("fpath should not empty")
}
mtx.Lock()
defer mtx.Unlock()
if root != nil {
return nil
}
var err error
root, err = newRootLogger(fpath, level, options)
if err != nil {
return err
}
return nil
}
// SetGlobalRootLogger deprecated, use InitRoot() instead.
func SetGlobalRootLogger(fpath, level string, options int) error {
return doSetGlobalRootLogger(fpath, level, options)
}
// InitRoot used to setup global root logger, include
// - log level
// - log path
// - set to disk file(with or without rotate)
// - set to some remtoe TCP/UDP server
// - a bounch of other OPT_XXXs
func InitRoot(opt *Option) error {
if opt == nil {
opt = defaultOption
}
switch opt.Level {
case DEBUG, INFO, WARN, ERROR, PANIC, FATAL, DPANIC:
case "": // 默认使用 DEBUG
opt.Level = DEBUG
default:
return fmt.Errorf("invalid log level `%s'", opt.Level)
}
if opt.Flags == 0 {
opt.Flags = OPT_DEFAULT
}
if opt.Path != "" && (opt.Flags&OPT_STDOUT != 0) {
return fmt.Errorf("set stdout logging with log path '%s', flag:%b", opt.Path, opt.Flags)
}
switch opt.Path {
case "":
if v, ok := os.LookupEnv("LOGGER_PATH"); ok {
opt.Path = v
return setRootLoggerFromEnv(opt)
}
return doSetStdoutLogger(opt)
default:
return doSetGlobalRootLogger(opt.Path, opt.Level, opt.Flags)
}
}
func newRootLogger(fpath, level string, options int) (*zap.Logger, error) {
if fpath == "" {
return newNormalRootLogger(fpath, level, options)
}
u, err := url.Parse(fpath)
if err != nil {
return nil, err
}
switch strings.ToLower(u.Scheme) {
case SchemeTCP, SchemeUDP: // logs sending to some remote TCP/UDP server
return newCustomizeRootLogger(level,
options,
&remoteEndpoint{protocol: u.Scheme, host: u.Host})
default: // they must be some disk path file
if _, err := os.Stat(fpath); err != nil { // create file if not exists
if err := os.MkdirAll(filepath.Dir(fpath), 0o600); err != nil {
return nil, fmt.Errorf("MkdirAll(%s): %w", fpath, err)
}
// create empty log file
if err := os.WriteFile(fpath, nil, 0o600); err != nil {
return nil, fmt.Errorf("WriteFile(%s): %w", fpath, err)
}
}
}
// auto-rotate disk logging file
if options&OPT_ROTATE != 0 &&
options&OPT_STDOUT == 0 && // can't rotate stdout
fpath != os.DevNull { // can't rotate(rename) /dev/null
return newCustomizeRootLogger(level, options, &lumberjack.Logger{
Filename: fpath,
MaxSize: MaxSize,
MaxBackups: MaxBackups,
MaxAge: MaxAge,
})
}
return newNormalRootLogger(fpath, level, options)
}
// InitCustomizeRoot used to setup global root logger, include
// - log path
// - log maxsize
// - log compress
func InitCustomizeRoot(opt *Option) (*zap.Logger, error) {
mtx.Lock()
defer mtx.Unlock()
lumberLog := &lumberjack.Logger{
Filename: opt.Path,
MaxSize: opt.MaxSize,
Compress: opt.Compress,
}
c := cron.New(cron.WithSeconds())
if _, err := c.AddFunc("50 59 * * * *",
func() {
if err := lumberLog.Rotate(); err != nil {
log.Printf("lumberLog.Rotate: %s, ignored", err.Error())
}
}); err != nil {
return nil, err
}
c.Start()
return newOnlyMessageRootLogger(lumberLog)
}