Skip to content

Commit dd5bf8a

Browse files
joeybloggsjoeybloggs
authored andcommitted
Add filename display option Llongfile and Lshortfile
1 parent bcd870b commit dd5bf8a

File tree

7 files changed

+253
-22
lines changed

7 files changed

+253
-22
lines changed

handlers/console/console.go

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
colon = byte(':')
2727
base10 = 10
2828
v = "%v"
29+
gopath = "GOPATH"
2930
)
3031

3132
// Console is an instance of the console logger
@@ -36,6 +37,8 @@ type Console struct {
3637
writer io.Writer
3738
formatFunc FormatFunc
3839
timestampFormat string
40+
gopath string
41+
fileDisplay log.FilenameDisplay
3942
displayColor bool
4043
}
4144

@@ -61,13 +64,19 @@ func New() *Console {
6164
writer: os.Stderr,
6265
timestampFormat: defaultTS,
6366
displayColor: true,
67+
fileDisplay: log.Lshortfile,
6468
}
6569

6670
c.formatFunc = c.defaultFormatFunc
6771

6872
return c
6973
}
7074

75+
// SetFilenameDisplay tells Console the filename, when present, how to display
76+
func (c *Console) SetFilenameDisplay(fd log.FilenameDisplay) {
77+
c.fileDisplay = fd
78+
}
79+
7180
// DisplayColor tells Console to output in color or not
7281
// Default is : true
7382
func (c *Console) DisplayColor(color bool) {
@@ -111,6 +120,16 @@ func (c *Console) SetFormatFunc(fn FormatFunc) {
111120
// Run starts the logger consuming on the returned channed
112121
func (c *Console) Run() chan<- *log.Entry {
113122

123+
// pre-setup
124+
if c.fileDisplay == log.Llongfile {
125+
// gather $GOPATH for use in stripping off of full name
126+
// if not found still ok as will be blank
127+
c.gopath = os.Getenv(gopath)
128+
if len(c.gopath) != 0 {
129+
c.gopath += string(os.PathSeparator) + "src" + string(os.PathSeparator)
130+
}
131+
}
132+
114133
// in a big high traffic app, set a higher buffer
115134
ch := make(chan *log.Entry, c.buffer)
116135

@@ -172,11 +191,17 @@ func (c *Console) defaultFormatFunc() Formatter {
172191

173192
} else {
174193
file = e.File
175-
for i := len(file) - 1; i > 0; i-- {
176-
if file[i] == '/' {
177-
file = file[i+1:]
178-
break
194+
195+
if c.fileDisplay == log.Lshortfile {
196+
197+
for i = len(file) - 1; i > 0; i-- {
198+
if file[i] == '/' {
199+
file = file[i+1:]
200+
break
201+
}
179202
}
203+
} else {
204+
file = file[len(c.gopath):]
180205
}
181206

182207
b = append(b, e.Timestamp.Format(c.timestampFormat)...)
@@ -262,11 +287,17 @@ func (c *Console) defaultFormatFunc() Formatter {
262287

263288
} else {
264289
file = e.File
265-
for i := len(file) - 1; i > 0; i-- {
266-
if file[i] == '/' {
267-
file = file[i+1:]
268-
break
290+
291+
if c.fileDisplay == log.Lshortfile {
292+
293+
for i = len(file) - 1; i > 0; i-- {
294+
if file[i] == '/' {
295+
file = file[i+1:]
296+
break
297+
}
269298
}
299+
} else {
300+
file = file[len(c.gopath):]
270301
}
271302

272303
b = append(b, e.Timestamp.Format(c.timestampFormat)...)

handlers/console/console_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,44 @@ func TestCustomFormatFunc(t *testing.T) {
236236
buff.Reset()
237237
}
238238

239+
func TestSetFilename(t *testing.T) {
240+
buff := new(bytes.Buffer)
241+
242+
cLog := New()
243+
cLog.SetWriter(buff)
244+
cLog.DisplayColor(false)
245+
cLog.SetBuffersAndWorkers(3, 1)
246+
cLog.SetTimestampFormat("MST")
247+
cLog.SetFilenameDisplay(log.Llongfile)
248+
249+
log.RegisterHandler(cLog, log.AllLevels...)
250+
251+
log.Error("error")
252+
if !strings.Contains(buff.String(), "log/handlers/console/console_test.go:251 error") {
253+
t.Errorf("Expected '%s' Got '%s'", "log/handlers/console/console_test.go:251 error", buff.String())
254+
}
255+
buff.Reset()
256+
}
257+
258+
func TestSetFilenameColor(t *testing.T) {
259+
buff := new(bytes.Buffer)
260+
261+
cLog := New()
262+
cLog.SetWriter(buff)
263+
cLog.DisplayColor(true)
264+
cLog.SetBuffersAndWorkers(3, 1)
265+
cLog.SetTimestampFormat("MST")
266+
cLog.SetFilenameDisplay(log.Llongfile)
267+
268+
log.RegisterHandler(cLog, log.AllLevels...)
269+
270+
log.Error("error")
271+
if !strings.Contains(buff.String(), "log/handlers/console/console_test.go:270 error") {
272+
t.Errorf("Expected '%s' Got '%s'", "log/handlers/console/console_test.go:270 error", buff.String())
273+
}
274+
buff.Reset()
275+
}
276+
239277
type test struct {
240278
lvl log.Level
241279
msg string

handlers/http/http.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
stdlog "log"
88
stdhttp "net/http"
99
"net/url"
10+
"os"
1011
"strconv"
1112

1213
"github.com/go-playground/log"
@@ -27,6 +28,7 @@ const (
2728
colon = byte(':')
2829
base10 = 10
2930
v = "%v"
31+
gopath = "GOPATH"
3032
)
3133

3234
// HTTP is an instance of the http logger
@@ -39,6 +41,8 @@ type HTTP struct {
3941
httpClient stdhttp.Client
4042
header stdhttp.Header
4143
method string
44+
gopath string
45+
fileDisplay log.FilenameDisplay
4246
}
4347

4448
// New returns a new instance of the http logger
@@ -56,13 +60,19 @@ func New(remoteHost string, method string, header stdhttp.Header) (*HTTP, error)
5660
httpClient: stdhttp.Client{},
5761
header: header,
5862
method: method,
63+
fileDisplay: log.Lshortfile,
5964
}
6065

6166
h.formatFunc = h.defaultFormatFunc
6267

6368
return h, nil
6469
}
6570

71+
// SetFilenameDisplay tells HTTP the filename, when present, how to display
72+
func (h *HTTP) SetFilenameDisplay(fd log.FilenameDisplay) {
73+
h.fileDisplay = fd
74+
}
75+
6676
// SetBuffersAndWorkers sets the channels buffer size and number of concurrent workers.
6777
// These settings should be thought about together, hence setting both in the same function.
6878
func (h *HTTP) SetBuffersAndWorkers(size uint, workers uint) {
@@ -94,6 +104,16 @@ func (h *HTTP) SetFormatFunc(fn FormatFunc) {
94104
// Run starts the logger consuming on the returned channed
95105
func (h *HTTP) Run() chan<- *log.Entry {
96106

107+
// pre-setup
108+
if h.fileDisplay == log.Llongfile {
109+
// gather $GOPATH for use in stripping off of full name
110+
// if not found still ok as will be blank
111+
h.gopath = os.Getenv(gopath)
112+
if len(h.gopath) != 0 {
113+
h.gopath += string(os.PathSeparator) + "src" + string(os.PathSeparator)
114+
}
115+
}
116+
97117
ch := make(chan *log.Entry, h.buffer)
98118

99119
for i := 0; i <= int(h.numWorkers); i++ {
@@ -129,11 +149,17 @@ func (h *HTTP) defaultFormatFunc() Formatter {
129149

130150
} else {
131151
file = e.File
132-
for i := len(file) - 1; i > 0; i-- {
133-
if file[i] == '/' {
134-
file = file[i+1:]
135-
break
152+
153+
if h.fileDisplay == log.Lshortfile {
154+
155+
for i = len(file) - 1; i > 0; i-- {
156+
if file[i] == '/' {
157+
file = file[i+1:]
158+
break
159+
}
136160
}
161+
} else {
162+
file = file[len(h.gopath):]
137163
}
138164

139165
b = append(b, e.Timestamp.Format(h.timestampFormat)...)

handlers/http/http_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,41 @@ func TestBadValues(t *testing.T) {
134134
log.Debug("debug")
135135
}
136136

137+
func TestSetFilenameDisplay(t *testing.T) {
138+
139+
var msg string
140+
141+
server := httptest.NewServer(stdhttp.HandlerFunc(func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
142+
b, err := ioutil.ReadAll(r.Body)
143+
if err != nil {
144+
msg = err.Error()
145+
return
146+
}
147+
148+
msg = string(b)
149+
}))
150+
defer server.Close()
151+
152+
header := make(stdhttp.Header, 0)
153+
header.Set("Content-Type", "text/plain")
154+
155+
hLog, err := New(server.URL, "POST", header)
156+
if err != nil {
157+
log.Fatalf("Error initializing HTTP recieved '%s'", err)
158+
}
159+
160+
hLog.SetBuffersAndWorkers(0, 1)
161+
hLog.SetTimestampFormat("MST")
162+
hLog.SetFilenameDisplay(log.Llongfile)
163+
164+
log.RegisterHandler(hLog, log.AllLevels...)
165+
166+
log.Error("error")
167+
if msg != "UTC ERROR github.com/go-playground/log/handlers/http/http_test.go:166 error" {
168+
t.Errorf("Expected '%s' Got '%s'", "UTC ERROR github.com/go-playground/log/handlers/http/http_test.go:166 error", msg)
169+
}
170+
}
171+
137172
type test struct {
138173
lvl log.Level
139174
msg string

handlers/syslog/syslog.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
stdlog "log"
66
"log/syslog"
7+
"os"
78
"strconv"
89

910
"github.com/go-playground/log"
@@ -24,6 +25,7 @@ const (
2425
colon = byte(':')
2526
base10 = 10
2627
v = "%v"
28+
gopath = "GOPATH"
2729
)
2830

2931
// Syslog is an instance of the syslog logger
@@ -34,6 +36,8 @@ type Syslog struct {
3436
writer *syslog.Writer
3537
formatFunc FormatFunc
3638
timestampFormat string
39+
gopath string
40+
fileDisplay log.FilenameDisplay
3741
displayColor bool
3842
}
3943

@@ -64,6 +68,7 @@ func New(network string, raddr string, priority syslog.Priority, tag string) (*S
6468
colors: defaultColors,
6569
displayColor: false,
6670
timestampFormat: defaultTS,
71+
fileDisplay: log.Lshortfile,
6772
}
6873

6974
s.formatFunc = s.defaultFormatFunc
@@ -75,13 +80,18 @@ func New(network string, raddr string, priority syslog.Priority, tag string) (*S
7580
return s, nil
7681
}
7782

78-
// DisplayColor tells Console to output in color or not
83+
// SetFilenameDisplay tells Syslog the filename, when present, how to display
84+
func (s *Syslog) SetFilenameDisplay(fd log.FilenameDisplay) {
85+
s.fileDisplay = fd
86+
}
87+
88+
// DisplayColor tells Syslog to output in color or not
7989
// Default is : true
8090
func (s *Syslog) DisplayColor(color bool) {
8191
s.displayColor = color
8292
}
8393

84-
// SetTimestampFormat sets Console's timestamp output format
94+
// SetTimestampFormat sets Syslog's timestamp output format
8595
// Default is : 2006-01-02T15:04:05.000000000Z07:00
8696
func (s *Syslog) SetTimestampFormat(format string) {
8797
s.timestampFormat = format
@@ -112,6 +122,16 @@ func (s *Syslog) SetFormatFunc(fn FormatFunc) {
112122
// Run starts the logger consuming on the returned channed
113123
func (s *Syslog) Run() chan<- *log.Entry {
114124

125+
// pre-setup
126+
if s.fileDisplay == log.Llongfile {
127+
// gather $GOPATH for use in stripping off of full name
128+
// if not found still ok as will be blank
129+
s.gopath = os.Getenv(gopath)
130+
if len(s.gopath) != 0 {
131+
s.gopath += string(os.PathSeparator) + "src" + string(os.PathSeparator)
132+
}
133+
}
134+
115135
// in a big high traffic app, set a higher buffer
116136
ch := make(chan *log.Entry, s.buffer)
117137

@@ -188,11 +208,17 @@ func (s *Syslog) defaultFormatFunc() Formatter {
188208

189209
} else {
190210
file = e.File
191-
for i := len(file) - 1; i > 0; i-- {
192-
if file[i] == '/' {
193-
file = file[i+1:]
194-
break
211+
212+
if s.fileDisplay == log.Lshortfile {
213+
214+
for i = len(file) - 1; i > 0; i-- {
215+
if file[i] == '/' {
216+
file = file[i+1:]
217+
break
218+
}
195219
}
220+
} else {
221+
file = file[len(s.gopath):]
196222
}
197223

198224
b = append(b, e.Timestamp.Format(s.timestampFormat)...)
@@ -276,11 +302,17 @@ func (s *Syslog) defaultFormatFunc() Formatter {
276302

277303
} else {
278304
file = e.File
279-
for i := len(file) - 1; i > 0; i-- {
280-
if file[i] == '/' {
281-
file = file[i+1:]
282-
break
305+
306+
if s.fileDisplay == log.Lshortfile {
307+
308+
for i = len(file) - 1; i > 0; i-- {
309+
if file[i] == '/' {
310+
file = file[i+1:]
311+
break
312+
}
283313
}
314+
} else {
315+
file = file[len(s.gopath):]
284316
}
285317

286318
b = append(b, e.Timestamp.Format(s.timestampFormat)...)

0 commit comments

Comments
 (0)