log.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package options
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/spf13/pflag"
  6. "go.uber.org/zap/zapcore"
  7. "strings"
  8. )
  9. const (
  10. flagLevel = "log.level"
  11. flagDisableCaller = "log.disable-caller"
  12. flagDisableStacktrace = "log.disable-stacktrace"
  13. flagFormat = "log.format"
  14. flagEnableColor = "log.enable-color"
  15. flagOutputPaths = "log.output-paths"
  16. flagErrorOutputPaths = "log.error-output-paths"
  17. flagDevelopment = "log.development"
  18. flagName = "log.name"
  19. consoleFormat = "console"
  20. jsonFormat = "json"
  21. )
  22. type LogOptions struct {
  23. OutputPaths []string `json:"output-paths" mapstructure:"output-paths"`
  24. ErrorOutputPaths []string `json:"error-output-paths" mapstructure:"error-output-paths"`
  25. Level string `json:"level" mapstructure:"level"`
  26. Format string `json:"format" mapstructure:"format"`
  27. DisableCaller bool `json:"disable-caller" mapstructure:"disable-caller"`
  28. DisableStacktrace bool `json:"disable-stacktrace" mapstructure:"disable-stacktrace"`
  29. EnableColor bool `json:"enable-color" mapstructure:"enable-color"`
  30. Development bool `json:"development" mapstructure:"development"`
  31. Name string `json:"name" mapstructure:"name"`
  32. }
  33. func NewLogOptions() *LogOptions {
  34. return &LogOptions{
  35. Level: zapcore.InfoLevel.String(),
  36. DisableCaller: false,
  37. DisableStacktrace: false,
  38. Format: consoleFormat,
  39. EnableColor: false,
  40. Development: false,
  41. OutputPaths: []string{"stdout"},
  42. ErrorOutputPaths: []string{"stderr"},
  43. }
  44. }
  45. func (o *LogOptions) Validate() []error {
  46. var errs []error
  47. var zapLevel zapcore.Level
  48. if err := zapLevel.UnmarshalText([]byte(o.Level)); err != nil {
  49. errs = append(errs, err)
  50. }
  51. format := strings.ToLower(o.Format)
  52. if format != consoleFormat && format != jsonFormat {
  53. errs = append(errs, fmt.Errorf("not a valid log format: %q", o.Format))
  54. }
  55. return errs
  56. }
  57. func (o *LogOptions) AddFlags(fs *pflag.FlagSet) {
  58. fs.StringVar(&o.Level, flagLevel, o.Level, "Minimum log output `LEVEL`.")
  59. fs.BoolVar(&o.DisableCaller, flagDisableCaller, o.DisableCaller, "Disable output of caller information in the log.")
  60. fs.BoolVar(&o.DisableStacktrace, flagDisableStacktrace,
  61. o.DisableStacktrace, "Disable the log to record a stack trace for all messages at or above panic level.")
  62. fs.StringVar(&o.Format, flagFormat, o.Format, "Log output `FORMAT`, support plain or json format.")
  63. fs.BoolVar(&o.EnableColor, flagEnableColor, o.EnableColor, "Enable output ansi colors in plain format logs.")
  64. fs.StringSliceVar(&o.OutputPaths, flagOutputPaths, o.OutputPaths, "Output paths of log.")
  65. fs.StringSliceVar(&o.ErrorOutputPaths, flagErrorOutputPaths, o.ErrorOutputPaths, "Error output paths of log.")
  66. fs.BoolVar(
  67. &o.Development,
  68. flagDevelopment,
  69. o.Development,
  70. "Development puts the logger in development mode, which changes "+
  71. "the behavior of DPanicLevel and takes stacktraces more liberally.",
  72. )
  73. fs.StringVar(&o.Name, flagName, o.Name, "The name of the logger.")
  74. }
  75. func (o *LogOptions) String() string {
  76. data, _ := json.Marshal(o)
  77. return string(data)
  78. }