Browse Source

Added explicit logfmt logging format

master
Drew Short 3 years ago
parent
commit
871ec04109
  1. 21
      cmd/root.go

21
cmd/root.go

@ -31,13 +31,15 @@ import (
type LogFormat enumflag.Flag type LogFormat enumflag.Flag
const ( const (
TEXT LogFormat = iota
JSON
LoggingLogFmt LogFormat = iota
LoggingText
LoggingJson
) )
var LogFormatIds = map[LogFormat][]string{ var LogFormatIds = map[LogFormat][]string{
TEXT: {"text"},
JSON: {"json"},
LoggingLogFmt: {"logfmt"},
LoggingText: {"text"},
LoggingJson: {"json"},
} }
var ( var (
@ -79,7 +81,7 @@ func init() {
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.pinned-package-updater.yaml OR ./.pinned-package-updater.yaml)") rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.pinned-package-updater.yaml OR ./.pinned-package-updater.yaml)")
rootCmd.PersistentFlags().CountVarP(&logVerbosity, "verbose", "v", "verbose (-v, or -vv)") rootCmd.PersistentFlags().CountVarP(&logVerbosity, "verbose", "v", "verbose (-v, or -vv)")
rootCmd.PersistentFlags().VarP(enumflag.New(&logFormat, "logFormat", LogFormatIds, enumflag.EnumCaseInsensitive), "format", "", "The logging format to use [text, json]")
rootCmd.PersistentFlags().VarP(enumflag.New(&logFormat, "logFormat", LogFormatIds, enumflag.EnumCaseInsensitive), "format", "", "The logging format to use [logfmt, text, json]")
// Cobra also supports local flags, which will only run // Cobra also supports local flags, which will only run
// when this action is called directly. // when this action is called directly.
@ -131,9 +133,14 @@ func initLogging() {
} }
switch logFormat { switch logFormat {
case TEXT:
case LoggingLogFmt:
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
case LoggingText:
log.SetFormatter(&log.TextFormatter{}) log.SetFormatter(&log.TextFormatter{})
case JSON:
case LoggingJson:
log.SetFormatter(&log.JSONFormatter{}) log.SetFormatter(&log.JSONFormatter{})
default: default:
log.SetFormatter(&log.TextFormatter{}) log.SetFormatter(&log.TextFormatter{})

Loading…
Cancel
Save