-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add configurable log-level and log-format
- Loading branch information
Showing
6 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
logLevel string | ||
logFormat string | ||
) | ||
|
||
func init() { | ||
var err error | ||
|
||
Command.Flags().StringVarP(&logLevel, "log-level", "l", "info", "Log level (trace, debug, info, warning, error, fatal, panic)") | ||
err = Command.RegisterFlagCompletionFunc( | ||
"log-level", | ||
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"trace", "debug", "info", "warning", "error", "fatal", "panic"}, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
Command.Flags().StringVar(&logFormat, "log-format", "color", "Log format (color, plain, json)") | ||
err = Command.RegisterFlagCompletionFunc( | ||
"log-format", | ||
func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"text", "json"}, cobra.ShellCompDirectiveNoFileComp | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cobra.OnInitialize(initLog) | ||
} | ||
|
||
func initLogLevel(level string) log.Level { | ||
parsed, err := log.ParseLevel(level) | ||
if err != nil { | ||
log.WithField("level", logLevel).Warn("invalid log level. defaulting to info.") | ||
logLevel = "info" | ||
parsed = log.InfoLevel | ||
} | ||
log.SetLevel(parsed) | ||
|
||
return parsed | ||
} | ||
|
||
func initLogFormat(format string) log.Formatter { | ||
var formatter log.Formatter = &log.TextFormatter{} | ||
switch format { | ||
case "color", "c": | ||
break | ||
case "plain", "p": | ||
formatter.(*log.TextFormatter).DisableColors = true | ||
case "json", "j": | ||
formatter = &log.JSONFormatter{} | ||
default: | ||
log.WithField("format", logFormat).Warn("invalid log formatter. defaulting to color.") | ||
} | ||
log.SetFormatter(formatter) | ||
return formatter | ||
} | ||
|
||
func initLog() { | ||
initLogLevel(logLevel) | ||
initLogFormat(logFormat) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cmd | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_initLog(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
}{ | ||
{"default"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
initLog() | ||
}) | ||
} | ||
} | ||
|
||
func Test_initLogFormat(t *testing.T) { | ||
type args struct { | ||
format string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want log.Formatter | ||
}{ | ||
{"default", args{"color"}, &log.TextFormatter{}}, | ||
{"plain", args{"plain"}, &log.TextFormatter{DisableColors: true}}, | ||
{"json", args{"json"}, &log.JSONFormatter{}}, | ||
{"unknown", args{""}, &log.TextFormatter{}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := initLogFormat(tt.args.format); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("initLogFormat() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_initLogLevel(t *testing.T) { | ||
type args struct { | ||
level string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want log.Level | ||
}{ | ||
{"trace", args{"trace"}, log.TraceLevel}, | ||
{"debug", args{"debug"}, log.DebugLevel}, | ||
{"info", args{"info"}, log.InfoLevel}, | ||
{"warning", args{"warning"}, log.WarnLevel}, | ||
{"error", args{"error"}, log.ErrorLevel}, | ||
{"fatal", args{"fatal"}, log.FatalLevel}, | ||
{"panic", args{"panic"}, log.PanicLevel}, | ||
{"unknown", args{""}, log.InfoLevel}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := initLogLevel(tt.args.level); got != tt.want { | ||
t.Errorf("initLogLevel() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters