Skip to content

Commit

Permalink
feat: improve logging
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 18, 2024
1 parent e2715b6 commit 4c38fbd
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 19 deletions.
3 changes: 0 additions & 3 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
}()
}

// set a prefix on the default logger
log.SetPrefix("format")

var db *bolt.DB

// open the db unless --no-cache was specified
Expand Down
2 changes: 1 addition & 1 deletion cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestOnUnmatched(t *testing.T) {

checkOutput := func(level string, output []byte) {
for _, p := range paths {
as.Contains(string(output), fmt.Sprintf("%s composite-formatter: no formatter for path: %s", level, p))
as.Contains(string(output), fmt.Sprintf("%s no formatter for path: %s", level, p))
}
}

Expand Down
7 changes: 2 additions & 5 deletions format/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type CompositeFormatter struct {
stats *stats.Stats
globalExcludes []glob.Glob

log *log.Logger
unmatchedLevel log.Level

scheduler *scheduler
Expand All @@ -40,7 +39,7 @@ type CompositeFormatter struct {
func (c *CompositeFormatter) match(file *walk.File) []*Formatter {
// first check if this file has been globally excluded
if pathMatches(file.RelPath, c.globalExcludes) {
c.log.Debugf("path matched global excludes: %s", file.RelPath)
log.Debugf("path matched global excludes: %s", file.RelPath)

return nil
}
Expand Down Expand Up @@ -72,7 +71,7 @@ func (c *CompositeFormatter) Apply(ctx context.Context, files []*walk.File) erro
return fmt.Errorf("no formatter for path: %s", file.RelPath)
}

c.log.Logf(c.unmatchedLevel, "no formatter for path: %s", file.RelPath)
log.Logf(c.unmatchedLevel, "no formatter for path: %s", file.RelPath)

// no further processing to be done, append to the release list
toRelease = append(toRelease, file)
Expand Down Expand Up @@ -186,8 +185,6 @@ func NewCompositeFormatter(
cfg: cfg,
stats: statz,
globalExcludes: globalExcludes,

log: log.WithPrefix("composite-formatter"),
unmatchedLevel: unmatchedLevel,

scheduler: scheduler,
Expand Down
4 changes: 2 additions & 2 deletions format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ func newFormatter(

// initialise internal state
if cfg.Priority > 0 {
f.log = log.WithPrefix(fmt.Sprintf("format | %s[%d]", name, cfg.Priority))
f.log = log.WithPrefix(fmt.Sprintf("formatter | %s[%d]", name, cfg.Priority))
} else {
f.log = log.WithPrefix(fmt.Sprintf("format | %s", name))
f.log = log.WithPrefix(fmt.Sprintf("formatter | %s", name))
}

f.includes, err = compileGlobs(cfg.Includes)
Expand Down
4 changes: 1 addition & 3 deletions format/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func newBatchKey(formatters []*Formatter) batchKey {
}

type scheduler struct {
log *log.Logger
batchSize int
changeLevel log.Level
formatters map[string]*Formatter
Expand Down Expand Up @@ -179,7 +178,7 @@ func (s *scheduler) schedule(ctx context.Context, key batchKey, batch []*walk.Fi
s.stats.Add(stats.Changed, 1)

// log the change (useful for diagnosing issues)
s.log.Log(
log.Log(
s.changeLevel, "file has changed",
"path", file.RelPath,
"prev_size", file.Info.Size(),
Expand Down Expand Up @@ -247,7 +246,6 @@ func newScheduler(
eg.SetLimit(runtime.NumCPU())

return &scheduler{
log: log.WithPrefix("scheduler"),
batchSize: batchSize,
changeLevel: changeLevel,
formatters: formatters,
Expand Down
2 changes: 1 addition & 1 deletion walk/cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func NewCachedReader(db *bolt.DB, batchSize int, delegate Reader) (*CachedReader
db: db,
batchSize: batchSize,
delegate: delegate,
log: log.WithPrefix("walk[cache]"),
log: log.WithPrefix("walk | cache"),
eg: eg,
updateCh: make(chan *File, batchSize*runtime.NumCPU()),
}
Expand Down
2 changes: 1 addition & 1 deletion walk/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func NewFilesystemReader(
eg := errgroup.Group{}

r := FilesystemReader{
log: log.WithPrefix("walk[filesystem]"),
log: log.WithPrefix("walk | filesystem"),
root: root,
path: path,
batchSize: batchSize,
Expand Down
2 changes: 1 addition & 1 deletion walk/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ func NewGitReader(
path: path,
stats: statz,
eg: &errgroup.Group{},
log: log.WithPrefix("walk[git]"),
log: log.WithPrefix("walk | git"),
}, nil
}
3 changes: 1 addition & 2 deletions walk/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/fs"
"os"
"path/filepath"
"time"

"github.com/numtide/treefmt/stats"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -75,7 +74,7 @@ func (f *File) Stat() (changed bool, info fs.FileInfo, err error) {
// Some formatters mess with the mod time (e.g. dos2unix) but not to the same precision,
// triggering false positives.
// We truncate everything below a second.
if f.Info.ModTime().Truncate(time.Second) != current.ModTime().Truncate(time.Second) {
if f.Info.ModTime().Unix() != current.ModTime().Unix() {
return true, current, nil
}

Expand Down

0 comments on commit 4c38fbd

Please sign in to comment.