Skip to content

Commit

Permalink
Merge pull request #2 from omigo/develop
Browse files Browse the repository at this point in the history
cost print with format and tag
  • Loading branch information
omigo authored Aug 20, 2020
2 parents bc59be8 + 817228a commit a8ff3ae
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 26 additions & 8 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
25 changes: 24 additions & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit a8ff3ae

Please sign in to comment.