Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from upccup/dev
add custom log for context
- Loading branch information
Showing
9 changed files
with
899 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.