Skip to content

Commit 4b4ebd1

Browse files
joeybloggsjoeybloggs
authored andcommitted
Changed to pass handlers struct to FormatFunc + added Getters for easier handler format overriding.
1 parent 27ee1ab commit 4b4ebd1

File tree

10 files changed

+149
-93
lines changed

10 files changed

+149
-93
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
old.txt
26+
new.txt

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## log
22
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
3-
![Project status](https://img.shields.io/badge/version-2.3-green.svg)
3+
![Project status](https://img.shields.io/badge/version-3.0.0-green.svg)
44
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
@@ -18,7 +18,7 @@ Features
1818
- [x] Logger is simple, only logic to create the log entry and send it off to the handlers and they take it from there.
1919
- [x] Sends the log entry to the handlers asynchronously, but waits for all to complete; meaning all your handlers can be dealing with the log entry at the same time, but log will wait until all have completed before moving on.
2020
- [x] Ability to specify which log levels get sent to each handler
21-
- [x] Built-in console + syslog
21+
- [x] Built-in console, syslog, http and email handlers
2222
- [x] Handlers are simple to write + easy to register
2323
- [x] Logger is a singleton ( one of the few instances a singleton is desired ) so the root package registers which handlers are used and any libraries just follow suit.
2424

@@ -203,12 +203,12 @@ make one log library better than another!
203203
go test -cpu=4 -bench=. -benchmem=true
204204

205205
PASS
206-
BenchmarkLogConsoleTenFieldsParallel-4 1000000 1930 ns/op 1113 B/op 35 allocs/op
207-
BenchmarkLogConsoleSimpleParallel-4 3000000 450 ns/op 88 B/op 4 allocs/op
208-
BenchmarkLogrusText10Fields-4 300000 3943 ns/op 4291 B/op 63 allocs/op
209-
BenchmarkLogrusTextSimple-4 2000000 633 ns/op 672 B/op 15 allocs/op
210-
BenchmarkLog1510Fields-4 100000 16375 ns/op 4378 B/op 92 allocs/op
211-
BenchmarkLog15Simple-4 1000000 1982 ns/op 320 B/op 9 allocs/op
206+
BenchmarkLogConsoleTenFieldsParallel-4 1000000 1985 ns/op 1113 B/op 35 allocs/op
207+
BenchmarkLogConsoleSimpleParallel-4 3000000 455 ns/op 88 B/op 4 allocs/op
208+
BenchmarkLogrusText10Fields-4 300000 4179 ns/op 4291 B/op 63 allocs/op
209+
BenchmarkLogrusTextSimple-4 2000000 655 ns/op 672 B/op 15 allocs/op
210+
BenchmarkLog1510Fields-4 100000 16376 ns/op 4378 B/op 92 allocs/op
211+
BenchmarkLog15Simple-4 1000000 1983 ns/op 320 B/op 9 allocs/op
212212
ok github.com/go-playground/log/benchmarks 10.716s
213213
```
214214

benchmarks/benchmark_test.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ var _jane = user{
2828
CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
2929
}
3030

31-
const (
32-
newLine = byte('\n')
33-
defaultTS = "2006-01-02T15:04:05.000000000Z07:00"
34-
colorFields = "%s %s%6s%s %-25s"
35-
colorNoFields = "%s %s%6s%s %s"
36-
colorKeyValue = " %s%s%s=%v"
37-
colorFieldsCaller = "%s %s%6s%s %s:%d %-25s"
38-
colorNoFieldsCaller = "%s %s%6s%s %s:%d %s"
39-
noColorFields = "%s %6s %-25s"
40-
noColorNoFields = "%s %6s %s"
41-
noColorKeyValue = " %s=%v"
42-
noColorFieldsCaller = "%s %6s %s:%d %-25s"
43-
noColorNoFieldsCaller = "%s %6s %s:%d %s"
44-
equals = byte('=')
45-
v = "%v"
46-
base10 = 10
47-
space = byte(' ')
48-
colon = byte(':')
49-
)
50-
5131
// NOTE: log is a singleton, which means handlers need to be
5232
// setup only once otherwise each test just adds another log
5333
// handler and results are cumulative... makes benchmarking
@@ -56,7 +36,7 @@ const (
5636
func TestMain(m *testing.M) {
5737

5838
cLog := console.New()
59-
cLog.DisplayColor(false)
39+
cLog.SetDisplayColor(false)
6040
cLog.SetWriter(ioutil.Discard)
6141
cLog.SetBuffersAndWorkers(3, 3)
6242

handlers/console/console.go

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
2020
type 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
6060
func 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
7774
func (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.
8385
func (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"
95107
func (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
101123
func (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()

handlers/console/console_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestConsoleLogger(t *testing.T) {
2626

2727
cLog := New()
2828
cLog.SetWriter(buff)
29-
cLog.DisplayColor(false)
29+
cLog.SetDisplayColor(false)
3030
cLog.SetBuffersAndWorkers(3, 0)
3131
cLog.SetTimestampFormat("MST")
3232
log.SetCallerInfoLevels(log.WarnLevel, log.ErrorLevel, log.PanicLevel, log.AlertLevel, log.FatalLevel)
@@ -121,7 +121,7 @@ func TestConsoleLoggerColor(t *testing.T) {
121121

122122
cLog := New()
123123
cLog.SetWriter(buff)
124-
cLog.DisplayColor(true)
124+
cLog.SetDisplayColor(true)
125125
cLog.SetBuffersAndWorkers(3, 3)
126126
cLog.SetTimestampFormat("MST")
127127

@@ -216,7 +216,7 @@ func TestCustomFormatFunc(t *testing.T) {
216216
cLog.SetWriter(buff)
217217
cLog.SetTimestampFormat("2006")
218218
cLog.SetBuffersAndWorkers(3, 2)
219-
cLog.SetFormatFunc(func() Formatter {
219+
cLog.SetFormatFunc(func(c *Console) Formatter {
220220

221221
var b []byte
222222

@@ -241,7 +241,7 @@ func TestSetFilename(t *testing.T) {
241241

242242
cLog := New()
243243
cLog.SetWriter(buff)
244-
cLog.DisplayColor(false)
244+
cLog.SetDisplayColor(false)
245245
cLog.SetBuffersAndWorkers(3, 1)
246246
cLog.SetTimestampFormat("MST")
247247
cLog.SetFilenameDisplay(log.Llongfile)
@@ -260,7 +260,7 @@ func TestSetFilenameColor(t *testing.T) {
260260

261261
cLog := New()
262262
cLog.SetWriter(buff)
263-
cLog.DisplayColor(true)
263+
cLog.SetDisplayColor(true)
264264
cLog.SetBuffersAndWorkers(3, 1)
265265
cLog.SetTimestampFormat("MST")
266266
cLog.SetFilenameDisplay(log.Llongfile)
@@ -280,7 +280,7 @@ func TestConsoleSTDLogCapturing(t *testing.T) {
280280

281281
cLog := New()
282282
cLog.SetWriter(buff)
283-
cLog.DisplayColor(false)
283+
cLog.SetDisplayColor(false)
284284
cLog.SetBuffersAndWorkers(3, 3)
285285
cLog.SetTimestampFormat("MST")
286286
cLog.RedirectSTDLogOutput(true)

handlers/email/email.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ func (email *Email) To() []string {
124124
return email.to
125125
}
126126

127+
// Template returns the Email's template
128+
func (email *Email) Template() *template.Template {
129+
return email.template
130+
}
131+
127132
// SetTimestampFormat sets Email's timestamp output format
128133
// Default is : "2006-01-02T15:04:05.000000000Z07:00"
129134
func (email *Email) SetTimestampFormat(format string) {
@@ -187,13 +192,14 @@ func (email *Email) Run() chan<- *log.Entry {
187192
func defaultFormatFunc(email *Email) Formatter {
188193
var err error
189194
b := new(bytes.Buffer)
195+
template := email.Template()
190196
message := gomail.NewMessage()
191197
message.SetHeader("From", email.from)
192198
message.SetHeader("To", email.to...)
193199

194200
return func(e *log.Entry) *gomail.Message {
195201
b.Reset()
196-
if err = email.template.ExecuteTemplate(b, "email", e); err != nil {
202+
if err = template.ExecuteTemplate(b, "email", e); err != nil {
197203
log.WithFields(log.F("error", err)).Error("Error parsing Email handler template")
198204
}
199205

0 commit comments

Comments
 (0)