2
2
2019 © Postgres.ai
3
3
*/
4
4
5
+ // Package log formats and prints log messages.
5
6
package log
6
7
7
8
import (
8
9
"encoding/json"
9
10
"fmt"
10
11
"log"
11
12
"os"
13
+ "strings"
12
14
)
13
15
14
16
var debugMode = true
15
17
16
18
var std = log .New (os .Stderr , "" , log .LstdFlags | log .Lshortfile )
17
19
18
- const (
19
- WHITE = "\x1b [1;37m"
20
- RED = "\x1b [1;31m"
21
- GREEN = "\x1b [1;32m"
22
- YELLOW = "\x1b [1;33m"
23
- END = "\x1b [0m"
24
- OK = GREEN + "OK" + END
25
- FAIL = RED + "Fail" + END
26
- )
27
-
28
20
const (
29
21
calldepth = 3
30
22
)
@@ -33,47 +25,58 @@ func toString(i1 interface{}) string {
33
25
if i1 == nil {
34
26
return ""
35
27
}
28
+
36
29
switch i2 := i1 .(type ) {
37
- default :
38
- return fmt .Sprint (i2 )
39
30
case bool :
40
31
if i2 {
41
32
return "true"
42
- } else {
43
- return "false"
44
33
}
34
+
35
+ return "false"
36
+
45
37
case string :
46
38
return i2
39
+
47
40
case * bool :
48
41
if i2 == nil {
49
42
return ""
50
43
}
44
+
51
45
if * i2 {
52
46
return "true"
53
- } else {
54
- return "false"
55
47
}
48
+
49
+ return "false"
50
+
56
51
case * string :
57
52
if i2 == nil {
58
53
return ""
59
54
}
55
+
60
56
return * i2
57
+
61
58
case * json.Number :
62
59
return i2 .String ()
60
+
63
61
case json.Number :
64
62
return i2 .String ()
63
+
64
+ default :
65
+ return fmt .Sprint (i2 )
65
66
}
66
67
}
67
68
68
69
func prepareMessage (v ... interface {}) string {
69
- message := ""
70
+ builder := strings.Builder {}
71
+
70
72
for _ , value := range v {
71
- message = message + " " + toString (value )
73
+ builder . WriteString ( " " + toString (value ) )
72
74
}
73
- return message
75
+
76
+ return builder .String ()
74
77
}
75
78
76
- func println (v ... interface {}) {
79
+ func printLine (v ... interface {}) {
77
80
_ = std .Output (calldepth , fmt .Sprintln (v ... ))
78
81
}
79
82
@@ -92,38 +95,39 @@ func SetDebug(enable bool) {
92
95
}
93
96
}
94
97
95
- // Output message.
98
+ // Msg outputs message.
96
99
func Msg (v ... interface {}) {
97
- println ("[INFO] " + prepareMessage (v ... ))
100
+ printLine ("[INFO] " + prepareMessage (v ... ))
98
101
}
99
102
100
103
// Warn outputs a warning message.
101
104
func Warn (v ... interface {}) {
102
- println ("[WARNING] " + prepareMessage (v ... ))
105
+ printLine ("[WARNING] " + prepareMessage (v ... ))
103
106
}
104
107
105
- // Output debug message.
108
+ // Dbg outputs debug message.
106
109
func Dbg (v ... interface {}) {
107
110
if debugMode {
108
- println ("[DEBUG] " + prepareMessage (v ... ))
111
+ printLine ("[DEBUG] " + prepareMessage (v ... ))
109
112
}
110
113
}
111
114
112
- // Output error message.
115
+ // Err outputs error message.
113
116
func Err (v ... interface {}) {
114
- println ("[ERROR] " + prepareMessage (v ... ))
117
+ printLine ("[ERROR] " + prepareMessage (v ... ))
115
118
}
116
119
117
120
// Errf outputs formatted log.
118
121
func Errf (format string , v ... interface {}) {
119
122
printf ("[ERROR] " + format , v ... )
120
123
}
121
124
122
- // Messages for security audit.
125
+ // Audit outputs messages for security audit.
123
126
func Audit (v ... interface {}) {
124
- println ("[AUDIT] " + prepareMessage (v ... ))
127
+ printLine ("[AUDIT] " + prepareMessage (v ... ))
125
128
}
126
129
130
+ // Fatal prints fatal message and exits.
127
131
func Fatal (v ... interface {}) {
128
132
log .Fatal ("[FATAL] " + prepareMessage (v ... ))
129
133
}
0 commit comments