mirror of
https://github.com/anchore/syft.git
synced 2025-11-17 16:33:21 +01:00
add logging fields
This commit is contained in:
parent
3e71315195
commit
b7c7c5556d
@ -4,7 +4,9 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/anchore/imgbom/imgbom"
|
||||||
"github.com/anchore/imgbom/internal/config"
|
"github.com/anchore/imgbom/internal/config"
|
||||||
|
"github.com/anchore/imgbom/internal/logger"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -18,3 +20,15 @@ func loadAppConfig() {
|
|||||||
}
|
}
|
||||||
appConfig = cfg
|
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))
|
||||||
|
}
|
||||||
|
|||||||
20
cmd/root.go
20
cmd/root.go
@ -8,7 +8,7 @@ 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/logger"
|
"github.com/anchore/imgbom/internal/log"
|
||||||
"github.com/anchore/stereoscope"
|
"github.com/anchore/stereoscope"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@ -37,7 +37,7 @@ func init() {
|
|||||||
|
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -45,31 +45,31 @@ func Execute() {
|
|||||||
func doRunCmd(cmd *cobra.Command, args []string) {
|
func doRunCmd(cmd *cobra.Command, args []string) {
|
||||||
appCfgStr, err := json.MarshalIndent(&appConfig, " ", " ")
|
appCfgStr, err := json.MarshalIndent(&appConfig, " ", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debugf("could not display application config: %+v", err)
|
log.Debugf("could not display application config: %+v", err)
|
||||||
} else {
|
} else {
|
||||||
logger.Debugf("application config:\n%+v", string(appCfgStr))
|
log.Debugf("application config:\n%+v", string(appCfgStr))
|
||||||
}
|
}
|
||||||
|
|
||||||
userImageStr := args[0]
|
userImageStr := args[0]
|
||||||
logger.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 {
|
||||||
logger.Errorf("could not fetch image '%s': %w", userImageStr, err)
|
log.Errorf("could not fetch image '%s': %w", userImageStr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer stereoscope.Cleanup()
|
defer stereoscope.Cleanup()
|
||||||
|
|
||||||
logger.Info("cataloging image...")
|
log.Info("cataloging image...")
|
||||||
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
|
catalog, err := imgbom.CatalogImage(img, appConfig.ScopeOpt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("could not catalog image: %w", err)
|
log.Errorf("could not catalog image: %w", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info("done!")
|
log.Info("done!")
|
||||||
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 {
|
||||||
logger.Errorf("could not format catalog results: %w", err)
|
log.Errorf("could not format catalog results: %w", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,10 @@
|
|||||||
package imgbom
|
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) {
|
func SetLogger(logger logger.Logger) {
|
||||||
logger.SetLogger(l)
|
log.Log = logger
|
||||||
}
|
}
|
||||||
|
|||||||
10
imgbom/logger/logger.go
Normal file
10
imgbom/logger/logger.go
Normal 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
29
internal/log/log.go
Normal 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
12
internal/log/nop.go
Normal 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{} }
|
||||||
@ -1,11 +0,0 @@
|
|||||||
package logger
|
|
||||||
|
|
||||||
var Log Logger
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
SetLogger(&nopLogger{})
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetLogger(logger Logger) {
|
|
||||||
Log = logger
|
|
||||||
}
|
|
||||||
@ -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)
|
|
||||||
// }
|
|
||||||
@ -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{} }
|
|
||||||
@ -1,14 +1,14 @@
|
|||||||
package cmd
|
package logger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/anchore/imgbom/imgbom"
|
"github.com/anchore/imgbom/imgbom/logger"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"go.uber.org/zap/zapcore"
|
"go.uber.org/zap/zapcore"
|
||||||
)
|
)
|
||||||
|
|
||||||
type logConfig struct {
|
type LogConfig struct {
|
||||||
EnableConsole bool
|
EnableConsole bool
|
||||||
EnableFile bool
|
EnableFile bool
|
||||||
FormatAsJSON bool
|
FormatAsJSON bool
|
||||||
@ -16,52 +16,11 @@ type logConfig struct {
|
|||||||
FileLocation string
|
FileLocation string
|
||||||
}
|
}
|
||||||
|
|
||||||
type zapLogger struct {
|
type ZapLogger struct {
|
||||||
sugaredLogger *zap.SugaredLogger
|
sugaredLogger *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLoggingFromAppConfig() {
|
func NewZapLogger(config LogConfig) *ZapLogger {
|
||||||
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 {
|
|
||||||
cores := []zapcore.Core{}
|
cores := []zapcore.Core{}
|
||||||
|
|
||||||
if config.EnableConsole {
|
if config.EnableConsole {
|
||||||
@ -86,37 +45,62 @@ func newZapLogger(config logConfig) *zapLogger {
|
|||||||
zap.AddCaller(),
|
zap.AddCaller(),
|
||||||
).Sugar()
|
).Sugar()
|
||||||
|
|
||||||
return &zapLogger{
|
return &ZapLogger{
|
||||||
sugaredLogger: logger,
|
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...)
|
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...)
|
l.sugaredLogger.Infof(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *zapLogger) Debug(args ...interface{}) {
|
func (l *ZapLogger) Debug(args ...interface{}) {
|
||||||
l.sugaredLogger.Debug(args...)
|
l.sugaredLogger.Debug(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *zapLogger) Info(args ...interface{}) {
|
func (l *ZapLogger) Info(args ...interface{}) {
|
||||||
l.sugaredLogger.Info(args...)
|
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...)
|
l.sugaredLogger.Errorf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// func (l *zapLogger) WithFields(fields map[string]interface{}) logger.Logger {
|
func (l *ZapLogger) WithFields(fields map[string]interface{}) logger.Logger {
|
||||||
// var f = make([]interface{}, 0)
|
var f = make([]interface{}, 0)
|
||||||
// for k, v := range fields {
|
for k, v := range fields {
|
||||||
// f = append(f, k)
|
f = append(f, k)
|
||||||
// f = append(f, v)
|
f = append(f, v)
|
||||||
// }
|
}
|
||||||
// newLogger := l.sugaredLogger.With(f...)
|
newLogger := l.sugaredLogger.With(f...)
|
||||||
// return &zapLogger{newLogger}
|
return &ZapLogger{newLogger}
|
||||||
// }
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user