add logging fields

This commit is contained in:
Alex Goodman 2020-05-21 15:54:54 -04:00
parent 3e71315195
commit b7c7c5556d
No known key found for this signature in database
GPG key ID: 86E2870463D5E890
10 changed files with 126 additions and 130 deletions

View file

@ -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))
}

View file

@ -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)
}
}

View file

@ -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
}

10
imgbom/logger/logger.go Normal file
View file

@ -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
}

29
internal/log/log.go Normal file
View file

@ -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)
}

12
internal/log/nop.go Normal file
View file

@ -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{} }

View file

@ -1,11 +0,0 @@
package logger
var Log Logger
func init() {
SetLogger(&nopLogger{})
}
func SetLogger(logger Logger) {
Log = logger
}

View file

@ -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)
// }

View file

@ -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{} }

View file

@ -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}
}