From 69692ad90185b646d0b8de94ad21a0ad33f350f9 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Sat, 5 Feb 2022 12:04:36 -0500 Subject: [PATCH] use anchore/go-logger interface Signed-off-by: Alex Goodman --- cmd/cmd.go | 15 +-- go.mod | 7 +- go.sum | 12 +- internal/log/log.go | 7 +- internal/log/nop.go | 22 ++-- internal/logger/doc.go | 4 - internal/logger/logrus.go | 164 --------------------------- internal/ui/ephemeral_terminal_ui.go | 13 +-- syft/lib.go | 5 +- syft/logger/logger.go | 16 --- 10 files changed, 48 insertions(+), 217 deletions(-) delete mode 100644 internal/logger/doc.go delete mode 100644 internal/logger/logrus.go delete mode 100644 syft/logger/logger.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 6d71d6e87..8e7642693 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -6,10 +6,10 @@ import ( "os" "sort" + "github.com/anchore/go-logger/logrus" "github.com/anchore/stereoscope" "github.com/anchore/syft/internal/config" "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/internal/logger" "github.com/anchore/syft/internal/version" "github.com/anchore/syft/syft" "github.com/gookit/color" @@ -88,7 +88,7 @@ func initAppConfig() { } func initLogging() { - cfg := logger.LogrusConfig{ + cfg := logrus.Config{ EnableConsole: (appConfig.Log.FileLocation == "" || appConfig.CliOptions.Verbosity > 0) && !appConfig.Quiet, EnableFile: appConfig.Log.FileLocation != "", Level: appConfig.Log.LevelOpt, @@ -96,11 +96,12 @@ func initLogging() { FileLocation: appConfig.Log.FileLocation, } - logWrapper := logger.NewLogrusLogger(cfg) - syft.SetLogger(logWrapper) - stereoscope.SetLogger(&logger.LogrusNestedLogger{ - Logger: logWrapper.Logger.WithField("from-lib", "stereoscope"), - }) + logger, err := logrus.New(cfg) + if err != nil { + panic(err) + } + syft.SetLogger(logger) + stereoscope.SetLogger(logger.Nested("from-lib", "stereoscope")) } func logAppConfig() { diff --git a/go.mod b/go.mod index ee749a2a5..c15e46a65 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/adrg/xdg v0.2.1 github.com/alecthomas/jsonschema v0.0.0-20210301060011-54c507b6f074 github.com/anchore/client-go v0.0.0-20210222170800-9c70f9b80bcf + github.com/anchore/go-logger v0.0.0 github.com/anchore/go-rpmdb v0.0.0-20210914181456-a9c52348da63 github.com/anchore/go-testutils v0.0.0-20200925183923-d5f45b0d3c04 github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b @@ -29,7 +30,6 @@ require ( github.com/gookit/color v1.2.7 github.com/hashicorp/go-multierror v1.1.0 github.com/jinzhu/copier v0.3.2 - github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect github.com/mholt/archiver/v3 v3.5.1 github.com/mitchellh/go-homedir v1.1.0 github.com/mitchellh/hashstructure/v2 v2.0.2 @@ -50,10 +50,13 @@ require ( github.com/wagoodman/go-partybus v0.0.0-20210627031916-db1f5573bbc5 github.com/wagoodman/go-progress v0.0.0-20200731105512-1020f39e6240 github.com/wagoodman/jotframe v0.0.0-20211129225309-56b0d0a4aebb - github.com/x-cray/logrus-prefixed-formatter v0.5.2 github.com/xeipuuv/gojsonschema v1.2.0 golang.org/x/mod v0.4.2 golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b gopkg.in/yaml.v2 v2.4.0 ) + +replace github.com/anchore/stereoscope => ../stereoscope + +replace github.com/anchore/go-logger => ../go-logger diff --git a/go.sum b/go.sum index 87040014a..225b348f1 100644 --- a/go.sum +++ b/go.sum @@ -111,8 +111,6 @@ github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b h1:e1bmaoJfZV github.com/anchore/go-version v1.2.2-0.20200701162849-18adb9c92b9b/go.mod h1:Bkc+JYWjMCF8OyZ340IMSIi2Ebf3uwByOk6ho4wne1E= github.com/anchore/packageurl-go v0.0.0-20210922164639-b3fa992ebd29 h1:K9LfnxwhqvihqU0+MF325FNy7fsKV9EGaUxdfR4gnWk= github.com/anchore/packageurl-go v0.0.0-20210922164639-b3fa992ebd29/go.mod h1:Oc1UkGaJwY6ND6vtAqPSlYrptKRJngHwkwB6W7l1uP0= -github.com/anchore/stereoscope v0.0.0-20220201190559-f162f1e96f45 h1:GYwI1qXcGh7fmyUWLK41suUXYfmrvGWWKGh0cw+k6ug= -github.com/anchore/stereoscope v0.0.0-20220201190559-f162f1e96f45/go.mod h1:QpDHHV2h1NNfu7klzU75XC8RvSlaPK6HHgi0dy8A6sk= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y= github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY= @@ -578,12 +576,14 @@ github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= -github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= -github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA= github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= @@ -896,8 +896,9 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE= +golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -1105,6 +1106,7 @@ golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 h1:WecRHqgE09JBkh/584XIE6PMz5KKE/vER4izNUi30AQ= golang.org/x/sys v0.0.0-20211110154304-99a53858aa08/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= diff --git a/internal/log/log.go b/internal/log/log.go index 0dd2199c6..ba2a9e8dd 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -3,7 +3,7 @@ Package log contains the singleton object and helper functions for facilitating */ package log -import "github.com/anchore/syft/syft/logger" +import "github.com/anchore/go-logger" // Log is the singleton used to facilitate logging internally within syft var Log logger.Logger = &nopLogger{} @@ -47,3 +47,8 @@ func Debugf(format string, args ...interface{}) { func Debug(args ...interface{}) { Log.Debug(args...) } + +// WithFields returns a message logger with multiple key-value fields. +func WithFields(fields ...interface{}) logger.MessageLogger { + return Log.WithFields(fields...) +} diff --git a/internal/log/nop.go b/internal/log/nop.go index ff20e4612..e3c981139 100644 --- a/internal/log/nop.go +++ b/internal/log/nop.go @@ -1,12 +1,18 @@ package log +import ( + "github.com/anchore/go-logger" +) + type nopLogger struct{} -func (l *nopLogger) Errorf(format string, args ...interface{}) {} -func (l *nopLogger) Error(args ...interface{}) {} -func (l *nopLogger) Warnf(format string, args ...interface{}) {} -func (l *nopLogger) Warn(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) Errorf(format string, args ...interface{}) {} +func (l *nopLogger) Error(args ...interface{}) {} +func (l *nopLogger) Warnf(format string, args ...interface{}) {} +func (l *nopLogger) Warn(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 ...interface{}) logger.MessageLogger { return l } +func (l *nopLogger) Nested(fields ...interface{}) logger.Logger { return l } diff --git a/internal/logger/doc.go b/internal/logger/doc.go deleted file mode 100644 index 59647a80c..000000000 --- a/internal/logger/doc.go +++ /dev/null @@ -1,4 +0,0 @@ -/* -Package logger contains implementations for the syft.logger.Logger interface. -*/ -package logger diff --git a/internal/logger/logrus.go b/internal/logger/logrus.go deleted file mode 100644 index 0aea00c3e..000000000 --- a/internal/logger/logrus.go +++ /dev/null @@ -1,164 +0,0 @@ -package logger - -import ( - "fmt" - "io" - "io/fs" - "io/ioutil" - "os" - - "github.com/sirupsen/logrus" - prefixed "github.com/x-cray/logrus-prefixed-formatter" -) - -const defaultLogFilePermissions fs.FileMode = 0644 - -// LogrusConfig contains all configurable values for the Logrus logger -type LogrusConfig struct { - EnableConsole bool - EnableFile bool - Structured bool - Level logrus.Level - FileLocation string -} - -// LogrusLogger contains all runtime values for using Logrus with the configured output target and input configuration values. -type LogrusLogger struct { - Config LogrusConfig - Logger *logrus.Logger - Output io.Writer -} - -// LogrusNestedLogger is a wrapper for Logrus to enable nested logging configuration (loggers that always attach key-value pairs to all log entries) -type LogrusNestedLogger struct { - Logger *logrus.Entry -} - -// NewLogrusLogger creates a new LogrusLogger with the given configuration -func NewLogrusLogger(cfg LogrusConfig) *LogrusLogger { - appLogger := logrus.New() - - var output io.Writer - switch { - case cfg.EnableConsole && cfg.EnableFile: - logFile, err := os.OpenFile(cfg.FileLocation, os.O_WRONLY|os.O_CREATE, defaultLogFilePermissions) - if err != nil { - panic(fmt.Errorf("unable to setup log file: %w", err)) - } - output = io.MultiWriter(os.Stderr, logFile) - case cfg.EnableConsole: - output = os.Stderr - case cfg.EnableFile: - logFile, err := os.OpenFile(cfg.FileLocation, os.O_WRONLY|os.O_CREATE, defaultLogFilePermissions) - if err != nil { - panic(fmt.Errorf("unable to setup log file: %w", err)) - } - output = logFile - default: - output = ioutil.Discard - } - - appLogger.SetOutput(output) - appLogger.SetLevel(cfg.Level) - - if cfg.Structured { - appLogger.SetFormatter(&logrus.JSONFormatter{ - TimestampFormat: "2006-01-02 15:04:05", - DisableTimestamp: false, - DisableHTMLEscape: false, - PrettyPrint: false, - }) - } else { - appLogger.SetFormatter(&prefixed.TextFormatter{ - TimestampFormat: "2006-01-02 15:04:05", - ForceColors: true, - ForceFormatting: true, - }) - } - - return &LogrusLogger{ - Config: cfg, - Logger: appLogger, - Output: output, - } -} - -// Debugf takes a formatted template string and template arguments for the debug logging level. -func (l *LogrusLogger) Debugf(format string, args ...interface{}) { - l.Logger.Debugf(format, args...) -} - -// Infof takes a formatted template string and template arguments for the info logging level. -func (l *LogrusLogger) Infof(format string, args ...interface{}) { - l.Logger.Infof(format, args...) -} - -// Warnf takes a formatted template string and template arguments for the warning logging level. -func (l *LogrusLogger) Warnf(format string, args ...interface{}) { - l.Logger.Warnf(format, args...) -} - -// Errorf takes a formatted template string and template arguments for the error logging level. -func (l *LogrusLogger) Errorf(format string, args ...interface{}) { - l.Logger.Errorf(format, args...) -} - -// Debug logs the given arguments at the debug logging level. -func (l *LogrusLogger) Debug(args ...interface{}) { - l.Logger.Debug(args...) -} - -// Info logs the given arguments at the info logging level. -func (l *LogrusLogger) Info(args ...interface{}) { - l.Logger.Info(args...) -} - -// Warn logs the given arguments at the warning logging level. -func (l *LogrusLogger) Warn(args ...interface{}) { - l.Logger.Warn(args...) -} - -// Error logs the given arguments at the error logging level. -func (l *LogrusLogger) Error(args ...interface{}) { - l.Logger.Error(args...) -} - -// Debugf takes a formatted template string and template arguments for the debug logging level. -func (l *LogrusNestedLogger) Debugf(format string, args ...interface{}) { - l.Logger.Debugf(format, args...) -} - -// Infof takes a formatted template string and template arguments for the info logging level. -func (l *LogrusNestedLogger) Infof(format string, args ...interface{}) { - l.Logger.Infof(format, args...) -} - -// Warnf takes a formatted template string and template arguments for the warning logging level. -func (l *LogrusNestedLogger) Warnf(format string, args ...interface{}) { - l.Logger.Warnf(format, args...) -} - -// Errorf takes a formatted template string and template arguments for the error logging level. -func (l *LogrusNestedLogger) Errorf(format string, args ...interface{}) { - l.Logger.Errorf(format, args...) -} - -// Debug logs the given arguments at the debug logging level. -func (l *LogrusNestedLogger) Debug(args ...interface{}) { - l.Logger.Debug(args...) -} - -// Info logs the given arguments at the info logging level. -func (l *LogrusNestedLogger) Info(args ...interface{}) { - l.Logger.Info(args...) -} - -// Warn logs the given arguments at the warning logging level. -func (l *LogrusNestedLogger) Warn(args ...interface{}) { - l.Logger.Warn(args...) -} - -// Error logs the given arguments at the error logging level. -func (l *LogrusNestedLogger) Error(args ...interface{}) { - l.Logger.Error(args...) -} diff --git a/internal/ui/ephemeral_terminal_ui.go b/internal/ui/ephemeral_terminal_ui.go index 7cf7ad329..db7fcd633 100644 --- a/internal/ui/ephemeral_terminal_ui.go +++ b/internal/ui/ephemeral_terminal_ui.go @@ -7,12 +7,12 @@ import ( "bytes" "context" "fmt" + "github.com/anchore/go-logger" "io" "os" "sync" "github.com/anchore/syft/internal/log" - "github.com/anchore/syft/internal/logger" syftEvent "github.com/anchore/syft/syft/event" "github.com/anchore/syft/ui" "github.com/wagoodman/go-partybus" @@ -59,9 +59,8 @@ func (h *ephemeralTerminalUI) Setup(unsubscribe func() error) error { // prep the logger to not clobber the screen from now on (logrus only) h.logBuffer = bytes.NewBufferString("") - logWrapper, ok := log.Log.(*logger.LogrusLogger) - if ok { - logWrapper.Logger.SetOutput(h.logBuffer) + if rootLogger, ok := log.Log.(logger.Controller); ok { + rootLogger.SetOutput(h.logBuffer) } return h.openScreen() @@ -129,10 +128,10 @@ func (h *ephemeralTerminalUI) closeScreen(force bool) { func (h *ephemeralTerminalUI) flushLog() { // flush any errors to the screen before the report - logWrapper, ok := log.Log.(*logger.LogrusLogger) + rootLogger, ok := log.Log.(logger.Controller) if ok { - fmt.Fprint(logWrapper.Output, h.logBuffer.String()) - logWrapper.Logger.SetOutput(h.uiOutput) + fmt.Fprint(rootLogger.GetOutput(), h.logBuffer.String()) + rootLogger.SetOutput(h.uiOutput) } else { fmt.Fprint(h.uiOutput, h.logBuffer.String()) } diff --git a/syft/lib.go b/syft/lib.go index a712e510a..b2f82baa4 100644 --- a/syft/lib.go +++ b/syft/lib.go @@ -18,13 +18,12 @@ package syft import ( "fmt" - - "github.com/anchore/syft/syft/artifact" + "github.com/anchore/go-logger" "github.com/anchore/syft/internal/bus" "github.com/anchore/syft/internal/log" + "github.com/anchore/syft/syft/artifact" "github.com/anchore/syft/syft/linux" - "github.com/anchore/syft/syft/logger" "github.com/anchore/syft/syft/pkg" "github.com/anchore/syft/syft/pkg/cataloger" "github.com/anchore/syft/syft/source" diff --git a/syft/logger/logger.go b/syft/logger/logger.go deleted file mode 100644 index 3ace363f5..000000000 --- a/syft/logger/logger.go +++ /dev/null @@ -1,16 +0,0 @@ -/* -Package logger defines the logging interface which is used throughout the syft library. -*/ -package logger - -// Logger represents the behavior for logging within the syft library. -type Logger interface { - Errorf(format string, args ...interface{}) - Error(args ...interface{}) - Warnf(format string, args ...interface{}) - Warn(args ...interface{}) - Infof(format string, args ...interface{}) - Info(args ...interface{}) - Debugf(format string, args ...interface{}) - Debug(args ...interface{}) -}