-
Notifications
You must be signed in to change notification settings - Fork 0
/
loggerMock.go
62 lines (49 loc) · 1.51 KB
/
loggerMock.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
55
56
57
58
59
60
61
62
package mock
import "fmt"
// LoggerMock -
type LoggerMock struct {
}
// Trace will print a trace log
func (c LoggerMock) Trace(message string, args ...interface{}) {
fmt.Printf("[TRACE] %s %v\n", message, getPrintableArgs(args))
}
// Debug will print a debug log
func (c LoggerMock) Debug(message string, args ...interface{}) {
fmt.Printf("[DEBUG] %s %v\n", message, getPrintableArgs(args))
}
// Info will print an info log
func (c LoggerMock) Info(message string, args ...interface{}) {
fmt.Printf("[INFO] %s %v\n", message, getPrintableArgs(args))
}
// Warn will print a warn log
func (c LoggerMock) Warn(message string, args ...interface{}) {
fmt.Printf("[WARN] %s %v\n", message, getPrintableArgs(args))
}
// Error will print an error log
func (c LoggerMock) Error(message string, args ...interface{}) {
fmt.Printf("[ERROR] %s %v\n", message, getPrintableArgs(args))
}
// LogIfError will print an error if it is not nil
func (c LoggerMock) LogIfError(err error, args ...interface{}) {
if err != nil {
fmt.Printf("[ERROR] %s %v\n", err.Error(), getPrintableArgs(args))
}
}
func getPrintableArgs(args ...interface{}) string {
if len(args)%2 != 0 {
return fmt.Sprintf("%v", args)
}
printableArgs := ""
for idx, arg := range args {
if idx%2 == 0 {
printableArgs += fmt.Sprintf("%s = ", arg)
continue
}
printableArgs += fmt.Sprintf("%v ", arg)
}
return printableArgs
}
// IsInterfaceNil returns false as the struct doesn't use pointer receivers
func (c LoggerMock) IsInterfaceNil() bool {
return false
}