Skip to content

Commit fdcdf50

Browse files
author
Dean Karn
authored
Updates (#29)
1 parent 736ecb5 commit fdcdf50

29 files changed

+398
-924
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ _testmain.go
2323
*.test
2424
*.prof
2525
old.txt
26-
new.txt
26+
new.txt
27+
*.coverprofile

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
GOCMD=GO111MODULE=on go
2+
3+
lint:
4+
go vet ./...
5+
6+
test:
7+
$(GOCMD) test -cover -race ./...
8+
9+
bench:
10+
$(GOCMD) test -run=NONE -bench=. -benchmem ./...
11+
12+
.PHONY: test lint

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## log
2-
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">![Project status](https://img.shields.io/badge/version-6.3.0-green.svg)
2+
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">![Project status](https://img.shields.io/badge/version-6.4.0-green.svg)
33
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
44
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
55
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)

benchmarks_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package log
2+
3+
import (
4+
"bytes"
5+
stderr "errors"
6+
"testing"
7+
8+
"github.com/go-playground/errors"
9+
)
10+
11+
func BenchmarkWithError(b *testing.B) {
12+
err := stderr.New("new error")
13+
entry := Entry{}
14+
for i := 0; i < b.N; i++ {
15+
_ = errorsWithError(entry, err)
16+
}
17+
}
18+
19+
func BenchmarkWithErrorParallel(b *testing.B) {
20+
err := stderr.New("new error")
21+
entry := Entry{}
22+
b.RunParallel(func(pb *testing.PB) {
23+
var buf bytes.Buffer
24+
for pb.Next() {
25+
buf.Reset()
26+
_ = errorsWithError(entry, err)
27+
}
28+
})
29+
}
30+
31+
func BenchmarkWithErrorExisting(b *testing.B) {
32+
err := errors.New("new error")
33+
entry := Entry{}
34+
for i := 0; i < b.N; i++ {
35+
_ = errorsWithError(entry, err)
36+
}
37+
}
38+
39+
func BenchmarkWithErrorExistingParallel(b *testing.B) {
40+
err := errors.New("new error")
41+
entry := Entry{}
42+
b.RunParallel(func(pb *testing.PB) {
43+
var buf bytes.Buffer
44+
for pb.Next() {
45+
buf.Reset()
46+
_ = errorsWithError(entry, err)
47+
}
48+
})
49+
}

errors.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package log
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/go-playground/errors"
87
)
@@ -14,36 +13,35 @@ func errorsWithError(e Entry, err error) Entry {
1413
case errors.Chain:
1514
cause := t[0]
1615
errField := cause.Err.Error()
17-
types := make([]string, 0, len(t))
16+
types := make([]byte, 0, 64)
1817
tags := make([]Field, 0, len(t))
19-
for _, e := range t {
18+
for i, e := range t {
2019
if e.Prefix != "" {
2120
errField = fmt.Sprintf("%s: %s", e.Prefix, errField)
2221
}
2322
for _, tag := range e.Tags {
2423
tags = append(tags, Field{Key: tag.Key, Value: tag.Value})
2524

2625
}
27-
types = append(types, e.Types...)
26+
for j, typ := range e.Types {
27+
types = append(types, typ...)
28+
if i == len(t)-1 && j == len(e.Types)-1 {
29+
continue
30+
}
31+
types = append(types, ',')
32+
}
2833
}
2934
ne.Fields = append(ne.Fields, Field{Key: "error", Value: errField})
30-
ne.Fields = append(ne.Fields, Field{Key: "source", Value: cause.Source})
35+
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%d", cause.Source.Function(), cause.Source.File(), cause.Source.Line())})
3136
ne.Fields = append(ne.Fields, tags...) // we do it this way to maintain order of error, source as first fields
3237
if len(types) > 0 {
33-
ne.Fields = append(ne.Fields, Field{Key: "types", Value: strings.Join(types, ",")})
38+
ne.Fields = append(ne.Fields, Field{Key: "types", Value: string(types)})
3439
}
3540

3641
default:
3742
ne.Fields = append(ne.Fields, Field{Key: "error", Value: err.Error()})
3843
frame := errors.StackLevel(2)
39-
name := fmt.Sprintf("%n", frame)
40-
file := fmt.Sprintf("%+s", frame)
41-
line := fmt.Sprintf("%d", frame)
42-
parts := strings.Split(file, "\n\t")
43-
if len(parts) > 1 {
44-
file = parts[1]
45-
}
46-
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%s", name, file, line)})
44+
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%d", frame.Function(), frame.File(), frame.Line())})
4745
}
4846
return ne
4947
}

