Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed May 24, 2016
2 parents 576d799 + 76dccab commit e434ce9
Show file tree
Hide file tree
Showing 15 changed files with 4,616 additions and 2,309 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## log
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
![Project status](https://img.shields.io/badge/version-1.4-green.svg)
![Project status](https://img.shields.io/badge/version-2.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
96 changes: 96 additions & 0 deletions benchmarks/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package benchmarks

import (
"errors"
"io/ioutil"
"os"
"testing"
"time"

"github.com/go-playground/log"
"github.com/go-playground/log/handlers/console"
)

var errExample = errors.New("fail")

type user struct {
Name string `json:"name"`
Email string `json:"email"`
CreatedAt time.Time `json:"created_at"`
}

var _jane = user{
Name: "Jane Doe",
Email: "jane@test.com",
CreatedAt: time.Date(1980, 1, 1, 12, 0, 0, 0, time.UTC),
}

const (
newLine = byte('\n')
defaultTS = "2006-01-02T15:04:05.000000000Z07:00"
colorFields = "%s %s%6s%s %-25s"
colorNoFields = "%s %s%6s%s %s"
colorKeyValue = " %s%s%s=%v"
colorFieldsCaller = "%s %s%6s%s %s:%d %-25s"
colorNoFieldsCaller = "%s %s%6s%s %s:%d %s"
noColorFields = "%s %6s %-25s"
noColorNoFields = "%s %6s %s"
noColorKeyValue = " %s=%v"
noColorFieldsCaller = "%s %6s %s:%d %-25s"
noColorNoFieldsCaller = "%s %6s %s:%d %s"
equals = byte('=')
v = "%v"
base10 = 10
space = byte(' ')
colon = byte(':')
)

// NOTE: log is a singleton, which means handlers need to be
// setup only once otherwise each test just adds another log
// handler and results are cumulative... makes benchmarking
// annoying because you have to manipulate the TestMain before
// running the benchmark you want.
func TestMain(m *testing.M) {

cLog := console.New()
cLog.DisplayColor(false)
cLog.SetWriter(ioutil.Discard)
cLog.SetBuffersAndWorkers(3, 3)

log.RegisterHandler(cLog, log.AllLevels...)

os.Exit(m.Run())
}

func BenchmarkConsoleParallel(b *testing.B) {

// log setup in TestMain
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.WithFields(
log.F("int", 1),
log.F("int64", int64(1)),
log.F("float", 3.0),
log.F("string", "four!"),
log.F("bool", true),
log.F("time", time.Unix(0, 0)),
log.F("error", errExample.Error()),
log.F("duration", time.Second),
log.F("user-defined type", _jane),
log.F("another string", "done!"),
).Info("Go fast.")
}

})
}

func BenchmarkConsoleSimpleFieldsParallel(b *testing.B) {

// log setup in TestMain
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Info("Go fast.")
}

})
}
39 changes: 18 additions & 21 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"fmt"
"runtime"
"strings"
"sync"
"time"
Expand All @@ -29,16 +28,14 @@ type Entry struct {
func newEntry(level Level, message string, fields []Field, calldepth int) *Entry {

entry := Logger.entryPool.Get().(*Entry)
entry.Line = 0
entry.File = entry.File[0:0]
entry.calldepth = calldepth
entry.Level = level
entry.Message = strings.TrimRight(message, cutset) // need to trim for adding fields later in handlers + why send uneeded whitespace
entry.Fields = fields
entry.Timestamp = time.Now().UTC()

if Logger.logCallerInfo && level != TraceLevel {
_, entry.File, entry.Line, _ = runtime.Caller(entry.calldepth)
}

return entry
}

Expand All @@ -48,7 +45,7 @@ var _ LeveledLogger = new(Entry)
func (e *Entry) Debug(v ...interface{}) {
e.Level = DebugLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Trace starts a trace & returns Traceable object to End + log
Expand All @@ -68,36 +65,36 @@ func (e *Entry) Trace(v ...interface{}) Traceable {
func (e *Entry) Info(v ...interface{}) {
e.Level = InfoLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Notice level formatted message.
func (e *Entry) Notice(v ...interface{}) {
e.Level = NoticeLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Warn level message.
func (e *Entry) Warn(v ...interface{}) {
e.Level = WarnLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Error level message.
func (e *Entry) Error(v ...interface{}) {
e.Level = ErrorLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Panic logs an Error level formatted message and then panics
func (e *Entry) Panic(v ...interface{}) {
s := fmt.Sprint(v...)
e.Level = PanicLevel
e.Message = s
Logger.handleEntry(e)
Logger.HandleEntry(e)

for _, f := range e.Fields {
s += fmt.Sprintf(keyVal, f.Key, f.Value)
Expand All @@ -110,22 +107,22 @@ func (e *Entry) Panic(v ...interface{}) {
func (e *Entry) Alert(v ...interface{}) {
e.Level = AlertLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Fatal level message, followed by an exit.
func (e *Entry) Fatal(v ...interface{}) {
e.Level = FatalLevel
e.Message = fmt.Sprint(v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
exitFunc(1)
}

// Debugf level formatted message.
func (e *Entry) Debugf(msg string, v ...interface{}) {
e.Level = DebugLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Tracef starts a trace & returns Traceable object to End + log
Expand All @@ -145,36 +142,36 @@ func (e *Entry) Tracef(msg string, v ...interface{}) Traceable {
func (e *Entry) Infof(msg string, v ...interface{}) {
e.Level = InfoLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Noticef level formatted message.
func (e *Entry) Noticef(msg string, v ...interface{}) {
e.Level = NoticeLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Warnf level formatted message.
func (e *Entry) Warnf(msg string, v ...interface{}) {
e.Level = WarnLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Errorf level formatted message.
func (e *Entry) Errorf(msg string, v ...interface{}) {
e.Level = ErrorLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Panicf logs an Error level formatted message and then panics
func (e *Entry) Panicf(msg string, v ...interface{}) {
s := fmt.Sprintf(msg, v...)
e.Level = PanicLevel
e.Message = s
Logger.handleEntry(e)
Logger.HandleEntry(e)

for _, f := range e.Fields {
s += fmt.Sprintf(keyVal, f.Key, f.Value)
Expand All @@ -187,14 +184,14 @@ func (e *Entry) Panicf(msg string, v ...interface{}) {
func (e *Entry) Alertf(msg string, v ...interface{}) {
e.Level = AlertLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
}

// Fatalf level formatted message, followed by an exit.
func (e *Entry) Fatalf(msg string, v ...interface{}) {
e.Level = FatalLevel
e.Message = fmt.Sprintf(msg, v...)
Logger.handleEntry(e)
Logger.HandleEntry(e)
exitFunc(1)
}

Expand Down
Loading

0 comments on commit e434ce9

Please sign in to comment.