Refactoring
- logging interface - log levels - application interface Signed-off-by: Jan Tytgat <jan.tytgat@corelayer.eu>
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
|
||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -10,20 +13,25 @@ const (
|
||||
LogOutputFile = "file"
|
||||
)
|
||||
|
||||
var logLevelFlag string
|
||||
var logOutputFlag string
|
||||
var logTypeFlag string
|
||||
var (
|
||||
logLevelFlagName string = "log-level"
|
||||
logLevelFlag string
|
||||
logOutputFlagName string = "log-output"
|
||||
logOutputFlag string
|
||||
logTypeFlagName = "log-type"
|
||||
logTypeFlag string
|
||||
)
|
||||
|
||||
func addLogLevelFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().StringVarP(&logLevelFlag, "log-level", "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
||||
cmd.PersistentFlags().StringVarP(&logLevelFlag, logLevelFlagName, "", "info", "Set log level (trace, debug, info, warn, error, fatal)")
|
||||
}
|
||||
|
||||
func addLogOutputFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().StringVarP(&logOutputFlag, "log-outWriter", "", "stderr", "Set log outWriter (stdout, stderr, file)")
|
||||
cmd.PersistentFlags().StringVarP(&logOutputFlag, logOutputFlagName, "", "stderr", "Set log output (stdout, stderr, file)")
|
||||
}
|
||||
|
||||
func addLogTypeFlag(cmd *cobra.Command) {
|
||||
cmd.PersistentFlags().StringVarP(&logTypeFlag, "log-type", "", "text", "Set log type (text, json, color)")
|
||||
cmd.PersistentFlags().StringVarP(&logTypeFlag, logTypeFlagName, "", "text", "Set log type (text, json, color)")
|
||||
}
|
||||
|
||||
func configureLoggingFlags(cmd *cobra.Command) {
|
||||
@ -33,3 +41,12 @@ func configureLoggingFlags(cmd *cobra.Command) {
|
||||
|
||||
cmd.MarkFlagsMutuallyExclusive("no-color", "log-type")
|
||||
}
|
||||
|
||||
func GetLogLevelFromArgs(args []string) slog.Level {
|
||||
for i, arg := range args {
|
||||
if arg == "--log-level" && i+1 < len(args) {
|
||||
return slogd.Level(args[i+1])
|
||||
}
|
||||
}
|
||||
return slogd.LevelDefault
|
||||
}
|
||||
|
Reference in New Issue
Block a user