@@ -14,7 +14,7 @@ import (
1414// FormatFunc is the function that the workers use to create
1515// a new Formatter per worker allowing reusable go routine safe
1616// variable to be used within your Formatter function.
17- type FormatFunc func () Formatter
17+ type FormatFunc func (c * Console ) Formatter
1818
1919// Formatter is the function used to format the Redis entry
2020type Formatter func (e * log.Entry ) []byte
@@ -58,44 +58,66 @@ var defaultColors = [...]log.ANSIEscSeq{
5858
5959// New returns a new instance of the console logger
6060func New () * Console {
61- c := & Console {
61+ return & Console {
6262 buffer : 0 ,
6363 numWorkers : 1 ,
6464 colors : defaultColors ,
6565 writer : os .Stderr ,
6666 timestampFormat : log .DefaultTimeFormat ,
6767 displayColor : true ,
6868 fileDisplay : log .Lshortfile ,
69+ formatFunc : defaultFormatFunc ,
6970 }
70-
71- c .formatFunc = c .defaultFormatFunc
72-
73- return c
7471}
7572
7673// SetFilenameDisplay tells Console the filename, when present, how to display
7774func (c * Console ) SetFilenameDisplay (fd log.FilenameDisplay ) {
7875 c .fileDisplay = fd
7976}
8077
78+ // FilenameDisplay returns Console's current filename display setting
79+ func (c * Console ) FilenameDisplay () log.FilenameDisplay {
80+ return c .fileDisplay
81+ }
82+
8183// RedirectSTDLogOutput tells Console to redirect the std Logger output
8284// to the log package itself.
8385func (c * Console ) RedirectSTDLogOutput (b bool ) {
8486 c .redirStdOutput = b
8587}
8688
87- // DisplayColor tells Console to output in color or not
89+ // SetDisplayColor tells Console to output in color or not
8890// Default is : true
89- func (c * Console ) DisplayColor (color bool ) {
91+ func (c * Console ) SetDisplayColor (color bool ) {
9092 c .displayColor = color
9193}
9294
95+ // DisplayColor returns if logging color or not
96+ func (c * Console ) DisplayColor () bool {
97+ return c .displayColor
98+ }
99+
100+ // GetDisplayColor returns the color for the given log level
101+ func (c * Console ) GetDisplayColor (level log.Level ) log.ANSIEscSeq {
102+ return c .colors [level ]
103+ }
104+
93105// SetTimestampFormat sets Console's timestamp output format
94106// Default is : "2006-01-02T15:04:05.000000000Z07:00"
95107func (c * Console ) SetTimestampFormat (format string ) {
96108 c .timestampFormat = format
97109}
98110
111+ // TimestampFormat returns Console's current timestamp output format
112+ func (c * Console ) TimestampFormat () string {
113+ return c .timestampFormat
114+ }
115+
116+ // GOPATH returns the GOPATH calculated by Console
117+ func (c * Console ) GOPATH () string {
118+ return c .gopath
119+ }
120+
99121// SetWriter sets Console's wriiter
100122// Default is : os.Stderr
101123func (c * Console ) SetWriter (w io.Writer ) {
@@ -182,7 +204,7 @@ func (c *Console) handleLog(entries <-chan *log.Entry) {
182204 var e * log.Entry
183205 // var b io.WriterTo
184206 var b []byte
185- formatter := c .formatFunc ()
207+ formatter := c .formatFunc (c )
186208
187209 for e = range entries {
188210
@@ -194,24 +216,28 @@ func (c *Console) handleLog(entries <-chan *log.Entry) {
194216 }
195217}
196218
197- func (c * Console ) defaultFormatFunc ( ) Formatter {
219+ func defaultFormatFunc (c * Console ) Formatter {
198220
199221 var b []byte
200222 var file string
201223 var lvl string
202224 var i int
203225
204- if c .displayColor {
226+ gopath := c .GOPATH ()
227+ tsFormat := c .TimestampFormat ()
228+ fnameDisplay := c .FilenameDisplay ()
229+
230+ if c .DisplayColor () {
205231
206232 var color log.ANSIEscSeq
207233
208234 return func (e * log.Entry ) []byte {
209235 b = b [0 :0 ]
210- color = c .colors [ e .Level ]
236+ color = c .GetDisplayColor ( e .Level )
211237
212238 if e .Line == 0 {
213239
214- b = append (b , e .Timestamp .Format (c . timestampFormat )... )
240+ b = append (b , e .Timestamp .Format (tsFormat )... )
215241 b = append (b , space )
216242 b = append (b , color ... )
217243
@@ -228,7 +254,7 @@ func (c *Console) defaultFormatFunc() Formatter {
228254 } else {
229255 file = e .File
230256
231- if c . fileDisplay == log .Lshortfile {
257+ if fnameDisplay == log .Lshortfile {
232258
233259 for i = len (file ) - 1 ; i > 0 ; i -- {
234260 if file [i ] == '/' {
@@ -237,10 +263,10 @@ func (c *Console) defaultFormatFunc() Formatter {
237263 }
238264 }
239265 } else {
240- file = file [len (c . gopath ):]
266+ file = file [len (gopath ):]
241267 }
242268
243- b = append (b , e .Timestamp .Format (c . timestampFormat )... )
269+ b = append (b , e .Timestamp .Format (tsFormat )... )
244270 b = append (b , space )
245271 b = append (b , color ... )
246272
@@ -308,7 +334,7 @@ func (c *Console) defaultFormatFunc() Formatter {
308334
309335 if e .Line == 0 {
310336
311- b = append (b , e .Timestamp .Format (c . timestampFormat )... )
337+ b = append (b , e .Timestamp .Format (tsFormat )... )
312338 b = append (b , space )
313339
314340 lvl = e .Level .String ()
@@ -324,7 +350,7 @@ func (c *Console) defaultFormatFunc() Formatter {
324350 } else {
325351 file = e .File
326352
327- if c . fileDisplay == log .Lshortfile {
353+ if fnameDisplay == log .Lshortfile {
328354
329355 for i = len (file ) - 1 ; i > 0 ; i -- {
330356 if file [i ] == '/' {
@@ -333,10 +359,10 @@ func (c *Console) defaultFormatFunc() Formatter {
333359 }
334360 }
335361 } else {
336- file = file [len (c . gopath ):]
362+ file = file [len (gopath ):]
337363 }
338364
339- b = append (b , e .Timestamp .Format (c . timestampFormat )... )
365+ b = append (b , e .Timestamp .Format (tsFormat )... )
340366 b = append (b , space )
341367
342368 lvl = e .Level .String ()
0 commit comments