/
options.go
55 lines (48 loc) · 2.44 KB
/
options.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
// Copyright 2022 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. The original repo for
// this file is https://github.com/superproj/onex.
//
package log
import (
"github.com/spf13/pflag"
"go.uber.org/zap/zapcore"
)
// Options contains configuration options for logging.
type Options struct {
// DisableCaller specifies whether to include caller information in the log.
DisableCaller bool `json:"disable-caller,omitempty" mapstructure:"disable-caller"`
// DisableStacktrace specifies whether to record a stack trace for all messages at or above panic level.
DisableStacktrace bool `json:"disable-stacktrace,omitempty" mapstructure:"disable-stacktrace"`
// EnableColor specifies whether to output colored logs.
EnableColor bool `json:"enable-color" mapstructure:"enable-color"`
// Level specifies the minimum log level. Valid values are: debug, info, warn, error, dpanic, panic, and fatal.
Level string `json:"level,omitempty" mapstructure:"level"`
// Format specifies the log output format. Valid values are: console and json.
Format string `json:"format,omitempty" mapstructure:"format"`
// OutputPaths specifies the output paths for the logs.
OutputPaths []string `json:"output-paths,omitempty" mapstructure:"output-paths"`
}
// NewOptions creates a new Options object with default values.
func NewOptions() *Options {
return &Options{
Level: zapcore.InfoLevel.String(),
Format: "console",
OutputPaths: []string{"stdout"},
}
}
// Validate verifies flags passed to LogsOptions.
func (o *Options) Validate() []error {
errs := []error{}
return errs
}
// AddFlags adds command line flags for the configuration.
func (o *Options) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Level, "log.level", o.Level, "Minimum log output `LEVEL`.")
fs.BoolVar(&o.DisableCaller, "log.disable-caller", o.DisableCaller, "Disable output of caller information in the log.")
fs.BoolVar(&o.DisableStacktrace, "log.disable-stacktrace", o.DisableStacktrace, ""+
"Disable the log to record a stack trace for all messages at or above panic level.")
fs.BoolVar(&o.EnableColor, "log.enable-color", o.EnableColor, "Enable output ansi colors in plain format logs.")
fs.StringVar(&o.Format, "log.format", o.Format, "Log output `FORMAT`, support plain or json format.")
fs.StringSliceVar(&o.OutputPaths, "log.output-paths", o.OutputPaths, "Output paths of log.")
}