...

Source file src/github.com/Shopify/go-storage/logger_wrapper.go

Documentation: github.com/Shopify/go-storage

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

View as plain text