Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add custom log for context #33

Merged
merged 2 commits into from Nov 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 73 additions & 0 deletions log/context.go
@@ -0,0 +1,73 @@
package log

import (
"path"

"github.com/Sirupsen/logrus"
"golang.org/x/net/context"
)

var (
// G is an alias for GetLogger
G = GetLogger

// L is an alias for the standard logger
L = logrus.NewEntry(logrus.StandardLogger())
)

type (
loggerKey struct{}
moduleKey struct{}
)

// returns a new context with the provided logger
// Use in combination with logger.WithField(s) for great effect
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
return context.WithValue(ctx, loggerKey{}, logger)
}

// returns the current logger from the context
// If logger is available, the default logger is returned
func GetLogger(ctx context.Context) *logrus.Entry {
logger := ctx.Value(loggerKey{})

if logger == nil {
return L
}

return logger.(*logrus.Entry)
}

// adds the module to the context, appending it with a slash if a module already
// exists. A moudle is just an roughly correlated defined by the call tree for
// given context
//
// Modules represent the call path. If the new module and last modlue and last modlue
// are the same, a new modlue entry will noe be created. If the new modlue and old older
// modlue are the same bur separated bu other modlues, the cycle will be represented the module path
func WithModlue(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)
}

// returns the module path for the provided context. If no module is set
// am empty string is returned
func GetModulePath(ctx context.Context) string {
module := ctx.Value(moduleKey{})
if module == nil {
return ""
}

return module.(string)
}
41 changes: 41 additions & 0 deletions log/context_test.go
@@ -0,0 +1,41 @@
package log

import (
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)

func TestLoggerContext(t *testing.T) {
ctx := context.Background()
assert.Equal(t, GetLogger(ctx), L)
assert.Equal(t, G(ctx), GetLogger(ctx))

ctx = WithLogger(ctx, G(ctx).WithField("test", "one"))
assert.Equal(t, GetLogger(ctx).Data["test"], "one")
assert.Equal(t, G(ctx), GetLogger(ctx))
}

func TestModuleContest(t *testing.T) {
ctx := context.Background()
assert.Equal(t, GetModulePath(ctx), "")

ctx = WithModlue(ctx, "a")
assert.Equal(t, GetModulePath(ctx), "a")
logger := GetLogger(ctx)
assert.Equal(t, logger.Data["module"], "a")

parent, ctx := ctx, WithModlue(ctx, "a")
assert.Equal(t, ctx, parent)
assert.Equal(t, GetModulePath(ctx), "a")
assert.Equal(t, GetLogger(ctx).Data["module"], "a")

ctx = WithModlue(ctx, "b")
assert.Equal(t, GetModulePath(ctx), "a/b")
assert.Equal(t, GetLogger(ctx).Data["module"], "a/b")

ctx = WithModlue(ctx, "c")
assert.Equal(t, GetModulePath(ctx), "a/b/c")
assert.Equal(t, GetLogger(ctx).Data["module"], "a/b/c")
}
27 changes: 27 additions & 0 deletions vendor/golang.org/x/net/context/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 156 additions & 0 deletions vendor/golang.org/x/net/context/context.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.