Skip to content

Commit 476582b

Browse files
jinrenjiemholt
andauthored
Replace the file storage logger with the default logger (#311)
* refactor: replace logger with default logger * typo: fix typo * fix: avoid panic * Update filestorage.go Replace Default.Logger with defaultLogger --------- Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
1 parent 41f81ce commit 476582b

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

filestorage.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"io"
2323
"io/fs"
24-
"log"
2524
"os"
2625
"path"
2726
"path/filepath"
@@ -213,7 +212,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
213212
// the previous acquirer either crashed or had some sort of failure that
214213
// caused them to be unable to fully acquire or retain the lock, therefore
215214
// we should treat it as if the lockfile did not exist
216-
log.Printf("[INFO][%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2)
215+
defaultLogger.Sugar().Infof("[%s] %s: Empty lockfile (%v) - likely previous process crashed or storage medium failure; treating as stale", s, filename, err2)
217216
}
218217
} else if err2 != nil {
219218
return fmt.Errorf("decoding lockfile contents: %w", err2)
@@ -235,8 +234,7 @@ func (s *FileStorage) Lock(ctx context.Context, name string) error {
235234
// either have potential to cause infinite loops, as in caddyserver/caddy#4448,
236235
// or must give up on perfect mutual exclusivity; however, these cases are rare,
237236
// so we prefer the simpler solution that avoids infinite loops)
238-
log.Printf("[INFO][%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s",
239-
s, name, meta.Created, meta.Updated, filename)
237+
defaultLogger.Sugar().Infof("[%s] Lock for '%s' is stale (created: %s, last update: %s); removing then retrying: %s", s, name, meta.Created, meta.Updated, filename)
240238
if err = os.Remove(filename); err != nil { // hopefully we can replace the lock file quickly!
241239
if !errors.Is(err, fs.ErrNotExist) {
242240
return fmt.Errorf("unable to delete stale lockfile; deadlocked: %w", err)
@@ -311,15 +309,15 @@ func keepLockfileFresh(filename string) {
311309
if err := recover(); err != nil {
312310
buf := make([]byte, stackTraceBufferSize)
313311
buf = buf[:runtime.Stack(buf, false)]
314-
log.Printf("panic: active locking: %v\n%s", err, buf)
312+
defaultLogger.Sugar().Errorf("active locking: %v\n%s", err, buf)
315313
}
316314
}()
317315

318316
for {
319317
time.Sleep(lockFreshnessInterval)
320318
done, err := updateLockfileFreshness(filename)
321319
if err != nil {
322-
log.Printf("[ERROR] Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename)
320+
defaultLogger.Sugar().Errorf("Keeping lock file fresh: %v - terminating lock maintenance (lockfile: %s)", err, filename)
323321
return
324322
}
325323
if done {

0 commit comments

Comments
 (0)