* 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()`
* Added documentation for DurationFieldFormat
Added documentation for the new option added in PR#733
Added code markers some references to logger func/types.
Also, did a format-document pass to get rid of trailing blanks,
missing/extra blank lines, and other markdown warnings.
* Added documentation for Concurrency Safety
Added documentation to the Concurrency Safety section about not referencing
Event objects after they are written.
Closes#731
* Ensure that Event and Array objects returned to pool are cleared
Help ensure no corruptible state remains in objects being returned to the pool.
Always clear the contextual properties to ensure the recycled (or erroneously referenced)
Event or Array are not accidentally used/shared.
Sets the truncates the buffer (setting length, not capacity, to zero).
Make it really obvious when you hold references as mentioned in #733
Clarified the documentation about holding references to CreateDict() events.
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 array handling of IPAddr and IPPrefix
Updated documentation, benchmark, and tests including catching up some missing.
Cleaned up couple lint messages
* Added cbor encoding for []net.IP and []net.IPNet
* Fix binary_test
Typos galore, thanks CI
* Remove feral x character
* Added IPAddrs and IPPrefixes tests for json encoder
* Increase code coverage and test cleanup
Added Type to the BenchmarkLogFieldType and BenchmarkContexdtFieldType test cases, also to log_test.go
Moved the test-fixture data for them and also shared them with new event test for nil events.
Added a couple strings for encodeByteTest for FF, CR, LF
Added complete cbor testing for AppendInt*, AppendInts*, AppendUint*, and AppendUints* methods.
Added test for float IsNaN and Inf(0) and Inf(-1).
Added all min/max/smallest value tests for ints and floats
Extended the Array test to include IPPrefix, MACAddr, Interface, and object marshaled.
Better test of MarshalZerologObject.
Added test for encoder AppendStrings and AppendStringers.
* Remove merge detritus.
* Remove code duplication when appending objects
* Add missing Stringers support to Context
* Add test cases for Err and Object in Array
* Add missing benchmark for Events and Contexts
Events: Any, Bytes, Hex, Float32, Floats32, Stringer, Stringers, Timestamp
Context: Any, Bytes, Hex, Float32, Floats32, Stringers
(also renamed Float,Floats to Float64,Floats64)
* Add test of MarshalZeroLogObject of error
* Build out Event test cases
Test of nil Event
- write
- Array, Dict
- Fields
- Int8, Ints8, Int16, Ints16, Int32, Ints32, Int64, Ints64
- Uint8, Uints8, Uint16, Uints16, Uint32, Uints32, Uint64, Uints64
- RawCBOR, RawJSON, EmbedObject
- Stringers
- Caller, CallerSkip, Stack
- Send, Msg, Msgf, MsgFunc
- (renamed tests of Float, Floats to Float64, Floats64)
Added test of MsgFunc
* Add more tests
- Dur (With)
- Any (With, Fields, Fields map, Disabled)
- Interface
- Stack (Fields)
Fix issue with testing a nil loggableError
* Split out the With tests again type arrays
Simplifies the "want" maintenance
* Add missing test fixture data
Bytes, Floats32, Ints8, Ints16, Ints32, Ints64, Uints8, Uints16, Uints32, Uints64, RawJSON, RawCBOR
Stringers is now an array (not a single Stringer)
(also renamed Float, Floats to Float64, Floats64)
* Add examples for IPAddrs, IPPrefixes, Times
Missing examples that increase coverage
* Build out the complete ErrorMarshalFunc tests
* Add AppendStrings to CBOR tests
* Add CBOR AppendTimes, AppendDuration, AppendDurations tests
* Add CBOR tests
- BeginMarker, EndMarker, AppendArrayDim, AppendLineBreak
- AppendObjectData
- AppendIterface
- AppendType
- AppendHex
- Empty arrays for
- Bools
- Floats32, Floats64
- Large arrays for
- Bools,
- Uints8, Uints16, Uints32, Uints64
- Floats32, Floats64
- IPAddrs, IPPrefixes
- Small arrays for
- Ints, Ints8, Ints16, Ints32, Ints64
- Uints, Uints32, Uints64
- Floats32, Floats64
* Add CBOR test for AppendKey
* Added more string encoding tests
Test escape character handling.
* Fix test import cycle
* Rename hex to hexCharacters to avoid namespace.
* Remove leftover comment
* Add test cases to share between CBOR and JSON tests
* Switched CBOR to shared test cases
* Add JSON test for AppendKey
* Switch to shared test cases
Added test for AppendStringer
* Added JSON test
- AppendNil
- AppendBeginMarker, AppendEndMarker
- AppendArrayStart, AppendArrayEnd
- AppendArrayDelim
- AppendLineBreak
- AppendObjectData
- AppendInterface
- AppendBool, AppendBools
* Add JSON time tests
- AppendTime, AppendTimes
- AppendTime (past/present) (integer/float)
- AppendDuration, AppendDurations
* Added JSON tests for plurals
Split out the Int and Float tests into distinct files.
Add
- AppendInt
- AppendInts8, AppendInts16, AppendInts32, AppendInts64, AppendInts
- AppendUints8, AppendUints16, AppendUints32, AppendUints64, AppendUints
- AppendFloats32, AppendFloats64
* Add array handling of IPAddr and IPPrefix
Updated documentation, benchmark, and tests including catching up some missing.
Cleaned up couple lint messages
* Added cbor encoding for []net.IP and []net.IPNet
* Fix binary_test
Typos galore, thanks CI
* Remove feral x character
* Added IPAddrs and IPPrefixes tests for json encoder
Replace 'append append' with 'appends'.
This resolves what appears to be an unintentional
search/replace action that occurred as part of other
cleanup work.
Co-authored-by: Adam Chalkley <atc0005@users.noreply.github.com>
* 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
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.
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.