serve nfs: unify the nfs library logging with rclone's logging better

Before this we ignored the logging levels and logged everything as
debug. This will obey the rclone logging flags and log at the correct
level.
This commit is contained in:
Nick Craig-Wood 2024-08-26 10:35:28 +01:00
parent 6ba3e24853
commit 1a8b7662e7
2 changed files with 125 additions and 64 deletions

View File

@ -24,7 +24,8 @@ type Handler struct {
} }
// NewHandler creates a handler for the provided filesystem // NewHandler creates a handler for the provided filesystem
func NewHandler(vfs *vfs.VFS, opt *Options) (handler nfs.Handler, err error) { func NewHandler(ctx context.Context, vfs *vfs.VFS, opt *Options) (handler nfs.Handler, err error) {
ci := fs.GetConfig(ctx)
h := &Handler{ h := &Handler{
vfs: vfs, vfs: vfs,
opt: *opt, opt: *opt,
@ -35,7 +36,20 @@ func NewHandler(vfs *vfs.VFS, opt *Options) (handler nfs.Handler, err error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to make cache: %w", err) return nil, fmt.Errorf("failed to make cache: %w", err)
} }
nfs.SetLogger(&logIntercepter{Level: nfs.DebugLevel}) var level nfs.LogLevel
switch {
case ci.LogLevel >= fs.LogLevelDebug: // Debug level, needs -vv
level = nfs.TraceLevel
case ci.LogLevel >= fs.LogLevelInfo: // Transfers, needs -v
level = nfs.InfoLevel
case ci.LogLevel >= fs.LogLevelNotice: // Normal logging, -q suppresses
level = nfs.WarnLevel
case ci.LogLevel >= fs.LogLevelError: // Error - can't be suppressed
level = nfs.ErrorLevel
default:
level = nfs.WarnLevel
}
nfs.SetLogger(&logger{level: level})
return h, nil return h, nil
} }
@ -108,120 +122,167 @@ func onUnmount() {
} }
} }
// logIntercepter intercepts noisy go-nfs logs and reroutes them to DEBUG // logger handles go-nfs logs and reroutes them to rclone's logging system
type logIntercepter struct { type logger struct {
Level nfs.LogLevel level nfs.LogLevel
} }
// Intercept intercepts go-nfs logs and calls fs.Debugf instead // logPrint intercepts go-nfs logs and calls rclone's log system instead
func (l *logIntercepter) Intercept(args ...interface{}) { func (l *logger) logPrint(level fs.LogLevel, args ...interface{}) {
args = append([]interface{}{"[NFS DEBUG] "}, args...) fs.LogPrintf(level, "nfs", "%s", fmt.Sprint(args...))
argsS := fmt.Sprint(args...)
fs.Debugf(nil, "%v", argsS)
} }
// Interceptf intercepts go-nfs logs and calls fs.Debugf instead // logPrintf intercepts go-nfs logs and calls rclone's log system instead
func (l *logIntercepter) Interceptf(format string, args ...interface{}) { func (l *logger) logPrintf(level fs.LogLevel, format string, args ...interface{}) {
argsS := fmt.Sprint(args...) fs.LogPrintf(level, "nfs", format, args...)
// bit of a workaround... the real fix is probably https://github.com/willscott/go-nfs/pull/28
if strings.Contains(argsS, "mount.Umnt") {
onUnmount()
}
fs.Debugf(nil, "[NFS DEBUG] "+format, args...)
} }
// Debug reroutes go-nfs Debug messages to Intercept // Debug reroutes go-nfs Debug messages to Intercept
func (l *logIntercepter) Debug(args ...interface{}) { func (l *logger) Debug(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.DebugLevel {
return
}
l.logPrint(fs.LogLevelDebug, args...)
} }
// Debugf reroutes go-nfs Debugf messages to Interceptf // Debugf reroutes go-nfs Debugf messages to logPrintf
func (l *logIntercepter) Debugf(format string, args ...interface{}) { func (l *logger) Debugf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.DebugLevel {
return
}
l.logPrintf(fs.LogLevelDebug, format, args...)
} }
// Error reroutes go-nfs Error messages to Intercept // Error reroutes go-nfs Error messages to Intercept
func (l *logIntercepter) Error(args ...interface{}) { func (l *logger) Error(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.ErrorLevel {
return
}
l.logPrint(fs.LogLevelError, args...)
} }
// Errorf reroutes go-nfs Errorf messages to Interceptf // Errorf reroutes go-nfs Errorf messages to logPrintf
func (l *logIntercepter) Errorf(format string, args ...interface{}) { func (l *logger) Errorf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.ErrorLevel {
return
}
l.logPrintf(fs.LogLevelError, format, args...)
} }
// Fatal reroutes go-nfs Fatal messages to Intercept // Fatal reroutes go-nfs Fatal messages to Intercept
func (l *logIntercepter) Fatal(args ...interface{}) { func (l *logger) Fatal(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.FatalLevel {
return
}
l.logPrint(fs.LogLevelError, args...)
} }
// Fatalf reroutes go-nfs Fatalf messages to Interceptf // Fatalf reroutes go-nfs Fatalf messages to logPrintf
func (l *logIntercepter) Fatalf(format string, args ...interface{}) { func (l *logger) Fatalf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.FatalLevel {
return
}
l.logPrintf(fs.LogLevelError, format, args...)
} }
// GetLevel returns the nfs.LogLevel // GetLevel returns the nfs.LogLevel
func (l *logIntercepter) GetLevel() nfs.LogLevel { func (l *logger) GetLevel() nfs.LogLevel {
return l.Level return l.level
} }
// Info reroutes go-nfs Info messages to Intercept // Info reroutes go-nfs Info messages to Intercept
func (l *logIntercepter) Info(args ...interface{}) { func (l *logger) Info(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.InfoLevel {
return
}
l.logPrint(fs.LogLevelInfo, args...)
} }
// Infof reroutes go-nfs Infof messages to Interceptf // Infof reroutes go-nfs Infof messages to logPrintf
func (l *logIntercepter) Infof(format string, args ...interface{}) { func (l *logger) Infof(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.InfoLevel {
return
}
l.logPrintf(fs.LogLevelInfo, format, args...)
} }
// Panic reroutes go-nfs Panic messages to Intercept // Panic reroutes go-nfs Panic messages to Intercept
func (l *logIntercepter) Panic(args ...interface{}) { func (l *logger) Panic(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.PanicLevel {
return
}
l.logPrint(fs.LogLevelError, args...)
} }
// Panicf reroutes go-nfs Panicf messages to Interceptf // Panicf reroutes go-nfs Panicf messages to logPrintf
func (l *logIntercepter) Panicf(format string, args ...interface{}) { func (l *logger) Panicf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.PanicLevel {
return
}
l.logPrintf(fs.LogLevelError, format, args...)
} }
// ParseLevel parses the nfs.LogLevel // ParseLevel parses the nfs.LogLevel
func (l *logIntercepter) ParseLevel(level string) (nfs.LogLevel, error) { func (l *logger) ParseLevel(level string) (nfs.LogLevel, error) {
return nfs.Log.ParseLevel(level) return nfs.Log.ParseLevel(level)
} }
// Print reroutes go-nfs Print messages to Intercept // Print reroutes go-nfs Print messages to Intercept
func (l *logIntercepter) Print(args ...interface{}) { func (l *logger) Print(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.InfoLevel {
return
}
l.logPrint(fs.LogLevelInfo, args...)
} }
// Printf reroutes go-nfs Printf messages to Intercept // Printf reroutes go-nfs Printf messages to Intercept
func (l *logIntercepter) Printf(format string, args ...interface{}) { func (l *logger) Printf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.InfoLevel {
return
}
l.logPrintf(fs.LogLevelInfo, format, args...)
} }
// SetLevel sets the nfs.LogLevel // SetLevel sets the nfs.LogLevel
func (l *logIntercepter) SetLevel(level nfs.LogLevel) { func (l *logger) SetLevel(level nfs.LogLevel) {
l.Level = level l.level = level
} }
// Trace reroutes go-nfs Trace messages to Intercept // Trace reroutes go-nfs Trace messages to Intercept
func (l *logIntercepter) Trace(args ...interface{}) { func (l *logger) Trace(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.DebugLevel {
return
}
l.logPrint(fs.LogLevelDebug, args...)
} }
// Tracef reroutes go-nfs Tracef messages to Interceptf // Tracef reroutes go-nfs Tracef messages to logPrintf
func (l *logIntercepter) Tracef(format string, args ...interface{}) { func (l *logger) Tracef(format string, args ...interface{}) {
l.Interceptf(format, args...) // FIXME BODGE ... the real fix is probably https://github.com/willscott/go-nfs/pull/28
// This comes from `Log.Tracef("request: %v", w.req)` in conn.go
// DEBUG : nfs: request: RPC #3285799202 (mount.Umnt)
argsS := fmt.Sprint(args...)
if strings.Contains(argsS, "mount.Umnt") {
onUnmount()
}
if l.level < nfs.DebugLevel {
return
}
l.logPrintf(fs.LogLevelDebug, format, args...)
} }
// Warn reroutes go-nfs Warn messages to Intercept // Warn reroutes go-nfs Warn messages to Intercept
func (l *logIntercepter) Warn(args ...interface{}) { func (l *logger) Warn(args ...interface{}) {
l.Intercept(args...) if l.level < nfs.WarnLevel {
return
}
l.logPrint(fs.LogLevelNotice, args...)
} }
// Warnf reroutes go-nfs Warnf messages to Interceptf // Warnf reroutes go-nfs Warnf messages to logPrintf
func (l *logIntercepter) Warnf(format string, args ...interface{}) { func (l *logger) Warnf(format string, args ...interface{}) {
l.Interceptf(format, args...) if l.level < nfs.WarnLevel {
return
}
l.logPrintf(fs.LogLevelNotice, format, args...)
} }

View File

@ -37,7 +37,7 @@ func NewServer(ctx context.Context, vfs *vfs.VFS, opt *Options) (s *Server, err
ctx: ctx, ctx: ctx,
opt: *opt, opt: *opt,
} }
s.handler, err = NewHandler(vfs, opt) s.handler, err = NewHandler(ctx, vfs, opt)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to make NFS handler: %w", err) return nil, fmt.Errorf("failed to make NFS handler: %w", err)
} }