diff --git a/format.go b/format.go index f8c3203..8f787ee 100644 --- a/format.go +++ b/format.go @@ -19,7 +19,7 @@ const ( const DefaultFormat = "2006-01-02 15:04:05 info main.go:88 message" // DefaultFormatTag 默认日志格式带标签 -const DefaultFormatTag = "2006-01-02 15:04:05 tag info examples/main.go:88 message" +const DefaultFormatTag = "2006-01-02 15:04:05 tag info main.go:88 message" // ExtactDateTime 抽取日期和时间格式字符串串 func ExtactDateTime(format string) (dateFmt, timeFmt string) { diff --git a/log.go b/log.go index 30ca51b..25d356c 100644 --- a/log.go +++ b/log.go @@ -218,20 +218,38 @@ func JSONIndent(m ...interface{}) { } } -func Cost(m string) func() { - std.Tprintf(Linfo, "", "%s start...", m) +func Cost(m ...interface{}) func() { + std.Tprintf(Linfo, "", "%v start...", m) start := time.Now() return func() { - std.Tprintf(Linfo, "", "%s cost %s", m, - time.Now().Sub(start).Truncate(time.Second)) + std.Tprintf(Linfo, "", "%v cost "+ + time.Now().Sub(start).Truncate(time.Second).String(), m) } } -func Costf(format, m string) func() { - std.Tprintf(Linfo, "", format+" start...", m) +func Costf(format string, m ...interface{}) func() { + std.Tprintf(Linfo, "", format+" start...", m...) start := time.Now() return func() { - std.Tprintf(Linfo, "", format+" cost %s", m, - time.Now().Sub(start).Truncate(time.Second)) + std.Tprintf(Linfo, "", format+" cost "+ + time.Now().Sub(start).Truncate(time.Second).String(), m...) + } +} + +func Tcost(tag string, m ...interface{}) func() { + std.Tprintf(Linfo, tag, "%v start...", m) + start := time.Now() + return func() { + std.Tprintf(Linfo, tag, "%v cost "+ + time.Now().Sub(start).Truncate(time.Second).String(), m) + } +} + +func Tcostf(tag string, format string, m ...interface{}) func() { + std.Tprintf(Linfo, tag, format+" start...", m...) + start := time.Now() + return func() { + std.Tprintf(Linfo, tag, format+" cost "+ + time.Now().Sub(start).Truncate(time.Second).String(), m...) } } diff --git a/log_test.go b/log_test.go index 68dd6d8..dd5592f 100644 --- a/log_test.go +++ b/log_test.go @@ -124,7 +124,30 @@ func TestFormatLogWithTag(t *testing.T) { } func TestCost(t *testing.T) { - defer Cost("something")() + defer Cost(666, "migo")() + + time.Sleep(time.Second) + Info("do do do") +} + +func TestCostf(t *testing.T) { + defer Costf("id=%d&name=%s", 666, "migo")() + + time.Sleep(time.Second) + Info("do do do") +} + +func TestTcost(t *testing.T) { + SetFormat(DefaultFormatTag) + defer Tcost("1001", 666, "migo")() + + time.Sleep(time.Second) + Info("do do do") +} + +func TestTcostf(t *testing.T) { + SetFormat(DefaultFormatTag) + defer Tcostf("1001", "id=%d&name=%s", 666, "migo")() time.Sleep(time.Second) Info("do do do")