If a user is trying to fetch a logger from their context, they probably
want to log something. In order to allow returning a useable logger from Ctx()
without breaking backwards compatibilty with the previous behavior, we make it
configurable.
Added a package level variable "InterfaceMarshalFunc".
It's used to marshal interface to JSON encoded byte slice,
mostly when event.Interface("key", v) is called.
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")
* 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>
Without this hlog.AccessHnalder reports a size written of 0 when the
ReadFrom method is used. This is easily visible when using
http.FileServer where all files are served with a logged size of 0.
Fixes#281
Currently MultiLevelWriter would give up if any individual backing logger
failed. This could result in no logging ever happening if the bad logger
was set up in the first position.
As a motivating factor, this can happen in "normal" circumstances, such
as running as a Windows Service where stderr is not available and therefore
the ConsoleWriter will fail. If you happen to set it as the first logger,
then no logging ever takes place. To make matters worse, connecting a
debugger creates an stderr, so it made it a pretty daunting situation
to overcome.
The proposed solution is to go through all underlying loggers and return
the last error obtained, if any. In practice there isn't much being done
with those errors anyway, as the best way to address logging errors is
to hook a ErrorHandler, which will still be triggered despite this change.
Co-authored-by: Nuno Diegues <nuno@cloudflare.com>
* [ImgBot] Optimize images
/pretty.png -- 141.30kb -> 82.09kb (41.9%)
Signed-off-by: ImgBotApp <ImgBotHelp@gmail.com>
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* build: update pkg/errors from 0.8.1 to 0.9.1
According to pkg/errors 0.9.0 release[1], they have reduced the
allocations for stacktrace.
[1]: https://github.com/pkg/errors/releases/tag/v0.9.0
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
Co-authored-by: ImgBotApp <ImgBotHelp@gmail.com>