-
Notifications
You must be signed in to change notification settings - Fork 18
/
logger.go
54 lines (45 loc) · 1.49 KB
/
logger.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cute
import (
"fmt"
)
type tlogger interface {
Name() string
Logf(format string, args ...any)
Errorf(format string, args ...interface{})
}
// Info is a function to log info message
func (it *Test) Info(t tlogger, format string, args ...interface{}) {
it.logf(t, "INFO", format, args...)
}
// Error is a function to log error message
func (it *Test) Error(t tlogger, format string, args ...interface{}) {
it.errorf(t, "ERROR", format, args...)
}
// Debug is a function to log debug message
func (it *Test) Debug(t tlogger, format string, args ...interface{}) {
it.logf(t, "DEBUG", format, args...)
}
func (it *Test) logf(t tlogger, level, format string, args ...interface{}) {
name := it.Name
if it.Name == "" {
name = t.Name()
}
// If we are in a retry context, add some indication in the logs about the current attempt
if it.Retry.MaxAttempts != 1 {
t.Logf("[%s][%s](Attempt #%d) %v\n", name, level, it.Retry.currentCount, fmt.Sprintf(format, args...))
} else {
t.Logf("[%s][%s] %v\n", name, level, fmt.Sprintf(format, args...))
}
}
func (it *Test) errorf(t tlogger, level, format string, args ...interface{}) {
name := it.Name
if it.Name == "" {
name = t.Name()
}
// If we are in a retry context, add some indication in the logs about the current attempt
if it.Retry.MaxAttempts != 1 {
t.Logf("[%s][%s](Attempt #%d) %v\n", name, level, it.Retry.currentCount, fmt.Sprintf(format, args...))
} else {
t.Logf("[%s][%s] %v\n", name, level, fmt.Sprintf(format, args...))
}
}