Skip to content

Commit 8f427c6

Browse files
authoredMay 17, 2024··
Merge pull request #295 from numtide/feat/improve-logging
feat/improve logging
2 parents 567524b + 3b38282 commit 8f427c6

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed
 

‎cache/cache.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,23 @@ func Open(treeRoot string, clean bool, formatters map[string]*format.Formatter)
8989
return fmt.Errorf("failed to retrieve cache entry for formatter %v: %w", name, err)
9090
}
9191

92-
clean = clean || entry == nil || !(entry.Size == stat.Size() && entry.Modified == stat.ModTime())
93-
logger.Debug(
94-
"checking if formatter has changed",
95-
"name", name,
96-
"clean", clean,
97-
"entry", entry,
98-
"stat", stat,
99-
)
92+
isNew := entry == nil
93+
hasChanged := entry != nil && !(entry.Size == stat.Size() && entry.Modified == stat.ModTime())
94+
95+
if isNew {
96+
logger.Debugf("formatter '%s' is new", name)
97+
} else if hasChanged {
98+
logger.Debug("formatter '%s' has changed",
99+
name,
100+
"size", stat.Size(),
101+
"modTime", stat.ModTime(),
102+
"cachedSize", entry.Size,
103+
"cachedModTime", entry.Modified,
104+
)
105+
}
106+
107+
// update overall clean flag
108+
clean = clean || isNew || hasChanged
100109

101110
// record formatters info
102111
entry = &Entry{
@@ -182,7 +191,7 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
182191
start := time.Now()
183192

184193
defer func() {
185-
logger.Infof("finished generating change set in %v", time.Since(start))
194+
logger.Debugf("finished generating change set in %v", time.Since(start))
186195
}()
187196

188197
var tx *bolt.Tx
@@ -263,7 +272,7 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil
263272
func Update(files []*walk.File) error {
264273
start := time.Now()
265274
defer func() {
266-
logger.Infof("finished processing %v paths in %v", len(files), time.Since(start))
275+
logger.Debugf("finished processing %v paths in %v", len(files), time.Since(start))
267276
}()
268277

269278
if len(files) == 0 {

‎cli/format.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ func (f *Format) Run() (err error) {
5757
}
5858

5959
// create a prefixed logger
60-
l := log.WithPrefix("format")
60+
log.SetPrefix("format")
6161

6262
// ensure cache is closed on return
6363
defer func() {
6464
if err := cache.Close(); err != nil {
65-
l.Errorf("failed to close cache: %v", err)
65+
log.Errorf("failed to close cache: %v", err)
6666
}
6767
}()
6868

@@ -87,7 +87,7 @@ func (f *Format) Run() (err error) {
8787
formatterCfg := cfg.Formatters[name]
8888
formatter, err := format.NewFormatter(name, Cli.TreeRoot, formatterCfg, globalExcludes)
8989
if errors.Is(err, format.ErrCommandNotFound) && Cli.AllowMissingFormatter {
90-
l.Debugf("formatter not found: %v", name)
90+
log.Debugf("formatter not found: %v", name)
9191
continue
9292
} else if err != nil {
9393
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
@@ -383,6 +383,7 @@ func applyFormatters(ctx context.Context) func() error {
383383
if matched {
384384
stats.Add(stats.Matched, 1)
385385
} else {
386+
log.Debugf("no match found: %s", file.Path)
386387
// no match, so we send it direct to the processed channel
387388
processedCh <- file
388389
}

‎format/formatter.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ func (f *Formatter) Apply(ctx context.Context, files []*walk.File, filter bool)
8080
}
8181

8282
// execute the command
83-
cmd := exec.CommandContext(ctx, f.config.Command, args...)
83+
cmd := exec.CommandContext(ctx, f.executable, args...)
8484
cmd.Dir = f.workingDir
8585

86+
// log out the command being executed
87+
f.log.Debugf("executing: %s", cmd.String())
88+
8689
if out, err := cmd.CombinedOutput(); err != nil {
8790
if len(out) > 0 {
8891
_, _ = fmt.Fprintf(os.Stderr, "%s error:\n%s\n", f.name, out)

0 commit comments

Comments
 (0)
Please sign in to comment.