mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-21 23:00:42 -08:00
refactor: replace zap with slog (#6466)
Signed-off-by: knqyf263 <knqyf263@gmail.com> Co-authored-by: Nikita Pivkin <nikita.pivkin@smartforce.io> Co-authored-by: simar7 <1254783+simar7@users.noreply.github.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -44,7 +45,7 @@ func logDebug(_ context.Context, mod api.Module, params []uint64) {
|
||||
|
||||
buf := readMemory(mod.Memory(), offset, size)
|
||||
if buf != nil {
|
||||
log.Logger.Debug(string(buf))
|
||||
log.Debug(string(buf))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -56,7 +57,7 @@ func logInfo(_ context.Context, mod api.Module, params []uint64) {
|
||||
|
||||
buf := readMemory(mod.Memory(), offset, size)
|
||||
if buf != nil {
|
||||
log.Logger.Info(string(buf))
|
||||
log.Info(string(buf))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -68,7 +69,7 @@ func logWarn(_ context.Context, mod api.Module, params []uint64) {
|
||||
|
||||
buf := readMemory(mod.Memory(), offset, size)
|
||||
if buf != nil {
|
||||
log.Logger.Warn(string(buf))
|
||||
log.Warn(string(buf))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -80,7 +81,7 @@ func logError(_ context.Context, mod api.Module, params []uint64) {
|
||||
|
||||
buf := readMemory(mod.Memory(), offset, size)
|
||||
if buf != nil {
|
||||
log.Logger.Error(string(buf))
|
||||
log.Error(string(buf))
|
||||
}
|
||||
|
||||
return
|
||||
@@ -89,7 +90,8 @@ func logError(_ context.Context, mod api.Module, params []uint64) {
|
||||
func readMemory(mem api.Memory, offset, size uint32) []byte {
|
||||
buf, ok := mem.Read(offset, size)
|
||||
if !ok {
|
||||
log.Logger.Errorf("Memory.Read(%d, %d) out of range", offset, size)
|
||||
log.Error("Memory.Read() out of range",
|
||||
log.Int("offset", int(offset)), log.Int("size", int(size)))
|
||||
return nil
|
||||
}
|
||||
return buf
|
||||
@@ -129,7 +131,7 @@ func (m *Manager) loadModules(ctx context.Context) error {
|
||||
if os.IsNotExist(err) {
|
||||
return nil
|
||||
}
|
||||
log.Logger.Debugf("Module dir: %s", m.dir)
|
||||
log.Debug("Module dir", log.String("dir", m.dir))
|
||||
|
||||
err = filepath.Walk(m.dir, func(path string, info fs.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
@@ -143,7 +145,7 @@ func (m *Manager) loadModules(ctx context.Context) error {
|
||||
return xerrors.Errorf("failed to get a relative path: %w", err)
|
||||
}
|
||||
|
||||
log.Logger.Infof("Reading %s...", rel)
|
||||
log.Info("Reading a module...", log.String("path", rel))
|
||||
wasmCode, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("file read error: %w", err)
|
||||
@@ -159,7 +161,7 @@ func (m *Manager) loadModules(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Logger.Infof("%s loaded", rel)
|
||||
log.Info("Module loaded", log.String("path", rel))
|
||||
m.modules = append(m.modules, p)
|
||||
|
||||
return nil
|
||||
@@ -341,8 +343,9 @@ func newWASMPlugin(ctx context.Context, ccache wazero.CompilationCache, code []b
|
||||
}
|
||||
|
||||
if apiVersion != tapi.Version {
|
||||
log.Logger.Infof("Ignore %s@v%d module due to API version mismatch, got: %d, want: %d",
|
||||
name, version, apiVersion, tapi.Version)
|
||||
log.Info("Ignore the module due to API version mismatch",
|
||||
log.String("module", fmt.Sprintf("%s@v%d", name, version)),
|
||||
log.Int("got", apiVersion), log.Int("want", tapi.Version))
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -403,13 +406,14 @@ func newWASMPlugin(ctx context.Context, ccache wazero.CompilationCache, code []b
|
||||
}
|
||||
|
||||
func (m *wasmModule) Register() {
|
||||
log.Logger.Infof("Registering WASM module: %s@v%d", m.name, m.version)
|
||||
logger := log.With(log.String("name", m.name), log.Int("version", m.version))
|
||||
logger.Info("Registering WASM module")
|
||||
if m.isAnalyzer {
|
||||
log.Logger.Debugf("Registering custom analyzer in %s@v%d", m.name, m.version)
|
||||
logger.Debug("Registering custom analyzer")
|
||||
analyzer.RegisterAnalyzer(m)
|
||||
}
|
||||
if m.isPostScanner {
|
||||
log.Logger.Debugf("Registering custom post scanner in %s@v%d", m.name, m.version)
|
||||
logger.Debug("Registering custom post scanner")
|
||||
post.RegisterPostScanner(m)
|
||||
}
|
||||
}
|
||||
@@ -441,7 +445,7 @@ func (m *wasmModule) Required(filePath string, _ os.FileInfo) bool {
|
||||
|
||||
func (m *wasmModule) Analyze(ctx context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
|
||||
filePath := "/" + filepath.ToSlash(input.FilePath)
|
||||
log.Logger.Debugf("Module %s: analyzing %s...", m.name, filePath)
|
||||
log.Debug("Module analyzing...", log.String("module", m.name), log.String("file_path", filePath))
|
||||
|
||||
// Wasm module instances are not Goroutine safe, so we take look here since Analyze might be called concurrently.
|
||||
// TODO: This is temporary solution and we could improve the Analyze performance by having module instance pool.
|
||||
|
||||
Reference in New Issue
Block a user