Skip to content

Commit

Permalink
Updates (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Mar 11, 2019
1 parent 736ecb5 commit fdcdf50
Show file tree
Hide file tree
Showing 29 changed files with 398 additions and 924 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ _testmain.go
*.test
*.prof
old.txt
new.txt
new.txt
*.coverprofile
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
GOCMD=GO111MODULE=on go

lint:
go vet ./...

test:
$(GOCMD) test -cover -race ./...

bench:
$(GOCMD) test -run=NONE -bench=. -benchmem ./...

.PHONY: test lint
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## log
<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)
<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)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
Expand Down
49 changes: 49 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package log

import (
"bytes"
stderr "errors"
"testing"

"github.com/go-playground/errors"
)

func BenchmarkWithError(b *testing.B) {
err := stderr.New("new error")
entry := Entry{}
for i := 0; i < b.N; i++ {
_ = errorsWithError(entry, err)
}
}

func BenchmarkWithErrorParallel(b *testing.B) {
err := stderr.New("new error")
entry := Entry{}
b.RunParallel(func(pb *testing.PB) {
var buf bytes.Buffer
for pb.Next() {
buf.Reset()
_ = errorsWithError(entry, err)
}
})
}

func BenchmarkWithErrorExisting(b *testing.B) {
err := errors.New("new error")
entry := Entry{}
for i := 0; i < b.N; i++ {
_ = errorsWithError(entry, err)
}
}

func BenchmarkWithErrorExistingParallel(b *testing.B) {
err := errors.New("new error")
entry := Entry{}
b.RunParallel(func(pb *testing.PB) {
var buf bytes.Buffer
for pb.Next() {
buf.Reset()
_ = errorsWithError(entry, err)
}
})
}
26 changes: 12 additions & 14 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"fmt"
"strings"

"github.com/go-playground/errors"
)
Expand All @@ -14,36 +13,35 @@ func errorsWithError(e Entry, err error) Entry {
case errors.Chain:
cause := t[0]
errField := cause.Err.Error()
types := make([]string, 0, len(t))
types := make([]byte, 0, 64)
tags := make([]Field, 0, len(t))
for _, e := range t {
for i, e := range t {
if e.Prefix != "" {
errField = fmt.Sprintf("%s: %s", e.Prefix, errField)
}
for _, tag := range e.Tags {
tags = append(tags, Field{Key: tag.Key, Value: tag.Value})

}
types = append(types, e.Types...)
for j, typ := range e.Types {
types = append(types, typ...)
if i == len(t)-1 && j == len(e.Types)-1 {
continue
}
types = append(types, ',')
}
}
ne.Fields = append(ne.Fields, Field{Key: "error", Value: errField})
ne.Fields = append(ne.Fields, Field{Key: "source", Value: cause.Source})
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%d", cause.Source.Function(), cause.Source.File(), cause.Source.Line())})
ne.Fields = append(ne.Fields, tags...) // we do it this way to maintain order of error, source as first fields
if len(types) > 0 {
ne.Fields = append(ne.Fields, Field{Key: "types", Value: strings.Join(types, ",")})
ne.Fields = append(ne.Fields, Field{Key: "types", Value: string(types)})
}

default:
ne.Fields = append(ne.Fields, Field{Key: "error", Value: err.Error()})
frame := errors.StackLevel(2)
name := fmt.Sprintf("%n", frame)
file := fmt.Sprintf("%+s", frame)
line := fmt.Sprintf("%d", frame)
parts := strings.Split(file, "\n\t")
if len(parts) > 1 {
file = parts[1]
}
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%s", name, file, line)})
ne.Fields = append(ne.Fields, Field{Key: "source", Value: fmt.Sprintf("%s: %s:%d", frame.Function(), frame.File(), frame.Line())})
}
return ne
}
12 changes: 12 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module github.com/go-playground/log

go 1.11

