From 1b9effeb49d7a04ce523d998684b2b33ce17a1e8 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sat, 23 May 2020 19:22:52 -0400 Subject: [PATCH] remove WithFields; add stereoscope logging --- cmd/init.go | 5 +++- go.mod | 9 ++++-- go.sum | 14 ++++++++-- imgbom/analyzer/dpkg/analyzer.go | 12 ++------ imgbom/log.go | 6 ---- imgbom/logger/logger.go | 1 - imgbom/presenter/json/presenter.go | 10 ++----- internal/log/log.go | 4 --- internal/log/nop.go | 13 ++++----- internal/logger/zap.go | 44 ++++++++++++------------------ 10 files changed, 49 insertions(+), 69 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 2f82ec8bf..4d194047a 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -9,6 +9,7 @@ import ( "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" "gopkg.in/yaml.v2" ) @@ -33,7 +34,9 @@ func initLogging() { FileLocation: appConfig.Log.FileLocation, } - imgbom.SetLogger(logger.NewZapLogger(config)) + appLogger := logger.NewZapLogger(config) + imgbom.SetLogger(appLogger) + stereoscope.SetLogger(appLogger) } func logAppConfig() { diff --git a/go.mod b/go.mod index 2f313d7b8..100c637d3 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,17 @@ go 1.14 require ( github.com/adrg/xdg v0.2.1 github.com/anchore/go-testutils v0.0.0-20200520222037-edc2bf1864fe - github.com/anchore/stereoscope v0.0.0-20200520221116-025e07f1c93e + github.com/anchore/stereoscope v0.0.0-20200523232006-be5f3c18958f github.com/go-test/deep v1.0.6 + github.com/google/go-containerregistry v0.0.0-20200521151920-a873a21aff23 // indirect github.com/hashicorp/go-multierror v1.1.0 github.com/mitchellh/go-homedir v1.1.0 - github.com/mitchellh/mapstructure v1.3.0 + github.com/mitchellh/mapstructure v1.3.1 github.com/sergi/go-diff v1.1.0 github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.7.0 go.uber.org/zap v1.15.0 - gopkg.in/yaml.v2 v2.2.8 + golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect + google.golang.org/genproto v0.0.0-20200521103424-e9a78aa275b7 // indirect + gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index bb8d56a26..31fe0e62b 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,8 @@ github.com/anchore/go-testutils v0.0.0-20200520222037-edc2bf1864fe h1:YMXe4RA3qy github.com/anchore/go-testutils v0.0.0-20200520222037-edc2bf1864fe/go.mod h1:D3rc2L/q4Hcp9eeX6AIJH4Q+kPjOtJCFhG9za90j+nU= github.com/anchore/stereoscope v0.0.0-20200520221116-025e07f1c93e h1:QBwtrM0MXi0z+GcHk3RoSyzaQ+CLgas0bC/uOd1P+PQ= github.com/anchore/stereoscope v0.0.0-20200520221116-025e07f1c93e/go.mod h1:bkyLl5VITnrmgErv4S1vDfVz/TGAZ5il6161IQo7w2g= +github.com/anchore/stereoscope v0.0.0-20200523232006-be5f3c18958f h1:aPJQyXi8Y7PhnzhUszZfS/23TA5o29UCc3XGreflaqo= +github.com/anchore/stereoscope v0.0.0-20200523232006-be5f3c18958f/go.mod h1:bkyLl5VITnrmgErv4S1vDfVz/TGAZ5il6161IQo7w2g= github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY= @@ -191,6 +193,8 @@ github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-containerregistry v0.0.0-20200430153450-5cbd060f5c92 h1:SR1vV7r+Vn/Ps59OMxxm8T2uQC4LBRsSMtZ4NODG9BA= github.com/google/go-containerregistry v0.0.0-20200430153450-5cbd060f5c92/go.mod h1:pD1UFYs7MCAx+ZLShBdttcaOSbyc8F9Na/9IZLNwJeA= +github.com/google/go-containerregistry v0.0.0-20200521151920-a873a21aff23 h1:42qjTtGOVRefA89EPBmd2Vm+m9x/WrZiCq9n022khSE= +github.com/google/go-containerregistry v0.0.0-20200521151920-a873a21aff23/go.mod h1:pD1UFYs7MCAx+ZLShBdttcaOSbyc8F9Na/9IZLNwJeA= github.com/google/gofuzz v0.0.0-20161122191042-44d81051d367/go.mod h1:HP5RmnzzSNb993RKQDq4+1A4ia9nllfqcQFTQJedwGI= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= @@ -300,8 +304,8 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.3.0 h1:iDwIio/3gk2QtLLEsqU5lInaMzos0hDTz8a6lazSFVw= -github.com/mitchellh/mapstructure v1.3.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA= +github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180320133207-05fbef0ca5da/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -555,6 +559,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= +golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -622,6 +628,8 @@ google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84 h1:pSLkPbrjnPyLDYU google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200519141106-08726f379972 h1:6ydLqG65DIMNJf6p97WudGsmd1w3Ickm/LiZnBrREPI= google.golang.org/genproto v0.0.0-20200519141106-08726f379972/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= +google.golang.org/genproto v0.0.0-20200521103424-e9a78aa275b7 h1:JUs1uIDQ46c7iI0QuMPzAHqXaSmqKF0f9freFMk2ivs= +google.golang.org/genproto v0.0.0-20200521103424-e9a78aa275b7/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= @@ -669,6 +677,8 @@ gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/imgbom/analyzer/dpkg/analyzer.go b/imgbom/analyzer/dpkg/analyzer.go index 26cf69f77..3f18877d2 100644 --- a/imgbom/analyzer/dpkg/analyzer.go +++ b/imgbom/analyzer/dpkg/analyzer.go @@ -42,11 +42,7 @@ func (a *Analyzer) Analyze(contents map[file.Reference]string) ([]pkg.Package, e content, ok := contents[reference] if !ok { // TODO: test case - log.WithFields(map[string]interface{}{ - "path": reference.Path, - "id": reference.ID(), - "analyzer": a.Name(), - }).Errorf("analyzer file content missing") + log.Errorf("analyzer '%s' file content missing: %+v", a.Name(), reference) continue } @@ -54,11 +50,7 @@ func (a *Analyzer) Analyze(contents map[file.Reference]string) ([]pkg.Package, e entries, err := ParseEntries(strings.NewReader(content)) if err != nil { // TODO: test case - log.WithFields(map[string]interface{}{ - "path": reference.Path, - "id": reference.ID(), - "analyzer": a.Name(), - }).Errorf("analyzer failed to parse entries: %w", err) + log.Errorf("analyzer failed to parse entries (reference=%+v): %w", reference, err) continue } diff --git a/imgbom/log.go b/imgbom/log.go index 42a51bd36..ac2d5d4d1 100644 --- a/imgbom/log.go +++ b/imgbom/log.go @@ -8,9 +8,3 @@ import ( func SetLogger(logger logger.Logger) { log.Log = logger } - -func SetLoggerWithTags(logger logger.Logger, tags map[string]interface{}) { - log.Log = logger.WithFields(map[string]interface{}{ - "source": LibraryName, - }) -} diff --git a/imgbom/logger/logger.go b/imgbom/logger/logger.go index 60518ab83..c76b9776a 100644 --- a/imgbom/logger/logger.go +++ b/imgbom/logger/logger.go @@ -6,5 +6,4 @@ type Logger interface { Info(args ...interface{}) Debugf(format string, args ...interface{}) Debug(args ...interface{}) - WithFields(map[string]interface{}) Logger } diff --git a/imgbom/presenter/json/presenter.go b/imgbom/presenter/json/presenter.go index d2ff415ae..ddd3f35db 100644 --- a/imgbom/presenter/json/presenter.go +++ b/imgbom/presenter/json/presenter.go @@ -90,11 +90,7 @@ func (pres *Presenter) Present(output io.Writer, img *stereoscopeImg.Image, cata fileMetadata, err := img.FileCatalog.Get(src) if err != nil { // TODO: test case - log.WithFields(map[string]interface{}{ - "path": src.Path, - "id": src.ID(), - "presenter": "json", - }).Errorf("could not get metadata from catalog") + log.Errorf("could not get metadata from catalog (presenter=json): %+v", src) } srcObj := source{ @@ -110,9 +106,7 @@ func (pres *Presenter) Present(output io.Writer, img *stereoscopeImg.Image, cata bytes, err := json.Marshal(&doc) if err != nil { - log.WithFields(map[string]interface{}{ - "presenter": "json", - }).Errorf("failed to marshal json: %w", err) + log.Errorf("failed to marshal json (presenter=json): %w", err) } _, err = output.Write(bytes) diff --git a/internal/log/log.go b/internal/log/log.go index bb93f3739..d7b609443 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -23,7 +23,3 @@ func Debugf(format string, args ...interface{}) { 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 index e403f190f..da5db9cd9 100644 --- a/internal/log/nop.go +++ b/internal/log/nop.go @@ -1,12 +1,9 @@ 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{} } +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{}) {} diff --git a/internal/logger/zap.go b/internal/logger/zap.go index d8e56c807..d5143556a 100644 --- a/internal/logger/zap.go +++ b/internal/logger/zap.go @@ -3,7 +3,6 @@ package logger import ( "os" - "github.com/anchore/imgbom/imgbom/logger" "github.com/anchore/imgbom/internal/format" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -28,6 +27,7 @@ type LogConfig struct { } type ZapLogger struct { + config LogConfig sugaredLogger *zap.SugaredLogger } @@ -38,18 +38,21 @@ type ZapLogger struct { // - New interface example: https://github.com/uber-go/zap/blob/c2633d6de2d6e1170ad8f150660e3cf5310067c8/zapcore/json_encoder.go // - Register the encoder: https://github.com/uber-go/zap/blob/v1.15.0/encoder.go func NewZapLogger(config LogConfig) *ZapLogger { + appLogger := ZapLogger{ + config: config, + } cores := []zapcore.Core{} if config.EnableConsole { // note: the report should go to stdout, all logs should go to stderr writer := zapcore.Lock(os.Stderr) - core := zapcore.NewCore(getConsoleEncoder(config), writer, config.Level) + core := zapcore.NewCore(appLogger.getConsoleEncoder(config), writer, config.Level) cores = append(cores, core) } if config.EnableFile { - writer := zapcore.AddSync(logFileWriter(config.FileLocation)) - core := zapcore.NewCore(fileEncoder(config), writer, config.Level) + writer := zapcore.AddSync(appLogger.logFileWriter(config.FileLocation)) + core := zapcore.NewCore(appLogger.fileEncoder(config), writer, config.Level) cores = append(cores, core) } @@ -57,14 +60,13 @@ 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) - logger := zap.New(combinedCore, + appLogger.sugaredLogger = zap.New( + combinedCore, zap.AddCallerSkip(2), zap.AddCaller(), ).Sugar() - return &ZapLogger{ - sugaredLogger: logger, - } + return &appLogger } func (l *ZapLogger) GetNamedLogger(name string) *ZapLogger { @@ -73,7 +75,7 @@ func (l *ZapLogger) GetNamedLogger(name string) *ZapLogger { } } -func getConsoleEncoder(config LogConfig) zapcore.Encoder { +func (l *ZapLogger) getConsoleEncoder(config LogConfig) zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() if config.Structured { encoderConfig.EncodeName = zapcore.FullNameEncoder @@ -82,17 +84,17 @@ func getConsoleEncoder(config LogConfig) zapcore.Encoder { } encoderConfig.EncodeTime = nil encoderConfig.EncodeCaller = nil - encoderConfig.EncodeLevel = consoleLevelEncoder - encoderConfig.EncodeName = nameEncoder + encoderConfig.EncodeLevel = l.consoleLevelEncoder + encoderConfig.EncodeName = l.nameEncoder return zapcore.NewConsoleEncoder(encoderConfig) } -func nameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) { +func (l *ZapLogger) nameEncoder(loggerName string, enc zapcore.PrimitiveArrayEncoder) { enc.AppendString("[" + loggerName + "]") } -func consoleLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { - if level != zapcore.InfoLevel { +func (l *ZapLogger) consoleLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { + if level != zapcore.InfoLevel || l.config.Level == zapcore.DebugLevel { color, ok := levelToColor[level] if !ok { enc.AppendString("[" + level.CapitalString() + "]") @@ -102,7 +104,7 @@ func consoleLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) } } -func fileEncoder(config LogConfig) zapcore.Encoder { +func (l *ZapLogger) fileEncoder(config LogConfig) zapcore.Encoder { encoderConfig := zap.NewProductionEncoderConfig() encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeName = zapcore.FullNameEncoder @@ -113,7 +115,7 @@ func fileEncoder(config LogConfig) zapcore.Encoder { return zapcore.NewConsoleEncoder(encoderConfig) } -func logFileWriter(location string) zapcore.WriteSyncer { +func (l *ZapLogger) logFileWriter(location string) zapcore.WriteSyncer { file, _ := os.Create(location) return zapcore.AddSync(file) } @@ -137,13 +139,3 @@ func (l *ZapLogger) Info(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} -}