expose real zap logger to main package

This commit is contained in:
Alex Goodman 2020-05-26 08:21:49 -04:00
parent 1b9effeb49
commit bd09327d40
No known key found for this signature in database
GPG Key ID: 86E2870463D5E890
3 changed files with 27 additions and 21 deletions

View File

@ -7,14 +7,15 @@ import (
"github.com/anchore/imgbom/imgbom" "github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/internal/config" "github.com/anchore/imgbom/internal/config"
"github.com/anchore/imgbom/internal/format" "github.com/anchore/imgbom/internal/format"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/imgbom/internal/logger" "github.com/anchore/imgbom/internal/logger"
"github.com/anchore/stereoscope" "github.com/anchore/stereoscope"
"github.com/spf13/viper" "github.com/spf13/viper"
"go.uber.org/zap"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
) )
var appConfig *config.Application var appConfig *config.Application
var log *zap.SugaredLogger
func initAppConfig() { func initAppConfig() {
cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOpts) cfg, err := config.LoadConfigFromFile(viper.GetViper(), &cliOpts)
@ -34,9 +35,10 @@ func initLogging() {
FileLocation: appConfig.Log.FileLocation, FileLocation: appConfig.Log.FileLocation,
} }
appLogger := logger.NewZapLogger(config) logWrapper := logger.NewZapLogger(config)
imgbom.SetLogger(appLogger) log = logWrapper.Logger
stereoscope.SetLogger(appLogger) imgbom.SetLogger(logWrapper)
stereoscope.SetLogger(logWrapper)
} }
func logAppConfig() { func logAppConfig() {

View File

@ -7,7 +7,6 @@ import (
"github.com/anchore/imgbom/imgbom" "github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/imgbom/presenter" "github.com/anchore/imgbom/imgbom/presenter"
"github.com/anchore/imgbom/internal" "github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope" "github.com/anchore/stereoscope"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -24,7 +23,7 @@ Supports the following image sources:
"appName": internal.ApplicationName, "appName": internal.ApplicationName,
}), }),
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
Run: doRunCmd, Run: runCmdWrapper,
} }
func init() { func init() {
@ -42,13 +41,17 @@ func Execute() {
} }
} }
func doRunCmd(cmd *cobra.Command, args []string) { func runCmdWrapper(cmd *cobra.Command, args []string) {
os.Exit(doRunCmd(cmd, args))
}
func doRunCmd(cmd *cobra.Command, args []string) int {
userImageStr := args[0] userImageStr := args[0]
log.Infof("Fetching image '%s'", userImageStr) log.Infof("Fetching image '%s'", userImageStr)
img, err := stereoscope.GetImage(userImageStr) img, err := stereoscope.GetImage(userImageStr)
if err != nil { if err != nil {
log.Errorf("could not fetch image '%s': %w", userImageStr, err) log.Errorf("could not fetch image '%s': %w", userImageStr, err)
os.Exit(1) return 1
} }
defer stereoscope.Cleanup() defer stereoscope.Cleanup()
@ -56,13 +59,14 @@ func doRunCmd(cmd *cobra.Command, args []string) {
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt) catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
if err != nil { if err != nil {
log.Errorf("could not catalog image: %w", err) log.Errorf("could not catalog image: %w", err)
os.Exit(1) return 1
} }
log.Info("Complete!") log.Info("Complete!")
err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog) err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog)
if err != nil { if err != nil {
log.Errorf("could not format catalog results: %w", err) log.Errorf("could not format catalog results: %w", err)
os.Exit(1) return 1
} }
return 0
} }

View File

@ -27,8 +27,8 @@ type LogConfig struct {
} }
type ZapLogger struct { type ZapLogger struct {
config LogConfig Config LogConfig
sugaredLogger *zap.SugaredLogger Logger *zap.SugaredLogger
} }
// TODO: Consider a human readable text encoder for better field handeling: // TODO: Consider a human readable text encoder for better field handeling:
@ -39,7 +39,7 @@ type ZapLogger struct {
// - Register the encoder: https://github.com/uber-go/zap/blob/v1.15.0/encoder.go // - Register the encoder: https://github.com/uber-go/zap/blob/v1.15.0/encoder.go
func NewZapLogger(config LogConfig) *ZapLogger { func NewZapLogger(config LogConfig) *ZapLogger {
appLogger := ZapLogger{ appLogger := ZapLogger{
config: config, Config: config,
} }
cores := []zapcore.Core{} cores := []zapcore.Core{}
@ -60,7 +60,7 @@ func NewZapLogger(config LogConfig) *ZapLogger {
// AddCallerSkip skips 2 number of callers, this is important else the file that gets // AddCallerSkip skips 2 number of callers, this is important else the file that gets
// logged will always be the wrapped file (In our case logger.go) // logged will always be the wrapped file (In our case logger.go)
appLogger.sugaredLogger = zap.New( appLogger.Logger = zap.New(
combinedCore, combinedCore,
zap.AddCallerSkip(2), zap.AddCallerSkip(2),
zap.AddCaller(), zap.AddCaller(),
@ -71,7 +71,7 @@ func NewZapLogger(config LogConfig) *ZapLogger {
func (l *ZapLogger) GetNamedLogger(name string) *ZapLogger { func (l *ZapLogger) GetNamedLogger(name string) *ZapLogger {
return &ZapLogger{ return &ZapLogger{
sugaredLogger: l.sugaredLogger.Named(name), Logger: l.Logger.Named(name),
} }
} }
@ -94,7 +94,7 @@ func (l *ZapLogger) nameEncoder(loggerName string, enc zapcore.PrimitiveArrayEnc
} }
func (l *ZapLogger) consoleLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { func (l *ZapLogger) consoleLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
if level != zapcore.InfoLevel || l.config.Level == zapcore.DebugLevel { if level != zapcore.InfoLevel || l.Config.Level == zapcore.DebugLevel {
color, ok := levelToColor[level] color, ok := levelToColor[level]
if !ok { if !ok {
enc.AppendString("[" + level.CapitalString() + "]") enc.AppendString("[" + level.CapitalString() + "]")
@ -121,21 +121,21 @@ func (l *ZapLogger) logFileWriter(location string) zapcore.WriteSyncer {
} }
func (l *ZapLogger) Debugf(format string, args ...interface{}) { func (l *ZapLogger) Debugf(format string, args ...interface{}) {
l.sugaredLogger.Debugf(format, args...) l.Logger.Debugf(format, args...)
} }
func (l *ZapLogger) Infof(format string, args ...interface{}) { func (l *ZapLogger) Infof(format string, args ...interface{}) {
l.sugaredLogger.Infof(format, args...) l.Logger.Infof(format, args...)
} }
func (l *ZapLogger) Debug(args ...interface{}) { func (l *ZapLogger) Debug(args ...interface{}) {
l.sugaredLogger.Debug(args...) l.Logger.Debug(args...)
} }
func (l *ZapLogger) Info(args ...interface{}) { func (l *ZapLogger) Info(args ...interface{}) {
l.sugaredLogger.Info(args...) l.Logger.Info(args...)
} }
func (l *ZapLogger) Errorf(format string, args ...interface{}) { func (l *ZapLogger) Errorf(format string, args ...interface{}) {
l.sugaredLogger.Errorf(format, args...) l.Logger.Errorf(format, args...)
} }