-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger-time.go
38 lines (33 loc) · 1.32 KB
/
logger-time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package logger
import "time"
// TimeFunc will log the duration of the given function call
func (l *Logger) TimeFunc(message string, code func()) {
start := time.Now()
code()
duration := time.Since(start)
l.Record("duration", duration.Seconds()).Infof("%s. executed in %s", message, duration)
}
// TimeFuncV will log the duration of the given function call that returns a value
func (l *Logger) TimeFuncV(message string, code func() interface{}) interface{} {
start := time.Now()
result := code()
duration := time.Since(start)
l.Record("duration", duration.Seconds()).Infof("%s. executed in %s", message, duration)
return result
}
// TimeFuncE will log the duration of the given function call that returns an error
func (l *Logger) TimeFuncE(message string, code func() error) error {
start := time.Now()
err := code()
duration := time.Since(start)
l.Record("duration", duration.Seconds()).Infof("%s. executed in %s", message, duration)
return err
}
// TimeFuncVE will log the duration of the given function call that returns a value and an error
func (l *Logger) TimeFuncVE(message string, code func() (interface{}, error)) (interface{}, error) {
start := time.Now()
result, err := code()
duration := time.Since(start)
l.Record("duration", duration.Seconds()).Infof("%s. executed in %s", message, duration)
return result, err
}