zlog/event.go

295 lines
6.7 KiB
Go
Raw Normal View History

2017-05-12 05:24:39 +00:00
package zerolog
import (
"fmt"
2017-05-18 07:10:45 +00:00
"io/ioutil"
2017-05-12 05:24:39 +00:00
"sync"
"time"
)
var eventPool = &sync.Pool{
2017-05-12 05:24:39 +00:00
New: func() interface{} {
return &Event{
buf: make([]byte, 0, 500),
}
2017-05-12 05:24:39 +00:00
},
}
2017-05-17 04:52:22 +00:00
// Event represents a log event. It is instancied by one of the level method of
// Logger and finalized by the Msg or Msgf method.
2017-05-12 05:24:39 +00:00
type Event struct {
buf []byte
2017-05-12 05:24:39 +00:00
w LevelWriter
level Level
enabled bool
done func(msg string)
}
func newEvent(w LevelWriter, level Level, enabled bool) *Event {
2017-05-12 05:24:39 +00:00
if !enabled {
return &Event{}
2017-05-12 05:24:39 +00:00
}
e := eventPool.Get().(*Event)
e.buf = e.buf[:1]
e.buf[0] = '{'
e.w = w
e.level = level
e.enabled = true
return e
2017-05-12 05:24:39 +00:00
}
func (e *Event) write() (err error) {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return nil
2017-05-12 05:24:39 +00:00
}
e.buf = append(e.buf, '}', '\n')
_, err = e.w.WriteLevel(e.level, e.buf)
eventPool.Put(e)
2017-05-12 05:24:39 +00:00
return
}
// Enabled return false if the *Event is going to be filtered out by
2017-05-12 05:24:39 +00:00
// log level or sampling.
func (e *Event) Enabled() bool {
2017-05-12 05:24:39 +00:00
return e.enabled
}
// Msg sends the *Event with msg added as the message field if not empty.
2017-05-17 04:52:22 +00:00
//
// NOTICE: once this methid is called, the *Event should be disposed.
2017-05-17 04:52:22 +00:00
// Calling Msg twice can have unexpected result.
func (e *Event) Msg(msg string) error {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return nil
2017-05-12 05:24:39 +00:00
}
if msg != "" {
e.buf = appendString(e.buf, MessageFieldName, msg)
2017-05-12 05:24:39 +00:00
}
if e.done != nil {
defer e.done(msg)
}
return e.write()
}
2017-05-20 05:48:00 +00:00
// Msgf sends the event with formated msg added as the message field if not empty.
2017-05-17 04:52:22 +00:00
//
// NOTICE: once this methid is called, the *Event should be disposed.
2017-05-17 04:52:22 +00:00
// Calling Msg twice can have unexpected result.
func (e *Event) Msgf(format string, v ...interface{}) error {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return nil
2017-05-12 05:24:39 +00:00
}
msg := fmt.Sprintf(format, v...)
if msg != "" {
e.buf = appendString(e.buf, MessageFieldName, msg)
2017-05-12 05:24:39 +00:00
}
if e.done != nil {
defer e.done(msg)
}
return e.write()
}
2017-05-20 07:22:37 +00:00
// Dict adds the field key with a dict to the event context.
2017-05-18 07:10:45 +00:00
// Use zerolog.Dict() to create the dictionary.
func (e *Event) Dict(key string, dict *Event) *Event {
2017-05-18 07:10:45 +00:00
if !e.enabled {
return e
}
e.buf = append(append(appendKey(e.buf, key), dict.buf...), '}')
eventPool.Put(dict)
2017-05-18 07:10:45 +00:00
return e
}
// Dict creates an Event to be used with the *Event.Dict method.
2017-05-18 07:10:45 +00:00
// Call usual field methods like Str, Int etc to add fields to this
// event and give it as argument the *Event.Dict method.
func Dict() *Event {
2017-05-18 07:10:45 +00:00
return newEvent(levelWriterAdapter{ioutil.Discard}, 0, true)
}
// Str adds the field key with val as a string to the *Event context.
func (e *Event) Str(key, val string) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendString(e.buf, key, val)
return e
2017-05-12 05:24:39 +00:00
}
2017-06-02 07:56:14 +00:00
// AnErr adds the field key with err as a string to the *Event context.
// If err is nil, no field is added.
func (e *Event) AnErr(key string, err error) *Event {
if !e.enabled {
return e
}
e.buf = appendErrorKey(e.buf, key, err)
return e
}
// Err adds the field "error" with err as a string to the *Event context.
2017-06-02 07:56:14 +00:00
// If err is nil, no field is added.
2017-05-17 04:52:22 +00:00
// To customize the key name, change zerolog.ErrorFieldName.
func (e *Event) Err(err error) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendError(e.buf, err)
return e
2017-05-12 05:24:39 +00:00
}
// Bool adds the field key with val as a Boolean to the *Event context.
func (e *Event) Bool(key string, b bool) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendBool(e.buf, key, b)
return e
2017-05-12 05:24:39 +00:00
}
// Int adds the field key with i as a int to the *Event context.
func (e *Event) Int(key string, i int) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendInt(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Int8 adds the field key with i as a int8 to the *Event context.
func (e *Event) Int8(key string, i int8) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendInt8(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Int16 adds the field key with i as a int16 to the *Event context.
func (e *Event) Int16(key string, i int16) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendInt16(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Int32 adds the field key with i as a int32 to the *Event context.
func (e *Event) Int32(key string, i int32) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendInt32(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Int64 adds the field key with i as a int64 to the *Event context.
func (e *Event) Int64(key string, i int64) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendInt64(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Uint adds the field key with i as a uint to the *Event context.
func (e *Event) Uint(key string, i uint) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendUint(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Uint8 adds the field key with i as a uint8 to the *Event context.
func (e *Event) Uint8(key string, i uint8) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendUint8(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Uint16 adds the field key with i as a uint16 to the *Event context.
func (e *Event) Uint16(key string, i uint16) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendUint16(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Uint32 adds the field key with i as a uint32 to the *Event context.
func (e *Event) Uint32(key string, i uint32) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendUint32(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Uint64 adds the field key with i as a uint64 to the *Event context.
func (e *Event) Uint64(key string, i uint64) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendUint64(e.buf, key, i)
return e
2017-05-12 05:24:39 +00:00
}
// Float32 adds the field key with f as a float32 to the *Event context.
func (e *Event) Float32(key string, f float32) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendFloat32(e.buf, key, f)
return e
2017-05-12 05:24:39 +00:00
}
// Float64 adds the field key with f as a float64 to the *Event context.
func (e *Event) Float64(key string, f float64) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendFloat64(e.buf, key, f)
return e
2017-05-12 05:24:39 +00:00
}
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key.
2017-05-17 04:52:22 +00:00
// To customize the key name, change zerolog.TimestampFieldName.
func (e *Event) Timestamp() *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendTimestamp(e.buf)
return e
2017-05-12 05:24:39 +00:00
}
2017-05-17 04:52:22 +00:00
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (e *Event) Time(key string, t time.Time) *Event {
2017-05-12 05:24:39 +00:00
if !e.enabled {
return e
}
e.buf = appendTime(e.buf, key, t)
return e
2017-05-12 05:24:39 +00:00
}
2017-05-21 04:08:42 +00:00
// Dur adds the fields key with duration d stored as zerolog.DurationFieldUnit.
// If zerolog.DurationFieldInteger is true, durations are rendered as integer
// instead of float.
func (e *Event) Dur(key string, d time.Duration) *Event {
2017-05-20 05:43:10 +00:00
if !e.enabled {
return e
}
2017-05-21 04:08:42 +00:00
e.buf = appendDuration(e.buf, key, d)
2017-05-20 05:43:10 +00:00
return e
}
2017-05-20 05:25:37 +00:00
// Interface adds the field key with i marshaled using reflection.
func (e *Event) Interface(key string, i interface{}) *Event {
if !e.enabled {
return e
}
2017-05-20 05:25:37 +00:00
e.buf = appendInterface(e.buf, key, i)
return e
}