1
0
mirror of https://github.com/rs/zerolog synced 2026-06-08 17:13:30 +00:00

47 Commits

Author SHA1 Message Date
Marc Brooks f6fbd330be Test coverage improvements (#748)
* 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()`
2026-01-12 15:03:52 +00:00
sted 1869fa55be FormatPartValueByName for flexible custom formatting for ConsoleWriter (#541) 2025-01-03 23:53:36 +00:00
Abdullah Alaadine 31e7995c5b remove unnecessary nil checks (#701)
Co-authored-by: Abdullah Alaadine <abdullah.aladdine@montymobile.com>
2024-12-27 02:41:46 +00:00
Marc M. Adkins c78e50e2da Add fields order (#550) 2024-05-04 15:43:27 +02:00
Andrey Karpov 0efa414907 Fix panic caused by an extra malformed level field (#665) 2024-04-24 02:13:48 +02:00
Felipe Gasper 582007f21d Add a time.Location to ConsoleWriter. (This allows UTC timestamps.) (#531)
* Add a time.Location to ConsoleWriter. (This allows UTC timestamps.)

* add test
2024-03-02 01:52:31 +01:00
Garret Buell 4d78dc5ffa Add forwarding close methods to several writer implementations (#636) 2024-01-13 19:08:26 +01:00
Mitar 507a426bf8 Use map for formatted levels. (#599) 2023-10-20 16:17:01 +02:00
Mitar 6ed7439d9c Do not colorize if not necessary. (#598) 2023-10-14 12:38:38 +02:00
Mitar f115bfade5 Make console output more readable. (#597) 2023-10-13 23:38:04 +02:00
Mitar 11f895c15e Add FormatPrepare to ConsoleWriter. (#595) 2023-10-13 10:32:42 +02:00
Lasse Gaardsholt 1bac5cca50 added support for NO_COLOR (#586)
* added support for NO_COLOR

Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com>

* added unit test for `NO_COLOR`

Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com>

* NO_COLOR can now be set to anything

Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com>

---------

Signed-off-by: Lasse Gaardsholt <lasse.gaardsholt@bestseller.com>
2023-09-05 11:18:05 +02:00
Ted Lilley 5bdc93f7eb Fix console formatter for timestampunixnano (#502)
Co-authored-by: Ted Lilley <ted.lilley@digi.com>
2022-11-03 01:35:19 +01:00
Patrick Scheid a9a8199d2d ConsoleWriter fallbacks to local timezone for missing TZ indicator (#497)
Closes issue #483

Before:
We use time.Parse which defaults to TZ UTC if there is no time zone
indicator specified in the time layout string. During the reparsing in
ConsoleWriter we therefore added the TZ difference to UTC twice.

After:
We use time.ParseInLocal where we need to provide a dedicated fallback
TZ as a fallback. Since we now fallback to the local TZ, we don't add
the TZ difference to UTC twice.
2022-10-21 23:32:46 +02:00
Yuki Furuyama 55aaf043cf Show original level message if it's not predefined value. (#476) 2022-09-18 16:53:56 +02:00
Mitar 4099072c03 Support extra arbitrary data at the end of console log (#416) 2022-07-18 23:00:50 +01:00
Olivier Poitrey afdf9978ec Revert "remove fields written into "PartsOrder" (#383)"
This reverts commit 2a13872817.
2022-07-16 21:17:58 +01:00
Mitar dbdec88d16 Use everywhere InterfaceMarshalFunc (#414) 2022-07-16 21:03:29 +01:00
Mitar b30730fab2 Show local time in console (#415) 2022-07-16 21:02:45 +01:00
Adam Horacek 2a13872817 remove fields written into "PartsOrder" (#383) 2022-06-29 01:36:09 +02:00
Chelsea Hoover 263b0bde36 #411 Add FieldsExclude parameter to console writer (#418) 2022-02-27 18:33:36 +01:00
Mitar 361cdf616a Remove extra space in console when there is no message (#413) 2022-02-19 16:39:18 +01:00
nichady 665519c4da Fix ConsoleWriter color on Windows (#390)
* fix ConsoleWriter colors on windows
* fix ConsoleWriter color when created with struct literal
2021-12-19 23:50:53 +01:00
Asger Noer 0d2f0be4ee fix: error field name color (#377) 2021-11-09 12:34:43 +01:00
adw1n 0872592ea2 Make default console formatter work with customized level names (#330) 2021-07-15 22:04:15 +02:00
Jason McVetta b7e31f4fe7 Log relative path for caller when using ConsoleWriter (#267)
* use relative file path
2020-12-28 22:38:24 +01:00
CrazyMax 29d8dac5e8 Allow to exclude parts from console output (#272)
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
2020-11-29 10:13:56 +01:00
Jason McVetta cac3894be4 don't trim leading slash on callers outside cwd (#266)
Fixes https://github.com/rs/zerolog/issues/265
2020-11-02 13:52:51 +01:00
wphan d2a97b366b fix ConsoleWriter for TimeFormatMicro (#206) 2019-12-20 09:34:21 -08:00
CrazyMax 4502cc1942 Add TraceLevel (#158) 2019-11-04 11:39:22 -08:00
Olivier Poitrey acf3980132 console: handle timestamp in ms correctly + fix UTC 2019-04-25 12:44:49 -07:00
Olivier Poitrey 33f552ec3d Fix console write with missing level field 2019-04-25 12:12:24 -07:00
Olivier Poitrey 2a07580c27 Fix console writer when unix time stamp and/or no message field is used 2019-04-25 12:01:27 -07:00
Kevin McConnell 4daee2b758 Don't use faint text in colorized console output (#131)
The faint text style doesn't seem to be supported by all terminals,
which means the default console output loses most of its coloring on
them.

This commit changes the color scheme to more widely-supported colors.
This also matches how it looked in earlier versions of this library.
2019-02-07 07:45:02 -08:00
Ingmar Stein c482b20623 ConsoleWriter: fix race condition (#120)
Alternative approach to #118: fix the race condition by replacing the package-level customization settings with curried functions.
2018-12-07 09:59:01 -08:00
Ingmar Stein 7179aeef58 ConsoleWriter: reset buffer before returning it to the pool (#119)
The buffers put back into the pool should be equivalent to those generated by the `New` function.
2018-12-05 07:46:12 +00:00
Yongzheng Lai 7bcaa1a99e fix: console ts with json number (#115) 2018-11-12 00:50:17 -08:00
Karel Minarik a4c54e5d8b Fix the ConsoleWriter default parts order (#113)
In order to prevent incorrect output when somebody uses a different name eg. for the "MessageFieldName",
the `consoleDefaultPartsOrder` variable has been switched to a function, which is called in `Write()`,
in order to pick up the custom name.

Related: rs/zerolog#92
2018-11-07 09:39:38 -08:00
Karel Minarik 96f91bb4f5 Refactored zerolog.ConsoleWriter to allow customization (#92)
* Added a simple benchmarking test for the ConsoleWriter
* Refactored `zerolog.ConsoleWriter` to allow customization

Closes #84
2018-11-05 02:15:13 -08:00
Dave McCormick bae001d86b Allow devs to change the width of the logging level column in consolewriter (#87)
* Allow user to change the width of the logging level column
* Change default level width to 0 (no dynamic width)
2018-07-25 10:05:55 -07:00
Olivier Poitrey c62533f761 Fix ConsoleWriter test 2018-05-17 16:35:57 -07:00
Olivier Poitrey fb469685aa Fix ConsoleWriter when zerolog is configured to use UNIX timestamp 2018-05-17 16:10:49 -07:00
Olivier Poitrey d2b7a51951 Do not print large ints using scientific notation with ConsoleWriter
Fixes #55
2018-04-19 13:13:40 -07: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
Olivier Poitrey a717e7cbed Improve ConsoleWriter of non-scalar types 2018-02-13 00:20:42 -08:00
Olivier Poitrey 2ed2f2c974 Fix pretty logging and add a screenshot to the README 2017-08-05 20:45:41 -07:00
Olivier Poitrey 90fdb63d84 Add missing console file 2017-08-05 19:59:04 -07:00