Skip to content

Commit

Permalink
fix: improve logging (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Ramkumar Chinchani <[email protected]>
  • Loading branch information
rchincha authored Apr 27, 2023
1 parent c3736c9 commit 6a95d26
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
15 changes: 4 additions & 11 deletions cmd/bom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@ package main

import (
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sirupsen/logrus"
zlog "github.com/rs/zerolog/log"
"stackerbuild.io/stacker-bom/pkg/cli"
"stackerbuild.io/stacker-bom/pkg/log"
)

func main() {
// setup logging
logrus.SetLevel(logrus.ErrorLevel)

zerolog.TimeFieldFormat = time.RFC3339
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.SetLevel(log.InfoLevel)

if err := cli.NewRootCmd().Execute(); err != nil {
log.Error().Err(err).Msg("action failed")
zlog.Error().Err(err).Msg("action failed")
os.Exit(1)
}
}
15 changes: 12 additions & 3 deletions pkg/bom/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func MergeDocuments(dir, name, author, organization, output string) error {
sdoc.Creator.Organization = organization
sdoc.Creator.Tool = []string{fmt.Sprintf("stackerbuild.io/stacker-bom@%s", buildgen.Commit)}

mcount := 0

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -85,6 +87,9 @@ func MergeDocuments(dir, name, author, organization, output string) error {
sdoc.Files = MergeMaps(sdoc.Files, doc.Files)
sdoc.Packages = MergeMaps(sdoc.Packages, doc.Packages)

log.Info().Str("path", path).Msg("file found for merging")
mcount++

return nil
})
if err != nil {
Expand All @@ -93,10 +98,14 @@ func MergeDocuments(dir, name, author, organization, output string) error {
return err
}

if err := WriteDocument(sdoc, output); err != nil {
log.Error().Err(err).Str("name", output).Msg("unable to write merged doc")
if mcount > 0 {
if err := WriteDocument(sdoc, output); err != nil {
log.Error().Err(err).Str("name", output).Msg("unable to write merged doc")

return err
return err
}

log.Info().Int("files", mcount).Str("dir", dir).Str("output", output).Msg("merged files")
}

return nil
Expand Down
32 changes: 32 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package log

import (
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sirupsen/logrus"
)

type Level int

const (
// DebugLevel defines debug log level.
DebugLevel Level = iota
// InfoLevel defines info log level.
InfoLevel
// WarnLevel defines warn log level.
WarnLevel
// ErrorLevel defines error log level.
ErrorLevel
)

func SetLevel(level Level) {
// setup logging
logrus.SetLevel(logrus.ErrorLevel)

zerolog.TimeFieldFormat = time.RFC3339
zerolog.SetGlobalLevel(zerolog.Level(level))
log.Logger = log.With().Caller().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
}

0 comments on commit 6a95d26

Please sign in to comment.