Skip to content

Commit

Permalink
refactor(log): add log.FilePath
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Jul 2, 2024
1 parent 82c2620 commit 5f3f694
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/dependency/parser/java/jar/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p *Parser) Parse(r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependenc
}

func (p *Parser) parseArtifact(filePath string, size int64, r xio.ReadSeekerAt) ([]ftypes.Package, []ftypes.Dependency, error) {
p.logger.Debug("Parsing Java artifacts...", log.String("file", filePath))
p.logger.Debug("Parsing Java artifacts...", log.FilePath(filePath))

// Try to extract artifactId and version from the file name
// e.g. spring-core-5.3.4-SNAPSHOT.jar => sprint-core, 5.3.4-SNAPSHOT
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ func (ag AnalyzerGroup) AnalyzeFile(ctx context.Context, wg *sync.WaitGroup, lim
}
rc, err := opener()
if errors.Is(err, fs.ErrPermission) {
ag.logger.Debug("Permission error", log.String("file_path", filePath))
ag.logger.Debug("Permission error", log.FilePath(filePath))
break
} else if err != nil {
return xerrors.Errorf("unable to open %s: %w", filePath, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func toApplication(fileType types.LangType, filePath, libFilePath string, r xio.
// Calculate the file digest when one of `spdx` formats is selected
d, err := calculateDigest(r)
if err != nil {
log.Warn("Unable to get checksum", log.String("file_path", filePath), log.Err(err))
log.Warn("Unable to get checksum", log.FilePath(filePath), log.Err(err))
}

deps := make(map[string][]string)
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/analyzer/language/php/composer/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (a composerAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnal
// Parse composer.json alongside composer.lock to identify the direct dependencies
if err = a.mergeComposerJson(input.FS, filepath.Dir(path), app); err != nil {
log.Warn("Unable to parse composer.json to identify direct dependencies",
log.String("path", filepath.Join(filepath.Dir(path), types.ComposerJson)), log.Err(err))
log.FilePath(filepath.Join(filepath.Dir(path), types.ComposerJson)), log.Err(err))
}
sort.Sort(app.Packages)
apps = append(apps, *app)
Expand Down Expand Up @@ -109,7 +109,7 @@ func (a composerAnalyzer) mergeComposerJson(fsys fs.FS, dir string, app *types.A
p, err := a.parseComposerJson(fsys, path)
if errors.Is(err, fs.ErrNotExist) {
// Assume all the packages are direct dependencies as it cannot identify them from composer.lock
log.Debug("Unable to determine the direct dependencies, composer.json not found", log.String("path", path))
log.Debug("Unable to determine the direct dependencies, composer.json not found", log.FilePath(path))
return nil
} else if err != nil {
return xerrors.Errorf("unable to parse %s: %w", path, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a sbomAnalyzer) Analyze(ctx context.Context, input analyzer.AnalysisInput)
return nil, xerrors.Errorf("failed to detect SBOM format: %w", err)
}

ctx = log.WithContextAttrs(ctx, log.String("file", input.FilePath))
ctx = log.WithContextAttrs(ctx, log.FilePath(input.FilePath))
bom, err := sbom.Decode(ctx, input.Content, format)
if err != nil {
return nil, xerrors.Errorf("SBOM decode error: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/fanal/handler/unpackaged/unpackaged.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func (h unpackagedHook) Handle(ctx context.Context, res *analyzer.AnalysisResult
}

// Parse the fetched SBOM
ctx = log.WithContextAttrs(ctx, log.String("file", filePath))
ctx = log.WithContextAttrs(ctx, log.FilePath(filePath))
bom, err := sbom.Decode(ctx, bytes.NewReader(raw), format)
if err != nil {
return err
}

if len(bom.Applications) > 0 {
h.logger.Info("Found SBOM attestation in Rekor", log.String("file_path", filePath))
h.logger.Info("Found SBOM attestation in Rekor", log.FilePath(filePath))
// Take the first app since this SBOM should contain a single application.
app := bom.Applications[0]
app.FilePath = filePath // Use the original file path rather than the one in the SBOM.
Expand Down
5 changes: 5 additions & 0 deletions pkg/log/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ func Prefix(prefix string) slog.Attr {
return slog.Any(prefixKey, logPrefix("["+prefix+"] "))
}

// FilePath returns an Attr that represents a filePath.
func FilePath(filePath string) slog.Attr {
return String("file_path", filePath)
}

func isLogPrefix(a slog.Attr) bool {
_, ok := a.Value.Any().(logPrefix)
return ok
Expand Down

0 comments on commit 5f3f694

Please sign in to comment.