log: remove log "module" system
After comtemplation, the complexity of the logging module system outweighs its usefulness. This changeset removes the system and restores lighter weight code paths. As a concession, we can always provide more context when necessary to log messages to understand them without having to fork the context for a certain set of calls. Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -20,7 +19,6 @@ var (
|
||||
|
||||
type (
|
||||
loggerKey struct{}
|
||||
moduleKey struct{}
|
||||
)
|
||||
|
||||
// WithLogger returns a new context with the provided logger. Use in
|
||||
@@ -40,42 +38,3 @@ func GetLogger(ctx context.Context) *logrus.Entry {
|
||||
|
||||
return logger.(*logrus.Entry)
|
||||
}
|
||||
|
||||
// WithModule adds the module to the context, appending it with a slash if a
|
||||
// module already exists. A module is just an roughly correlated defined by the
|
||||
// call tree for a given context.
|
||||
//
|
||||
// As an example, we might have a "node" module already part of a context. If
|
||||
// this function is called with "tls", the new value of module will be
|
||||
// "node/tls".
|
||||
//
|
||||
// Modules represent the call path. If the new module and last module are the
|
||||
// same, a new module entry will not be created. If the new module and old
|
||||
// older module are the same but separated by other modules, the cycle will be
|
||||
// represented by the module path.
|
||||
func WithModule(ctx context.Context, module string) context.Context {
|
||||
parent := GetModulePath(ctx)
|
||||
|
||||
if parent != "" {
|
||||
// don't re-append module when module is the same.
|
||||
if path.Base(parent) == module {
|
||||
return ctx
|
||||
}
|
||||
|
||||
module = path.Join(parent, module)
|
||||
}
|
||||
|
||||
ctx = WithLogger(ctx, GetLogger(ctx).WithField("module", module))
|
||||
return context.WithValue(ctx, moduleKey{}, module)
|
||||
}
|
||||
|
||||
// GetModulePath returns the module path for the provided context. If no module
|
||||
// is set, an empty string is returned.
|
||||
func GetModulePath(ctx context.Context) string {
|
||||
module := ctx.Value(moduleKey{})
|
||||
if module == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
return module.(string)
|
||||
}
|
||||
|
||||
@@ -16,26 +16,3 @@ func TestLoggerContext(t *testing.T) {
|
||||
assert.Equal(t, GetLogger(ctx).Data["test"], "one")
|
||||
assert.Equal(t, G(ctx), GetLogger(ctx)) // these should be the same.
|
||||
}
|
||||
|
||||
func TestModuleContext(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
assert.Equal(t, GetModulePath(ctx), "")
|
||||
|
||||
ctx = WithModule(ctx, "a") // basic behavior
|
||||
assert.Equal(t, GetModulePath(ctx), "a")
|
||||
logger := GetLogger(ctx)
|
||||
assert.Equal(t, logger.Data["module"], "a")
|
||||
|
||||
parent, ctx := ctx, WithModule(ctx, "a")
|
||||
assert.Equal(t, ctx, parent) // should be a no-op
|
||||
assert.Equal(t, GetModulePath(ctx), "a")
|
||||
assert.Equal(t, GetLogger(ctx).Data["module"], "a")
|
||||
|
||||
ctx = WithModule(ctx, "b") // new module
|
||||
assert.Equal(t, GetModulePath(ctx), "a/b")
|
||||
assert.Equal(t, GetLogger(ctx).Data["module"], "a/b")
|
||||
|
||||
ctx = WithModule(ctx, "c") // new module
|
||||
assert.Equal(t, GetModulePath(ctx), "a/b/c")
|
||||
assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user