Commit Graph

67 Commits

Author SHA1 Message Date
elee c7d28c47c7 change log 2022-03-20 14:19:42 -05:00
RobRimmer fc26014bd4
MsgFunc function added to Event (#406)
Allows lazy evaluation of msg text, only if log level is appropriate.
2022-02-03 15:03:11 +01:00
nichady c0c2e11fc3
Consistent casing, redundancy, and spelling/grammar (#391)
* fix casing
* remove redundant type conversions
* remove unnecessary append
x = append(y) is equivalent to x = y
* fix grammar and spelling
* rename file to enforce consistent casing with other READMEs in this repo
2021-12-21 13:07:54 +01:00
Matas 6f8a5f9ccb
Add Stringers support (#360) 2021-09-18 19:22:21 +02:00
hn8 65adfd88ec
Make Fields method accept both map and slice (#352) 2021-09-08 18:00:06 +02:00
Spike^ekipS c1533bd5f8
patched; panic Event.Object() and Event.EmbedObject() with nil (#338) 2021-08-01 17:24:18 +02:00
Olivier Poitrey 6ed1127758 Fix panic on disabled event with CallerSkipFrame
Fixes #319
2021-05-20 18:40:53 +02:00
Marcus Watkins ffbd37b8d7
Add Func log method (#321)
This adds the Func log method to log using an anonymous function
only if the level is currently enabled.

The use case is for when you don't own an object and therefore can't
create your own marshaller but need to do some translation prior to
logging.

For example, this:

msg := log.Debug()
if msg.Enabled() {
  msg.Str("complicated_thing", makeBinaryThingLoggable(thing))
}
msg.Msg("Sending complicated thing")

Turns into this:

log.Debug().
  Func(func(e *Event) { e.Str("complicated_thing", makeBinaryThingLoggable(thing)) }).
  Msg("Sending complicated thing")
2021-05-20 01:33:11 +02:00
Zephaniah Loss-Cutler-Hull 4de2fcc128
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 17:22:27 +02:00
Dima 7825d86337
stringer event method (#185) 2020-05-28 10:43:18 -07:00
Emir Ribić de5a95dced
Remove duplicate comment (#227) 2020-05-10 21:54:46 -07:00
haozibi 1c32ee06a7
Fix: Event.stack initialization error (#219) 2020-03-30 10:16:40 -07:00
Olivier Poitrey 68a3fd989d Fix a crash condition when AnErr is used with a nil interface 2020-02-10 17:15:06 -08:00
guonaihong e709c5d91e Allow custom caller level (#196)
To modify the Caller, you can pass a jump to a few function call stacks.

```go
package main

import (
    "github.com/rs/zerolog"
    "github.com/rs/zerolog/log"
)

func myError() {
    log.Debug().Caller(1).Str("test2", "v2").Send()
}

func foo() {
    log.Debug().Caller(2).Str("test2", "v2").Send()
}

func myError2() {
    foo()
}
func main() {
    zerolog.TimeFieldFormat = zerolog.TimeFormatUnix

    log.Debug().Caller().Str("test1", "v1").Send()
    myError()
    myError2()
}
```
2019-11-11 01:14:19 -08:00
Veselkov Konstantin 2e41c37ac4 Fixed comment for documentations (#194) 2019-10-24 03:02:36 -07:00
Stefan VanBuren b806a5ecbe Fix a few typos / clarify. (#169) 2019-07-19 18:10:43 +01:00
Zhang Sen a7f9fc2a17 simplify code of event.msg() (#165) 2019-07-19 12:35:57 +01:00
i6du 77a1695358 add send function for convenience (#164) 2019-07-03 23:16:03 -07:00
mikeyrcamp 299ff038c1 Add support for customizing caller field format (#133) (#134) 2019-02-20 11:39:29 -08:00
Olivier Poitrey aa55558e4c
Add support for stack trace extration of error fields (#35) 2019-01-03 11:04:23 -08:00
Olivier Poitrey 8747b7b3a5 Add ErrorHandler global to allow handling of write errors 2018-11-20 10:56:21 -08:00
Olivier Poitrey 848482bc3d Fix "could not write" error missing carriage return (again) 2018-11-12 17:50:30 -08:00
Olivier Poitrey 8e30c71369 Revert the wrapping of write errors
All errors generated by Go libraries on stderr won't be json encoded
anyway, so it does not make sense to have such a treatment for this one.
2018-11-08 14:49:44 -08:00
Olivier Poitrey 3f112dae87 Fix "could not write" error missing carriage return 2018-11-07 15:21:38 -08:00
Olivier Poitrey 20ad1708e7 Fix nil pointer exception on Discard when called with nil logger
Fixes #108
2018-09-26 09:52:52 -07:00
Olivier Poitrey 338f9bc140 Fix typo 2018-09-19 07:40:00 -07:00
Olivier Poitrey e0f8de6c35 Fix usage of sync.Pool
The current usage of sync.Pool is leaky because it stores an arbitrary
sized buffer into the pool. However, sync.Pool assumes that all items in the
pool are interchangeable from a memory cost perspective. Due to the unbounded
size of a buffer that may be added, it is possible for the pool to eventually
pin arbitrarily large amounts of memory in a live-lock situation.

As a simple fix, we just set a maximum size that we permit back into the pool.
2018-09-19 00:20:01 -07:00
Olivier Poitrey 972f27185c Remove unused hook field on event 2018-09-19 00:20:01 -07:00
Olivier Poitrey 71e1f5e052 Add the ability to discard an event from a hook
The Discard method has been added to the Event type so it can be called
from a hook to prevent the event from behing printed. This new method
works outside of the context of a hook too.

Fixes #90
2018-07-26 15:53:02 -07:00
su21 e8a8508f09 fix caller file and line number in context hook (#89)
* fix caller file and line number report in context hook

* update comment

* update comment
2018-07-25 02:48:22 -07:00
Dušan Kasan 1c6d99b455 Add custom error serialization support and provide sane defaults (#78)
As per https://github.com/rs/zerolog/issues/9 and to offer a different approach from  https://github.com/rs/zerolog/pull/11 and https://github.com/rs/zerolog/pull/35 this PR introduces custom error serialization with sane defaults without breaking the existing APIs.

This is just a first draft and is missing tests. Also, a bit of code duplication which I feel could be reduced but it serves to get the idea across.

It provides global error marshalling by exposing a `var ErrorMarshalFunc func(error) interface{}` in zerolog package that by default is  a function that returns the passed argument. It should be overriden if you require custom error marshalling.

Then in every function that accept error or array of errors `ErrorMarshalFunc` is called on the error and then the result of it is processed like this:
- if it implements `LogObjectMarshaler`, serialize it as an object
- if it is a string serialize as a string
- if it is an error, serialize as a string with the result of `Error()`
- else serialize it as an interface

The side effect of this change is that the encoders don't need the `AppendError/s` methods anymore, as the errors are serialized directly to other types.
2018-07-02 12:46:01 -07:00
Ravi Raju a025d45231 EmbedObject() API and knob to change caller frames (#66)
* Fix for a bug in cbor decodeFloat
* Add EmbedObject() method
* knob to change the depth of caller frames to skip
* removed EmbedObj() for array - since it is same as Object()
2018-05-16 18:42:33 -07:00
Olivier Poitrey ea1184be2b Get back some ns by removing the extra inferance added by binary support
benchstat old new
name                                   old time/op    new time/op    delta
LogEmpty-8                               15.2ns ±14%    13.4ns ± 3%  -12.11%  (p=0.008 n=5+5)
Disabled-8                               2.50ns ± 1%    2.28ns ± 6%   -8.81%  (p=0.008 n=5+5)
Info-8                                   44.4ns ± 1%    36.4ns ± 4%  -17.99%  (p=0.008 n=5+5)
ContextFields-8                          47.6ns ± 1%    39.4ns ± 7%  -17.30%  (p=0.008 n=5+5)
ContextAppend-8                          18.9ns ± 4%    15.2ns ± 4%  -19.68%  (p=0.008 n=5+5)
LogFields-8                               181ns ± 2%     173ns ± 2%   -4.63%  (p=0.008 n=5+5)
LogArrayObject-8                          530ns ± 3%     487ns ± 3%   -8.11%  (p=0.008 n=5+5)
LogFieldType/Int-8                       29.5ns ± 3%    28.8ns ± 2%     ~     (p=0.167 n=5+5)
LogFieldType/Interface-8                  180ns ± 7%     175ns ± 4%     ~     (p=0.579 n=5+5)
LogFieldType/Interface(Object)-8         87.8ns ± 3%    80.5ns ± 1%   -8.29%  (p=0.008 n=5+5)
LogFieldType/Object-8                    83.7ns ± 2%    77.2ns ± 3%   -7.76%  (p=0.008 n=5+5)
LogFieldType/Bools-8                     34.6ns ± 3%    32.3ns ± 6%   -6.64%  (p=0.032 n=5+5)
LogFieldType/Float-8                     43.0ns ± 4%    40.5ns ± 4%   -5.86%  (p=0.016 n=5+5)
LogFieldType/Str-8                       29.8ns ± 2%    26.5ns ± 5%  -11.01%  (p=0.008 n=5+5)
LogFieldType/Err-8                       32.8ns ± 2%    29.8ns ± 4%   -9.21%  (p=0.008 n=5+5)
LogFieldType/Durs-8                       309ns ± 3%     304ns ± 3%     ~     (p=0.238 n=5+5)
LogFieldType/Floats-8                     175ns ± 2%     174ns ± 3%     ~     (p=0.968 n=5+5)
LogFieldType/Strs-8                      51.0ns ± 3%    48.4ns ± 6%   -5.06%  (p=0.032 n=5+5)
LogFieldType/Dur-8                       44.5ns ± 3%    41.3ns ± 3%   -7.11%  (p=0.008 n=5+5)
LogFieldType/Interface(Objects)-8         758ns ± 3%     760ns ± 6%     ~     (p=1.000 n=5+5)
LogFieldType/Interfaces-8                 772ns ± 5%     762ns ± 4%     ~     (p=0.794 n=5+5)
LogFieldType/Bool-8                      28.0ns ± 6%    26.5ns ± 9%     ~     (p=0.143 n=5+5)
LogFieldType/Ints-8                      49.6ns ± 2%    46.2ns ± 2%   -6.70%  (p=0.008 n=5+5)
LogFieldType/Errs-8                      46.5ns ±11%    40.9ns ± 4%  -11.92%  (p=0.008 n=5+5)
LogFieldType/Time-8                       115ns ± 3%     113ns ± 3%     ~     (p=0.167 n=5+5)
LogFieldType/Times-8                      810ns ± 1%     811ns ± 3%     ~     (p=0.889 n=5+5)
ContextFieldType/Errs-8                   158ns ± 6%     156ns ±12%     ~     (p=1.000 n=5+5)
ContextFieldType/Times-8                  165ns ±11%     173ns ± 9%     ~     (p=0.651 n=5+5)
ContextFieldType/Interface-8              289ns ±13%     287ns ±11%     ~     (p=0.690 n=5+5)
ContextFieldType/Interface(Object)-8      285ns ±12%     297ns ± 6%     ~     (p=0.238 n=5+5)
ContextFieldType/Interface(Objects)-8     941ns ± 6%     941ns ± 5%     ~     (p=1.000 n=5+5)
ContextFieldType/Object-8                 201ns ± 5%     210ns ±12%     ~     (p=0.262 n=5+5)
ContextFieldType/Ints-8                   173ns ±10%     165ns ± 9%     ~     (p=0.198 n=5+5)
ContextFieldType/Floats-8                 297ns ± 6%     292ns ± 7%     ~     (p=0.579 n=5+5)
ContextFieldType/Timestamp-8              174ns ± 9%     174ns ±11%     ~     (p=0.810 n=5+5)
ContextFieldType/Durs-8                   445ns ± 9%     425ns ± 3%     ~     (p=0.151 n=5+5)
ContextFieldType/Interfaces-8             944ns ± 6%     876ns ±10%     ~     (p=0.095 n=5+5)
ContextFieldType/Strs-8                   179ns ±11%     165ns ±13%     ~     (p=0.135 n=5+5)
ContextFieldType/Dur-8                    158ns ± 8%     160ns ±19%     ~     (p=1.000 n=5+5)
ContextFieldType/Time-8                   152ns ±15%     148ns ±14%     ~     (p=0.952 n=5+5)
ContextFieldType/Str-8                    146ns ±12%     147ns ±16%     ~     (p=0.841 n=5+5)
ContextFieldType/Err-8                    138ns ±12%     145ns ±17%     ~     (p=0.595 n=5+5)
ContextFieldType/Int-8                    145ns ±10%     146ns ±13%     ~     (p=0.873 n=5+5)
ContextFieldType/Float-8                  181ns ± 9%     162ns ±12%     ~     (p=0.151 n=5+5)
ContextFieldType/Bool-8                   153ns ±10%     131ns ±19%     ~     (p=0.063 n=5+5)
ContextFieldType/Bools-8                  149ns ±11%     160ns ±16%     ~     (p=0.500 n=5+5)
2018-05-10 15:01:41 -07:00
Olivier Poitrey a572c9d1f6 Add missing support for zerolog marshable objects to Fields 2018-05-09 03:52:30 -07:00
Ravi Raju 2ccfab3e07 Support for adding IP Address/Prefix + stream based decoder (#49)
* added IPAddr, IPPrefix and stream based cbor decoder
* Update README with cbor decoder tool info
* Update README in cbor with comparison data
2018-04-03 23:07:18 +02:00
Ravi Raju ddfae1b613 Binary format support (#37)
Adds support for binary logging (with cbor encoding) in addition to JSON. Use the binary_log compile tag to enable the feature.
2018-03-28 11:49:41 -07:00
Johan Sim Jian An 5250a1ba2d Check nil in RawJSON. (#45) 2018-03-22 21:08:22 -07:00
Max Wolter 1c575db928 Add support for hex-encoded of byte slice (#42) 2018-03-15 10:29:26 -07:00
Kai Ren b62d797a8d Mention fields duplication caveat in documentation (#41) 2018-03-08 07:41:28 -08:00
Giovanni Bajo 9ee98f91c4 Remove allocations while logging an Array of Objects. (#38) 2018-02-13 11:18:01 -08:00
Olivier Poitrey 56a970de51 Add RawJSON field type 2018-02-12 16:05:27 -08:00
Olivier Poitrey 27e0a22cbc Add the ability to capture the logger caller file and line number
Fixes #34, #22
2018-02-07 13:54:26 -08:00
Olivier Poitrey fcbdf23e9e Use new hook internally to handle timestamp in context 2018-02-07 13:31:00 -08:00
Rodrigo Coelho c3d02683c7 Add hook support (#24) 2017-12-01 10:52:37 -07:00
Giovanni Bajo 89e128fdc1 Speed up operations when logging is disabled. (#18)
Low-level optimizations to help the compiler generate better code
when logging is disabled. Measured improvement is ~30% on amd64
(from 21 ns/op to 16 ns/op).
2017-11-05 05:22:20 -08:00
Olivier Poitrey 89ff8dbc5f Small comment fix 2017-07-26 23:42:12 -07:00
Olivier Poitrey 614d88bbf8 Add support for typed array. 2017-07-26 00:30:03 -07:00
Olivier Poitrey 6cdd9977c4 Refactor JSON encoding code 2017-07-25 22:05:32 -07:00
Olivier Poitrey fdbdb09630 Add support for custom object marshaling 2017-07-25 18:41:05 -07:00
Olivier Poitrey 2aa3c3ae4f Add some array types support 2017-07-25 12:50:35 -07:00