mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 22:12:28 +01:00
add logging fields
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
-122
@@ -1,122 +0,0 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/anchore/imgbom/imgbom"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
type logConfig struct {
|
||||
EnableConsole bool
|
||||
EnableFile bool
|
||||
FormatAsJSON bool
|
||||
Level zapcore.Level
|
||||
FileLocation string
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
cores = append(cores, core)
|
||||
}
|
||||
|
||||
if config.EnableFile {
|
||||
writer := zapcore.AddSync(getLogWriter(config.FileLocation))
|
||||
core := zapcore.NewCore(getFileEncoder(config), writer, config.Level)
|
||||
cores = append(cores, core)
|
||||
}
|
||||
|
||||
combinedCore := zapcore.NewTee(cores...)
|
||||
|
||||
// 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,
|
||||
zap.AddCallerSkip(2),
|
||||
zap.AddCaller(),
|
||||
).Sugar()
|
||||
|
||||
return &zapLogger{
|
||||
sugaredLogger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *zapLogger) Debugf(format string, args ...interface{}) {
|
||||
l.sugaredLogger.Debugf(format, args...)
|
||||
}
|
||||
|
||||
func (l *zapLogger) Infof(format string, args ...interface{}) {
|
||||
l.sugaredLogger.Infof(format, args...)
|
||||
}
|
||||
|
||||
func (l *zapLogger) Debug(args ...interface{}) {
|
||||
l.sugaredLogger.Debug(args...)
|
||||
}
|
||||
|
||||
func (l *zapLogger) Info(args ...interface{}) {
|
||||
l.sugaredLogger.Info(args...)
|
||||
}
|
||||
|
||||
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}
|
||||
// }
|
||||
+10
-10
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user