require (
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91
github.com/go-playground/ansi v2.1.0+incompatible
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b
github.com/pkg/errors v0.8.1
github.com/segmentio/errors-go v1.0.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91 h1:vX+gnvBc56EbWYrmlhYbFYRaeikAke1GL84N4BEYOFE=
github.com/RackSec/srslog v0.0.0-20180709174129-a4725f04ec91/go.mod h1:cDLGBht23g0XQdLjzn6xOGXDkLK182YfINAaZEQLCHQ=
github.com/aws/aws-sdk-go v1.17.14/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/go-playground/ansi v2.1.0+incompatible h1:f9ldskdk1seTFmYjbmPaYB+WYsDKWc4UXcGb+e9JrN8=
github.com/go-playground/ansi v2.1.0+incompatible/go.mod h1:OCdnfTFO/GfFtp+ktUt+PhElbGOwyTRUuRUsA+Y5pSU=
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b h1:kP7Mu+FdZU+aG3BkVe5wM2JP7H0F/uQJfbCsZgcMQPE=
github.com/go-playground/errors v0.0.0-20190311043724-4050dd2e2e3b/go.mod h1:z6q0ljiLHVElL1MHPEMKW+U4THB6xoXSi4GGkc1unu4=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/segmentio/errors-go v1.0.0 h1:B4mbo4hP3+XffV1GhwyAcHlvWoZtYdTyc3BOVPxspTQ=
github.com/segmentio/errors-go v1.0.0/go.mod h1:RDVEREUrpa4/jM8rt5KsQpu+JoXPi6i07vG7m4tX0MY=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
8 changes: 5 additions & 3 deletions handlers/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ func (c *Console) SetWriter(w io.Writer) {
// this will redirect the output of
func handleStdLogger(done chan<- struct{}) {
r, w := io.Pipe()
defer r.Close()
defer w.Close()
defer func() {
_ = r.Close()
_ = w.Close()
}()

stdlog.SetOutput(w)

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

b = append(b, newLine)
}
c.writer.Write(b)
_, _ = c.writer.Write(b)
}
122 changes: 118 additions & 4 deletions handlers/console/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package console

import (
"bytes"
"io"
stdlog "log"
"strings"
"sync"
"testing"
"time"

Expand All @@ -21,7 +23,7 @@ import (

func TestConsoleLogger(t *testing.T) {
tests := getConsoleLoggerTests()
buff := new(bytes.Buffer)
buff := new(buffer)

log.SetExitFunc(func(int) {})

Expand Down Expand Up @@ -94,7 +96,7 @@ func TestConsoleLogger(t *testing.T) {
func TestConsoleLoggerColor(t *testing.T) {
log.SetExitFunc(func(int) {})
tests := getConsoleLoggerColorTests()
buff := new(bytes.Buffer)
buff := new(buffer)
cLog := New(false)
cLog.SetWriter(buff)
cLog.SetDisplayColor(true)
Expand Down Expand Up @@ -145,7 +147,7 @@ func TestConsoleLoggerColor(t *testing.T) {
case log.PanicLevel:
func() {
defer func() {
recover()
_ = recover()
}()

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

func TestConsoleSTDLogCapturing(t *testing.T) {
buff := new(bytes.Buffer)
buff := new(buffer)
cLog := New(true)
cLog.SetDisplayColor(false)
cLog.SetTimestampFormat("MST")
Expand Down Expand Up @@ -676,3 +678,115 @@ func getConsoleLoggerColorTests() []test {
},
}
}

type buffer struct {
b bytes.Buffer
m sync.Mutex
}

func (b *buffer) Read(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Read(p)
}
func (b *buffer) Write(p []byte) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.Write(p)
}
func (b *buffer) String() string {
b.m.Lock()
defer b.m.Unlock()
return b.b.String()
}

func (b *buffer) Bytes() []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Bytes()
}
func (b *buffer) Cap() int {
b.m.Lock()
defer b.m.Unlock()
return b.b.Cap()
}
func (b *buffer) Grow(n int) {
b.m.Lock()
defer b.m.Unlock()
b.b.Grow(n)
}
func (b *buffer) Len() int {
b.m.Lock()
defer b.m.Unlock()
return b.b.Len()
}
func (b *buffer) Next(n int) []byte {
b.m.Lock()
defer b.m.Unlock()
return b.b.Next(n)
}
func (b *buffer) ReadByte() (c byte, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadByte()
}
func (b *buffer) ReadBytes(delim byte) (line []byte, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadBytes(delim)
}
func (b *buffer) ReadFrom(r io.Reader) (n int64, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadFrom(r)
}
func (b *buffer) ReadRune() (r rune, size int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadRune()
}
func (b *buffer) ReadString(delim byte) (line string, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.ReadString(delim)
}
func (b *buffer) Reset() {
b.m.Lock()
defer b.m.Unlock()
b.b.Reset()
}
func (b *buffer) Truncate(n int) {
b.m.Lock()
defer b.m.Unlock()
b.b.Truncate(n)
}
func (b *buffer) UnreadByte() error {
b.m.Lock()
defer b.m.Unlock()
return b.b.UnreadByte()
}
func (b *buffer) UnreadRune() error {
b.m.Lock()
defer b.m.Unlock()
return b.b.UnreadRune()
}
func (b *buffer) WriteByte(c byte) error {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteByte(c)
}
func (b *buffer) WriteRune(r rune) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteRune(r)
}
func (b *buffer) WriteString(s string) (n int, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteString(s)
}
func (b *buffer) WriteTo(w io.Writer) (n int64, err error) {
b.m.Lock()
defer b.m.Unlock()
return b.b.WriteTo(w)
}
6 changes: 3 additions & 3 deletions handlers/email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"html/template"
"sync"

"gopkg.in/gomail.v2"
gomail "gopkg.in/gomail.v2"

"github.com/go-playground/log"
)
Expand Down Expand Up @@ -193,7 +193,7 @@ func (email *Email) Log(e log.Entry) {
message = email.formatter(e)

REOPEN:
// check if smtp connection open
// check if SMTP connection open
if !open {
count++
if s, err = d.Dial(); err != nil {
Expand All @@ -218,7 +218,7 @@ func (email *Email) Log(e log.Entry) {
// maybe we got disconnected...
alreadyTriedSending = true
open = false
s.Close()
_ = s.Close()
goto REOPEN
} else if alreadyTriedSending {
// we reopened and tried 2 more times, can't say we didn't try
Expand Down
Loading

0 comments on commit fdcdf50

Please sign in to comment.