Files
kopia/internal/testlogging/ctx.go
Jarek Kowalski c8fcae93aa logging: refactored logging
This is mostly mechanical and changes how loggers are instantiated.

Logger is now associated with a context, passed around all methods,
(most methods had ctx, but had to add it in a few missing places).

By default Kopia does not produce any logs, but it can be overridden,
either locally for a nested context, by calling

ctx = logging.WithLogger(ctx, newLoggerFunc)

To override logs globally, call logging.SetDefaultLogger(newLoggerFunc)

This refactoring allowed removing dependency from Kopia repo
and go-logging library (the CLI still uses it, though).

It is now also possible to have all test methods emit logs using
t.Logf() so that they show up in failure reports, which should make
debugging of test failures suck less.
2020-02-25 17:24:44 -08:00

83 lines
1.7 KiB
Go

// Package testlogging implements logger that writes to testing.T log.
package testlogging
import (
"context"
"testing"
"github.com/kopia/kopia/repo/logging"
)
// Level specifies log level.
type Level int
// log levels
const (
LevelDebug Level = iota
LevelInfo
LevelWarning
LevelError
LevelFatal
)
type testLogger struct {
t *testing.T
prefix string
minLevel Level
}
func (l *testLogger) Debugf(msg string, args ...interface{}) {
if l.minLevel > LevelDebug {
return
}
l.t.Helper()
l.t.Logf(l.prefix+msg, args...)
}
func (l *testLogger) Infof(msg string, args ...interface{}) {
if l.minLevel > LevelInfo {
return
}
l.t.Helper()
l.t.Logf(l.prefix+msg, args...)
}
func (l *testLogger) Warningf(msg string, args ...interface{}) {
if l.minLevel > LevelWarning {
return
}
l.t.Helper()
l.t.Logf(l.prefix+"warning: "+msg, args...)
}
func (l *testLogger) Errorf(msg string, args ...interface{}) {
if l.minLevel > LevelError {
return
}
l.t.Helper()
l.t.Errorf(l.prefix+msg, args...)
}
func (l *testLogger) Fatalf(msg string, args ...interface{}) {
if l.minLevel > LevelFatal {
return
}
l.t.Helper()
l.t.Fatalf(l.prefix+msg, args...)
}
var _ logging.Logger = &testLogger{}
// Context returns a context with attached logger that emits all log entries to go testing.T log output.
func Context(t *testing.T) context.Context {
return ContextWithLevel(t, LevelDebug)
}
// ContextWithLevel returns a context with attached logger that emits all log entries with given log level or above.
func ContextWithLevel(t *testing.T, level Level) context.Context {
return logging.WithLogger(context.Background(), func(module string) logging.Logger {
return &testLogger{t, "[" + module + "] ", level}
})
}