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
8090func (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
8696func (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
113123func (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