@@ -38,12 +38,15 @@ import (
38
38
"regexp"
39
39
"strconv"
40
40
"strings"
41
+ "sync"
41
42
)
42
43
43
44
var PLACEHOLDER = regexp .MustCompile ("{(\\ d)}" )
44
45
45
46
type Logger interface {
46
47
Fprintln (w io.Writer , level string , format string , a ... interface {})
48
+ UnformattedFprintln (w io.Writer , s string )
49
+ UnformattedWrite (w io.Writer , data []byte )
47
50
Println (level string , format string , a ... interface {})
48
51
Name () string
49
52
}
@@ -52,6 +55,10 @@ type NoopLogger struct{}
52
55
53
56
func (s NoopLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {}
54
57
58
+ func (s NoopLogger ) UnformattedFprintln (w io.Writer , str string ) {}
59
+
60
+ func (s NoopLogger ) UnformattedWrite (w io.Writer , data []byte ) {}
61
+
55
62
func (s NoopLogger ) Println (level string , format string , a ... interface {}) {}
56
63
57
64
func (s NoopLogger ) Name () string {
@@ -62,55 +69,101 @@ type HumanTagsLogger struct{}
62
69
63
70
func (s HumanTagsLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
64
71
format = "[" + level + "] " + format
65
- fmt . Fprintln (w , Format (format , a ... ))
72
+ fprintln (w , Format (format , a ... ))
66
73
}
67
74
68
75
func (s HumanTagsLogger ) Println (level string , format string , a ... interface {}) {
69
76
s .Fprintln (os .Stdout , level , format , a ... )
70
77
}
71
78
79
+ func (s HumanTagsLogger ) UnformattedFprintln (w io.Writer , str string ) {
80
+ fprintln (w , str )
81
+ }
82
+
83
+ func (s HumanTagsLogger ) UnformattedWrite (w io.Writer , data []byte ) {
84
+ write (w , data )
85
+ }
86
+
72
87
func (s HumanTagsLogger ) Name () string {
73
88
return "humantags"
74
89
}
75
90
91
+
76
92
type HumanLogger struct {}
77
93
78
94
func (s HumanLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
79
- fmt . Fprintln (w , Format (format , a ... ))
95
+ fprintln (w , Format (format , a ... ))
80
96
}
81
97
82
98
func (s HumanLogger ) Println (level string , format string , a ... interface {}) {
83
99
s .Fprintln (os .Stdout , level , format , a ... )
84
100
}
85
101
102
+ func (s HumanLogger ) UnformattedFprintln (w io.Writer , str string ) {
103
+ fprintln (w , str )
104
+ }
105
+
106
+ func (s HumanLogger ) UnformattedWrite (w io.Writer , data []byte ) {
107
+ write (w , data )
108
+ }
109
+
86
110
func (s HumanLogger ) Name () string {
87
111
return "human"
88
112
}
89
113
114
+
90
115
type MachineLogger struct {}
91
116
92
- func (s MachineLogger ) printWithoutFormatting (w io.Writer , level string , format string , a []interface {}) {
117
+ func (s MachineLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
118
+ printMachineFormattedLogLine (w , level , format , a )
119
+ }
120
+
121
+ func (s MachineLogger ) Println (level string , format string , a ... interface {}) {
122
+ printMachineFormattedLogLine (os .Stdout , level , format , a )
123
+ }
124
+
125
+ func (s MachineLogger ) UnformattedFprintln (w io.Writer , str string ) {
126
+ fprintln (w , str )
127
+ }
128
+
129
+ func (s MachineLogger ) Name () string {
130
+ return "machine"
131
+ }
132
+
133
+ func (s MachineLogger ) UnformattedWrite (w io.Writer , data []byte ) {
134
+ write (w , data )
135
+ }
136
+
137
+
138
+ func printMachineFormattedLogLine (w io.Writer , level string , format string , a []interface {}) {
93
139
a = append ([]interface {}(nil ), a ... )
94
140
for idx , value := range a {
95
141
typeof := reflect .Indirect (reflect .ValueOf (value )).Kind ()
96
142
if typeof == reflect .String {
97
143
a [idx ] = url .QueryEscape (value .(string ))
98
144
}
99
145
}
100
- fmt .Fprintf (w , "===%s ||| %s ||| %s" , level , format , a )
101
- fmt .Fprintln (w )
146
+ fprintf (w , "===%s ||| %s ||| %s\n " , level , format , a )
102
147
}
103
148
104
- func (s MachineLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
105
- s .printWithoutFormatting (w , level , format , a )
149
+ var lock sync.Mutex
150
+
151
+ func fprintln (w io.Writer , s string ) {
152
+ lock .Lock ()
153
+ defer lock .Unlock ()
154
+ fmt .Fprintln (w , s )
106
155
}
107
156
108
- func (s MachineLogger ) Println (level string , format string , a ... interface {}) {
109
- s .printWithoutFormatting (os .Stdout , level , format , a )
157
+ func write (w io.Writer , data []byte ) {
158
+ lock .Lock ()
159
+ defer lock .Unlock ()
160
+ w .Write (data )
110
161
}
111
162
112
- func (s MachineLogger ) Name () string {
113
- return "machine"
163
+ func fprintf (w io.Writer , format string , a ... interface {}) {
164
+ lock .Lock ()
165
+ defer lock .Unlock ()
166
+ fmt .Fprintf (w , format , a ... )
114
167
}
115
168
116
169
func FromJavaToGoSyntax (s string ) string {
0 commit comments