Configure logging level and format

This commit is contained in:
Dan Sosedoff
2022-12-06 12:09:21 -06:00
parent 3e1a93296e
commit dbd3b26f6c
3 changed files with 36 additions and 3 deletions

View File

@@ -158,8 +158,9 @@ func initOptions() {
os.Exit(0)
}
if options.Debug {
logger.SetLevel(logrus.DebugLevel)
if err := configureLogger(opts); err != nil {
exitWithMessage(err.Error())
return
}
if options.ReadOnly {
@@ -175,6 +176,29 @@ func initOptions() {
printVersion()
}
func configureLogger(opts command.Options) error {
if options.Debug {
logger.SetLevel(logrus.DebugLevel)
} else {
lvl, err := logrus.ParseLevel(opts.LogLevel)
if err != nil {
return err
}
logger.SetLevel(lvl)
}
switch options.LogFormat {
case "text":
logger.SetFormatter(&logrus.TextFormatter{})
case "json":
logger.SetFormatter(&logrus.JSONFormatter{})
default:
return fmt.Errorf("invalid logger format: %v", options.LogFormat)
}
return nil
}
func printVersion() {
chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)}