Skip to content

Commit

Permalink
simplify API for version 0.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
francoispqt committed May 7, 2018
1 parent a5cabcb commit 4552d92
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 105 deletions.
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
# name = "github.com/x/y"
# version = "2.4.0"

ignored = ["github.com/francoispqt/benchmarks*","github.com/stretchr/testify*","github.com/stretchr/testify","github.com/json-iterator/go","github.com/buger/jsonparser"]
ignored = ["github.com/stretchr/testify*"]

[[constraint]]
name = "github.com/francoispqt/gojay"
version = "0.10.5"
version = "0.10.6"
56 changes: 32 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
[![codecov](https://codecov.io/gh/francoispqt/onelog/branch/master/graph/badge.svg)](https://codecov.io/gh/francoispqt/onelog)
[![Go Report Card](https://goreportcard.com/badge/github.com/francoispqt/onelog)](https://goreportcard.com/report/github.com/francoispqt/onelog)

# onelog

onelog is a dead simple but very efficient JSON logger.
# Onelog
Onelog is a dead simple but very efficient JSON logger.
It is one of the fastest JSON logger out there and the fastest when logging extra fields. Also, it is one of the logger with the lowest allocation.

It gives more control over log levels enabled by using bitwise operation for setting levels on a logger.
Expand All @@ -31,7 +30,7 @@ func main() {
// create a new Logger
// first argument is an io.Writer
// second argument is the level, which is an integer
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.ALL, // shortcut for onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
)
Expand All @@ -47,7 +46,7 @@ When creating a logger you must use the `|` operator with different levels to to

Example if you want levels INFO and WARN:
```go
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.INFO|onelog.WARN,
)
Expand All @@ -60,13 +59,13 @@ var logger *onelog.Logger
func init() {
// if we are in debug mode, enable DEBUG lvl
if os.Getenv("DEBUG") != "" {
logger = onelog.NewLogger(
logger = onelog.New(
os.Stdout,
onelog.ALL, // shortcut for onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL
)
return
}
logger = onelog.NewLogger(
logger = onelog.New(
os.Stdout,
onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
)
Expand All @@ -91,7 +90,7 @@ You can define a hook which will be run for every log message.

Example:
```go
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
)
Expand All @@ -106,7 +105,7 @@ logger.Info("hello world !") // {"level":"info","message":"hello world","time":"
### Without extra fields
Logging without extra fields is easy as:
```go
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
)
Expand All @@ -120,33 +119,42 @@ logger.Fatal("oh my...") // {"level":"fatal","message":"oh my..."}
### With extra fields
Logging with extra fields is quite simple, specially if you have used gojay:
```go
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
)

logger.DebugWithFields("i'm not sure what's going on", func(e onelog.Entry) {
e.String("userID", "123455")
e.Object("user", func() {
e.String("string", "foobar")
e.Int("int", 12345)
e.Int64("int64", 12345)
e.Bool("bool", true)
e.Error("err", errors.New("someError"))
e.ObjectFunc("user", func() {
e.String("name", "somename")
})
}) // {"level":"debug","message":"i'm not sure what's going on","userID":"123456","user":{"name":"somename"}}
})
// {"level":"debug","message":"i'm not sure what's going on","string":"foobar","int":12345,"int64":12345,"bool":true,"err":"someError","user":{"name":"somename"}}

logger.Info("breaking news !", func(e onelog.Entry) {
logger.InfoWithFields("breaking news !", func(e onelog.Entry) {
e.String("userID", "123455")
}) // {"level":"info","message":"breaking news !","userID":"123456"}
})
// {"level":"info","message":"breaking news !","userID":"123456"}

logger.Warn("beware !", func(e onelog.Entry) {
logger.WarnWithFields("beware !", func(e onelog.Entry) {
e.String("userID", "123455")
}) // {"level":"warn","message":"beware !","userID":"123456"}
})
// {"level":"warn","message":"beware !","userID":"123456"}

logger.Error("my printer is on fire", func(e onelog.Entry) {
logger.ErrorWithFields("my printer is on fire", func(e onelog.Entry) {
e.String("userID", "123455")
}) // {"level":"error","message":"my printer is on fire","userID":"123456"}
})
// {"level":"error","message":"my printer is on fire","userID":"123456"}

logger.Fatal("oh my...", func(e onelog.Entry) {
logger.FatalWithFields("oh my...", func(e onelog.Entry) {
e.String("userID", "123455")
}) // {"level":"fatal","message":"oh my...","userID":"123456"}
})
// {"level":"fatal","message":"oh my...","userID":"123456"}
```

## Accumulate context
Expand All @@ -157,7 +165,7 @@ Internally it creates a copy of the current logger and returns it.

Example:
```go
logger := onelog.NewLogger(
logger := onelog.New(
os.Stdout,
onelog.DEBUG|onelog.INFO|onelog.WARN|onelog.ERROR|onelog.FATAL,
).With(func(e onelog.Entry) {
Expand Down Expand Up @@ -219,15 +227,15 @@ A pull request will be submitted to Zap to integrate onelog in the benchmarks.
| Zap | 203 | 0 | 0 |
| zerolog | 154 | 0 | 0 |
| logrus | 1256 | 1554 | 24 |
| onelog | 329 | 0 | 0 |
| onelog | 317 | 0 | 0 |

## Logging basic message and accumulated context
| | ns/op | bytes/op | allocs/op |
|-------------|-------|--------------|-----------|
| Zap | 276 | 0 | 0 |
| zerolog | 164 | 0 | 0 |
| logrus | 1256 | 1554 | 24 |
| onelog | 353 | 0 | 0 |
| onelog | 333 | 0 | 0 |

## Logging message with extra fields
| | ns/op | bytes/op | allocs/op |
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/onelog_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

func BenchmarkOnelog(b *testing.B) {
b.Run("with-fields", func(b *testing.B) {
logger := onelog.NewLogger(ioutil.Discard, onelog.ALL).
logger := onelog.New(ioutil.Discard, onelog.ALL).
Hook(func(e onelog.Entry) {
e.Int("time", int(time.Now().Unix()))
e.Int64("time", time.Now().Unix())
})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand All @@ -31,9 +31,9 @@ func BenchmarkOnelog(b *testing.B) {
})
})
b.Run("message-only", func(b *testing.B) {
logger := onelog.NewLogger(ioutil.Discard, onelog.ALL).
logger := onelog.New(ioutil.Discard, onelog.ALL).
Hook(func(e onelog.Entry) {
e.Int("time", int(time.Now().Unix()))
e.Int64("time", time.Now().Unix())
})
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
Expand Down
22 changes: 16 additions & 6 deletions entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,44 @@ type Entry struct {
enc *Encoder
}

// String adds a string to the log entry
// String adds a string to the log entry.
func (e Entry) String(k, v string) {
e.enc.AddStringKey(k, v)
}

// Int adds an int to the log entry
// Int adds an int to the log entry.
func (e Entry) Int(k string, v int) {
e.enc.AddIntKey(k, v)
}

// Bool adds a bool to the log entry
// Int adds an int to the log entry.
func (e Entry) Int64(k string, v int64) {
e.enc.AddInt64Key(k, v)
}

// Bool adds a bool to the log entry.
func (e Entry) Bool(k string, v bool) {
e.enc.AddBoolKey(k, v)
}

// Error adds an error to the log entry
// Error adds an error to the log entry.
func (e Entry) Error(k string, v error) {
e.enc.AddStringKey(k, v.Error())
}

// ObjectFunc adds an object to the log entry by calling a function
// ObjectFunc adds an object to the log entry by calling a function.
func (e Entry) ObjectFunc(k string, v func()) {
e.enc.AddObjectKey(k, Object(func(enc *Encoder) {
v()
}))
}

// Object adds an object to the log entry by passing an implementation of gojay.MarshalerObject
// Object adds an object to the log entry by passing an implementation of gojay.MarshalerObject.
func (e Entry) Object(k string, obj gojay.MarshalerObject) {
e.enc.AddObjectKey(k, obj)
}

// Array adds an object to the log entry by passing an implementation of gojay.MarshalerObject.
func (e Entry) Array(k string, obj gojay.MarshalerArray) {
e.enc.AddArrayKey(k, obj)
}
62 changes: 62 additions & 0 deletions log/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package log

import (
"os"
"time"

"github.com/francoispqt/onelog"
)

var logger = onelog.New(os.Stdout, onelog.ALL).Hook(func(e onelog.Entry) {
e.Int64("time", time.Now().Unix())
})

// Info prints a message with log level Info.
func Info(msg string) {
logger.Info(msg)
}

// InfoWithFields prints a message with log level INFO and fields.
func InfoWithFields(msg string, fields func(e onelog.Entry)) {
logger.InfoWithFields(msg, fields)
}

// Debug prints a message with log level DEBUG.
func Debug(msg string) {
logger.Debug(msg)
}

// DebugWithFields prints a message with log level DEBUG and fields.
func DebugWithFields(msg string, fields func(e onelog.Entry)) {
logger.DebugWithFields(msg, fields)
}

// Warn prints a message with log level INFO.
func Warn(msg string) {
logger.Warn(msg)
}

// WarnWithFields prints a message with log level WARN and fields.
func WarnWithFields(msg string, fields func(e onelog.Entry)) {
logger.WarnWithFields(msg, fields)
}

// Error prints a message with log level ERROR.
func Error(msg string) {
logger.Error(msg)
}

// ErrorWithFields prints a message with log level ERROR and fields.
func ErrorWithFields(msg string, fields func(e onelog.Entry)) {
logger.ErrorWithFields(msg, fields)
}

// Fatal prints a message with log level FATAL.
func Fatal(msg string) {
logger.Fatal(msg)
}

// FatalWithFields prints a message with log level FATAL and fields.
func FatalWithFields(msg string, fields func(e onelog.Entry)) {
logger.FatalWithFields(msg, fields)
}
Loading

0 comments on commit 4552d92

Please sign in to comment.