go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module github.com/go-playground/log
2+
3+
go 1.11
4+
5+
require (
6+
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91
7+
github.com/go-playground/ansi v2.1.0+incompatible
8+
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b
9+
github.com/pkg/errors v0.8.1
10+
github.com/segmentio/errors-go v1.0.0
11+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
12+
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91 h1:vX+gnvBc56EbWYrmlhYbFYRaeikAke1GL84N4BEYOFE=
2+
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91/go.mod h1:cDLGBht23g0XQdLjzn6xOGXDkLK182YfINAaZEQLCHQ=
3+
github.com/aws/aws-sdk-go v1.17.14/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
4+
github.com/go-playground/ansi v2.1.0+incompatible h1:f9ldskdk1seTFmYjbmPaYB+WYsDKWc4UXcGb+e9JrN8=
5+
github.com/go-playground/ansi v2.1.0+incompatible/go.mod h1:OCdnfTFO/GfFtp+ktUt+PhElbGOwyTRUuRUsA+Y5pSU=
6+
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b h1:kP7Mu+FdZU+aG3BkVe5wM2JP7H0F/uQJfbCsZgcMQPE=
7+
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b/go.mod h1:z6q0ljiLHVElL1MHPEMKW+U4THB6xoXSi4GGkc1unu4=
8+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
9+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
10+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
11+
github.com/segmentio/errors-go v1.0.0 h1:B4mbo4hP3+XffV1GhwyAcHlvWoZtYdTyc3BOVPxspTQ=
12+
github.com/segmentio/errors-go v1.0.0/go.mod h1:RDVEREUrpa4/jM8rt5KsQpu+JoXPi6i07vG7m4tX0MY=
13+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
14+
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=

handlers/console/console.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ func (c *Console) SetWriter(w io.Writer) {
7777
// this will redirect the output of
7878
func handleStdLogger(done chan<- struct{}) {
7979
r, w := io.Pipe()
80-
defer r.Close()
81-
defer w.Close()
80+
defer func() {
81+
_ = r.Close()
82+
_ = w.Close()
83+
}()
8284

8385
stdlog.SetOutput(w)
8486

@@ -214,5 +216,5 @@ func (c *Console) Log(e log.Entry) {
214216

215217
b = append(b, newLine)
216218
}
217-
c.writer.Write(b)
219+
_, _ = c.writer.Write(b)
218220
}

handlers/console/console_test.go

Lines changed: 118 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package console
22

33
import (
44
"bytes"
5+
"io"
56
stdlog "log"
67
"strings"
8+
"sync"
79
"testing"
810
"time"
911

@@ -21,7 +23,7 @@ import (
2123

2224
func TestConsoleLogger(t *testing.T) {
2325
tests := getConsoleLoggerTests()
24-
buff := new(bytes.Buffer)
26+
buff := new(buffer)
2527

2628
log.SetExitFunc(func(int) {})
2729

@@ -94,7 +96,7 @@ func TestConsoleLogger(t *testing.T) {
9496
func TestConsoleLoggerColor(t *testing.T) {
9597
log.SetExitFunc(func(int) {})
9698
tests := getConsoleLoggerColorTests()
97-
buff := new(bytes.Buffer)
99+
buff := new(buffer)
98100
cLog := New(false)
99101
cLog.SetWriter(buff)
100102
cLog.SetDisplayColor(true)
@@ -145,7 +147,7 @@ func TestConsoleLoggerColor(t *testing.T) {
145147
case log.PanicLevel:
146148
func() {
147149
defer func() {
148-
recover()
150+
_ = recover()
149151
}()
150152

151153
if len(tt.printf) == 0 {
@@ -169,7 +171,7 @@ func TestConsoleLoggerColor(t *testing.T) {
169171
}
170172

171173
func TestConsoleSTDLogCapturing(t *testing.T) {
172-
buff := new(bytes.Buffer)
174+
buff := new(buffer)
173175
cLog := New(true)
174176
cLog.SetDisplayColor(false)
175177
cLog.SetTimestampFormat("MST")
@@ -676,3 +678,115 @@ func getConsoleLoggerColorTests() []test {
676678
},
677679
}
678680
}
681+
682+
type buffer struct {
683+
b bytes.Buffer
684+
m sync.Mutex
685+
}
686+
687+
func (b *buffer) Read(p []byte) (n int, err error) {
688+
b.m.Lock()
689+
defer b.m.Unlock()
690+
return b.b.Read(p)
691+
}
692+
func (b *buffer) Write(p []byte) (n int, err error) {
693+
b.m.Lock()
694+
defer b.m.Unlock()
695+
return b.b.Write(p)
696+
}
697+
func (b *buffer) String() string {
698+
b.m.Lock()
699+
defer b.m.Unlock()
700+
return b.b.String()
701+
}
702+
703+
func (b *buffer) Bytes() []byte {
704+
b.m.Lock()
705+
defer b.m.Unlock()
706+
return b.b.Bytes()
707+
}
708+
func (b *buffer) Cap() int {
709+
b.m.Lock()
710+
defer b.m.Unlock()
711+
return b.b.Cap()
712+
}
713+
func (b *buffer) Grow(n int) {
714+
b.m.Lock()
715+
defer b.m.Unlock()
716+
b.b.Grow(n)
717+
}
718+
func (b *buffer) Len() int {
719+
b.m.Lock()
720+
defer b.m.Unlock()
721+
return b.b.Len()
722+
}
723+
func (b *buffer) Next(n int) []byte {
724+
b.m.Lock()
725+
defer b.m.Unlock()
726+
return b.b.Next(n)
727+
}
728+
func (b *buffer) ReadByte() (c byte, err error) {
729+
b.m.Lock()
730+
defer b.m.Unlock()
731+
return b.b.ReadByte()
732+
}
733+
func (b *buffer) ReadBytes(delim byte) (line []byte, err error) {
734+
b.m.Lock()
735+
defer b.m.Unlock()
736+
return b.b.ReadBytes(delim)
737+
}
738+
func (b *buffer) ReadFrom(r io.Reader) (n int64, err error) {
739+
b.m.Lock()
740+
defer b.m.Unlock()
741+
return b.b.ReadFrom(r)
742+
}
743+
func (b *buffer) ReadRune() (r rune, size int, err error) {
744+
b.m.Lock()
745+
defer b.m.Unlock()
746+
return b.b.ReadRune()
747+
}
748+
func (b *buffer) ReadString(delim byte) (line string, err error) {
749+
b.m.Lock()
750+
defer b.m.Unlock()
751+
return b.b.ReadString(delim)
752+
}
753+
func (b *buffer) Reset() {
754+
b.m.Lock()
755+
defer b.m.Unlock()
756+
b.b.Reset()
757+
}
758+
func (b *buffer) Truncate(n int) {
759+
b.m.Lock()
760+
defer b.m.Unlock()
761+
b.b.Truncate(n)
762+
}
763+
func (b *buffer) UnreadByte() error {
764+
b.m.Lock()
765+
defer b.m.Unlock()
766+
return b.b.UnreadByte()
767+
}
768+
func (b *buffer) UnreadRune() error {
769+
b.m.Lock()
770+
defer b.m.Unlock()
771+
return b.b.UnreadRune()
772+
}
773+
func (b *buffer) WriteByte(c byte) error {
774+
b.m.Lock()
775+
defer b.m.Unlock()
776+
return b.b.WriteByte(c)
777+
}
778+
func (b *buffer) WriteRune(r rune) (n int, err error) {
779+
b.m.Lock()
780+
defer b.m.Unlock()
781+
return b.b.WriteRune(r)
782+
}
783+
func (b *buffer) WriteString(s string) (n int, err error) {
784+
b.m.Lock()
785+
defer b.m.Unlock()
786+
return b.b.WriteString(s)
787+
}
788+
func (b *buffer) WriteTo(w io.Writer) (n int64, err error) {
789+
b.m.Lock()
790+
defer b.m.Unlock()
791+
return b.b.WriteTo(w)
792+
}

handlers/email/email.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"html/template"
66
"sync"
77

8-
"gopkg.in/gomail.v2"
8+
gomail "gopkg.in/gomail.v2"
99

1010
"github.com/go-playground/log"
1111
)
@@ -193,7 +193,7 @@ func (email *Email) Log(e log.Entry) {
193193
message = email.formatter(e)
194194

195195
REOPEN:
196-
// check if smtp connection open
196+
// check if SMTP connection open
197197
if !open {
198198
count++
199199
if s, err = d.Dial(); err != nil {
@@ -218,7 +218,7 @@ func (email *Email) Log(e log.Entry) {
218218
// maybe we got disconnected...
219219
alreadyTriedSending = true
220220
open = false
221-
s.Close()
221+
_ = s.Close()
222222
goto REOPEN
223223
} else if alreadyTriedSending {
224224
// we reopened and tried 2 more times, can't say we didn't try

0 commit comments

Comments
 (0)