2022-03-20 19:19:42 +00:00
|
|
|
package zlog
|
2017-05-12 05:24:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-04-03 21:07:18 +00:00
|
|
|
"net"
|
2017-06-06 17:08:58 +00:00
|
|
|
"os"
|
2018-02-07 21:54:26 +00:00
|
|
|
"runtime"
|
2017-05-12 05:24:39 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
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 {
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
buf []byte
|
|
|
|
w LevelWriter
|
|
|
|
level Level
|
|
|
|
done func(msg string)
|
|
|
|
stack bool // enable error stack trace
|
|
|
|
ch []Hook // hooks from context
|
|
|
|
skipFrame int // The number of additional frames to skip when printing the caller.
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 07:18:07 +00:00
|
|
|
func putEvent(e *Event) {
|
|
|
|
// Proper usage of a sync.Pool requires each entry to have approximately
|
|
|
|
// the same memory cost. To obtain this property when the stored type
|
|
|
|
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
|
|
|
|
// to place back in the pool.
|
|
|
|
//
|
|
|
|
// See https://golang.org/issue/23199
|
|
|
|
const maxSize = 1 << 16 // 64KiB
|
|
|
|
if cap(e.buf) > maxSize {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
eventPool.Put(e)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-05-09 10:51:52 +00:00
|
|
|
func newEvent(w LevelWriter, level Level) *Event {
|
2017-05-20 02:43:59 +00:00
|
|
|
e := eventPool.Get().(*Event)
|
2018-03-28 18:49:41 +00:00
|
|
|
e.buf = e.buf[:0]
|
2018-09-19 07:17:39 +00:00
|
|
|
e.ch = nil
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendBeginMarker(e.buf)
|
2017-05-20 02:43:59 +00:00
|
|
|
e.w = w
|
|
|
|
e.level = level
|
2020-03-30 17:16:40 +00:00
|
|
|
e.stack = false
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
e.skipFrame = 0
|
2017-05-20 02:43:59 +00:00
|
|
|
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
|
|
|
}
|
2018-07-26 22:53:02 +00:00
|
|
|
if e.level != Disabled {
|
|
|
|
e.buf = enc.AppendEndMarker(e.buf)
|
|
|
|
e.buf = enc.AppendLineBreak(e.buf)
|
|
|
|
if e.w != nil {
|
|
|
|
_, err = e.w.WriteLevel(e.level, e.buf)
|
|
|
|
}
|
2018-02-13 19:18:01 +00:00
|
|
|
}
|
2018-09-19 07:18:07 +00:00
|
|
|
putEvent(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 {
|
2018-07-26 22:53:02 +00:00
|
|
|
return e != nil && e.level != Disabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard disables the event so Msg(f) won't print it.
|
|
|
|
func (e *Event) Discard() *Event {
|
2018-09-26 16:52:52 +00:00
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-07-26 22:53:02 +00:00
|
|
|
e.level = Disabled
|
|
|
|
return 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-07-25 09:48:22 +00:00
|
|
|
e.msg(msg)
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:10:43 +00:00
|
|
|
// Send is equivalent to calling Msg("").
|
2019-07-04 06:16:03 +00:00
|
|
|
//
|
|
|
|
// NOTICE: once this method is called, the *Event should be disposed.
|
|
|
|
func (e *Event) Send() {
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.msg("")
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:10:43 +00:00
|
|
|
// Msgf sends the event with formatted msg added as the message field if not empty.
|
2018-07-25 09:48:22 +00:00
|
|
|
//
|
2019-07-19 17:10:43 +00:00
|
|
|
// NOTICE: once this method is called, the *Event should be disposed.
|
|
|
|
// Calling Msgf twice can have unexpected result.
|
2018-07-25 09:48:22 +00:00
|
|
|
func (e *Event) Msgf(format string, v ...interface{}) {
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.msg(fmt.Sprintf(format, v...))
|
|
|
|
}
|
|
|
|
|
2022-02-03 14:03:11 +00:00
|
|
|
func (e *Event) MsgFunc(createMsg func() string) {
|
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e.msg(createMsg())
|
|
|
|
}
|
|
|
|
|
2018-07-25 09:48:22 +00:00
|
|
|
func (e *Event) msg(msg string) {
|
2019-07-19 11:35:57 +00:00
|
|
|
for _, hook := range e.ch {
|
|
|
|
hook.Run(e, e.level, msg)
|
2018-02-07 21:24:48 +00:00
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
if msg != "" {
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendString(enc.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 {
|
2018-11-20 18:54:42 +00:00
|
|
|
if ErrorHandler != nil {
|
|
|
|
ErrorHandler(err)
|
|
|
|
} else {
|
2022-03-20 19:19:42 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "zlog: could not write event: %v\n", err)
|
2018-11-20 18:54:42 +00:00
|
|
|
}
|
2017-06-06 17:08:58 +00:00
|
|
|
}
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2021-09-08 16:00:06 +00:00
|
|
|
// Fields is a helper function to use a map or slice to set fields using type assertion.
|
|
|
|
// Only map[string]interface{} and []interface{} are accepted. []interface{} must
|
|
|
|
// alternate string keys and arbitrary values, and extraneous ones are ignored.
|
|
|
|
func (e *Event) Fields(fields 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.
|
2022-03-20 19:19:42 +00:00
|
|
|
// Use zlog.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
dict.buf = enc.AppendEndMarker(dict.buf)
|
|
|
|
e.buf = append(enc.AppendKey(e.buf, key), dict.buf...)
|
2018-09-19 14:40:00 +00:00
|
|
|
putEvent(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-05-09 10:51:52 +00:00
|
|
|
return newEvent(nil, 0)
|
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.
|
2022-03-20 19:19:42 +00:00
|
|
|
// Use zlog.Arr() to create the array or pass a type that
|
2017-07-26 07:14:43 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendKey(e.buf, key)
|
2017-07-26 07:14:43 +00:00
|
|
|
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) {
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendBeginMarker(e.buf)
|
2017-07-26 01:41:05 +00:00
|
|
|
obj.MarshalZerologObject(e)
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendEndMarker(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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendKey(e.buf, key)
|
2021-08-01 15:24:18 +00:00
|
|
|
if obj == nil {
|
|
|
|
e.buf = enc.AppendNil(e.buf)
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-07-26 03:51:49 +00:00
|
|
|
e.appendObject(obj)
|
2017-07-26 01:41:05 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:33:11 +00:00
|
|
|
// Func allows an anonymous func to run only if the event is enabled.
|
|
|
|
func (e *Event) Func(f func(e *Event)) *Event {
|
|
|
|
if e != nil && e.Enabled() {
|
|
|
|
f(e)
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-10-24 10:02:36 +00:00
|
|
|
// EmbedObject marshals an object that implement the LogObjectMarshaler interface.
|
2018-05-17 01:42:33 +00:00
|
|
|
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2021-08-01 15:24:18 +00:00
|
|
|
if obj == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-17 01:42:33 +00:00
|
|
|
obj.MarshalZerologObject(e)
|
|
|
|
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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendString(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendStrings(enc.AppendKey(e.buf, key), vals)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-09-18 17:22:21 +00:00
|
|
|
// Stringer adds the field key with val.String() (or null if val is nil)
|
|
|
|
// to the *Event context.
|
2020-05-28 17:43:18 +00:00
|
|
|
func (e *Event) Stringer(key string, val fmt.Stringer) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2021-09-18 17:22:21 +00:00
|
|
|
e.buf = enc.AppendStringer(enc.AppendKey(e.buf, key), val)
|
|
|
|
return e
|
|
|
|
}
|
2020-05-28 17:43:18 +00:00
|
|
|
|
2021-09-18 17:22:21 +00:00
|
|
|
// Stringers adds the field key with vals where each individual val
|
|
|
|
// is used as val.String() (or null if val is empty) to the *Event
|
|
|
|
// context.
|
|
|
|
func (e *Event) Stringers(key string, vals []fmt.Stringer) *Event {
|
|
|
|
if e == nil {
|
2020-05-28 17:43:18 +00:00
|
|
|
return e
|
|
|
|
}
|
2021-09-18 17:22:21 +00:00
|
|
|
e.buf = enc.AppendStringers(enc.AppendKey(e.buf, key), vals)
|
2020-05-28 17:43:18 +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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendBytes(enc.AppendKey(e.buf, key), val)
|
2017-07-01 19:47:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:29:26 +00:00
|
|
|
// Hex adds the field key with val as a hex string to the *Event context.
|
|
|
|
func (e *Event) Hex(key string, val []byte) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendHex(enc.AppendKey(e.buf, key), val)
|
2018-03-15 17:29:26 +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 {
|
2018-03-23 04:08:22 +00:00
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = appendJSON(enc.AppendKey(e.buf, key), b)
|
2018-02-13 00:05:27 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-06-18 23:30:44 +00:00
|
|
|
// RawCBOR adds already encoded CBOR to the log line under key.
|
|
|
|
//
|
|
|
|
// No sanity check is performed on b
|
|
|
|
// Note: The full featureset of CBOR is supported as data will not be mapped to json but stored as data-url
|
|
|
|
func (e *Event) RawCBOR(key string, b []byte) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.buf = appendCBOR(enc.AppendKey(e.buf, key), b)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2018-07-02 19:46:01 +00:00
|
|
|
// AnErr adds the field key with serialized err to the *Event context.
|
2017-06-02 07:56:14 +00:00
|
|
|
// If err is nil, no field is added.
|
|
|
|
func (e *Event) AnErr(key string, err error) *Event {
|
2019-01-03 19:04:23 +00:00
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
switch m := ErrorMarshalFunc(err).(type) {
|
2018-07-02 19:46:01 +00:00
|
|
|
case nil:
|
2017-06-02 07:56:14 +00:00
|
|
|
return e
|
2018-07-02 19:46:01 +00:00
|
|
|
case LogObjectMarshaler:
|
|
|
|
return e.Object(key, m)
|
|
|
|
case error:
|
2020-02-11 01:14:39 +00:00
|
|
|
if m == nil || isNilValue(m) {
|
|
|
|
return e
|
|
|
|
} else {
|
|
|
|
return e.Str(key, m.Error())
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
case string:
|
|
|
|
return e.Str(key, m)
|
|
|
|
default:
|
|
|
|
return e.Interface(key, m)
|
2017-06-02 07:56:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-25 09:48:22 +00:00
|
|
|
|
2018-07-02 19:46:01 +00:00
|
|
|
// Errs adds the field key with errs as an array of serialized errors to the
|
|
|
|
// *Event context.
|
2017-07-25 19:50:35 +00:00
|
|
|
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
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
arr := Arr()
|
|
|
|
for _, err := range errs {
|
2019-01-03 19:04:23 +00:00
|
|
|
switch m := ErrorMarshalFunc(err).(type) {
|
2018-07-02 19:46:01 +00:00
|
|
|
case LogObjectMarshaler:
|
|
|
|
arr = arr.Object(m)
|
|
|
|
case error:
|
|
|
|
arr = arr.Err(m)
|
|
|
|
case string:
|
|
|
|
arr = arr.Str(m)
|
|
|
|
default:
|
|
|
|
arr = arr.Interface(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.Array(key, arr)
|
2017-07-25 19:50:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 19:46:01 +00:00
|
|
|
// Err adds the field "error" with serialized err to the *Event context.
|
2017-06-02 07:56:14 +00:00
|
|
|
// If err is nil, no field is added.
|
2019-01-03 19:04:23 +00:00
|
|
|
//
|
2022-03-20 19:19:42 +00:00
|
|
|
// To customize the key name, change zlog.ErrorFieldName.
|
2019-01-03 19:04:23 +00:00
|
|
|
//
|
2022-03-20 19:19:42 +00:00
|
|
|
// If Stack() has been called before and zlog.ErrorStackMarshaler is defined,
|
2019-01-03 19:04:23 +00:00
|
|
|
// the err is passed to ErrorStackMarshaler and the result is appended to the
|
2022-03-20 19:19:42 +00:00
|
|
|
// zlog.ErrorStackFieldName.
|
2017-05-20 02:43:59 +00:00
|
|
|
func (e *Event) Err(err error) *Event {
|
2019-01-03 19:04:23 +00:00
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e.stack && ErrorStackMarshaler != nil {
|
|
|
|
switch m := ErrorStackMarshaler(err).(type) {
|
|
|
|
case nil:
|
|
|
|
case LogObjectMarshaler:
|
|
|
|
e.Object(ErrorStackFieldName, m)
|
|
|
|
case error:
|
2020-02-11 01:14:39 +00:00
|
|
|
if m != nil && !isNilValue(m) {
|
|
|
|
e.Str(ErrorStackFieldName, m.Error())
|
|
|
|
}
|
2019-01-03 19:04:23 +00:00
|
|
|
case string:
|
|
|
|
e.Str(ErrorStackFieldName, m)
|
|
|
|
default:
|
|
|
|
e.Interface(ErrorStackFieldName, m)
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 19:46:01 +00:00
|
|
|
return e.AnErr(ErrorFieldName, err)
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2019-01-03 19:04:23 +00:00
|
|
|
// Stack enables stack trace printing for the error passed to Err().
|
|
|
|
//
|
|
|
|
// ErrorStackMarshaler must be set for this method to do something.
|
|
|
|
func (e *Event) Stack() *Event {
|
|
|
|
if e != nil {
|
|
|
|
e.stack = true
|
|
|
|
}
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendBool(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendBools(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInt(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInts(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInt8(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInts8(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInt16(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInts16(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInt32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInts32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInt64(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInts64(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUint(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUints(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUint8(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUints8(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUint16(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUints16(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUint32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUints32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUint64(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendUints64(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendFloat32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendFloats32(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendFloat64(enc.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendFloats64(enc.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.
|
2022-03-20 19:19:42 +00:00
|
|
|
// To customize the key name, change zlog.TimestampFieldName.
|
2018-03-08 15:41:28 +00:00
|
|
|
//
|
|
|
|
// NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one
|
|
|
|
// already.
|
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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendTime(enc.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
|
2017-05-20 02:43:59 +00:00
|
|
|
return e
|
2017-05-12 05:24:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Time adds the field key with t formatted as string using zlog.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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendTime(enc.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
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Times adds the field key with t formatted as string using zlog.TimeFieldFormat.
|
2017-07-25 19:50:35 +00:00
|
|
|
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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendTimes(enc.AppendKey(e.buf, key), t, TimeFieldFormat)
|
2017-07-25 19:50:35 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Dur adds the field key with duration d stored as zlog.DurationFieldUnit.
|
|
|
|
// If zlog.DurationFieldInteger is true, durations are rendered as integer
|
2017-05-21 04:08:42 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-05-20 05:43:10 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Durs adds the field key with duration d stored as zlog.DurationFieldUnit.
|
|
|
|
// If zlog.DurationFieldInteger is true, durations are rendered as integer
|
2017-07-25 19:50:35 +00:00
|
|
|
// 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
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendDurations(enc.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)
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
2017-06-07 04:58:33 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-01-19 11:10:08 +00:00
|
|
|
// Any is a wrapper around Event.Interface.
|
|
|
|
func (e *Event) Any(key string, i interface{}) *Event {
|
|
|
|
return e.Interface(key, i)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendInterface(enc.AppendKey(e.buf, key), i)
|
2017-05-20 02:56:18 +00:00
|
|
|
return e
|
|
|
|
}
|
2018-02-07 21:54:26 +00:00
|
|
|
|
2022-10-18 00:51:54 +00:00
|
|
|
// Type adds the field key with val's type using reflection.
|
|
|
|
func (e *Event) Type(key string, val interface{}) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
e.buf = enc.AppendType(enc.AppendKey(e.buf, key), val)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
// CallerSkipFrame instructs any future Caller calls to skip the specified number of frames.
|
|
|
|
// This includes those added via hooks from the context.
|
|
|
|
func (e *Event) CallerSkipFrame(skip int) *Event {
|
2021-05-20 16:40:53 +00:00
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
Fix handling of printing caller with Write, Print, and Printf. (#315)
* Add event.CallerSkipFrame(skip int)
This indicates that, for this event, we should skip an additional
specified number of frames.
This is cumulative, calling it twice for the same event will add both
numbers together, and this is in addition to any skip frame settings set
through the context, or globally.
The indended purpose is for wrappers to Msg or Msgf, so that the actual
caller is always printed correctly.
* Use CallerSkipFrame for Print, Printf, and Write.
This allows us to use the correct caller when using these 3 functions.
Co-authored-by: Zephaniah E. Loss-Cutler-Hull <warp@aehallh.com>
2021-05-13 15:22:27 +00:00
|
|
|
e.skipFrame += skip
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-03-20 19:19:42 +00:00
|
|
|
// Caller adds the file:line of the caller with the zlog.CallerFieldName key.
|
2019-11-11 09:14:19 +00:00
|
|
|
// The argument skip is the number of stack frames to ascend
|
|
|
|
// Skip If not passed, use the global variable CallerSkipFrameCount
|
|
|
|
func (e *Event) Caller(skip ...int) *Event {
|
|
|
|
sk := CallerSkipFrameCount
|
|
|
|
if len(skip) > 0 {
|
|
|
|
sk = skip[0] + CallerSkipFrameCount
|
|
|
|
}
|
|
|
|
return e.caller(sk)
|
2018-02-07 21:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Event) caller(skip int) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2022-07-29 14:29:02 +00:00
|
|
|
pc, file, line, ok := runtime.Caller(skip + e.skipFrame)
|
2018-02-07 21:54:26 +00:00
|
|
|
if !ok {
|
|
|
|
return e
|
|
|
|
}
|
2022-07-29 14:29:02 +00:00
|
|
|
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(pc, file, line))
|
2018-02-07 21:54:26 +00:00
|
|
|
return e
|
|
|
|
}
|
2018-04-03 21:07:18 +00:00
|
|
|
|
|
|
|
// IPAddr adds IPv4 or IPv6 Address to the event
|
|
|
|
func (e *Event) IPAddr(key string, ip net.IP) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendIPAddr(enc.AppendKey(e.buf, key), ip)
|
2018-04-03 21:07:18 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the event
|
|
|
|
func (e *Event) IPPrefix(key string, pfx net.IPNet) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendIPPrefix(enc.AppendKey(e.buf, key), pfx)
|
2018-04-03 21:07:18 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// MACAddr adds MAC address to the event
|
|
|
|
func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event {
|
|
|
|
if e == nil {
|
|
|
|
return e
|
|
|
|
}
|
2018-05-10 22:01:41 +00:00
|
|
|
e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha)
|
2018-04-03 21:07:18 +00:00
|
|
|
return e
|
|
|
|
}
|