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

View File

@ -7,7 +7,6 @@ import (
"github.com/anchore/imgbom/imgbom"
"github.com/anchore/imgbom/imgbom/presenter"
"github.com/anchore/imgbom/internal"
"github.com/anchore/imgbom/internal/log"
"github.com/anchore/stereoscope"
"github.com/spf13/cobra"
)
@ -24,7 +23,7 @@ Supports the following image sources:
"appName": internal.ApplicationName,
}),
Args: cobra.MaximumNArgs(1),
Run: doRunCmd,
Run: runCmdWrapper,
}
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]
log.Infof("Fetching image '%s'", userImageStr)
img, err := stereoscope.GetImage(userImageStr)
if err != nil {
log.Errorf("could not fetch image '%s': %w", userImageStr, err)
os.Exit(1)
return 1
}
defer stereoscope.Cleanup()
@ -56,13 +59,14 @@ func doRunCmd(cmd *cobra.Command, args []string) {
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
if err != nil {
log.Errorf("could not catalog image: %w", err)
os.Exit(1)
return 1
}
log.Info("Complete!")
err = presenter.GetPresenter(appConfig.PresenterOpt).Present(os.Stdout, img, catalog)
if err != nil {
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 {
config LogConfig
sugaredLogger *zap.SugaredLogger
Config LogConfig
Logger *zap.SugaredLogger
}
// 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
func NewZapLogger(config LogConfig) *ZapLogger {
appLogger := ZapLogger{
config: config,
Config: config,
}
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
// logged will always be the wrapped file (In our case logger.go)
appLogger.sugaredLogger = zap.New(
appLogger.Logger = zap.New(
combinedCore,
zap.AddCallerSkip(2),
zap.AddCaller(),
@ -71,7 +71,7 @@ func NewZapLogger(config LogConfig) *ZapLogger {
func (l *ZapLogger) GetNamedLogger(name string) *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) {
if level != zapcore.InfoLevel || l.config.Level == zapcore.DebugLevel {
if level != zapcore.InfoLevel || l.Config.Level == zapcore.DebugLevel {
color, ok := levelToColor[level]
if !ok {
enc.AppendString("[" + level.CapitalString() + "]")
@ -121,21 +121,21 @@ func (l *ZapLogger) logFileWriter(location string) zapcore.WriteSyncer {
}
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{}) {
l.sugaredLogger.Infof(format, args...)
l.Logger.Infof(format, args...)
}
func (l *ZapLogger) Debug(args ...interface{}) {
l.sugaredLogger.Debug(args...)
l.Logger.Debug(args...)
}
func (l *ZapLogger) Info(args ...interface{}) {
l.sugaredLogger.Info(args...)
l.Logger.Info(args...)
}
func (l *ZapLogger) Errorf(format string, args ...interface{}) {
l.sugaredLogger.Errorf(format, args...)
l.Logger.Errorf(format, args...)
}