This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
logger_wrapper.go
92 lines (75 loc) · 2.4 KB
/
logger_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package storage
import (
"context"
"fmt"
"io"
)
// Logger can be a *log.Logger
type Logger interface {
Print(v ...interface{})
}
// NewLoggerWrapper creates a new FS which logs all calls to FS.
func NewLoggerWrapper(fs FS, name string, l Logger) FS {
return &loggerWrapper{
fs: fs,
name: name,
logger: l,
}
}
// loggerWrapper is an FS implementation which logs all filesystem calls.
type loggerWrapper struct {
fs FS
name string
logger Logger
}
func (l *loggerWrapper) printf(format string, v ...interface{}) {
l.logger.Print(fmt.Sprintf(format, v...))
}
// Open implements FS. All calls to Open are logged and errors are logged separately.
func (l *loggerWrapper) Open(ctx context.Context, path string, options *ReaderOptions) (*File, error) {
l.printf("%v: open: %v", l.name, path)
f, err := l.fs.Open(ctx, path, options)
if err != nil {
l.printf("%v: open error: %v: %v", l.name, path, err)
}
return f, err
}
// Attributes implements FS.
func (l *loggerWrapper) Attributes(ctx context.Context, path string, options *ReaderOptions) (*Attributes, error) {
l.printf("%v: attrs: %v", l.name, path)
a, err := l.fs.Attributes(ctx, path, options)
if err != nil {
l.printf("%v: attrs error: %v: %v", l.name, path, err)
}
return a, err
}
// Create implements FS. All calls to Create are logged and errors are logged separately.
func (l *loggerWrapper) Create(ctx context.Context, path string, options *WriterOptions) (io.WriteCloser, error) {
l.printf("%v: create: %v", l.name, path)
wc, err := l.fs.Create(ctx, path, options)
if err != nil {
l.printf("%v: create error: %v: %v", l.name, path, err)
}
return wc, err
}
// Delete implements FS. All calls to Delete are logged and errors are logged separately.
func (l *loggerWrapper) Delete(ctx context.Context, path string) error {
l.printf("%v: delete: %v", l.name, path)
err := l.fs.Delete(ctx, path)
if err != nil {
l.printf("%v: delete error: %v: %v", l.name, path, err)
}
return err
}
// Walk implements FS. No logs are written at this time.
func (l *loggerWrapper) Walk(ctx context.Context, path string, fn WalkFn) error {
return l.fs.Walk(ctx, path, fn)
}
func (l *loggerWrapper) URL(ctx context.Context, path string, options *SignedURLOptions) (string, error) {
l.printf("%v: URL: %v", l.name, path)
url, err := l.fs.URL(ctx, path, options)
if err != nil {
l.printf("%v: URL error: %v: %v", l.name, path, err)
}
return url, err
}