diff --git a/cmd/config.go b/cmd/config.go index b5676635f..730810d1f 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -4,7 +4,9 @@ import ( "fmt" "os" + "github.com/anchore/imgbom/imgbom" "github.com/anchore/imgbom/internal/config" + "github.com/anchore/imgbom/internal/logger" "github.com/spf13/viper" ) @@ -18,3 +20,15 @@ func loadAppConfig() { } appConfig = cfg } + +func setupLoggingFromAppConfig() { + config := logger.LogConfig{ + EnableConsole: appConfig.Log.FileLocation == "" && !appConfig.Quiet, + EnableFile: appConfig.Log.FileLocation != "", + Level: appConfig.Log.LevelOpt, + FormatAsJSON: appConfig.Log.FormatAsJSON, + FileLocation: appConfig.Log.FileLocation, + } + + imgbom.SetLogger(logger.NewZapLogger(config)) +} diff --git a/cmd/root.go b/cmd/root.go index cda2ba9d2..830c979ec 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -8,7 +8,7 @@ import ( "github.com/anchore/imgbom/imgbom" "github.com/anchore/imgbom/imgbom/presenter" "github.com/anchore/imgbom/internal" - "github.com/anchore/imgbom/internal/logger" + "github.com/anchore/imgbom/internal/log" "github.com/anchore/stereoscope" "github.com/spf13/cobra" ) @@ -37,7 +37,7 @@ func init() { func Execute() { if err := rootCmd.Execute(); err != nil { - logger.Errorf("could not start application: %w", err) + log.Errorf("could not start application: %w", err) os.Exit(1) } } @@ -45,31 +45,31 @@ func Execute() { func doRunCmd(cmd *cobra.Command, args []string) { appCfgStr, err := json.MarshalIndent(&appConfig, " ", " ") if err != nil { - logger.Debugf("could not display application config: %+v", err) + log.Debugf("could not display application config: %+v", err) } else { - logger.Debugf("application config:\n%+v", string(appCfgStr)) + log.Debugf("application config:\n%+v", string(appCfgStr)) } userImageStr := args[0] - logger.Infof("fetching image %s...", userImageStr) + log.Infof("fetching image %s...", userImageStr) img, err := stereoscope.GetImage(userImageStr) if err != nil { - logger.Errorf("could not fetch image '%s': %w", userImageStr, err) + log.Errorf("could not fetch image '%s': %w", userImageStr, err) os.Exit(1) } defer stereoscope.Cleanup() - logger.Info("cataloging image...") + log.Info("cataloging image...") catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt) if err != nil { - logger.Errorf("could not catalog image: %w", err) + log.Errorf("could not catalog image: %w", err) os.Exit(1) } - logger.Info("done!") + log.Info("done!") err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog) if err != nil { - logger.Errorf("could not format catalog results: %w", err) + log.Errorf("could not format catalog results: %w", err) os.Exit(1) } } diff --git a/imgbom/log.go b/imgbom/log.go index 299d91af9..ac2d5d4d1 100644 --- a/imgbom/log.go +++ b/imgbom/log.go @@ -1,7 +1,10 @@ package imgbom -import "github.com/anchore/imgbom/internal/logger" +import ( + "github.com/anchore/imgbom/imgbom/logger" + "github.com/anchore/imgbom/internal/log" +) -func SetLogger(l logger.Logger) { - logger.SetLogger(l) +func SetLogger(logger logger.Logger) { + log.Log = logger } diff --git a/imgbom/logger/logger.go b/imgbom/logger/logger.go new file mode 100644 index 000000000..60518ab83 --- /dev/null +++ b/imgbom/logger/logger.go @@ -0,0 +1,10 @@ +package logger + +type Logger interface { + Errorf(format string, args ...interface{}) + Infof(format string, args ...interface{}) + Info(args ...interface{}) + Debugf(format string, args ...interface{}) + Debug(args ...interface{}) + WithFields(map[string]interface{}) Logger +} diff --git a/internal/log/log.go b/internal/log/log.go new file mode 100644 index 000000000..bb93f3739 --- /dev/null +++ b/internal/log/log.go @@ -0,0 +1,29 @@ +package log + +import "github.com/anchore/imgbom/imgbom/logger" + +var Log logger.Logger = &nopLogger{} + +func Errorf(format string, args ...interface{}) { + Log.Errorf(format, args...) +} + +func Infof(format string, args ...interface{}) { + Log.Infof(format, args...) +} + +func Info(args ...interface{}) { + Log.Info(args...) +} + +func Debugf(format string, args ...interface{}) { + Log.Debugf(format, args...) +} + +func Debug(args ...interface{}) { + Log.Debug(args...) +} + +func WithFields(fields map[string]interface{}) logger.Logger { + return Log.WithFields(fields) +} diff --git a/internal/log/nop.go b/internal/log/nop.go new file mode 100644 index 000000000..e403f190f --- /dev/null +++ b/internal/log/nop.go @@ -0,0 +1,12 @@ +package log + +import "github.com/anchore/imgbom/imgbom/logger" + +type nopLogger struct{} + +func (l *nopLogger) Errorf(format string, args ...interface{}) {} +func (l *nopLogger) Infof(format string, args ...interface{}) {} +func (l *nopLogger) Info(args ...interface{}) {} +func (l *nopLogger) Debugf(format string, args ...interface{}) {} +func (l *nopLogger) Debug(args ...interface{}) {} +func (l *nopLogger) WithFields(fields map[string]interface{}) logger.Logger { return &nopLogger{} } diff --git a/internal/logger/log.go b/internal/logger/log.go deleted file mode 100644 index 279fb90da..000000000 --- a/internal/logger/log.go +++ /dev/null @@ -1,11 +0,0 @@ -package logger - -var Log Logger - -func init() { - SetLogger(&nopLogger{}) -} - -func SetLogger(logger Logger) { - Log = logger -} diff --git a/internal/logger/logger.go b/internal/logger/logger.go deleted file mode 100644 index a2eb2abe8..000000000 --- a/internal/logger/logger.go +++ /dev/null @@ -1,34 +0,0 @@ -package logger - -type Logger interface { - Errorf(format string, args ...interface{}) - Infof(format string, args ...interface{}) - Info(args ...interface{}) - Debugf(format string, args ...interface{}) - Debug(args ...interface{}) - // WithFields(map[string]interface{}) Logger -} - -func Errorf(format string, args ...interface{}) { - Log.Errorf(format, args...) -} - -func Infof(format string, args ...interface{}) { - Log.Infof(format, args...) -} - -func Info(args ...interface{}) { - Log.Info(args...) -} - -func Debugf(format string, args ...interface{}) { - Log.Debugf(format, args...) -} - -func Debug(args ...interface{}) { - Log.Debug(args...) -} - -// func WithFields(fields map[string]interface{}) Logger { -// return Log.WithFields(fields) -// } diff --git a/internal/logger/nop_logger.go b/internal/logger/nop_logger.go deleted file mode 100644 index 01e0b264d..000000000 --- a/internal/logger/nop_logger.go +++ /dev/null @@ -1,11 +0,0 @@ -package logger - -type nopLogger struct{} - -func (l *nopLogger) Errorf(format string, args ...interface{}) {} -func (l *nopLogger) Infof(format string, args ...interface{}) {} -func (l *nopLogger) Info(args ...interface{}) {} -func (l *nopLogger) Debugf(format string, args ...interface{}) {} -func (l *nopLogger) Debug(args ...interface{}) {} - -// func (l *nopLogger) WithFields(fields map[string]interface{}) Logger { return &nopLogger{} } diff --git a/cmd/logger.go b/internal/logger/zap.go similarity index 59% rename from cmd/logger.go rename to internal/logger/zap.go index e91603c1a..53b02bde1 100644 --- a/cmd/logger.go +++ b/internal/logger/zap.go @@ -1,14 +1,14 @@ -package cmd +package logger import ( "os" - "github.com/anchore/imgbom/imgbom" + "github.com/anchore/imgbom/imgbom/logger" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) -type logConfig struct { +type LogConfig struct { EnableConsole bool EnableFile bool FormatAsJSON bool @@ -16,52 +16,11 @@ type logConfig struct { FileLocation string } -type zapLogger struct { +type ZapLogger struct { sugaredLogger *zap.SugaredLogger } -func setupLoggingFromAppConfig() { - setupLogging( - logConfig{ - EnableConsole: appConfig.Log.FileLocation == "" && !appConfig.Quiet, - EnableFile: appConfig.Log.FileLocation != "", - Level: appConfig.Log.LevelOpt, - FormatAsJSON: appConfig.Log.FormatAsJSON, - FileLocation: appConfig.Log.FileLocation, - }, - ) -} - -func setupLogging(config logConfig) { - imgbom.SetLogger(newZapLogger(config)) -} - -func getConsoleEncoder(config logConfig) zapcore.Encoder { - encoderConfig := zap.NewProductionEncoderConfig() - if config.FormatAsJSON { - return zapcore.NewJSONEncoder(encoderConfig) - } - encoderConfig.EncodeTime = nil - encoderConfig.EncodeCaller = nil - encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder - return zapcore.NewConsoleEncoder(encoderConfig) -} - -func getFileEncoder(config logConfig) zapcore.Encoder { - encoderConfig := zap.NewProductionEncoderConfig() - encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder - if config.FormatAsJSON { - return zapcore.NewJSONEncoder(encoderConfig) - } - return zapcore.NewConsoleEncoder(encoderConfig) -} - -func getLogWriter(location string) zapcore.WriteSyncer { - file, _ := os.Create(location) - return zapcore.AddSync(file) -} - -func newZapLogger(config logConfig) *zapLogger { +func NewZapLogger(config LogConfig) *ZapLogger { cores := []zapcore.Core{} if config.EnableConsole { @@ -86,37 +45,62 @@ func newZapLogger(config logConfig) *zapLogger { zap.AddCaller(), ).Sugar() - return &zapLogger{ + return &ZapLogger{ sugaredLogger: logger, } } -func (l *zapLogger) Debugf(format string, args ...interface{}) { +func getConsoleEncoder(config LogConfig) zapcore.Encoder { + encoderConfig := zap.NewProductionEncoderConfig() + if config.FormatAsJSON { + return zapcore.NewJSONEncoder(encoderConfig) + } + encoderConfig.EncodeTime = nil + encoderConfig.EncodeCaller = nil + encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + return zapcore.NewConsoleEncoder(encoderConfig) +} + +func getFileEncoder(config LogConfig) zapcore.Encoder { + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder + if config.FormatAsJSON { + return zapcore.NewJSONEncoder(encoderConfig) + } + return zapcore.NewConsoleEncoder(encoderConfig) +} + +func getLogWriter(location string) zapcore.WriteSyncer { + file, _ := os.Create(location) + return zapcore.AddSync(file) +} + +func (l *ZapLogger) Debugf(format string, args ...interface{}) { l.sugaredLogger.Debugf(format, args...) } -func (l *zapLogger) Infof(format string, args ...interface{}) { +func (l *ZapLogger) Infof(format string, args ...interface{}) { l.sugaredLogger.Infof(format, args...) } -func (l *zapLogger) Debug(args ...interface{}) { +func (l *ZapLogger) Debug(args ...interface{}) { l.sugaredLogger.Debug(args...) } -func (l *zapLogger) Info(args ...interface{}) { +func (l *ZapLogger) Info(args ...interface{}) { l.sugaredLogger.Info(args...) } -func (l *zapLogger) Errorf(format string, args ...interface{}) { +func (l *ZapLogger) Errorf(format string, args ...interface{}) { l.sugaredLogger.Errorf(format, args...) } -// func (l *zapLogger) WithFields(fields map[string]interface{}) logger.Logger { -// var f = make([]interface{}, 0) -// for k, v := range fields { -// f = append(f, k) -// f = append(f, v) -// } -// newLogger := l.sugaredLogger.With(f...) -// return &zapLogger{newLogger} -// } +func (l *ZapLogger) WithFields(fields map[string]interface{}) logger.Logger { + var f = make([]interface{}, 0) + for k, v := range fields { + f = append(f, k) + f = append(f, v) + } + newLogger := l.sugaredLogger.With(f...) + return &ZapLogger{newLogger} +}