Skip to content

Commit 36296fb

Browse files
committed
添加root Logger meta
1 parent bce8a33 commit 36296fb

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

logger/zap/config.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
21
package zap
32

43
import (
54
"time"
5+
6+
"go.uber.org/zap"
67
)
78

89
// Config contains the configuration options for the logger. To create a Config
910
// from a common.Config use logp/config.Build.
1011
type Config struct {
11-
Name string `config:",ignore"` // Name of the Logger (for default file name).
12-
JSON bool `config:"json"` // Write logs as JSON.
13-
Level Level `config:"level"` // Logging level (error, warning, info, debug).
14-
Selectors []string `config:"selectors"` // Selectors for debug level logging.
15-
16-
ToStderr bool `config:"to_stderr"`
17-
ToSyslog bool `config:"to_syslog"`
18-
ToFiles bool `config:"to_files"`
19-
ToEventLog bool `config:"to_eventlog"`
12+
Name string // Name of the Logger (for default file name).
13+
JSON bool // Write logs as JSON.
14+
Level Level // Logging level (error, warning, info, debug).
15+
Metas []zap.Field // Root Logger Metas
16+
Selectors []string // Selectors for debug level logging.
17+
18+
ToStderr bool
19+
ToSyslog bool
20+
ToFiles bool
21+
ToEventLog bool
2022
toObserver bool
2123
toIODiscard bool
2224

23-
Files FileConfig `config:"files"`
25+
Files FileConfig
2426

2527
addCaller bool // Adds package and line number info to messages.
2628
development bool // Controls how DPanic behaves.
2729
}
2830

2931
// FileConfig contains the configuration options for the file output.
3032
type FileConfig struct {
31-
Path string `config:"path"`
32-
Name string `config:"name"`
33-
MaxSize uint `config:"rotateeverybytes" validate:"min=1"`
34-
MaxBackups uint `config:"keepfiles" validate:"max=1024"`
35-
Permissions uint32 `config:"permissions"`
36-
Interval time.Duration `config:"interval"`
37-
RotateOnStartup bool `config:"rotateonstartup"`
38-
RedirectStderr bool `config:"redirect_stderr"`
33+
Path string
34+
Name string
35+
MaxSize uint
36+
MaxBackups uint
37+
Permissions uint32
38+
Interval time.Duration
39+
RotateOnStartup bool
40+
RedirectStderr bool
3941
}
4042

4143
var defaultConfig = Config{

logger/zap/core.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ func makeOptions(cfg Config) []zap.Option {
154154
if cfg.development {
155155
options = append(options, zap.Development())
156156
}
157+
158+
if len(cfg.Metas) > 0 {
159+
options = append(options, zap.Fields(cfg.Metas...))
160+
}
161+
157162
return options
158163
}
159164

0 commit comments

Comments
 (0)