add named logger + color formatting

This commit is contained in:
Alex Goodman
2020-05-22 11:30:45 -04:00
parent 09c7ca8f8f
commit ae6feed8fc
8 changed files with 103 additions and 27 deletions
+1 -1
View File
@@ -42,5 +42,5 @@ func setCliOptions() {
fmt.Printf("unable to bind flag '%s': %+v", flag, err)
}
rootCmd.Flags().CountVarP(&cliOpts.Verbosity, "verbose", "v", "increase verbosity (-v, -vv, -vvv ...)")
rootCmd.Flags().CountVarP(&cliOpts.Verbosity, "verbose", "v", "increase verbosity (-v = info, -vv = debug)")
}
+7 -5
View File
@@ -1,15 +1,16 @@
package cmd
import (
"encoding/json"
"fmt"
"os"
"github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/internal/config"
"github.com/anchore/imgbom/internal/format"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/imgbom/internal/logger"
"github.com/spf13/viper"
"gopkg.in/yaml.v2"
)
var appConfig *config.Application
@@ -25,10 +26,10 @@ func initAppConfig() {
func initLogging() {
config := logger.LogConfig{
EnableConsole: appConfig.Log.FileLocation == "" && !appConfig.Quiet,
EnableConsole: (appConfig.Log.FileLocation == "" || appConfig.CliOptions.Verbosity > 0) && !appConfig.Quiet,
EnableFile: appConfig.Log.FileLocation != "",
Level: appConfig.Log.LevelOpt,
FormatAsJSON: appConfig.Log.FormatAsJSON,
Structured: appConfig.Log.Structured,
FileLocation: appConfig.Log.FileLocation,
}
@@ -36,10 +37,11 @@ func initLogging() {
}
func logAppConfig() {
appCfgStr, err := json.MarshalIndent(&appConfig, " ", " ")
appCfgStr, err := yaml.Marshal(&appConfig)
if err != nil {
log.Debugf("Could not display application config: %+v", err)
} else {
log.Debugf("Application config:\n%+v", string(appCfgStr))
log.Debugf("Application config:\n%+v", format.Magenta.Format(string(appCfgStr)))
}
}