2017-05-12 05:24:39 +00:00
|
|
|
package zerolog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-06-06 17:08:58 +00:00
|
|
|
"os"
|
2018-02-07 21:54:26 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2017-05-12 05:24:39 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-07-26 03:51:49 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog/internal/json"
|
2017-05-12 05:24:39 +00:00
|
|
|
)
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
var eventPool = &sync.Pool{
|
2017-05-12 05:24:39 +00:00
|
|
|
New: func() interface{} {
|
2017-05-20 02:43:59 +00:00
|
|
|
return &Event{
|
|
|
|
buf: make([]byte, 0, 500),
|
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-06-05 19:59:25 +00:00
|
|
|
// Event represents a log event. It is instanced by one of the level method of
|
2017-05-17 04:52:22 +00:00
|
|
|
// Logger and finalized by the Msg or Msgf method.
|
2017-05-12 05:24:39 +00:00
|
|
|
type Event struct {
|
2017-11-05 13:22:20 +00:00
|
|
|
buf []byte
|
|
|
|
w LevelWriter
|
|
|
|
level Level
|
|
|
|
done func(msg string)
|
2018-02-07 21:24:48 +00:00
|
|
|
ch []Hook // hooks from context
|
2017-12-01 17:52:37 +00:00
|
|
|
h []Hook
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// LogObjectMarshaler provides a strongly-typed and encoding-agnostic interface
|
|
|
|
// to be implemented by types used with Event/Context's Object methods.
|
2017-07-26 01:41:05 +00:00
|
|
|
type LogObjectMarshaler interface {
|
|
|
|
MarshalZerologObject(e *Event)
|
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// LogArrayMarshaler provides a strongly-typed and encoding-agnostic interface
|
|
|
|
// to be implemented by types used with Event/Context's Array methods.
|
|
|
|
type LogArrayMarshaler interface {
|
|
|
|
MarshalZerologArray(a *Array)
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
func newEvent(w LevelWriter, level Level, enabled bool) *Event {
|
2017-05-12 05:24:39 +00:00
|
|
|
if !enabled {
|
2017-05-20 02:43:59 +00:00
|
|
|
return &Event{}
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
e := eventPool.Get().(*Event)
|
|
|
|
e.buf = e.buf[:1]
|
2017-12-01 17:52:37 +00:00
|
|
|
e.h = e.h[:0]
|
2017-05-20 02:43:59 +00:00
|
|
|
e.buf[0] = '{'
|
|
|
|
e.w = w
|
|
|
|
e.level = level
|
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 06:17:28 +00:00
|
|
|
func (e *Event) write() (err error) {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-06-02 06:17:28 +00:00
|
|
|
return nil
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
e.buf = append(e.buf, '}', '\n')
|
2018-02-13 19:18:01 +00:00
|
|
|
if e.w != nil {
|
|
|
|
_, err = e.w.WriteLevel(e.level, e.buf)
|
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
eventPool.Put(e)
|
2017-05-12 05:24:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
// 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.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Enabled() bool {
|
2017-11-05 13:22:20 +00:00
|
|
|
return e != nil
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
// Msg sends the *Event with msg added as the message field if not empty.
|
2017-05-17 04:52:22 +00:00
|
|
|
//
|
2017-06-05 19:59:25 +00:00
|
|
|
// NOTICE: once this method is called, the *Event should be disposed.
|
2017-05-17 04:52:22 +00:00
|
|
|
// Calling Msg twice can have unexpected result.
|
2017-06-06 17:08:58 +00:00
|
|
|
func (e *Event) Msg(msg string) {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-06-06 17:08:58 +00:00
|
|
|
return
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2018-02-07 21:24:48 +00:00
|
|
|
if len(e.ch) > 0 {
|
|
|
|
e.ch[0].Run(e, e.level, msg)
|
|
|
|
if len(e.ch) > 1 {
|
|
|
|
for _, hook := range e.ch[1:] {
|
|
|
|
hook.Run(e, e.level, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-01 17:52:37 +00:00
|
|
|
if len(e.h) > 0 {
|
|
|
|
e.h[0].Run(e, e.level, msg)
|
|
|
|
if len(e.h) > 1 {
|
|
|
|
for _, hook := range e.h[1:] {
|
|
|
|
hook.Run(e, e.level, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
if msg != "" {
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendString(json.AppendKey(e.buf, MessageFieldName), msg)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
if e.done != nil {
|
|
|
|
defer e.done(msg)
|
|
|
|
}
|
2017-06-06 17:08:58 +00:00
|
|
|
if err := e.write(); err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v", err)
|
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2017-05-20 02:43:59 +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.
|
2017-06-06 17:08:58 +00:00
|
|
|
func (e *Event) Msgf(format string, v ...interface{}) {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-06-06 17:08:58 +00:00
|
|
|
return
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-12-01 17:52:37 +00:00
|
|
|
e.Msg(fmt.Sprintf(format, v...))
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-10 09:56:44 +00:00
|
|
|
// Fields is a helper function to use a map to set fields using type assertion.
|
|
|
|
func (e *Event) Fields(fields map[string]interface{}) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-10 09:56:44 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.buf = appendFields(e.buf, fields)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
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.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Dict(key string, dict *Event) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-18 07:10:45 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = append(append(json.AppendKey(e.buf, key), dict.buf...), '}')
|
2017-05-20 02:43:59 +00:00
|
|
|
eventPool.Put(dict)
|
2017-05-18 07:10:45 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
// 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
|
2017-05-20 02:43:59 +00:00
|
|
|
// event and give it as argument the *Event.Dict method.
|
|
|
|
func Dict() *Event {
|
2018-02-13 19:18:01 +00:00
|
|
|
return newEvent(nil, 0, true)
|
2017-05-18 07:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// Array adds the field key with an array to the event context.
|
|
|
|
// Use zerolog.Arr() to create the array or pass a type that
|
|
|
|
// implement the LogArrayMarshaler interface.
|
|
|
|
func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-26 07:14:43 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.buf = json.AppendKey(e.buf, key)
|
|
|
|
var a *Array
|
|
|
|
if aa, ok := arr.(*Array); ok {
|
|
|
|
a = aa
|
|
|
|
} else {
|
|
|
|
a = Arr()
|
|
|
|
arr.MarshalZerologArray(a)
|
|
|
|
}
|
|
|
|
e.buf = a.write(e.buf)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-26 03:51:49 +00:00
|
|
|
func (e *Event) appendObject(obj LogObjectMarshaler) {
|
2017-07-26 01:41:05 +00:00
|
|
|
pos := len(e.buf)
|
|
|
|
obj.MarshalZerologObject(e)
|
|
|
|
if pos < len(e.buf) {
|
|
|
|
// As MarshalZerologObject will use event API, the first field will be
|
2017-07-27 06:42:12 +00:00
|
|
|
// preceded by a comma. If at least one field has been added (buf grew),
|
2017-07-26 01:41:05 +00:00
|
|
|
// we replace this coma by the opening bracket.
|
|
|
|
e.buf[pos] = '{'
|
|
|
|
} else {
|
|
|
|
e.buf = append(e.buf, '{')
|
|
|
|
}
|
|
|
|
e.buf = append(e.buf, '}')
|
2017-07-26 03:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object marshals an object that implement the LogObjectMarshaler interface.
|
|
|
|
func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-26 07:14:43 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendKey(e.buf, key)
|
|
|
|
e.appendObject(obj)
|
2017-07-26 01:41:05 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
// Str adds the field key with val as a string to the *Event context.
|
|
|
|
func (e *Event) Str(key, val string) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendString(json.AppendKey(e.buf, key), val)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Strs adds the field key with vals as a []string to the *Event context.
|
|
|
|
func (e *Event) Strs(key string, vals []string) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendStrings(json.AppendKey(e.buf, key), vals)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-26 07:14:43 +00:00
|
|
|
// Bytes adds the field key with val as a string to the *Event context.
|
2017-07-27 06:42:12 +00:00
|
|
|
//
|
|
|
|
// Runes outside of normal ASCII ranges will be hex-encoded in the resulting
|
|
|
|
// JSON.
|
2017-07-01 19:47:19 +00:00
|
|
|
func (e *Event) Bytes(key string, val []byte) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-01 19:47:19 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendBytes(json.AppendKey(e.buf, key), val)
|
2017-07-01 19:47:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-02-13 00:05:27 +00:00
|
|
|
// RawJSON adds already encoded JSON to the log line under key.
|
|
|
|
//
|
|
|
|
// No sanity check is performed on b; it must not contain carriage returns and
|
|
|
|
// be valid JSON.
|
|
|
|
func (e *Event) RawJSON(key string, b []byte) *Event {
|
|
|
|
e.buf = append(json.AppendKey(e.buf, key), b...)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-06-02 07:56:14 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
e.buf = json.AppendError(json.AppendKey(e.buf, key), err)
|
|
|
|
}
|
2017-06-02 07:56:14 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Errs adds the field key with errs as an array of strings to the *Event context.
|
|
|
|
// If err is nil, no field is added.
|
|
|
|
func (e *Event) Errs(key string, errs []error) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendErrors(json.AppendKey(e.buf, key), errs)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +00:00
|
|
|
// 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.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Err(err error) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
e.buf = json.AppendError(json.AppendKey(e.buf, ErrorFieldName), err)
|
|
|
|
}
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Bool adds the field key with val as a bool to the *Event context.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Bool(key string, b bool) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendBool(json.AppendKey(e.buf, key), b)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Bools adds the field key with val as a []bool to the *Event context.
|
|
|
|
func (e *Event) Bools(key string, b []bool) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendBools(json.AppendKey(e.buf, key), b)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInt(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints adds the field key with i as a []int to the *Event context.
|
|
|
|
func (e *Event) Ints(key string, i []int) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInts(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInt8(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints8 adds the field key with i as a []int8 to the *Event context.
|
|
|
|
func (e *Event) Ints8(key string, i []int8) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInts8(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInt16(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints16 adds the field key with i as a []int16 to the *Event context.
|
|
|
|
func (e *Event) Ints16(key string, i []int16) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInts16(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInt32(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints32 adds the field key with i as a []int32 to the *Event context.
|
|
|
|
func (e *Event) Ints32(key string, i []int32) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInts32(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInt64(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Ints64 adds the field key with i as a []int64 to the *Event context.
|
|
|
|
func (e *Event) Ints64(key string, i []int64) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendInts64(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUint(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints adds the field key with i as a []int to the *Event context.
|
|
|
|
func (e *Event) Uints(key string, i []uint) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUints(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUint8(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints8 adds the field key with i as a []int8 to the *Event context.
|
|
|
|
func (e *Event) Uints8(key string, i []uint8) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUints8(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUint16(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints16 adds the field key with i as a []int16 to the *Event context.
|
|
|
|
func (e *Event) Uints16(key string, i []uint16) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUints16(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUint32(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints32 adds the field key with i as a []int32 to the *Event context.
|
|
|
|
func (e *Event) Uints32(key string, i []uint32) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUints32(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUint64(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Uints64 adds the field key with i as a []int64 to the *Event context.
|
|
|
|
func (e *Event) Uints64(key string, i []uint64) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendUints64(json.AppendKey(e.buf, key), i)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendFloat32(json.AppendKey(e.buf, key), f)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Floats32 adds the field key with f as a []float32 to the *Event context.
|
|
|
|
func (e *Event) Floats32(key string, f []float32) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendFloats32(json.AppendKey(e.buf, key), f)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendFloat64(json.AppendKey(e.buf, key), f)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Floats64 adds the field key with f as a []float64 to the *Event context.
|
|
|
|
func (e *Event) Floats64(key string, f []float64) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendFloats64(json.AppendKey(e.buf, key), f)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:43:59 +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.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Timestamp() *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendTime(json.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
|
2017-05-20 02:43:59 +00:00
|
|
|
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.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Time(key string, t time.Time) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-12 05:24:39 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendTime(json.AppendKey(e.buf, key), t, TimeFieldFormat)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
2017-05-20 02:56:18 +00:00
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
|
|
|
func (e *Event) Times(key string, t []time.Time) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendTimes(json.AppendKey(e.buf, key), t, TimeFieldFormat)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-06-07 04:58:33 +00:00
|
|
|
// Dur adds the field key with duration d stored as zerolog.DurationFieldUnit.
|
2017-05-21 04:08:42 +00:00
|
|
|
// If zerolog.DurationFieldInteger is true, durations are rendered as integer
|
|
|
|
// instead of float.
|
|
|
|
func (e *Event) Dur(key string, d time.Duration) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-20 05:43:10 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendDuration(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-05-20 05:43:10 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:50:35 +00:00
|
|
|
// Durs adds the field key with duration d stored as zerolog.DurationFieldUnit.
|
|
|
|
// If zerolog.DurationFieldInteger is true, durations are rendered as integer
|
|
|
|
// instead of float.
|
|
|
|
func (e *Event) Durs(key string, d []time.Duration) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendDurations(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-06-07 04:58:33 +00:00
|
|
|
// TimeDiff adds the field key with positive duration between time t and start.
|
|
|
|
// If time t is not greater than start, duration will be 0.
|
|
|
|
// Duration format follows the same principle as Dur().
|
|
|
|
func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-06-07 04:58:33 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
var d time.Duration
|
|
|
|
if t.After(start) {
|
|
|
|
d = t.Sub(start)
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
e.buf = json.AppendDuration(json.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-06-07 04:58:33 +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 {
|
2017-11-05 13:22:20 +00:00
|
|
|
if e == nil {
|
2017-05-20 02:56:18 +00:00
|
|
|
return e
|
|
|
|
}
|
2017-07-26 03:51:49 +00:00
|
|
|
if obj, ok := i.(LogObjectMarshaler); ok {
|
|
|
|
return e.Object(key, obj)
|
|
|
|
}
|
|
|
|
e.buf = json.AppendInterface(json.AppendKey(e.buf, key), i)
|
2017-05-20 02:56:18 +00:00
|
|
|
return e
|
|
|
|
}
|
2018-02-07 21:54:26 +00:00
|
|
|
|
|
|
|
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
|
|
|
func (e *Event) Caller() *Event {
|
|
|
|
return e.caller(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Event) caller(skip int) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
_, file, line, ok := runtime.Caller(skip)
|
|
|
|
if !ok {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.buf = json.AppendString(json.AppendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
|
|
|
|
return e
|
|
|
|
}
|