UpdateContext previously used pointer equality (l == disabledLogger) to
detect disabled loggers. This only caught the package-level singleton
returned by Ctx() but missed loggers created via Nop() or zero-value
Logger{}, which could lead to data races when a shared Nop logger was
used from multiple goroutines.
Replace the pointer comparison with a check for nil writer or Disabled
level, consistent with how Logger.should() determines if logging is
active. This prevents unnecessary context mutations on loggers that
will never produce output.
Fixes#643
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* Test coverage improvements
Implements #397 and #591, might help with #473
Test coverage for core is 97.6% with only real fringe cases remaining.
Improve `Fields` `isNilValue()` portability.
Added a new global handler `FatalExitFunc` to allow intercepting `Fatal()` messages.
Fixed CBOR float constants (removed the CBOR prefix)
Added tests for:
- global `FatalExitFunc` to allow intercepting Fatal messages (both for testing and public use).
- `Logger`
- `DisableSampling`
- `.With()` copying existing context if present.
- `.With().Fields()` all forms of `ErrorStackMarshaler` returns.
- `.WithLevel()` for `FatalLevel`, `PanicLevel`, and `DisabledLevel`.
- `.Err()` with `nil` and non-`nil` `error` and test the resulting log level.
- `.should()` covering `nil` writer.
- `.Output()` gets context values.
- `.UpdateContext()` on a disabled logger doesn't panic and is a nop.
- `.With()` all forms of `ErrorStackMarshaler` returns.
- with a `nil`writer.
- `.Hook()` passing no hooks.
- `Array`
- `.MarshalZerologArray` is a nop that won't panic.
- `Context`
- ` .Err()` and `.AnErr()` for `nil` errors and all forms of `ErrorStackMarshaler` returns.
- `Event`
- `.Caller()` to ensure we don't panic or add invalid information if `runtime.Caller()` fails
-` .Err()` and `.AnErr()` for `nil` errors and all forms of `ErrorStackMarshaler` returns.
- `Fields`
- ` .appendFields()` all forms of `ErrorStackMarshaler` returns.
- `HookLevel`
- `.Run()` methods.
- `LevelSampler`
- `.Sample() methods.
- `Syslog`
- `.Write()`, `.WriteLevel()`, and `.Close()` methods.
- `.WriteLevel()` with an `InvalidLevel`.
- `Writer`
- `.Write()` short write and error cases.
- `MultiLevelWriter` `.WriteLevel()` and for `.Write()` error and `.Close()` cases.
- test of unmarshalling a level byte returns correct error.
- CBOR decodeStream
- `.decodeFloat()`, `.binaryFmt()`, `.DecodeIfBinaryToString()`, `.DecodeObjectToStr()`, `.DecodeIfBinaryToBytes()`, `.decodeTagData()`, and `.decodeSimpleFloat()`
- handling of invalid UTF-8 sequences
- handling of UTC times.
- handling of timestamps
- handling of various map lengths
Restructure `Event` `.caller()` so we test for ok and eliminate untestable coverage hole.
Restructure `Context` `.Err()` when the `ErrorStackMarshaler` returns a `nil` so there's code to cover.
Inverted logic for`Event` `.Caller()`'s call to `runtime.Caller()` for simpler testing.
Inverted logic for `Array` `.putArray()` and `Event` `.putEvent()` so there isn't uncoverable code.
Restructure `Field` `.appendFieldList()` to early return when `ErrorStackMarshaler` returns a `nil`
Added comments for things we can't get coverage on.
Did a go fmt ./...
Coverage of core is now 100% on `Array`, `Context`, `Ctx`, `Event`, `Field`, `Hook`, and `Syslog`.
Coverage of `Globals`, `Log`, `Sampler`, and `Writer` is almost all except some real edge-cases.
JSON encoder coverage is 100%
CBOR encoder coverage is 96.3% with base, cbor, string, time and types at 100% and decode_stream (which is lacks coverage on some panic states, and two incorrect coverage-tool lapses)
* Fix CBOR tests for StackMarshaler
Forgot to use the `decodeIfBinaryToString()`
Copies over event settings for ctx, hooks, and stack
- Fix missing context for Event.appendObjects passes event's stack, ctx, and hooks (ch).
- Add Array.Errs support.
- Context.Errs, Event.Errs, use new Array.Errs()
- Array.Err handles nil correctly.
- Fix Event.Err code ignoring modified event in ErrorStackMarshaler handling.
- Fix Fields.appendFieldList when ErrorMarshalFunc returns a nil.
- Fields.appendFieldList does no longer adds the ErrorStackFieldName field when ErrorStackMarshaler returns nil.
- Don't call the ErrorMarshalFunc on nil errors.
- Removed unreachable code NOP in Context.Array
- Rewrite ErrorMarshalFunc testing.
- Made diode tests more stable.
- Log copy error in test.
- Address review comments
- Added CreateArray() and CreateDict() for both Context and Event and deprecate the Arr() and Dict().
Array now carries then stack/context/hooks so they are available to LogObjectMarshal of Array elements.
Added unit tests for error marshal that returns an interface{}
Add Ctx(context.Context) to Event and Context, allowing
log.Info().Ctx(ctx).Msg("hello"). Registered hooks can retrieve the
context from Event.GetCtx(). Facilitates writing hooks which fetch
tracing context from the go context.
* 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
* 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>
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).