Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
shangxuejin committed Feb 22, 2023
1 parent e392bdc commit 3196275
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func main() {
}

func method1(ctx context.Context) {
defer g.Cost(ctx, "method1")()
defer g.Cost(ctx, "method1", "enable test")()
g.Trace(ctx, 1)
g.Debug(ctx, 1)
if g.IsEnabled(ctx, g.Linfo) {
Expand All @@ -52,7 +52,7 @@ func method1(ctx context.Context) {
}

func method2(ctx context.Context) {
defer g.Costf(ctx, "method%d", 2)()
defer g.Costf(ctx, "method%d %s", 2, "stack test")()
g.Trace(ctx, 2)
g.Debug(ctx, 2)
g.Info(ctx, 2)
Expand Down
7 changes: 3 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func cost(logger *Logger, level Level) func(ctx context.Context, msg ...interfac
logger.output(ctx, level, "", s...)
start := time.Now()
return func() {
e = append(msg, "cost", time.Since(start).Truncate(time.Millisecond).String())
logger.output(ctx, level, "", msg...)
e := append(msg, "cost", time.Since(start).Truncate(time.Millisecond).String())
logger.output(ctx, level, "", e...)
}
}
}
Expand All @@ -77,8 +77,7 @@ func costf(logger *Logger, level Level) func(ctx context.Context, format string,
logger.output(ctx, level, format+" start...", msg...)
start := time.Now()
return func() {
logger.output(ctx, level, format+" cost "+
time.Now().Sub(start).Truncate(time.Millisecond).String(), msg...)
logger.output(ctx, level, format+" cost "+time.Since(start).Truncate(time.Millisecond).String(), msg...)
}
}
}
Expand Down

0 comments on commit 3196275

Please sign in to comment.