@@ -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,98 @@ 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
76
91
type HumanLogger struct {}
77
92
78
93
func (s HumanLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
79
- fmt . Fprintln (w , Format (format , a ... ))
94
+ fprintln (w , Format (format , a ... ))
80
95
}
81
96
82
97
func (s HumanLogger ) Println (level string , format string , a ... interface {}) {
83
98
s .Fprintln (os .Stdout , level , format , a ... )
84
99
}
85
100
101
+ func (s HumanLogger ) UnformattedFprintln (w io.Writer , str string ) {
102
+ fprintln (w , str )
103
+ }
104
+
105
+ func (s HumanLogger ) UnformattedWrite (w io.Writer , data []byte ) {
106
+ write (w , data )
107
+ }
108
+
86
109
func (s HumanLogger ) Name () string {
87
110
return "human"
88
111
}
89
112
90
113
type MachineLogger struct {}
91
114
92
- func (s MachineLogger ) printWithoutFormatting (w io.Writer , level string , format string , a []interface {}) {
115
+ func (s MachineLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
116
+ printMachineFormattedLogLine (w , level , format , a )
117
+ }
118
+
119
+ func (s MachineLogger ) Println (level string , format string , a ... interface {}) {
120
+ printMachineFormattedLogLine (os .Stdout , level , format , a )
121
+ }
122
+
123
+ func (s MachineLogger ) UnformattedFprintln (w io.Writer , str string ) {
124
+ fprintln (w , str )
125
+ }
126
+
127
+ func (s MachineLogger ) Name () string {
128
+ return "machine"
129
+ }
130
+
131
+ func (s MachineLogger ) UnformattedWrite (w io.Writer , data []byte ) {
132
+ write (w , data )
133
+ }
134
+
135
+ func printMachineFormattedLogLine (w io.Writer , level string , format string , a []interface {}) {
93
136
a = append ([]interface {}(nil ), a ... )
94
137
for idx , value := range a {
95
138
typeof := reflect .Indirect (reflect .ValueOf (value )).Kind ()
96
139
if typeof == reflect .String {
97
140
a [idx ] = url .QueryEscape (value .(string ))
98
141
}
99
142
}
100
- fmt .Fprintf (w , "===%s ||| %s ||| %s" , level , format , a )
101
- fmt .Fprintln (w )
143
+ fprintf (w , "===%s ||| %s ||| %s\n " , level , format , a )
102
144
}
103
145
104
- func (s MachineLogger ) Fprintln (w io.Writer , level string , format string , a ... interface {}) {
105
- s .printWithoutFormatting (w , level , format , a )
146
+ var lock sync.Mutex
147
+
148
+ func fprintln (w io.Writer , s string ) {
149
+ lock .Lock ()
150
+ defer lock .Unlock ()
151
+ fmt .Fprintln (w , s )
106
152
}
107
153
108
- func (s MachineLogger ) Println (level string , format string , a ... interface {}) {
109
- s .printWithoutFormatting (os .Stdout , level , format , a )
154
+ func write (w io.Writer , data []byte ) {
155
+ lock .Lock ()
156
+ defer lock .Unlock ()
157
+ w .Write (data )
110
158
}
111
159
112
- func (s MachineLogger ) Name () string {
113
- return "machine"
160
+ func fprintf (w io.Writer , format string , a ... interface {}) {
161
+ lock .Lock ()
162
+ defer lock .Unlock ()
163
+ fmt .Fprintf (w , format , a ... )
114
164
}
115
165
116
166
func FromJavaToGoSyntax (s string ) string {
0 commit comments