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

Compare commits

...

333 Commits

Author SHA1 Message Date
Ali Asghar 116c8060e0 event: restore Err() logging when ErrorStackMarshaler returns nil (#763)
Commit f6fbd33 added `case nil: return e` in Event.Err() so that when
ErrorStackMarshaler returns nil (the error has no stack trace), the
function returns early without logging the error itself.

This was a regression. The previous behaviour was to fall through the
switch and always call AnErr(ErrorFieldName, err), ensuring the error
message is logged even when no stack is attached.

Libraries such as elastic/ecs-logging-go-zerolog (and plain fmt.Errorf
errors) rely on the old behaviour: calling Stack().Err(err) should
always log the error field; the stack field is additional and optional.

Change the `nil` case from `return e` to a comment (fall-through) so
that AnErr is called unconditionally. Update the test that was written
to match the incorrect behaviour.

Fixes #762

Signed-off-by: alliasgher <alliasgher123@gmail.com>
2026-04-21 00:17:57 +01:00
Olivier Poitrey 13966551e7 Bump CI Go matrix minimum from 1.21 to 1.23
go.mod requires go >= 1.23 and the codebase uses Go 1.22+ features
(log/slog, range-over-integer), so Go 1.21 CI jobs cannot pass.
2026-03-27 20:13:06 +01:00
dependabot[bot] 4b65a2f6f6 Bump actions/cache from 4 to 5 (#741)
Bumps [actions/cache](https://github.com/actions/cache) from 4 to 5.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-03-27 18:51:13 +00:00
dependabot[bot] b83579670f Bump actions/setup-go from 5 to 6 (#742)
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5 to 6.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](https://github.com/actions/setup-go/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-03-27 18:50:38 +00:00
Marc Brooks 134caf82aa Added sanitization of journald keys (#751)
Also, raised text coverage for the journald sub-package to 92.2%
Fixes #668
2026-03-27 18:50:13 +00:00
Marc Brooks e133b6a517 Added variadic StrsV, ObjectsV, and StringersV (#752)
* Added variadic StrsV, ObjectsV, and StringersV

This allows you to just list all the string, object, or stringer that you want added to a Event or Context.

The variadic versions are suffixed with V so as to not be a breaking change.

If you have an existing array of objects that implement the LogObjectMarshaler or fmt.Stringer interfaces, unfortunately go doesn't consider those slices as identical types, so you have to allocate an array and copy the entries, there are generic helper methods in global_118.go zerolog.AsLogObjectMarshalers and zerolog.AsStringers if you're using go 1.18 or later.

Somewhat addresses #551

* Address CoPilot comments.

* Resolve odd Stringers on nil array
2026-03-27 18:49:07 +00:00
dependabot[bot] 82017d8fff Bump github.com/coreos/go-systemd/v22 from 22.6.0 to 22.7.0 (#753)
Bumps [github.com/coreos/go-systemd/v22](https://github.com/coreos/go-systemd) from 22.6.0 to 22.7.0.
- [Release notes](https://github.com/coreos/go-systemd/releases)
- [Commits](https://github.com/coreos/go-systemd/compare/v22.6.0...v22.7.0)

---
updated-dependencies:
- dependency-name: github.com/coreos/go-systemd/v22
  dependency-version: 22.7.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-03-27 18:48:46 +00:00
Varun Chawla 2f5b8a91be fix: UpdateContext skips Nop and zero-value loggers (#754)
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>
2026-03-17 13:52:29 +01:00
Varun Chawla d64c9a7138 Add slog.Handler implementation for zerolog (#755)
Closes #571

I needed slog interop in a project where zerolog handles all log output, but some dependencies use `log/slog`. Rather than losing zerolog's performance by switching to slog's built-in JSON handler, this adds a `SlogHandler` that implements `slog.Handler` and routes everything through zerolog.

### What this does

`zerolog.NewSlogHandler(logger)` returns a `slog.Handler` backed by the given `zerolog.Logger`. You can use it like:

```go
zl := zerolog.New(os.Stderr).With().Timestamp().Logger()
slog.SetDefault(slog.New(zerolog.NewSlogHandler(zl)))

slog.Info("request handled", "method", "GET", "status", 200)
// Output: {"level":"info","method":"GET","status":200,"time":...,"message":"request handled"}
```

**Level mapping:**
- `slog.LevelDebug-4` and below -> `zerolog.TraceLevel`
- `slog.LevelDebug` -> `zerolog.DebugLevel`
- `slog.LevelInfo` -> `zerolog.InfoLevel`
- `slog.LevelWarn` -> `zerolog.WarnLevel`
- `slog.LevelError` -> `zerolog.ErrorLevel`

**Supported features:**
- All slog attribute types encoded with zerolog's typed methods (no reflection for primitives)
- `WithAttrs` for pre-attaching fields to child handlers
- `WithGroup` for namespacing keys with dot-separated prefixes
- Nested groups work correctly
- `LogValuer` resolution
- Level filtering respects the zerolog Logger's configured level
- Zerolog contextual fields from `With()` are preserved

**Files:**
- `slog.go` - the handler implementation (~200 lines)
- `slog_test.go` - 26 tests covering levels, all attr types, groups, filtering, LogValuer, immutability

All existing tests continue to pass (the `RandomSampler` flake in `sampler_test.go` is pre-existing).

The `go.mod` already requires Go 1.23, so `log/slog` is available without any changes.
2026-03-17 00:43:02 +00:00
Marc Brooks a0d61dc2c7 fix: return dict to Event pool (#749)
Return dict to event pool in Array.Dict()
2026-01-12 22:51:46 +00:00
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
Marc Brooks 2094837a2c Ensure that Event and Array objects returned to pool are cleared (#747)
* 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.
2026-01-11 15:57:13 +00:00
Marc Brooks 0cf9361666 Fix missing context.Context in Object/EmbedObject (#737)
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{}
2026-01-05 18:19:15 +00:00
dependabot[bot] 0d69d7537a Bump github.com/coreos/go-systemd/v22 from 22.5.0 to 22.6.0 (#743)
Bumps [github.com/coreos/go-systemd/v22](https://github.com/coreos/go-systemd) from 22.5.0 to 22.6.0.
- [Release notes](https://github.com/coreos/go-systemd/releases)
- [Commits](https://github.com/coreos/go-systemd/compare/v22.5.0...v22.6.0)

---
updated-dependencies:
- dependency-name: github.com/coreos/go-systemd/v22
  dependency-version: 22.6.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-01-05 18:17:14 +00:00
dependabot[bot] 3d20da0255 Bump actions/checkout from 4 to 6 (#744)
Bumps [actions/checkout](https://github.com/actions/checkout) from 4 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v4...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-01-05 18:17:01 +00:00
Sebastian Rühl 4685edb80b feat: add new global DurationFieldFormat (#733)
This deprecates DurationFieldInteger in favor of DurationFieldFormat with the value of DurationFormatInt
2026-01-05 18:16:40 +00:00
Maksim Bolgarin 05348ba6f6 docs: add logze to related projects (#724) 2025-12-23 15:31:39 +00:00
dependabot[bot] a728d746c0 Bump github.com/mattn/go-colorable from 0.1.13 to 0.1.14 (#703)
Bumps [github.com/mattn/go-colorable](https://github.com/mattn/go-colorable) from 0.1.13 to 0.1.14.
- [Commits](https://github.com/mattn/go-colorable/compare/v0.1.13...v0.1.14)

---
updated-dependencies:
- dependency-name: github.com/mattn/go-colorable
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-12-23 15:08:25 +00:00
Marc Brooks a64148507a Adds Objects field type to logger fields, events, and context (#736)
* Improve code coverage before adding Objects

* Refactor ErrorMarshal tests.

Added testing of the error marshal in Fields Reduce the duplication of the test data and make it clearer what the "wants" represent.

* Add Objects support to Fields, Event, Context

* Add support for Object Fields

Passed as []LogObjectMarshaler.

* Fixed syntax of ExampleContext_Objects

Seems 1.24 accepts variadic for arrays and understands covariance but 1.21 doesn't.

* Fix problem with []uint8 serialized as a Base64 string

* Better syntax in the example use of Objects
2025-12-18 18:50:34 +00:00
Marc Brooks 42f33fff03 Add Type() to the Array (#738)
Fixes #729
2025-12-18 01:06:38 +01:00
Marc Brooks 8148645974 Chore improve test coverage (#735)
* 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
2025-12-15 20:55:37 +01:00
Marc Brooks 5391dd7c34 Add array handling of IPAddr and IPPrefix (#734)
* 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
2025-12-06 13:12:45 +01:00
wangcundashang 9dacc014f3 chore: fix some comments (#718)
Signed-off-by: wangcundashang <wangcundashang@qq.com>
2025-04-18 13:14:43 +02:00
Olivier Poitrey a21d6107dc Remvoe ExampleBinaryNew referring to nothing 2025-03-24 16:31:31 +01:00
Olivier Poitrey db9d1bebd9 Update go versions covered by CI 2025-03-21 01:06:46 +01:00
Olivier Poitrey 5f4b880a01 Delete _config.yml 2025-03-21 01:03:04 +01:00
Olivier Poitrey ffb27080ca Remove CNAME file 2025-03-20 17:01:12 -07:00
Olivier Poitrey cc4dde7383 Create CONTRIBUTING.md 2025-03-21 00:58:35 +01:00
Andrey36652 04ea0f4371 Implement Close() for zerolog.FilteredLevelWriter (#715) 2025-03-12 13:16:56 +01:00
crazy-pe 039860087c fix: reset condition in burst sampler (#711) (#712) 2025-02-26 15:21:09 +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
Oleg Schwann 582f820cf0 Get BasicSampler(0), RandomSampler(0), and BurstSampler(0) to behave the same and not write anything (#696) 2024-11-14 16:50:12 +01:00
dependabot[bot] 6abadab488 Bump github.com/rs/xid from 1.5.0 to 1.6.0 (#684) 2024-08-26 13:12:21 +02:00
Marc M. Adkins c78e50e2da Add fields order (#550) 2024-05-04 15:43:27 +02:00
crazy-pe 8582bed24f fix: use TimestampFunc in busrt sampler (#671) (#672) 2024-05-01 04:19:37 +02:00
Mitar 7d9db06a53 Allow setting floating point precision in JSON. (#663) 2024-04-28 17:02:53 +02:00
Olivier Poitrey e5aa7e3627 Revert #662
PR approved by mistake.
2024-04-24 02:19:07 +02:00
Andrey Karpov 0efa414907 Fix panic caused by an extra malformed level field (#665) 2024-04-24 02:13:48 +02:00
camcui eb081e1fa2 chore: fix some typos in comments (#667) 2024-04-12 11:07:29 +02:00
Ruben Poppe 2d899f0cf9 set debug log color (#662) 2024-04-05 16:58:55 +02:00
Pavel Griaznov 74cf37a396 Add EmptyFields method to remove all the fileds from logger (#575)
Co-authored-by: Olivier Poitrey <rs@rhapsodyk.net>
Co-authored-by: tlipoca9 <160737620+tlipoca9@users.noreply.github.com>
2024-03-05 22:57:20 -08:00
Konstantin Pechenenko e5edd4b8ec Refactor: make code in comment valid and runable (#654) 2024-03-03 00:52:46 +01: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
Youming Lin 54ebf468e5 doc: update readme to use os-specific path separator (#534)
* update readme to use os-specific path separator
* shorten getShortLineNo example to one liner
2024-03-02 01:50:13 +01:00
Joseph Cumines bda298df4a Fix JSON encoding of float exponents to be like json.Marshal / ES6 (#537)
* Fix JSON encoding of float exponents to be like json.Marshal / ES6
* Add test cases for the *e-9 JSON number encoding edge case
2024-03-02 01:49:38 +01:00
Mitar 0d16f63a8a Disable HTML escaping in InterfaceMarshalFunc. (#568) 2024-03-02 01:33:56 +01:00
赵士杰 dfd022fdfd Update README.md (#593)
default log level is trace , not debug
2024-03-02 01:30:48 +01:00
Anuraag (Rag) Agrawal 159cb37bb9 Fix context retrieval in hook example (#603) 2024-03-02 01:30:20 +01:00
dependabot[bot] bd2896587d Bump actions/setup-go from 4 to 5 (#625)
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 4 to 5.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](https://github.com/actions/setup-go/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-04 15:50:57 +01:00
dependabot[bot] af58707cd9 Bump actions/cache from 3 to 4 (#638)
Bumps [actions/cache](https://github.com/actions/cache) from 3 to 4.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-02-04 15:49:00 +01:00
Sergey 9e60b1cb8e Add prettylog time format (#645) 2024-02-04 15:48:38 +01:00
Sergey 147ae65350 Fix prettylog piping (#644) 2024-02-01 01:56:59 +01:00
Garret Buell 4d78dc5ffa Add forwarding close methods to several writer implementations (#636) 2024-01-13 19:08:26 +01:00
Garret Buell c1ab4ed9bf Make Log.Fatal() call Close on the writer before os.Exit(1) (#634) 2024-01-13 10:25:59 +01:00
Hyuga 417580d1ce add: ContextLogger Interface LogObjectMarshaler (#623) 2024-01-04 14:45:09 +01:00
Naveen 602e90aeea Fixed failing tests (#626) 2024-01-04 14:41:14 +01:00
Vitalii Solodilov a9ec232b3e Add stacktrace to Context (#630) 2024-01-03 05:40:23 +01:00
Halimao 3e8ae07aba Refactor: change Hook(h Hook) to Hook(hooks ...Hook) (#629) 2023-12-24 14:05:00 +01:00
Naveen 7fa45a4dda fixed documentation for tracing hook (#621) 2023-11-29 04:24:25 +01:00
Mitar 93fb5cb215 Add TriggerLevelWriter. (#602)
See: https://github.com/rs/zerolog/issues/583
2023-11-27 22:20:40 -05:00
Sami Kerola 83e03c75d9 stop using deprecated io/ioutils package (#620) (#620) 2023-11-24 22:16:01 +01:00
Mitar bb14b8b9de Add additional hlog logging handlers (#607)
* Add HTTPVersionHandler.

* Add RemoteIPHandler.

* Add trimPort to HostHandler.

* Add EtagHandler.

* Add ResponseHeaderHandler.

* Add TestGetHost.

* Call AccessHandler's f also on panic.
2023-11-08 21:04:17 +01:00
guangwu e7034c2572 chore: use buf.String() instead of string(buf.Bytes()) (#608) 2023-10-24 04:08:36 +02:00
Mitar c4046fe2cb Zero Logger behaves like Nop logger. (#606) 2023-10-21 04:20:52 +02:00
Mitar 507a426bf8 Use map for formatted levels. (#599) 2023-10-20 16:17:01 +02:00
Gabriel Estavaringo Ferreira 8834256667 Add Println top level method (#533)
Co-authored-by: estavaringo <gabriel.estavaringo@arquivei.com.br>
2023-10-20 16:15:13 +02:00
Michikawa Masayoshi 2c1cbecf52 hlog: add HostHandler (#604) 2023-10-20 11:59:49 +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 f70fcca1ab Update example image to how it is currently rendered. (#596) 2023-10-13 13:55:44 +02:00
Olivier Poitrey 6e8841b038 prettylog: add support for file input 2023-10-13 13:54:49 +02:00
Mitar 11f895c15e Add FormatPrepare to ConsoleWriter. (#595) 2023-10-13 10:32:42 +02:00
Julien Robert ed609e7fe6 feat: display error stack when using .Fields() (#560) 2023-10-04 17:54:14 +02:00
Clint Armstrong 9e34cb475c Add Type function to context (#592) 2023-10-04 00:33:11 +02:00
dependabot[bot] 8344fc0c93 Bump actions/checkout from 3 to 4 (#588)
Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-25 11:28:20 +02:00
Olivier Poitrey 4cb8cc5622 Update dependencies 2023-09-25 11:27:41 +02:00
Subhrodip Mohanta ae9b265137 Update Build Status Badge (#589) 2023-09-22 07:16:02 -07: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
Mitar b81cc57e5d Fix the standard logger output example (#584) 2023-08-20 15:04:54 +02:00
Olivier Poitrey ad77222f68 chore: improve coverage report 2023-08-18 20:05:14 +02:00
Olivier Poitrey 95cf29c88c chore: switch to go-goverage-report action as gocover.io is closing 2023-08-18 19:50:46 +02:00
Manjunatha Shetty H 802c88f065 chore: adding any function to context (#580)
* chore: adding any function to context
* fixes
2023-08-18 19:41:23 +02:00
Olivier Poitrey 158e4ad5c3 Update logbench URL in README.md 2023-08-14 23:42:26 +02:00
Mitar 7d5aa987d0 Deprecate lint in favor of https://github.com/ykadowak/zerologlint (#574) 2023-08-02 10:27:39 +02:00
Mitar 70ac648f5c FilteredLevelWriter writes only logs at Level or above (#573) 2023-08-02 10:24:51 +02:00
Mitar 9c29f785f9 Make LevelWriterAdapter public (#572) 2023-08-02 09:58:42 +02:00
Scott Davis 06ec071bc1 Clarify the godoc warning for Logger.UpdateContext (#566) 2023-07-29 16:32:05 +02:00
Dan Price 873cbf13ee Allow callers to pass go context through to hooks (#559)
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.
2023-07-25 15:47:18 +02:00
finkandreas 61485f3857 Fix #564 (#565)
This could be a potential fix for #564
2023-07-12 14:29:38 +02:00
stergiotis 9070d49a1a Add event method RawCBOR analogous to RawJSON (#556)
CBOR is encoded as data-url in JSON encoding and tagged in CBOR encoding.
2023-06-19 01:30:44 +02:00
Harish Kukreja b662f088b9 docs: context.Timestamp uses zerolog.TimeFieldFormat (#554) 2023-06-11 17:20:21 +02:00
Basten Gao 4612e098d2 Fix error chain from pkgerrors (#552) 2023-05-15 14:07:32 +02:00
Angus 8981d80ed3 Correct logger variable name in stdout example (#549) 2023-05-11 14:00:42 +02:00
Koung 927516bcf1 doc(readme): fix a typo (#548)
Co-authored-by: jirasak <jirasak@lmwn.com>
2023-05-01 14:04:49 +02:00
Kirill a712f61936 doc(readme): explain zerolog with context (#544)
Add section about `context.Context` usage with zerolog instance
2023-04-21 11:55:19 +02:00
dependabot[bot] 64a5863c5e Bump github.com/rs/xid from 1.4.0 to 1.5.0 (#542)
Bumps [github.com/rs/xid](https://github.com/rs/xid) from 1.4.0 to 1.5.0.
- [Release notes](https://github.com/rs/xid/releases)
- [Commits](https://github.com/rs/xid/compare/v1.4.0...v1.5.0)

---
updated-dependencies:
- dependency-name: github.com/rs/xid
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-04-17 11:56:52 +02:00
dependabot[bot] 1f50797d7d Bump actions/setup-go from 3 to 4 (#529)
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 3 to 4.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](https://github.com/actions/setup-go/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-03-20 12:29:36 -07:00
Menno van Rahden 902d72012d fix level parser (#523) 2023-03-10 07:15:23 -08:00
dependabot[bot] 4fff5db29c Bump github.com/coreos/go-systemd/v22 (#526)
Bumps [github.com/coreos/go-systemd/v22](https://github.com/coreos/go-systemd) from 22.3.3-0.20220203105225-a9a7ef127534 to 22.5.0.
- [Release notes](https://github.com/coreos/go-systemd/releases)
- [Commits](https://github.com/coreos/go-systemd/commits/v22.5.0)

---
updated-dependencies:
- dependency-name: github.com/coreos/go-systemd/v22
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-02-27 13:02:23 +01:00
Adam Chalkley 762546b5c6 Replace duplicate 'append' text in doc comments (#520)
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>
2023-01-28 00:04:14 +01:00
Olivier Poitrey fa9bf3742a Fix missing cbor encoder AppendType method 2023-01-25 17:34:40 +01:00
Sylvain Rabot db22191211 Use recent go versions in test workflow (#519)
Signed-off-by: Sylvain Rabot <sylvain@abstraction.fr>
2023-01-25 17:05:09 +01:00
dils2k 164f7aa1a6 Add Any field function (#515) 2023-01-19 12:10:08 +01:00
Pavel Gryaznov 3543e9d94b parsing CAPS log levels (#506) 2022-11-11 14:15:40 +01: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
Tim Peoples e3027a5732 Fix docs and behavior of WithContext (#499) 2022-10-22 19:29:38 +02: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
Mario Cormier 89617ff99b Adding Event.Type(...) method to log the type of an object (#437)
Co-authored-by: Mario Cormier <mcormier@rossvideo.com>
2022-10-18 02:51:54 +02:00
Yuki Furuyama 55aaf043cf Show original level message if it's not predefined value. (#476) 2022-09-18 16:53:56 +02:00
Donald Nguyen 315967f32d Avoid race in diode.Close with waiter (#481) 2022-09-15 10:15:42 +02:00
Michael Nikitochkin e218d18951 Update example of usage CallerMarshalFunc (#475)
There was changes in https://github.com/rs/zerolog/pull/457  pass program counter to CallerMarshalFunc.
Update example in README.md
2022-09-09 23:18:08 +02:00
Uros Marolt c2b9d0e2de CLI utility to pipe JSON logs through to pretty print and colorize them (#449) 2022-09-03 04:06:22 -07:00
Martin Rauscher d894f123bc pass program counter to CallerMarshalFunc (#457) 2022-07-29 15:29:02 +01:00
Mitar 4099072c03 Support extra arbitrary data at the end of console log (#416) 2022-07-18 23:00:50 +01:00
lazarenkovegor 4c85986254 Unixnano time format support (#454)
Co-authored-by: Лазаренков Егор Алексеевич <ealazarenkov@sberbank.ru>
2022-07-18 17:25:16 +01:00
dependabot[bot] 43be301386 Bump actions/cache from 3.0.1 to 3.0.5 (#453) 2022-07-18 10:41:56 +01:00
Olivier Poitrey afdf9978ec Revert "remove fields written into "PartsOrder" (#383)"
This reverts commit 2a13872817.
2022-07-16 21:17:58 +01:00
xsteadfastx 14d6629e41 hlog: adds ProtoHandler (#396) 2022-07-16 21:04:30 +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
dependabot[bot] 68a6bd49b5 Bump github.com/rs/xid from 1.3.0 to 1.4.0 (#430)
Bumps [github.com/rs/xid](https://github.com/rs/xid) from 1.3.0 to 1.4.0.
- [Release notes](https://github.com/rs/xid/releases)
- [Commits](https://github.com/rs/xid/compare/v1.3.0...v1.4.0)

---
updated-dependencies:
- dependency-name: github.com/rs/xid
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-07-16 20:59:27 +01:00
dependabot[bot] 5c08a2724f Bump actions/cache from 2 to 3.0.1 (#432)
Bumps [actions/cache](https://github.com/actions/cache) from 2 to 3.0.1.
- [Release notes](https://github.com/actions/cache/releases)
- [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md)
- [Commits](https://github.com/actions/cache/compare/v2...v3.0.1)

---
updated-dependencies:
- dependency-name: actions/cache
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-07-16 20:58:58 +01:00
Aniruddha Maru 60f57432ed Implement encoding.TextUnmarshaler and encoding.TextMarshaler for Level (#440) 2022-07-16 20:51:01 +01:00
Adam Horacek 2a13872817 remove fields written into "PartsOrder" (#383) 2022-06-29 01:36:09 +02:00
Robson Roberto Souza Peixoto a4ec5e4cdd typo: using https to access cbor.io (#439) 2022-05-05 19:17:37 +02:00
Igor Beliakov e9344a8c50 docs: add an example for Lshortfile-like implementation of CallerMarshalFunc (#423)
Signed-off-by: Igor Beliakov <demtis.register@gmail.com>
2022-03-12 08:33:09 -08:00
Chelsea Hoover 263b0bde36 #411 Add FieldsExclude parameter to console writer (#418) 2022-02-27 18:33:36 +01:00
Nick Corin 588a61c2df ctx: Modify WithContext to use a non-pointer receiver (#409) 2022-02-24 01:17:11 +01:00
Mitar 361cdf616a Remove extra space in console when there is no message (#413) 2022-02-19 16:39:18 +01: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
Nicola Murino 025f9f1819 journald: don't call Enabled before each write (#407)
Enabled opens and close a socket connection by reusing or initializing
a global connection.

I also updated go-systemd to the current development release to include
the following fix:

https://github.com/coreos/go-systemd/commit/75f33b08dbe229fb37b96bf0076907b6b8159af1

This is the only journal related change since the latest stable release
2022-02-03 14:49:04 +01:00
Arnau 3efdd82416 call done function when logger is disabled (#393) 2022-01-04 17:12:02 +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
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
Nicola Murino 0c8d3c0b10 move the lint command to its own package (#389)
in this way we remove several dependencies from the actual library
2021-12-17 03:03:37 +01:00
Chris Harrison 791ca15d99 Update the x/crypto dependency
Removing the security concern brought up by third parties

Co-authored-by: Chris Harrison <chris.harrison@modicagroup.com>

Fixes #387
2021-12-16 00:37:18 +01:00
Asger Noer 0d2f0be4ee fix: error field name color (#377) 2021-11-09 12:34:43 +01:00
Federico G. Schwindt 6973188a74 Add support for dicts in array (#375)
Fixes #111.
2021-11-02 01:53:02 +01:00
Edward McFarlane 78448ee023 New TestWriter for logging to testing.TB (#369) 2021-10-05 01:07:40 +02:00
dependabot[bot] 197adb44cc Bump golang.org/x/tools from 0.1.5 to 0.1.7 (#370)
Bumps [golang.org/x/tools](https://github.com/golang/tools) from 0.1.5 to 0.1.7.
- [Release notes](https://github.com/golang/tools/releases)
- [Commits](https://github.com/golang/tools/compare/v0.1.5...v0.1.7)

---
updated-dependencies:
- dependency-name: golang.org/x/tools
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-10-04 12:59:57 +02:00
hn8 07d347e658 Update zerologr url (#364)
Transferred to go-logr
2021-09-25 09:57:54 +02:00
Matas 6f8a5f9ccb Add Stringers support (#360) 2021-09-18 19:22:21 +02:00
hn8 9b9fc5c6b7 Update zerologr url (#359)
Transferred from hn8 to screenleap.
2021-09-14 13:27:57 +02:00
hn8 65adfd88ec Make Fields method accept both map and slice (#352) 2021-09-08 18:00:06 +02:00
Tim Hockin cdd74175d0 Allow arbitrary negative levels (#350)
This allows logr to use high V() levels.
2021-08-27 22:34:08 +02:00
Tyler Cosgrove fe931004ff Make RemoteAddrHandler copy the RemoteAddr untouched (#347)
Do not remove the port part of the RemoteAddr and accept values with no ports.
2021-08-19 20:46:16 +02:00
Matthieu MOREL be6f6fd8f1 chore(ci) : update dependencies (#345)
* Create dependabot.yml
* Update test.yml
* Bump golang.org/x/tools from 0.1.3 to 0.1.5 (#1)
* Bump github.com/rs/xid from 1.2.1 to 1.3.0 (#2)

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-08-16 11:47:56 +02:00
Sean d92a906fca Allow user configuration of which logger to return from Ctx() (#343)
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.
2021-08-12 02:18:16 +02:00
Steve Carlson fad20d83d3 docs: sampler typo (#342)
fix BasicSampler docstring typo

Co-authored-by: Steve Carlson <steve.carlson@crowdstrike.com>
2021-08-10 21:43:41 +02:00
hn8 164ec91b0c Add logr implementation to README and json.RawMessage to Fields() (#337) 2021-08-03 11:31:54 +02:00
Spike^ekipS c1533bd5f8 patched; panic Event.Object() and Event.EmbedObject() with nil (#338) 2021-08-01 17:24:18 +02:00
adw1n 0872592ea2 Make default console formatter work with customized level names (#330) 2021-07-15 22:04:15 +02:00
Eugene Klimov da1cb97713 Update x/tools dependency 2021-06-23 17:16:12 +02:00
adw1n 117cb53bc6 Fix copying stack setting in logger's Output method (#325)
Minimum example to reproduce the problem:
```go
package main

import (
	"os"

	"github.com/pkg/errors"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"github.com/rs/zerolog/pkgerrors"
)

func main() {
	log.Logger = log.With().Caller().Stack().Logger().Output(zerolog.ConsoleWriter{Out: os.Stderr})
	zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack

	log.Info().Err(errors.New("test")).Send()
}
```
Without my changes `stack` isn't printed.
2021-06-07 12:16:53 +02:00
Mitsuo Heijo e05605c215 Use github.com/coreos/go-systemd/v22 (#322) 2021-05-23 10:29:31 +02:00
Olivier Poitrey 6ed1127758 Fix panic on disabled event with CallerSkipFrame
Fixes #319
2021-05-20 18:40:53 +02:00
povsister 47a03bc5eb Add ability to customize internal json marshaler (#318)
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.
2021-05-20 14:46:36 +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
Olivier Poitrey 3c3b4a354e Add ability to customize level values 2021-05-14 00:07:21 +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
Olivier Poitrey f09463fbe1 Migrate to github actions 2021-05-05 15:10:23 +02:00
Tabitha 19c98f6d3e If LevelFieldName is empty don't log level (#313) 2021-05-05 14:40:45 +02:00
Jack Christensen 0f923d7926 Fix: mutil.fancyWriter.ReadFrom records number of bytes written (#256)
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.
2021-04-22 03:22:35 +02:00
Dan Gillis 582f0cf0e3 add Disabled to String and ParseLevel; add tests (#307) 2021-04-13 07:32:06 +02:00
Olivier Poitrey 98f889fcde Revert "Fix Typo (#306)"
This reverts commit f85e803cc5.
2021-04-07 19:58:46 +02:00
Redha Juanda f85e803cc5 Fix Typo (#306) 2021-04-07 16:12:54 +02:00
Ben Visness f2cc3cf8b7 Fix Context.Stack() (#303) 2021-03-23 23:01:01 +01:00
Emre Arslan 72b5b1ea58 added overlog to related project (#302)
Co-authored-by: Emre Arslan <emre.arslan@trendyol.com>
2021-03-18 16:21:38 +01:00
Jean Morais 7ccd4c940b Bump golang.org/x/tools to v0.1.0 (#295)
Fixes #291: There is a known vulnerability in one of the golang.org/x/tools dependencies [CVE-2019-11840].
2021-03-03 00:42:31 +01:00
Dan Gillis 7a3aa8746f Allow setting context using idKey. Assist for issue #293. (#296) 2021-03-02 11:01:32 +01:00
Nuno Diegues a8f5328bb7 Make MultiLevelWriter resilient to individual log failure (#282)
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>
2021-01-20 17:03:52 +01:00
Eng Zer Jun 4f50ae2ed0 build: update github.com/pkg/errors from 0.8.1 to 0.9.1 (#280)
* [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>
2021-01-11 11:12:13 +01:00
Dan Gillis 0aba2e4ae5 Add Error logging section and section links (#275) 2020-12-30 00:57:10 +01:00
Jason McVetta a3b272d512 Test newer Go versions (#276) 2020-12-29 21:23:45 +01: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
Dionisio Romero Díaz 9336c4d0ed Fix AppendFloats64 first element as 32 (#263) 2020-10-27 17:05:59 +01:00
mathew e11d470c08 Add syslog CEE support (#262) 2020-10-12 15:16:04 +02:00
Mikhail Lukianchenko 72acd6cfe8 Fix typo in diode.NewWriter argument name (#254) 2020-08-06 12:19:27 +02:00
Eugene 9e51190d47 Fix some tests (#245)
* TestUpdateEmptyContext for CBOR
* Properly name CBOR 64 bit tests
2020-06-25 14:48:07 -07:00
Eugene 1b763497ee Fix UpdateContext panic with empty Context (#244) 2020-06-24 16:11:26 -07:00
Chris Camel 7248ae2fb4 add stringer support for sub-logger (#241)
Same than #185 but for zerolog.Context.
2020-06-17 20:42:03 -07:00
Dima 7825d86337 stringer event method (#185) 2020-05-28 10:43:18 -07:00
Fred Muya 63767a55ec Add Example for Multiple Outputs using Multiwriter (#198)
Fixes #73
2020-05-28 10:41:39 -07:00
Giuseppe f83de79b81 Improve documentation wording for SyncWriter (#235)
There is already a lock in place for both POSIX (Linux/Unix/Darwin) and Windows, see:

https://github.com/golang/go/blob/go1.14.3/src/internal/poll/fd_windows.go#L684
https://github.com/golang/go/blob/go1.14.3/src/internal/poll/fd_unix.go#L255
2020-05-25 10:57:14 -07:00
Stig Otnes Kolstad 663cbb4c84 docs: minor typos (#234) 2020-05-14 08:27:19 -07:00
Giuseppe e027a834ab Typo fix (#233) 2020-05-14 01:29:30 -07:00
Ravi Raju 50ffd2b67d Moved 8btye integer tests to a different file (#231)
and that file has a build tag to be excluded from 386 arch

Signed-off-by: Ravi Raju <toravir@yahoo.com>

Co-authored-by: Ravi Raju <toravir@yahoo.com>
2020-05-12 01:24:15 -07:00
Jean-Sébastien Didierlaurent e86e8f2f49 fix(diode): atomic.AddUint64 in 32-bit cause panic (#229)
see: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
2020-05-11 11:49:43 -07:00
Emir Ribić de5a95dced Remove duplicate comment (#227) 2020-05-10 21:54:46 -07:00
Eugene 14dcf38e7d Make AppendKey in json inlinable (#226)
This change increase performance by inlining this frequently called function

Benchmark:

| name                                   | old time/op |    new time/op |    delta | params |
|---|---|---|---|---|
| LogEmpty-8                               | 14.1ns ±10% |    15.6ns ±13% |  +10.43% |  (p=0.003 n=10+10) |
| Disabled-8                               | 1.27ns ± 0% |    1.35ns ± 1% |   +6.54% |  (p=0.000 n=9+10) |
| Info-8                                   | 39.8ns ± 5% |    37.9ns ± 2% |   -4.55% |  (p=0.000 n=10+10) |
| ContextFields-8                          | 42.2ns ± 4% |    39.7ns ± 3% |   -6.04% |  (p=0.000 n=10+10) |
| ContextAppend-8                          | 15.3ns ± 0% |    14.5ns ± 0% |   -5.23% |  (p=0.000 n=9+10) |
| LogFields-8                              |  179ns ± 0% |     176ns ± 1% |   -1.23% |  (p=0.000 n=10+10) |
| LogArrayObject-8                         |  526ns ± 1% |     491ns ± 1% |   -6.65% |  (p=0.000 n=9+10) |
| LogFieldType/Bools-8                     | 34.5ns ± 2% |    33.3ns ± 4% |   -3.63% |  (p=0.000 n=9+10) |
| LogFieldType/Int-8                       | 29.7ns ± 4% |    28.3ns ± 6% |   -4.71% |  (p=0.003 n=10+10) |
| LogFieldType/Str-8                       | 29.6ns ± 3% |    27.1ns ± 2% |   -8.19% |  (p=0.000 n=10+10) |
| LogFieldType/Time-8                      |  119ns ± 0% |     118ns ± 0% |   -0.84% |  (p=0.000 n=9+9) |
| LogFieldType/Interfaces-8                |  504ns ± 1% |     516ns ± 1% |   +2.46% |  (p=0.000 n=10+10) |
| LogFieldType/Object-8                    | 73.7ns ± 2% |    71.6ns ± 1% |   -2.94% |  (p=0.000 n=10+10) |
| LogFieldType/Bool-8                      | 28.9ns ± 6% |    25.9ns ± 3% |  -10.35% |  (p=0.000 n=9+10) |
| LogFieldType/Strs-8                      | 53.7ns ± 2% |    51.6ns ± 1% |   -3.95% |  (p=0.000 n=9+10) |
| LogFieldType/Err-8                       | 39.2ns ± 2% |    38.1ns ± 4% |   -2.66% |  (p=0.006 n=10+10) |
| LogFieldType/Interface-8                 |  147ns ± 1% |     145ns ± 1% |   -1.22% |  (p=0.001 n=10+10) |
| LogFieldType/Interface(Object)-8         | 78.8ns ± 1% |    76.9ns ± 1% |   -2.43% |  (p=0.000 n=10+10) |
| LogFieldType/Interface(Objects)-8        |  516ns ± 0% |     507ns ± 1% |   -1.66% |  (p=0.000 n=8+10) |
| LogFieldType/Ints-8                      | 52.4ns ± 1% |    48.1ns ± 1% |   -8.28% |  (p=0.000 n=10+10) |
| LogFieldType/Float-8                     | 39.8ns ± 1% |    38.4ns ± 2% |   -3.33% |  (p=0.000 n=10+9) |
| LogFieldType/Times-8                     |  887ns ± 0% |     887ns ± 0% |     ~    |  (p=0.164 n=8+10) |
| LogFieldType/Dur-8                       | 41.1ns ± 2% |    40.2ns ± 6% |   -2.10% |  (p=0.014 n=10+10) |
| LogFieldType/Durs-8                      |  266ns ± 0% |     262ns ± 0% |   -1.35% |  (p=0.000 n=7+10) |
| LogFieldType/Floats-8                    |  166ns ± 0% |     165ns ± 1% |     ~    |  (p=0.238 n=10+10) |
| LogFieldType/Errs-8                      |  126ns ± 1% |     120ns ± 2% |   -4.61% |  (p=0.000 n=10+10) |
| ContextFieldType/Bool-8                  |  140ns ± 2% |     140ns ± 2% |     ~    |  (p=0.701 n=10+9) |
| ContextFieldType/Err-8                   |  150ns ± 3% |     150ns ± 2% |     ~    |  (p=0.932 n=10+10) |
| ContextFieldType/Interfaces-8            |  631ns ± 1% |     628ns ± 1% |     ~    |  (p=0.108 n=10+10) |
| ContextFieldType/Floats-8                |  263ns ± 1% |     257ns ± 1% |   -2.05% |  (p=0.000 n=10+10) |
| ContextFieldType/Errs-8                  |  205ns ± 2% |     204ns ± 2% |     ~    |  (p=0.499 n=10+9) |
| ContextFieldType/Dur-8                   |  146ns ± 3% |     149ns ± 2% |   +1.86% |  (p=0.011 n=9+10) |
| ContextFieldType/Durs-8                  |  372ns ± 1% |     367ns ± 1% |   -1.24% |  (p=0.000 n=10+10) |
| ContextFieldType/Bools-8                 |  145ns ± 2% |     144ns ± 3% |     ~    |  (p=0.447 n=10+10) |
| ContextFieldType/Ints-8                  |  157ns ± 3% |     157ns ± 2% |     ~    |  (p=0.976 n=9+9) |
| ContextFieldType/Float-8                 |  147ns ± 3% |     148ns ± 4% |     ~    |  (p=0.385 n=9+10) |
| ContextFieldType/Strs-8                  |  158ns ± 3% |     157ns ± 2% |     ~    |  (p=0.666 n=10+10) |
| ContextFieldType/Time-8                  |  143ns ± 2% |     141ns ± 3% |   -1.75% |  (p=0.035 n=10+10) |
| ContextFieldType/Times-8                 |  159ns ± 4% |     159ns ± 2% |     ~    |  (p=0.978 n=10+10) |
| ContextFieldType/Interface(Object)-8     |  252ns ± 2% |     251ns ± 2% |     ~    |  (p=0.415 n=10+10) |
| ContextFieldType/Timestamp-8             |  167ns ± 2% |     167ns ± 3% |     ~    |  (p=0.662 n=10+10) |
| ContextFieldType/Int-8                   |  142ns ± 4% |     140ns ± 2% |   -2.04% |  (p=0.026 n=10+10) |
| ContextFieldType/Str-8                   |  142ns ± 1% |     141ns ± 3% |   -1.00% |  (p=0.016 n=9+10) |
| ContextFieldType/Interface-8             |  253ns ± 1% |     249ns ± 3% |     ~    |  (p=0.055 n=9+10) |
| ContextFieldType/Interface(Objects)-8    |  632ns ± 1% |     624ns ± 1% |   -1.20% |  (p=0.000 n=10+9) |
| ContextFieldType/Object-8                |  194ns ± 2% |     192ns ± 2% |     ~    |  (p=0.083 n=10+10) |
| Hooks/Nop/Single-8                       | 16.5ns ± 6% |    15.5ns ± 5% |   -5.95% |  (p=0.001 n=10+10) |
| Hooks/Nop/Multi-8                        | 18.5ns ± 6% |    17.8ns ± 4% |   -3.66% |  (p=0.009 n=10+9) |
| Hooks/Simple-8                           | 31.0ns ± 2% |    28.1ns ± 2% |   -9.24% |  (p=0.000 n=8+9) |
| Samplers/BasicSampler_1-8                | 0.65ns ± 1% |    0.63ns ± 1% |   -1.78% |  (p=0.000 n=10+9) |
| Samplers/BasicSampler_5-8                | 30.0ns ± 1% |    32.2ns ± 0% |   +7.29% |  (p=0.000 n=10+9) |
| Samplers/RandomSampler-8                 | 92.8ns ± 1% |    90.8ns ± 1% |   -2.25% |  (p=0.000 n=10+10) |
| Samplers/BurstSampler-8                  | 34.5ns ± 1% |    36.6ns ± 1% |   +5.95% |  (p=0.000 n=9+9) |
| Samplers/BurstSamplerNext-8              | 46.3ns ± 0% |    46.1ns ± 0% |   -0.41% |  (p=0.001 n=9+7) |
| ConsoleWriter-8                          | 5.90µs ± 0% |    5.84µs ± 1% |   -0.91% |  (p=0.000 n=10+10) |
2020-05-05 19:44:34 -07:00
Olivier Poitrey a06edf20d7 Fix zerolog.SetGlobalLevel readme 2020-04-20 13:50:01 -07:00
Blake Williams fe394c81ce Remove dependency on Goji (#223) 2020-04-12 22:02:06 -07:00
haozibi 1c32ee06a7 Fix: Event.stack initialization error (#219) 2020-03-30 10:16:40 -07:00
Milo d9df1802de Updated README (#212)
Updated README to reflect changes in the code - included new field types available, corrected typos.
2020-02-15 09:27:26 -08:00
Olivier Poitrey f7c93dce1c Fix test on non linux platform 2020-02-10 17:16:38 -08:00
Olivier Poitrey 68a3fd989d Fix a crash condition when AnErr is used with a nil interface 2020-02-10 17:15:06 -08:00
Ilja Neumann 505b18daf2 Return written size when message was sent without error (#190)
- Fixes ErrShortWrite error if used with io.MultiWriter
2020-01-15 13:01:51 -08:00
Ryo Ota 65ed30bfb0 Use ``bash instead of ``go (#209) 2020-01-01 12:32:59 -08:00
Dmitry Savintsev cb951d468e fix the typo in 'guarantee' (#203) 2019-12-27 09:23:15 -08:00
wphan d2a97b366b fix ConsoleWriter for TimeFormatMicro (#206) 2019-12-20 09:34:21 -08:00
Olivier Poitrey f1dd50b8c6 Fix trace again 2019-11-18 16:32:52 -08:00
Olivier Poitrey 686705b4f0 Remove Trace from SyslogWriter interface 2019-11-18 16:29:37 -08:00
Olivier Poitrey 5d9d7660cc Expose the Err helper from the global logger 2019-11-18 16:22:50 -08:00
Olivier Poitrey 54e95fe699 Fix numbering of levels 2019-11-18 09:34:23 -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
CrazyMax 4502cc1942 Add TraceLevel (#158) 2019-11-04 11:39:22 -08:00
aca 43d05e8ddf hlog: add custom header handler (#197) 2019-11-04 10:14:00 -08:00
Nelz 5861452d64 enable adding raw JSON to an array (#145)
* enable adding JSON to an array

* do not forget the comment
2019-11-04 09:47:06 -08:00
hazimj 7592fcbe60 make TimeFormatUnixMicro var exportable (#189) 2019-10-24 17:02:51 -07:00
Veselkov Konstantin 2e41c37ac4 Fixed comment for documentations (#194) 2019-10-24 03:02:36 -07:00
hazimj 19e454b4c7 Add Unix time format for microseconds (#188) 2019-10-09 20:35:32 -07:00
Veselkov Konstantin 33a4561a07 remove golint errors (#187) 2019-10-04 07:31:06 -07:00
Tim Satke 06599535fa Bumped golang.org/x/tools (#183) 2019-08-28 14:58:12 -07:00
Charles Chan 61d1749124 Fix description in README for Global Settings (#176) 2019-08-11 07:03:49 +02: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
Christian Muehlhaeuser 42d101e9f7 Fixed code formatting and import order using goimports (#167) 2019-07-19 12:07:15 +01:00
i6du 77a1695358 add send function for convenience (#164) 2019-07-03 23:16:03 -07:00
wangyuehong 60d4b07b61 Add GetLevel method (#161) 2019-06-20 16:44:55 -07:00
IxDay 9938a23cba Fix JSON when object is first to be pushed (#154)
When pushing an object to the logger, and this object was the first
field added. Zerolog was outputting an invalid json blob, issuing an
extra comma before the object. This patch ensure that JSON is still valid
even if an object is pushed first to the logger.

Fixes #152
2019-06-04 23:48:09 -07:00
Vasiliy Faronov 1a2c7daec4 Fix misleading text about duplicate keys in JSON (#141)
Per RFC 8259 Section 4, behavior on duplicate keys is unspecified.

Fixes #140.
2019-05-25 18:51:33 -07:00
Olivier Poitrey ffd0e9625d Optimize basic sampler 2019-05-23 17:01:18 -07:00
Olivier Poitrey ad0401954a Remove go 1.12 requirement 2019-05-23 11:44:28 -07: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
Olivier Poitrey 3e85c4b21c Add a new time format for UNIX timestamp in milliseconds 2019-04-19 15:48:31 -07:00
Olivier Poitrey 509d727fba Add the Err function to ease the log errors. 2019-04-19 15:24:32 -07:00
Mike Camp 651d361cfe Add CallerWithSkipFrameCount to the Context (#98) (#135)
Add the ability to skip a specified number of stack frames
on a per context basis. Before this toe CallerSkipFrameCount
could only be set globally.
2019-03-04 16:41:18 -08:00
Soloman Weng 8e5449ab35 Allow using custom level field format (#136) 2019-03-01 16:08:23 -08:00
Olivier Poitrey 6d6350a511 Fix go1.12 test regression
Breakage is due to this change in go 1.12:

Tracebacks, runtime.Caller, and runtime.Callers no longer include compiler-generated initialization functions. Doing a traceback during the initialization of a global variable will now show a function named PKG.init.ializers.

Fix #137
2019-02-27 12:02:51 -08:00
mikeyrcamp 299ff038c1 Add support for customizing caller field format (#133) (#134) 2019-02-20 11:39:29 -08: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
Olivier Poitrey aa55558e4c Add support for stack trace extration of error fields (#35) 2019-01-03 11:04:23 -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
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
Yongzheng Lai 7bcaa1a99e fix: console ts with json number (#115) 2018-11-12 00:50:17 -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
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
Olivier Poitrey 51c79ca476 Fix com typo 2018-11-02 13:06:29 -07:00
Olivier Poitrey e7627a4f73 diode: let use a waiter instead of a poller by using 0 as a poolInterval 2018-10-31 16:57:15 -07:00
anthony baa31cfa85 Fix nil pointer dereference when call Fields with a typed nil value (#112) 2018-10-31 10:40:46 -07:00
Vojtech Vitek 8e36cbf881 Don't call panic() and os.Exit(1) on .WithLevel() (#110)
* Don't call panic() and os.Exit(1) on .WithLevel()
* Explain behavior of WithLevel(), compared to Panic() & Fatal()
2018-09-27 18:11:43 -07: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
Thiago Caiubi 624b3116d8 Fix sub-logger by context example (#106)
Not quite sure but looks like the example is using the wrong API.
2018-09-18 07:57:53 -07:00
Olivier Poitrey 785a567b10 Add a mention to logbench 2018-09-18 02:18:32 -07:00
Olivier Poitrey 84794124e9 Use gh-readme template for zerolog.io 2018-09-17 10:28:18 -07:00
Olivier Poitrey fc5bbcd9d6 Create CNAME 2018-09-16 19:21:46 -07:00
Olivier Poitrey 1c8b5945b1 Set theme jekyll-theme-hacker 2018-09-16 19:21:33 -07:00
Olivier Poitrey 2da253048d Fix binary test too 2018-09-16 19:15:55 -07:00
jayven 8aa660046f Add hlog.IDFromCtx 2018-09-16 19:00:10 -07:00
Olivier Poitrey 470da8d0bb Fix failing test introduced by last commit 2018-09-16 18:53:09 -07:00
Olivier Poitrey 1dde226d45 BasicSampler prints first message (fix #104) 2018-09-16 15:54:54 -07:00
Duncan Hall b6f076edc8 Add note for default writing to os.Stderr (#97) 2018-08-31 09:46:32 -07:00
Marcelo Aymone 85255a5e26 Fix typo on documentation (#94) 2018-08-14 19:23:11 -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
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
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 372015deb4 add linter to check for missing finishers (#85) 2018-07-20 08:05:08 -07:00
Olivier Poitrey 9cd6f6eef2 ctx: store logger in context when missing or different that stored one (#81)
Current implementation stores a copy of the logger as a pointer and
update its content, which is now unecessary since the introduction of
WithContext.

The new WithContext now takes the pointer and store it in the context if
none is stored already or if the last one stored is a different pointer.
This way it is still possible to update the context of a logger stored
in the context, but it is also possible to store a copy of the logger in
a sub-context.

Fix #80
2018-07-02 18:23:53 -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
Josh Rendek 1a88fbfdd0 Update readme at example for Caller() (#76)
* Update readme at example for Caller()
2018-06-03 22:57:37 -07:00
Pichugin Dmitry dabc72c15b fix README (#74) 2018-05-31 10:33:44 -07:00
Rafael Passos c19f1e5eed Diode module Documentation update (#71)
* Updated Diode example to match new style

* DOC: changed w to wr in assigment to reduce ambiguity
2018-05-25 14:45:33 -07:00
Olivier Poitrey 77db4b4f35 Embed the diode lib to avoid test dependencies
This commit introduces a breaking change in the diode API in order to
hide the diodes package interface. This removes a good number of
dependencies introduced by the test framework used by the diodes
package.
2018-05-24 19:15:40 -07:00
Olivier Poitrey 64faaa6980 Add go.mod file 2018-05-23 09:50:46 -07:00
Olivier Poitrey 80d6806aae Fix comments (bis) 2018-05-21 11:01:34 -07:00
Olivier Poitrey ea197802eb Fix comments 2018-05-17 16:48:29 -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
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 b5207c012d Fix type in README (fix #62) 2018-05-12 22:12:35 -07:00
Ravi Raju 533ee32d5d fix needed after calling encoder via interface (#60) 2018-05-10 18:21:30 -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
Olivier Poitrey 79281e4bf6 Fix Event.Times when format is set to UNIX time 2018-05-09 03:52:30 -07:00
Ravi Raju 57da509ee1 Add JournalD Writer (#57)
JournalD writer decodes each log event and map fields to journald fields. The JSON payload is kept in the `JSON` field.
2018-04-26 23:15:29 -07:00
Olivier Poitrey 89162918d0 Add grpc-zerolog reference (fix #58) 2018-04-26 13:41:11 -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
Dan Gillis 711d95f5f1 Some new readability updates (#54) 2018-04-18 20:29:59 -07:00
Ilya Galimyanov 1e2ce57d98 Make GlobalLevel a public function (#53) 2018-04-17 15:52:22 -07:00
Ravi Raju 70bea47cc0 Fix for a bug in cbor decodeFloat (#51) 2018-04-13 00:13:41 -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
Olivier Poitrey 05eafee0eb Update README with instruction about binary encoding 2018-03-28 11:59:26 -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 3ab376bc30 Add "Who uses zerolog" wiki page link 2018-03-25 22:34:03 -07:00
Olivier Poitrey d0ca9bbceb Add support for pointer values in Fields
Fixes #46
2018-03-25 20:18:47 -07:00
Olivier Poitrey 24cc441b11 Add more json type tests 2018-03-23 09:58:31 -07:00
Olivier Poitrey 4ea03de40d Optimize JSON string encoding using a lookup table
benchstat old new
name                             old time/op    new time/op    delta
AppendString/MultiBytesFirst-8     77.9ns ± 5%    70.2ns ± 1%   -9.88%  (p=0.008 n=5+5)
AppendString/MultiBytesMiddle-8    64.2ns ± 1%    56.3ns ± 5%  -12.19%  (p=0.008 n=5+5)
AppendString/MultiBytesLast-8      51.2ns ± 2%    45.2ns ± 4%  -11.65%  (p=0.008 n=5+5)
AppendString/NoEncoding-8          36.2ns ± 4%    34.0ns ± 6%     ~     (p=0.087 n=5+5)
AppendString/EncodingFirst-8       67.7ns ± 2%    59.4ns ± 2%  -12.26%  (p=0.008 n=5+5)
AppendString/EncodingMiddle-8      56.5ns ± 2%    50.6ns ± 5%  -10.54%  (p=0.008 n=5+5)
AppendString/EncodingLast-8        41.3ns ± 1%    39.6ns ± 5%   -4.11%  (p=0.024 n=5+5)
AppendBytes/MultiBytesLast-8       53.5ns ± 6%    45.6ns ± 4%  -14.79%  (p=0.008 n=5+5)
AppendBytes/NoEncoding-8           36.3ns ± 3%    28.6ns ± 3%  -21.10%  (p=0.008 n=5+5)
AppendBytes/EncodingFirst-8        67.3ns ± 4%    62.1ns ± 4%   -7.75%  (p=0.008 n=5+5)
AppendBytes/EncodingMiddle-8       59.2ns ± 7%    51.0ns ± 6%  -13.85%  (p=0.008 n=5+5)
AppendBytes/EncodingLast-8         43.7ns ± 6%    34.4ns ± 2%  -21.32%  (p=0.008 n=5+5)
AppendBytes/MultiBytesFirst-8      77.7ns ± 2%    71.2ns ± 3%   -8.37%  (p=0.008 n=5+5)
AppendBytes/MultiBytesMiddle-8     63.6ns ± 3%    57.8ns ± 5%   -9.12%  (p=0.008 n=5+5)
2018-03-23 04:24:50 -07:00
Johan Sim Jian An 5250a1ba2d Check nil in RawJSON. (#45) 2018-03-22 21:08:22 -07:00
Ryan Boehning be4b7c1474 Update for Go 1.10 (#39)
* Add Go 1.10 to .travis.yml.
* Add quotes to Go versions in .travis.yml, because unquoted 1.10 is interpreted
as Go 1.1.
* Change .travis.yml references from 'tip' to 'master'. 'tip' is a legacy reference
coming from the days when the Go project used mercurial instead of git.
2018-03-15 10:50:30 -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
Olivier Poitrey 8c1c6a0cd7 Add diode.Writer, a thread-safe, lock-free, non-blocking writer wrapper 2018-02-20 02:33:26 -08:00
Giovanni Bajo 9ee98f91c4 Remove allocations while logging an Array of Objects. (#38) 2018-02-13 11:18:01 -08:00
Olivier Poitrey a717e7cbed Improve ConsoleWriter of non-scalar types 2018-02-13 00:20:42 -08:00
Olivier Poitrey 56a970de51 Add RawJSON field type 2018-02-12 16:05:27 -08:00
Olivier Poitrey 9a92fd2536 Fix typoes 2018-02-10 18:57:53 -08:00
Olivier Poitrey 7d8f9e5cf0 Fix unit tests 2018-02-08 21:29:16 -08:00
Dan Gillis 8eb285b62b Updates to README.md to help with readability (#32)
* Added missed >m< in github.com
* Added backquotes to denote "code" where appropriate
* Cleanup based on feedback
* Added three examples from README
* Removed Output for 3 examples
* Updated Setting Global Log Level section w/ flags
* Removed unnecessary blank lines
* Moved flags from init into main
* Update log_example_test.go
* Cosmetic change based on Olivier's feedback
* Pushed back to original
* New Examples for Log package
* Removed extra spaces
* Reorganized file and added examples
2018-02-08 21:00:09 -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
Olivier Poitrey cbec2377ee Add benchmarks for context 2018-02-07 13:07:46 -08:00
Olivier Poitrey b53826c57a Add go 1.9 to travis file 2018-01-04 11:28:02 -08:00
zy 1cc67e6325 fix: performance link to invalid section in README.md (#30)
- No performance section in README.md, change link to performance to benchmarks
2018-01-04 11:19:48 -08:00
nogoegst c2fc1c63dc Add missing WithLevel method to log package (#27) 2017-12-14 10:33:32 -08:00
Rodrigo Coelho c3d02683c7 Add hook support (#24) 2017-12-01 10:52:37 -07:00
millerlogic 1251b38a89 Fix sampler for low Burst values (#19) 2017-11-27 10:05:35 -08:00
Rodrigo Coelho c8e50a6043 Fix tests for Windows. (#23) 2017-11-27 10:01:32 -08:00
Ravi Raju 9a65e7ccd2 Fix Output with existing context (fix #20)
Also includes tests for Output()
2017-11-08 10:47:56 -08: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 9d194eb6f5 Remove redundant condition 2017-09-11 14:52:32 -07:00
Olivier Poitrey 3ac71fc58d Simplify copies 2017-09-03 11:01:28 -07:00
Olivier Poitrey 26094019c8 Fix godoc 2017-09-01 20:07:47 -07:00
Olivier Poitrey 8c682b3b12 Add Print and Printf top level methods 2017-09-01 19:59:48 -07:00
Olivier Poitrey 96c2125038 Update readme to reference Mário Freitas' benchmarks 2017-09-01 19:45:46 -07:00
Olivier Poitrey d76a89fffc Add benchmark for context appending 2017-08-29 23:10:40 -07:00
Olivier Poitrey e26050b2a3 Improve hlog handlers performance by switching to pointer logger
Update request logger's context thru its pointer in order to avoid
multiple copies/allocations.
2017-08-29 22:53:32 -07:00
Olivier Poitrey 560e8848f1 Remove the SampleFieldName global 2017-08-29 10:35:43 -07:00
Olivier Poitrey 9e5c06cf0e Add more advanced sampling modes 2017-08-28 23:30:54 -07:00
Olivier Poitrey 46339da83a Improve doc for WithContext 2017-08-12 16:16:31 -07: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
99 changed files with 19391 additions and 1252 deletions
+10
View File
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
- package-ecosystem: gomod
directory: /
schedule:
interval: weekly
+36
View File
@@ -0,0 +1,36 @@
on: [push, pull_request]
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.23.x, 1.24.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v6
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v6
- uses: actions/cache@v5
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Test
run: go test -race -bench . -benchmem ./...
- name: Test CBOR
run: go test -tags binary_log -race ./...
coverage:
runs-on: ubuntu-latest
steps:
- name: Update coverage report
uses: ncruces/go-coverage-report@main
with:
report: 'true'
chart: 'true'
amend: 'true'
continue-on-error: true
+3
View File
@@ -6,6 +6,7 @@
# Folders
_obj
_test
tmp
# Architecture specific extensions/prefixes
*.[568vq]
@@ -22,3 +23,5 @@ _testmain.go
*.exe
*.test
*.prof
coverage.out
-10
View File
@@ -1,10 +0,0 @@
language: go
go:
- 1.7
- 1.8
- tip
matrix:
allow_failures:
- go: tip
script:
go test -v -race -cpu=1,2,4 ./...
+43
View File
@@ -0,0 +1,43 @@
# Contributing to Zerolog
Thank you for your interest in contributing to **Zerolog**!
Zerolog is a **feature-complete**, high-performance logging library designed to be **lean** and **non-bloated**. The focus of ongoing development is on **bug fixes**, **performance improvements**, and **modernization efforts** (such as keeping up with Go best practices and compatibility with newer Go versions).
## What We're Looking For
We welcome contributions in the following areas:
- **Bug Fixes**: If you find an issue or unexpected behavior, please open an issue and/or submit a fix.
- **Performance Optimizations**: Improvements that reduce memory usage, allocation count, or CPU cycles without introducing complexity are appreciated.
- **Modernization**: Compatibility updates for newer Go versions or idiomatic improvements that do not increase library size or complexity.
- **Documentation Enhancements**: Corrections, clarifications, and improvements to documentation or code comments.
## What We're *Not* Looking For
Zerolog is intended to remain **minimalistic and efficient**. Therefore, we are **not accepting**:
- New features that add optional behaviors or extend API surface area.
- Built-in support for frameworks or external systems (e.g., bindings, integrations).
- General-purpose abstractions or configuration helpers.
If you're unsure whether a change aligns with the project's philosophy, feel free to open an issue for discussion before submitting a PR.
## Contributing Guidelines
1. **Fork the repository**
2. **Create a branch** for your fix or improvement
3. **Write tests** to cover your changes
4. Ensure `go test ./...` passes
5. Run `go fmt` and `go vet` to ensure code consistency
6. **Submit a pull request** with a clear explanation of the motivation and impact
## Code Style
- Keep the code simple, efficient, and idiomatic.
- Avoid introducing new dependencies.
- Preserve backwards compatibility unless explicitly discussed.
---
We appreciate your effort in helping us keep Zerolog fast, minimal, and reliable!
+662 -133
View File
@@ -1,65 +1,315 @@
# Zero Allocation JSON Logger
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/zerolog.svg?branch=master)](https://travis-ci.org/rs/zerolog) [![Coverage](http://gocover.io/_badge/github.com/rs/zerolog)](http://gocover.io/github.com/rs/zerolog)
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://github.com/rs/zerolog/actions/workflows/test.yml/badge.svg)](https://github.com/rs/zerolog/actions/workflows/test.yml) [![Go Coverage](https://github.com/rs/zerolog/wiki/coverage.svg)](https://raw.githack.com/wiki/rs/zerolog/coverage.html)
The zerolog package provides a fast and simple logger dedicated to JSON output.
Zerolog's API is designed to provide both a great developer experience and stunning [performance](#performance). Its unique chaining API allows zerolog to write JSON log events by avoiding allocations and reflection.
Zerolog's API is designed to provide both a great developer experience and stunning [performance](#benchmarks). Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection.
The uber's [zap](https://godoc.org/go.uber.org/zap) library pioneered this approach. Zerolog is taking this concept to the next level with simpler to use API and even better performance.
Uber's [zap](https://godoc.org/go.uber.org/zap) library pioneered this approach. Zerolog is taking this concept to the next level with a simpler to use API and even better performance.
To keep the code base and the API simple, zerolog focuses on JSON logging only. Pretty logging on the console is made possible using the provided `zerolog.ConsoleWriter`.
To keep the code base and the API simple, zerolog focuses on efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) [`zerolog.ConsoleWriter`](#pretty-logging).
![Pretty Logging Image](pretty.png)
## Who uses zerolog
Find out [who uses zerolog](https://github.com/rs/zerolog/wiki/Who-uses-zerolog) and add your company / project to the list.
## Features
* Blazing fast
* Low to zero allocation
* Level logging
* Sampling
* Contextual fields
* `context.Context` integration
* `net/http` helpers
* Pretty logging for development
- [Blazing fast](#benchmarks)
- [Low to zero allocation](#benchmarks)
- [Leveled logging](#leveled-logging)
- [Sampling](#log-sampling)
- [Hooks](#hooks)
- [Contextual fields](#contextual-logging)
- [`context.Context` integration](#contextcontext-integration)
- [Integration with `net/http`](#integration-with-nethttp)
- [JSON and CBOR encoding formats](#binary-encoding)
- [Pretty logging for development](#pretty-logging)
- [Error Logging (with optional Stacktrace)](#error-logging)
- [`log/slog` integration](#integration-with-logslog)
## Usage
## Installation
```go
import "github.com/rs/zerolog/log"
```bash
go get -u github.com/rs/zerolog/log
```
### A global logger can be use for simple logging
## Getting Started
### Simple Logging Example
For simple logging, import the global logger package **github.com/rs/zerolog/log**
```go
log.Info().Msg("hello world")
package main
// Output: {"level":"info","time":1494567715,"message":"hello world"}
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
// UNIX Time is faster and smaller than most timestamps
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Print("hello world")
}
// Output: {"time":1516134303,"level":"debug","message":"hello world"}
```
NOTE: To import the global logger, import the `log` subpackage `github.com/rs/zerolog/log`.
> Note: By default log writes to `os.Stderr`
> Note: The default log level for `log.Print` is _trace_
### Contextual Logging
**zerolog** allows data to be added to log messages in the form of key:value pairs. The data added to the message adds "context" about the log event that can be critical for debugging as well as myriad other purposes. An example of this is below:
```go
log.Fatal().
Err(err).
Str("service", service).
Msgf("Cannot start %s", service)
package main
// Output: {"level":"fatal","time":1494567715,"message":"Cannot start myservice","error":"some error","service":"myservice"}
// Exit 1
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Debug().
Str("Scale", "833 cents").
Float64("Interval", 833.09).
Msg("Fibonacci is everywhere")
log.Debug().
Str("Name", "Tom").
Send()
}
// Output: {"level":"debug","Scale":"833 cents","Interval":833.09,"time":1562212768,"message":"Fibonacci is everywhere"}
// Output: {"level":"debug","Name":"Tom","time":1562212768}
```
NOTE: Using `Msgf` generates one allocation even when the logger is disabled.
> You'll note in the above example that when adding contextual fields, the fields are strongly typed. You can find the full list of supported fields [here](#standard-types)
### Fields can be added to log messages
### Leveled Logging
#### Simple Leveled Logging Example
```go
log.Info().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
package main
// Output: {"level":"info","time":1494567715,"foo":"bar","n":123,"message":"hello world"}
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Info().Msg("hello world")
}
// Output: {"time":1516134303,"level":"info","message":"hello world"}
```
> It is very important to note that when using the **zerolog** chaining API, as shown above (`log.Info().Msg("hello world"`), the chain must have either the `Msg` or `Msgf` method call. If you forget to add either of these, the log will not occur and there is no compile time error to alert you of this.
**zerolog** allows for logging at the following levels (from highest to lowest):
- panic (`zerolog.PanicLevel`, 5)
- fatal (`zerolog.FatalLevel`, 4)
- error (`zerolog.ErrorLevel`, 3)
- warn (`zerolog.WarnLevel`, 2)
- info (`zerolog.InfoLevel`, 1)
- debug (`zerolog.DebugLevel`, 0)
- trace (`zerolog.TraceLevel`, -1)
You can set the Global logging level to any of these options using the `SetGlobalLevel` function in the zerolog package, passing in one of the given constants above, e.g. `zerolog.InfoLevel` would be the "info" level. Whichever level is chosen, all logs with a level greater than or equal to that level will be written. To turn off logging entirely, pass the `zerolog.Disabled` constant.
#### Setting Global Log Level
This example uses command-line flags to demonstrate various outputs depending on the chosen log level.
```go
package main
import (
"flag"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Debug().Msg("This message appears only when log level set to Debug")
log.Info().Msg("This message appears when log level set to Debug or Info")
if e := log.Debug(); e.Enabled() {
// Compute log output only if enabled.
value := "bar"
e.Str("foo", value).Msg("some debug message")
}
}
```
Info Output (no flag)
```bash
$ ./logLevelExample
{"time":1516387492,"level":"info","message":"This message appears when log level set to Debug or Info"}
```
Debug Output (debug flag set)
```bash
$ ./logLevelExample -debug
{"time":1516387573,"level":"debug","message":"This message appears only when log level set to Debug"}
{"time":1516387573,"level":"info","message":"This message appears when log level set to Debug or Info"}
{"time":1516387573,"level":"debug","foo":"bar","message":"some debug message"}
```
#### Logging without Level or Message
You may choose to log without a specific level by using the `Log` method. You may also write without a message by setting an empty string in the `msg string` parameter of the `Msg` method. Both are demonstrated in the example below.
```go
package main
import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Log().
Str("foo", "bar").
Msg("")
}
// Output: {"time":1494567715,"foo":"bar"}
```
### Error Logging
You can log errors using the `Err` method
```go
package main
import (
"errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
err := errors.New("seems we have an error here")
log.Error().Err(err).Msg("")
}
// Output: {"level":"error","error":"seems we have an error here","time":1609085256}
```
> The default field name for errors is `error`, you can change this by setting `zerolog.ErrorFieldName` to meet your needs.
#### Error Logging with Stacktrace
Using `github.com/pkg/errors`, you can add a formatted stacktrace to your errors.
```go
package main
import (
"github.com/pkg/errors"
"github.com/rs/zerolog/pkgerrors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.ErrorStackMarshaler = pkgerrors.MarshalStack
err := outer()
log.Error().Stack().Err(err).Msg("")
}
func inner() error {
return errors.New("seems we have an error here")
}
func middle() error {
err := inner()
if err != nil {
return err
}
return nil
}
func outer() error {
err := middle()
if err != nil {
return err
}
return nil
}
// Output: {"level":"error","stack":[{"func":"inner","line":"20","source":"errors.go"},{"func":"middle","line":"24","source":"errors.go"},{"func":"outer","line":"32","source":"errors.go"},{"func":"main","line":"15","source":"errors.go"},{"func":"main","line":"204","source":"proc.go"},{"func":"goexit","line":"1374","source":"asm_amd64.s"}],"error":"seems we have an error here","time":1609086683}
```
> zerolog.ErrorStackMarshaler must be set in order for the stack to output anything.
#### Logging Fatal Messages
```go
package main
import (
"errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
err := errors.New("A repo man spends his life getting into tense situations")
service := "myservice"
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Fatal().
Err(err).
Str("service", service).
Msgf("Cannot start %s", service)
}
// Output: {"time":1516133263,"level":"fatal","error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
// exit status 1
```
> NOTE: Using `Msgf` generates one allocation even when the logger is disabled.
### Create logger instance to manage different outputs
```go
@@ -74,40 +324,78 @@ logger.Info().Str("foo", "bar").Msg("hello world")
```go
sublogger := log.With().
Str("component": "foo").
Str("component", "foo").
Logger()
sublogger.Info().Msg("hello world")
// Output: {"level":"info","time":1494567715,"message":"hello world","component":"foo"}
```
### Level logging
```go
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Debug().Msg("filtered out message")
log.Info().Msg("routed message")
if e := log.Debug(); e.Enabled() {
// Compute log output only if enabled.
value := compute()
e.Str("foo": value).Msg("some debug message")
}
// Output: {"level":"info","time":1494567715,"message":"routed message"}
```
### Pretty logging
To log a human-friendly, colorized output, use `zerolog.ConsoleWriter`:
```go
if isConsole {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Str("foo", "bar").Msg("Hello world")
// Output: 1494567715 |INFO| Hello world foo=bar
// Output: 3:04PM INF Hello World foo=bar
```
To customize the configuration and formatting:
```go
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
output.FormatLevel = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
}
output.FormatMessage = func(i interface{}) string {
return fmt.Sprintf("***%s****", i)
}
output.FormatFieldName = func(i interface{}) string {
return fmt.Sprintf("%s:", i)
}
output.FormatFieldValue = func(i interface{}) string {
return strings.ToUpper(fmt.Sprintf("%s", i))
}
log := zerolog.New(output).With().Timestamp().Logger()
log.Info().Str("foo", "bar").Msg("Hello World")
// Output: 2006-01-02T15:04:05Z07:00 | INFO | ***Hello World**** foo:BAR
```
To use custom advanced formatting:
```go
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true,
PartsOrder: []string{"level", "one", "two", "three", "message"},
FieldsExclude: []string{"one", "two", "three"}}
output.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s", i)) }
output.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) }
output.FormatPartValueByName = func(i interface{}, s string) string {
var ret string
switch s {
case "one":
ret = strings.ToUpper(fmt.Sprintf("%s", i))
case "two":
ret = strings.ToLower(fmt.Sprintf("%s", i))
case "three":
ret = strings.ToLower(fmt.Sprintf("(%s)", i))
}
return ret
}
log := zerolog.New(output)
log.Info().Str("foo", "bar").
Str("two", "TEST_TWO").
Str("one", "test_one").
Str("three", "test_three").
Msg("Hello World")
// Output: INFO TEST_ONE test_two (test_three) Hello World foo:bar
```
### Sub dictionary
@@ -117,7 +405,7 @@ log.Info().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1)
Int("n", 1),
).Msg("hello world")
// Output: {"level":"info","time":1494567715,"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
@@ -135,33 +423,97 @@ log.Info().Msg("hello world")
// Output: {"l":"info","t":1494567715,"m":"hello world"}
```
### Log with no level nor message
```go
log.Log().Str("foo","bar").Msg("")
// Output: {"time":1494567715,"foo":"bar"}
```
### Add contextual fields to the global logger
```go
log.Logger = log.With().Str("foo", "bar").Logger()
```
### Add file and line number to log
Equivalent of `Llongfile`:
```go
log.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")
// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"}
```
Equivalent of `Lshortfile`:
```go
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return filepath.Base(file) + ":" + strconv.Itoa(line)
}
log.Logger = log.With().Caller().Logger()
log.Info().Msg("hello world")
// Output: {"level": "info", "message": "hello world", "caller": "some_file:21"}
```
### Thread-safe, lock-free, non-blocking writer
If your writer might be slow or not thread-safe and you need your log producers to never get slowed down by a slow writer, you can use a `diode.Writer` as follows:
```go
wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
fmt.Printf("Logger Dropped %d messages", missed)
})
log := zerolog.New(wr)
log.Print("test")
```
You will need to install `code.cloudfoundry.org/go-diodes` to use this feature.
### Log Sampling
```go
sampled := log.Sample(10)
sampled := log.Sample(&zerolog.BasicSampler{N: 10})
sampled.Info().Msg("will be logged every 10 messages")
// Output: {"time":1494567715,"sample":10,"message":"will be logged every 10 messages"}
// Output: {"time":1494567715,"level":"info","message":"will be logged every 10 messages"}
```
More advanced sampling:
```go
// Will let 5 debug messages per period of 1 second.
// Over 5 debug message, 1 every 100 debug messages are logged.
// Other levels are not sampled.
sampled := log.Sample(zerolog.LevelSampler{
DebugSampler: &zerolog.BurstSampler{
Burst: 5,
Period: 1*time.Second,
NextSampler: &zerolog.BasicSampler{N: 100},
},
})
sampled.Debug().Msg("hello world")
// Output: {"time":1494567715,"level":"debug","message":"hello world"}
```
### Hooks
```go
type SeverityHook struct{}
func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
if level != zerolog.NoLevel {
e.Str("severity", level.String())
}
}
hooked := log.Hook(SeverityHook{})
hooked.Warn().Msg("")
// Output: {"level":"warn","severity":"warn"}
```
### Pass a sub-logger by context
```go
ctx := log.With("component", "module").Logger().WithContext(ctx)
ctx := log.With().Str("component", "module").Logger().WithContext(ctx)
log.Ctx(ctx).Info().Msg("hello world")
@@ -183,6 +535,58 @@ stdlog.Print("hello world")
// Output: {"foo":"bar","message":"hello world"}
```
### context.Context integration
Go contexts are commonly passed throughout Go code, and this can help you pass
your Logger into places it might otherwise be hard to inject. The `Logger`
instance may be attached to Go context (`context.Context`) using
`Logger.WithContext(ctx)` and extracted from it using `zerolog.Ctx(ctx)`.
For example:
```go
func f() {
logger := zerolog.New(os.Stdout)
ctx := context.Background()
// Attach the Logger to the context.Context
ctx = logger.WithContext(ctx)
someFunc(ctx)
}
func someFunc(ctx context.Context) {
// Get Logger from the go Context. if it's nil, then
// `zerolog.DefaultContextLogger` is returned, if
// `DefaultContextLogger` is nil, then a disabled logger is returned.
logger := zerolog.Ctx(ctx)
logger.Info().Msg("Hello")
}
```
A second form of `context.Context` integration allows you to pass the current
`context.Context` into the logged event, and retrieve it from hooks. This can be
useful to log trace and span IDs or other information stored in the go context,
and facilitates the unification of logging and tracing in some systems:
```go
type TracingHook struct{}
func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
ctx := e.GetCtx()
spanId := getSpanIdFromContext(ctx) // as per your tracing framework
e.Str("span-id", spanId)
}
func f() {
// Setup the logger
logger := zerolog.New(os.Stdout)
logger = logger.Hook(TracingHook{})
ctx := context.Background()
// Use the Ctx function to make the context available to the hook
logger.Info().Ctx(ctx).Msg("Hello")
}
```
### Integration with `net/http`
The `github.com/rs/zerolog/hlog` package provides some helpers to integrate zerolog with `http.Handler`.
@@ -202,11 +606,11 @@ c := alice.New()
c = c.Append(hlog.NewHandler(log))
// Install some provided extra handler to set some request's context fields.
// Thanks to those handler, all our logs will come with some pre-populated fields.
// Thanks to that handler, all our logs will come with some prepopulated fields.
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("url", r.URL.String()).
Stringer("url", r.URL).
Int("status", status).
Int("size", size).
Dur("duration", duration).
@@ -236,94 +640,219 @@ if err := http.ListenAndServe(":8080", nil); err != nil {
}
```
## Multiple Log Output
`zerolog.MultiLevelWriter` may be used to send the log message to multiple outputs.
In this example, we send the log message to both `os.Stdout` and the in-built `ConsoleWriter`.
```go
func main() {
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout}
multi := zerolog.MultiLevelWriter(consoleWriter, os.Stdout)
logger := zerolog.New(multi).With().Timestamp().Logger()
logger.Info().Msg("Hello World!")
}
// Output (Line 1: Console; Line 2: Stdout)
// 12:36PM INF Hello World!
// {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"}
```
## Global Settings
Some settings can be changed and will by applied to all loggers:
Some settings can be changed and will be applied to all loggers:
* `log.Logger`: You can set this value to customize the global logger (the one used by package level methods).
* `zerolog.SetGlobalLevel`: Can raise the minimum level of all loggers. Set this to `zerolog.Disable` to disable logging altogether (quiet mode).
* `zerolog.DisableSampling`: If argument is `true`, all sampled loggers will stop sampling and issue 100% of their log events.
* `zerolog.TimestampFieldName`: Can be set to customize `Timestamp` field name.
* `zerolog.LevelFieldName`: Can be set to customize level field name.
* `zerolog.MessageFieldName`: Can be set to customize message field name.
* `zerolog.ErrorFieldName`: Can be set to customize `Err` field name.
* `zerolog.SampleFieldName`: Can be set to customize the field name added when sampling is enabled.
* `zerolog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with an empty string, times are formated as UNIX timestamp.
// DurationFieldUnit defines the unit for time.Duration type fields added
// using the Dur method.
* `DurationFieldUnit`: Sets the unit of the fields added by `Dur` (default: `time.Millisecond`).
* `DurationFieldInteger`: If set to true, `Dur` fields are formatted as integers instead of floats.
- `log.Logger`: You can set this value to customize the global logger (the one used by package level methods).
- `zerolog.SetGlobalLevel`: Can raise the minimum level of all loggers. Call this with `zerolog.Disabled` to disable logging altogether (quiet mode).
- `zerolog.DisableSampling`: If argument is `true`, all sampled loggers will stop sampling and issue 100% of their log events.
- `zerolog.TimestampFieldName`: Can be set to customize `Timestamp` field name.
- `zerolog.LevelFieldName`: Can be set to customize level field name.
- `zerolog.MessageFieldName`: Can be set to customize message field name.
- `zerolog.ErrorFieldName`: Can be set to customize `Err` field name.
- `zerolog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with `zerolog.TimeFormatUnix`, `zerolog.TimeFormatUnixMs` or `zerolog.TimeFormatUnixMicro`, times are formatted as UNIX timestamp.
- `zerolog.DurationFieldUnit`: Can be set to customize the unit for time.Duration type fields added by `Dur` (default: `time.Millisecond`).
- `zerolog.DurationFieldFormat`: Can be set to `DurationFormatFloat`, `DurationFormatInt`, or `DurationFormatString` (default: `DurationFormatFloat`) to append the `Duration` as a `Float64`, `Int64`, or by calling `String()` (respectively).
- `zerolog.DurationFieldInteger`: If set to `true`, `Dur` fields are formatted as integers instead of floats (default: `false`). Deprecated: Use `zerolog.DurationFieldFormat = DurationFormatInt` instead.
- `zerolog.ErrorHandler`: Called whenever zerolog fails to write an event on its output. If not set, an error is printed on the stderr. This handler must be thread safe and non-blocking.
- `zerolog.FloatingPointPrecision`: If set to a value other than -1, controls the number of digits when formatting float numbers in JSON. See [strconv.FormatFloat](https://pkg.go.dev/strconv#FormatFloat)
for more details.
## Field Types
### Standard Types
* `Str`
* `Bool`
* `Int`, `Int8`, `Int16`, `Int32`, `Int64`
* `Uint`, `Uint8`, `Uint16`, `Uint32`, `Uint64`
* `Float32`, `Float64`
- `Str`
- `Bool`
- `Int`, `Int8`, `Int16`, `Int32`, `Int64`
- `Uint`, `Uint8`, `Uint16`, `Uint32`, `Uint64`
- `Float32`, `Float64`
### Advanced Fields
* `Err`: Takes an `error` and render it as a string using the `zerolog.ErrorFieldName` field name.
* `Timestamp`: Insert a timestamp field with `zerolog.TimestampFieldName` field name and formatted using `zerolog.TimeFieldFormat`.
* `Time`: Adds a field with the time formated with the `zerolog.TimeFieldFormat`.
* `Dur`: Adds a field with a `time.Duration`.
* `Dict`: Adds a sub-key/value as a field of the event.
* `Interface`: Uses reflection to marshal the type.
- `Err`: Takes an `error` and renders it as a string using the `zerolog.ErrorFieldName` field name.
- `Func`: Run a `func` only if the level is enabled.
- `Timestamp`: Inserts a timestamp field with `zerolog.TimestampFieldName` field name, formatted using `zerolog.TimeFieldFormat`.
- `Time`: Adds a field with time formatted with `zerolog.TimeFieldFormat`.
- `Dur`: Adds a field with `time.Duration`.
- `Dict`: Adds a sub-key/value as a field of the event.
- `RawJSON`: Adds a field with an already encoded JSON (`[]byte`)
- `Hex`: Adds a field with value formatted as a hexadecimal string (`[]byte`)
- `Interface`: Uses reflection to marshal the type.
- `IPAddr`: Adds a field with `net.IP`.
- `IPPrefix`: Adds a field with `net.IPNet`.
- `MACAddr`: Adds a field with `net.HardwareAddr`
## Performance
Most fields are also available in the slice format (`Strs` for `[]string`, `Errs` for `[]error` etc.)
All operations are allocation free (those numbers *include* JSON encoding):
## Binary Encoding
```
BenchmarkLogEmpty-8 100000000 19.1 ns/op 0 B/op 0 allocs/op
BenchmarkDisabled-8 500000000 4.07 ns/op 0 B/op 0 allocs/op
BenchmarkInfo-8 30000000 42.5 ns/op 0 B/op 0 allocs/op
BenchmarkContextFields-8 30000000 44.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogFields-8 10000000 184 ns/op 0 B/op 0 allocs/op
In addition to the default JSON encoding, `zerolog` can produce binary logs using [CBOR](https://cbor.io) encoding. The choice of encoding can be decided at compile time using the build tag `binary_log` as follows:
```bash
go build -tags binary_log .
```
Using Uber's zap [comparison benchmark](https://github.com/uber-go/zap#performance):
To decode binary encoded log files you can use any CBOR decoder. One has been tested to work
with zerolog library is [CSD](https://github.com/toravir/csd/).
## Integration with `log/slog`
zerolog provides a `slog.Handler` implementation that routes `log/slog` records through a zerolog logger. This lets you use the standard library's `slog` API while keeping zerolog's performance and encoding:
```go
package main
import (
"log/slog"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
zl := log.Logger
handler := zerolog.NewSlogHandler(zl)
logger := slog.New(handler)
logger.Info("user logged in", "user", "alice", "role", "admin")
}
// Output: {"level":"info","user":"alice","role":"admin","time":"...","message":"user logged in"}
```
The handler supports all `slog` features including `WithAttrs`, `WithGroup`, nested groups, and `LogValuer` resolution. slog levels are mapped to zerolog levels (e.g. `slog.LevelDebug` to `zerolog.DebugLevel`).
## Related Projects
- [grpc-zerolog](https://github.com/cheapRoc/grpc-zerolog): Implementation of `grpclog.LoggerV2` interface using `zerolog`
- [overlog](https://github.com/Trendyol/overlog): Implementation of `Mapped Diagnostic Context` interface using `zerolog`
- [zerologr](https://github.com/go-logr/zerologr): Implementation of `logr.LogSink` interface using `zerolog`
- [logze](https://github.com/maxbolgarin/logze): Implementation of `log/slog` interface using `zerolog`
## Benchmarks
See [logbench](http://bench.zerolog.io/) for more comprehensive and up-to-date benchmarks.
All operations are allocation free (those numbers _include_ JSON encoding):
```text
BenchmarkLogEmpty-8 100000000 19.1 ns/op 0 B/op 0 allocs/op
BenchmarkDisabled-8 500000000 4.07 ns/op 0 B/op 0 allocs/op
BenchmarkInfo-8 30000000 42.5 ns/op 0 B/op 0 allocs/op
BenchmarkContextFields-8 30000000 44.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogFields-8 10000000 184 ns/op 0 B/op 0 allocs/op
```
There are a few Go logging benchmarks and comparisons that include zerolog.
- [imkira/go-loggers-bench](https://github.com/imkira/go-loggers-bench)
- [uber-common/zap](https://github.com/uber-go/zap#performance)
Using Uber's zap comparison benchmark:
Log a message and 10 fields:
| Library | Time | Bytes Allocated | Objects Allocated |
| :--- | :---: | :---: | :---: |
| zerolog | 767 ns/op | 552 B/op | 6 allocs/op |
| :zap: zap | 848 ns/op | 704 B/op | 2 allocs/op |
| :zap: zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op |
| go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op |
| lion | 5392 ns/op | 5807 B/op | 63 allocs/op |
| logrus | 5661 ns/op | 6092 B/op | 78 allocs/op |
| apex/log | 15332 ns/op | 3832 B/op | 65 allocs/op |
| log15 | 20657 ns/op | 5632 B/op | 93 allocs/op |
| Library | Time | Bytes Allocated | Objects Allocated |
| :------------------ | :---------: | :-------------: | :---------------: |
| zerolog | 767 ns/op | 552 B/op | 6 allocs/op |
| :zap: zap | 848 ns/op | 704 B/op | 2 allocs/op |
| :zap: zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op |
| go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op |
| lion | 5392 ns/op | 5807 B/op | 63 allocs/op |
| logrus | 5661 ns/op | 6092 B/op | 78 allocs/op |
| apex/log | 15332 ns/op | 3832 B/op | 65 allocs/op |
| log15 | 20657 ns/op | 5632 B/op | 93 allocs/op |
Log a message with a logger that already has 10 fields of context:
| Library | Time | Bytes Allocated | Objects Allocated |
| :--- | :---: | :---: | :---: |
| zerolog | 52 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap | 283 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
| lion | 2702 ns/op | 4074 B/op | 38 allocs/op |
| go-kit | 3378 ns/op | 3046 B/op | 52 allocs/op |
| logrus | 4309 ns/op | 4564 B/op | 63 allocs/op |
| apex/log | 13456 ns/op | 2898 B/op | 51 allocs/op |
| log15 | 14179 ns/op | 2642 B/op | 44 allocs/op |
| Library | Time | Bytes Allocated | Objects Allocated |
| :------------------ | :---------: | :-------------: | :---------------: |
| zerolog | 52 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap | 283 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
| lion | 2702 ns/op | 4074 B/op | 38 allocs/op |
| go-kit | 3378 ns/op | 3046 B/op | 52 allocs/op |
| logrus | 4309 ns/op | 4564 B/op | 63 allocs/op |
| apex/log | 13456 ns/op | 2898 B/op | 51 allocs/op |
| log15 | 14179 ns/op | 2642 B/op | 44 allocs/op |
Log a static string, without any context or `printf`-style templating:
| Library | Time | Bytes Allocated | Objects Allocated |
| :--- | :---: | :---: | :---: |
| zerolog | 50 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap | 236 ns/op | 0 B/op | 0 allocs/op |
| standard library | 453 ns/op | 80 B/op | 2 allocs/op |
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
| go-kit | 508 ns/op | 656 B/op | 13 allocs/op |
| lion | 771 ns/op | 1224 B/op | 10 allocs/op |
| logrus | 1244 ns/op | 1505 B/op | 27 allocs/op |
| apex/log | 2751 ns/op | 584 B/op | 11 allocs/op |
| log15 | 5181 ns/op | 1592 B/op | 26 allocs/op |
| Library | Time | Bytes Allocated | Objects Allocated |
| :------------------ | :--------: | :-------------: | :---------------: |
| zerolog | 50 ns/op | 0 B/op | 0 allocs/op |
| :zap: zap | 236 ns/op | 0 B/op | 0 allocs/op |
| standard library | 453 ns/op | 80 B/op | 2 allocs/op |
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
| go-kit | 508 ns/op | 656 B/op | 13 allocs/op |
| lion | 771 ns/op | 1224 B/op | 10 allocs/op |
| logrus | 1244 ns/op | 1505 B/op | 27 allocs/op |
| apex/log | 2751 ns/op | 584 B/op | 11 allocs/op |
| log15 | 5181 ns/op | 1592 B/op | 26 allocs/op |
## Caveats
### Field duplication
Note that zerolog does no de-duplication of fields. Using the same key multiple times creates multiple keys in final JSON:
```go
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
logger.Info().
Timestamp().
Msg("dup")
// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
```
In this case, many consumers will take the last value, but this is not guaranteed; check yours if in doubt.
### Concurrency safety
Be careful when calling `UpdateContext`. It is not concurrency safe. Use the `With()` method to create a child logger:
```go
func handler(w http.ResponseWriter, r *http.Request) {
// Create a child logger for concurrency safety
logger := log.Logger.With().Logger()
// Add context fields, for example User-Agent from HTTP headers
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
...
})
}
```
The `Event` object returned from the `Logger` level-specific message functions (e.g. `Log()`, `Trace()`, `Debug()`, etc.)
is allocated in `sync.Pool` memory that will be returned to the pool as soon as the `Msg()`, `Msgf()`, `Send()`,
or `MsgFunc()` writes the message and **must not** be accessed afterwards.
**Do not** hold a reference to the `*Event` while in callback functions or your own code. This is especially important in
`Hook.Run()` and `HookFunc` functions or `MarshalZerologObject(e *Event)` callback (e.g. `LogObjectMarshaler` implementations).
Any `Array` objects returned from `Context.CreateArray()` or `Event.CreateArray()` are from a `sync.Pool` so **do not** hold
references to them from within any `MarshalZerologArray(a *Array)` callback (e.g. `LogArrayMarshaler` implementations) or your
own code as they will be cleared and returned to the pool after being buffered by a call to `Context.Array()` or `Event.Array()`.
Any _dictionary_ `Event` returned from `Context.CreateDict()` or `Event.CreateDict()` **must not** be referenced after being
buffered by a call to `Array.Dict()`, `Context.Dict()`, or `Event.Dict()` as they will be cleared and returned to the pool.
+160 -53
View File
@@ -1,10 +1,10 @@
package zerolog
import (
"context"
"net"
"sync"
"time"
"github.com/rs/zerolog/internal/json"
)
var arrayPool = &sync.Pool{
@@ -15,155 +15,262 @@ var arrayPool = &sync.Pool{
},
}
// Array is used to prepopulate an array of items
// which can be re-used to add to log messages.
type Array struct {
buf []byte
buf []byte
stack bool // enable error stack trace
ctx context.Context // Optional Go context
ch []Hook // hooks
}
func putArray(a *Array) {
// prevent any subsequent use of the Array contextual state and truncate the buffer
a.stack = false
a.ctx = nil
a.ch = nil
a.buf = a.buf[:0]
// Proper usage of a sync.Pool requires each entry to have approximately
// the same memory cost. To obtain this property when the stored type
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
// to place back in the pool.
//
// See https://golang.org/issue/23199
const maxSize = 1 << 16 // 64KiB
if cap(a.buf) <= maxSize {
arrayPool.Put(a)
}
}
// Arr creates an array to be added to an Event or Context.
// WARNING: This function is deprecated because it does not preserve
// the stack, hooks, and context from the parent event.
// Deprecated: Use Event.CreateArray or Context.CreateArray instead.
func Arr() *Array {
a := arrayPool.Get().(*Array)
a.buf = a.buf[:0]
a.stack = false
a.ctx = nil
a.ch = nil
return a
}
// MarshalZerologArray method here is no-op - since data is
// already in the needed format.
func (*Array) MarshalZerologArray(*Array) {
// untestable: there's no code to be covered
}
func (a *Array) write(dst []byte) []byte {
if len(a.buf) == 0 {
dst = append(dst, `[]`...)
} else {
a.buf[0] = '['
dst = append(append(dst, a.buf...), ']')
dst = enc.AppendArrayStart(dst)
if len(a.buf) > 0 {
dst = append(dst, a.buf...)
}
arrayPool.Put(a)
dst = enc.AppendArrayEnd(dst)
putArray(a)
return dst
}
// Object marshals an object that implement the LogObjectMarshaler
// interface and append it to the array.
// interface and appends it to the array.
func (a *Array) Object(obj LogObjectMarshaler) *Array {
a.buf = append(a.buf, ',')
e := Dict()
obj.MarshalZerologObject(e)
e.buf = append(e.buf, '}')
a.buf = append(a.buf, e.buf...)
a.buf = appendObject(enc.AppendArrayDelim(a.buf), obj, a.stack, a.ctx, a.ch)
return a
}
// Str append the val as a string to the array.
// Str appends the val as a string to the array.
func (a *Array) Str(val string) *Array {
a.buf = json.AppendString(append(a.buf, ','), val)
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val)
return a
}
// Bytes append the val as a string to the array.
// Bytes appends the val as a string to the array.
func (a *Array) Bytes(val []byte) *Array {
a.buf = json.AppendBytes(append(a.buf, ','), val)
a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
return a
}
// Err append the err as a string to the array.
// Hex appends the val as a hex string to the array.
func (a *Array) Hex(val []byte) *Array {
a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
return a
}
// RawJSON adds already encoded JSON to the array.
func (a *Array) RawJSON(val []byte) *Array {
a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val)
return a
}
// Err serializes and appends the err to the array.
func (a *Array) Err(err error) *Array {
a.buf = json.AppendError(append(a.buf, ','), err)
switch m := ErrorMarshalFunc(err).(type) {
case nil:
a.buf = enc.AppendNil(enc.AppendArrayDelim(a.buf))
case LogObjectMarshaler:
a = a.Object(m)
case error:
if !isNilValue(m) {
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error())
}
case string:
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m)
default:
a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), m)
}
return a
}
// Bool append the val as a bool to the array.
// Errs serializes and appends errors to the array.
func (a *Array) Errs(errs []error) *Array {
for _, err := range errs {
switch m := ErrorMarshalFunc(err).(type) {
case nil:
a = a.Interface(nil)
case LogObjectMarshaler:
a = a.Object(m)
case error:
if !isNilValue(m) {
a = a.Str(m.Error())
}
case string:
a = a.Str(m)
default:
a = a.Interface(m)
}
}
return a
}
// Bool appends the val as a bool to the array.
func (a *Array) Bool(b bool) *Array {
a.buf = json.AppendBool(append(a.buf, ','), b)
a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
return a
}
// Int append i as a int to the array.
// Int appends i as a int to the array.
func (a *Array) Int(i int) *Array {
a.buf = json.AppendInt(append(a.buf, ','), i)
a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
return a
}
// Int8 append i as a int8 to the array.
// Int8 appends i as a int8 to the array.
func (a *Array) Int8(i int8) *Array {
a.buf = json.AppendInt8(append(a.buf, ','), i)
a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
return a
}
// Int16 append i as a int16 to the array.
// Int16 appends i as a int16 to the array.
func (a *Array) Int16(i int16) *Array {
a.buf = json.AppendInt16(append(a.buf, ','), i)
a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
return a
}
// Int32 append i as a int32 to the array.
// Int32 appends i as a int32 to the array.
func (a *Array) Int32(i int32) *Array {
a.buf = json.AppendInt32(append(a.buf, ','), i)
a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
return a
}
// Int64 append i as a int64 to the array.
// Int64 appends i as a int64 to the array.
func (a *Array) Int64(i int64) *Array {
a.buf = json.AppendInt64(append(a.buf, ','), i)
a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
return a
}
// Uint append i as a uint to the array.
// Uint appends i as a uint to the array.
func (a *Array) Uint(i uint) *Array {
a.buf = json.AppendUint(append(a.buf, ','), i)
a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
return a
}
// Uint8 append i as a uint8 to the array.
// Uint8 appends i as a uint8 to the array.
func (a *Array) Uint8(i uint8) *Array {
a.buf = json.AppendUint8(append(a.buf, ','), i)
a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
return a
}
// Uint16 append i as a uint16 to the array.
// Uint16 appends i as a uint16 to the array.
func (a *Array) Uint16(i uint16) *Array {
a.buf = json.AppendUint16(append(a.buf, ','), i)
a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
return a
}
// Uint32 append i as a uint32 to the array.
// Uint32 appends i as a uint32 to the array.
func (a *Array) Uint32(i uint32) *Array {
a.buf = json.AppendUint32(append(a.buf, ','), i)
a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
return a
}
// Uint64 append i as a uint64 to the array.
// Uint64 appends i as a uint64 to the array.
func (a *Array) Uint64(i uint64) *Array {
a.buf = json.AppendUint64(append(a.buf, ','), i)
a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
return a
}
// Float32 append f as a float32 to the array.
// Float32 appends f as a float32 to the array.
func (a *Array) Float32(f float32) *Array {
a.buf = json.AppendFloat32(append(a.buf, ','), f)
a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f, FloatingPointPrecision)
return a
}
// Float64 append f as a float64 to the array.
// Float64 appends f as a float64 to the array.
func (a *Array) Float64(f float64) *Array {
a.buf = json.AppendFloat64(append(a.buf, ','), f)
a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f, FloatingPointPrecision)
return a
}
// Time append t formated as string using zerolog.TimeFieldFormat.
// Time appends t formatted as string using zerolog.TimeFieldFormat.
func (a *Array) Time(t time.Time) *Array {
a.buf = json.AppendTime(append(a.buf, ','), t, TimeFieldFormat)
a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
return a
}
// Dur append d to the array.
// Dur appends d to the array.
func (a *Array) Dur(d time.Duration) *Array {
a.buf = json.AppendDuration(append(a.buf, ','), d, DurationFieldUnit, DurationFieldInteger)
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return a
}
// Interface append i marshaled using reflection.
// Interface appends i marshaled using reflection.
func (a *Array) Interface(i interface{}) *Array {
if obj, ok := i.(LogObjectMarshaler); ok {
return a.Object(obj)
}
a.buf = json.AppendInterface(append(a.buf, ','), i)
a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i)
return a
}
// IPAddr adds a net.IP IPv4 or IPv6 address to the array
func (a *Array) IPAddr(ip net.IP) *Array {
a.buf = enc.AppendIPAddr(enc.AppendArrayDelim(a.buf), ip)
return a
}
// IPPrefix adds a net.IPNet IPv4 or IPv6 Prefix (IP + mask) to the array
func (a *Array) IPPrefix(pfx net.IPNet) *Array {
a.buf = enc.AppendIPPrefix(enc.AppendArrayDelim(a.buf), pfx)
return a
}
// MACAddr adds a net.HardwareAddr MAC (Ethernet) address to the array
func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
return a
}
// Dict adds the dict Event to the array
func (a *Array) Dict(dict *Event) *Array {
dict.buf = enc.AppendEndMarker(dict.buf)
a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...)
putEvent(dict)
return a
}
// Type adds the val's type using reflection to the array.
func (a *Array) Type(val interface{}) *Array {
a.buf = enc.AppendType(enc.AppendArrayDelim(a.buf), val)
return a
}
+41 -5
View File
@@ -1,6 +1,8 @@
package zerolog
import (
"fmt"
"net"
"testing"
"time"
)
@@ -18,13 +20,47 @@ func TestArray(t *testing.T) {
Uint16(8).
Uint32(9).
Uint64(10).
Float32(11).
Float64(12).
Float32(11.98122).
Float64(12.987654321).
Str("a").
Bytes([]byte("b")).
Hex([]byte{0x1f}).
RawJSON([]byte(`{"some":"json"}`)).
RawJSON([]byte(`{"longer":[1111,2222,3333,4444,5555]}`)).
Time(time.Time{}).
Dur(0)
want := `[true,1,2,3,4,5,6,7,8,9,10,11,12,"a","0001-01-01T00:00:00Z",0]`
if got := string(a.write([]byte{})); got != want {
IPAddr(net.IP{192, 168, 0, 10}).
IPPrefix(net.IPNet{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(24, 32)}).
MACAddr(net.HardwareAddr{0x01, 0x23, 0x45, 0x67, 0x89, 0xab}).
Interface(struct {
Pub string
Tag string `json:"tag"`
priv int
}{"A", "j", -5}).
Interface(logObjectMarshalerImpl{
name: "ZOT",
age: 35,
}).
Dur(0).
Dict(Dict().
Str("bar", "baz").
Int("n", 1),
).
Err(nil).
Err(fmt.Errorf("failure")).
Err(loggableError{fmt.Errorf("oops")}).
Object(logObjectMarshalerImpl{
name: "ZIT",
age: 22,
}).
Type(3.14)
want := `[true,1,2,3,4,5,6,7,8,9,10,11.98122,12.987654321,"a","b","1f",{"some":"json"},{"longer":[1111,2222,3333,4444,5555]},"0001-01-01T00:00:00Z","192.168.0.10","127.0.0.0/24","01:23:45:67:89:ab",{"Pub":"A","tag":"j"},{"name":"zot","age":-35},0,{"bar":"baz","n":1},null,"failure",{"l":"OOPS"},{"name":"zit","age":-22},"float64"]`
if got := decodeObjectToStr(a.write([]byte{})); got != want {
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
}
}
func TestArray_MarshalZerologArray(t *testing.T) {
a := Arr()
a.MarshalZerologArray(nil) // no-op method, should not panic
}
+251 -84
View File
@@ -2,7 +2,7 @@ package zerolog
import (
"errors"
"io/ioutil"
"io"
"testing"
"time"
)
@@ -13,7 +13,7 @@ var (
)
func BenchmarkLogEmpty(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@@ -23,7 +23,7 @@ func BenchmarkLogEmpty(b *testing.B) {
}
func BenchmarkDisabled(b *testing.B) {
logger := New(ioutil.Discard).Level(Disabled)
logger := New(io.Discard).Level(Disabled)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@@ -33,7 +33,7 @@ func BenchmarkDisabled(b *testing.B) {
}
func BenchmarkInfo(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@@ -43,7 +43,7 @@ func BenchmarkInfo(b *testing.B) {
}
func BenchmarkContextFields(b *testing.B) {
logger := New(ioutil.Discard).With().
logger := New(io.Discard).With().
Str("string", "four!").
Time("time", time.Time{}).
Int("int", 123).
@@ -57,8 +57,20 @@ func BenchmarkContextFields(b *testing.B) {
})
}
func BenchmarkContextAppend(b *testing.B) {
logger := New(io.Discard).With().
Str("foo", "bar").
Logger()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.With().Str("bar", "baz")
}
})
}
func BenchmarkLogFields(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
@@ -72,125 +84,142 @@ func BenchmarkLogFields(b *testing.B) {
})
}
type obj struct {
Pub string
Tag string `json:"tag"`
priv int
}
func (o obj) MarshalZerologObject(e *Event) {
e.Str("Pub", o.Pub).
Str("Tag", o.Tag).
Int("priv", o.priv)
func BenchmarkLogArrayObject(b *testing.B) {
obj1 := fixtureObj{"a", "b", 2}
obj2 := fixtureObj{"c", "d", 3}
obj3 := fixtureObj{"e", "f", 4}
logger := New(io.Discard)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
arr := Arr()
arr.Object(&obj1)
arr.Object(&obj2)
arr.Object(&obj3)
logger.Info().Array("objects", arr).Msg("test")
}
}
func BenchmarkLogFieldType(b *testing.B) {
bools := []bool{true, false, true, false, true, false, true, false, true, false}
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
floats := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
strings := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
durations := []time.Duration{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
times := []time.Time{
time.Unix(0, 0),
time.Unix(1, 0),
time.Unix(2, 0),
time.Unix(3, 0),
time.Unix(4, 0),
time.Unix(5, 0),
time.Unix(6, 0),
time.Unix(7, 0),
time.Unix(8, 0),
time.Unix(9, 0),
}
interfaces := []struct {
Pub string
Tag string `json:"tag"`
priv int
}{
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
{"a", "a", 0},
}
objects := []obj{
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
obj{"a", "a", 0},
}
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
fixtures := makeFieldFixtures()
types := map[string]func(e *Event) *Event{
"Any": func(e *Event) *Event {
return e.Any("k", fixtures.Interfaces[0])
},
"Bool": func(e *Event) *Event {
return e.Bool("k", bools[0])
return e.Bool("k", fixtures.Bools[0])
},
"Bools": func(e *Event) *Event {
return e.Bools("k", bools)
return e.Bools("k", fixtures.Bools)
},
"Bytes": func(e *Event) *Event {
return e.Bytes("k", fixtures.Bytes)
},
"Hex": func(e *Event) *Event {
return e.Hex("k", fixtures.Bytes)
},
"Int": func(e *Event) *Event {
return e.Int("k", ints[0])
return e.Int("k", fixtures.Ints[0])
},
"Ints": func(e *Event) *Event {
return e.Ints("k", ints)
return e.Ints("k", fixtures.Ints)
},
"Float": func(e *Event) *Event {
return e.Float64("k", floats[0])
"Float32": func(e *Event) *Event {
return e.Float32("k", fixtures.Floats32[0])
},
"Floats": func(e *Event) *Event {
return e.Floats64("k", floats)
"Floats32": func(e *Event) *Event {
return e.Floats32("k", fixtures.Floats32)
},
"Float64": func(e *Event) *Event {
return e.Float64("k", fixtures.Floats64[0])
},
"Floats64": func(e *Event) *Event {
return e.Floats64("k", fixtures.Floats64)
},
"Str": func(e *Event) *Event {
return e.Str("k", strings[0])
return e.Str("k", fixtures.Strings[0])
},
"Strs": func(e *Event) *Event {
return e.Strs("k", strings)
return e.Strs("k", fixtures.Strings)
},
"StrsV": func(e *Event) *Event {
return e.StrsV("k", fixtures.Strings...)
},
"Stringer": func(e *Event) *Event {
return e.Stringer("k", fixtures.Stringers[0])
},
"Stringers": func(e *Event) *Event {
return e.Stringers("k", fixtures.Stringers)
},
"StringersV": func(e *Event) *Event {
return e.StringersV("k", fixtures.Stringers...)
},
"Err": func(e *Event) *Event {
return e.Err(errs[0])
return e.Err(fixtures.Errs[0])
},
"Errs": func(e *Event) *Event {
return e.Errs("k", errs)
return e.Errs("k", fixtures.Errs)
},
"Ctx": func(e *Event) *Event {
return e.Ctx(fixtures.Ctx)
},
"Time": func(e *Event) *Event {
return e.Time("k", times[0])
return e.Time("k", fixtures.Times[0])
},
"Times": func(e *Event) *Event {
return e.Times("k", times)
return e.Times("k", fixtures.Times)
},
"Dur": func(e *Event) *Event {
return e.Dur("k", durations[0])
return e.Dur("k", fixtures.Durations[0])
},
"Durs": func(e *Event) *Event {
return e.Durs("k", durations)
return e.Durs("k", fixtures.Durations)
},
"Interface": func(e *Event) *Event {
return e.Interface("k", interfaces[0])
return e.Interface("k", fixtures.Interfaces[0])
},
"Interfaces": func(e *Event) *Event {
return e.Interface("k", interfaces)
return e.Interface("k", fixtures.Interfaces)
},
"Interface(Object)": func(e *Event) *Event {
return e.Interface("k", objects[0])
return e.Interface("k", fixtures.Objects[0])
},
"Interface(Objects)": func(e *Event) *Event {
return e.Interface("k", objects)
return e.Interface("k", fixtures.Objects)
},
"Object": func(e *Event) *Event {
return e.Object("k", objects[0])
return e.Object("k", fixtures.Objects[0])
},
"Objects": func(e *Event) *Event {
return e.Objects("k", fixtures.Objects)
},
"ObjectsV": func(e *Event) *Event {
return e.ObjectsV("k", fixtures.Objects...)
},
"Timestamp": func(e *Event) *Event {
return e.Timestamp()
},
"IPAddr": func(e *Event) *Event {
return e.IPAddr("k", fixtures.IPAddrs[0])
},
"IPAddrs": func(e *Event) *Event {
return e.IPAddrs("k", fixtures.IPAddrs)
},
"IPPrefix": func(e *Event) *Event {
return e.IPPrefix("k", fixtures.IPPfxs[0])
},
"IPPrefixes": func(e *Event) *Event {
return e.IPPrefixes("k", fixtures.IPPfxs)
},
"MACAddr": func(e *Event) *Event {
return e.MACAddr("k", fixtures.MACAddr)
},
"Type": func(e *Event) *Event {
return e.Type("k", fixtures.Type)
},
}
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
for name := range types {
f := types[name]
@@ -203,3 +232,141 @@ func BenchmarkLogFieldType(b *testing.B) {
})
}
}
func BenchmarkContextFieldType(b *testing.B) {
oldFormat := TimeFieldFormat
TimeFieldFormat = TimeFormatUnix
defer func() { TimeFieldFormat = oldFormat }()
fixtures := makeFieldFixtures()
types := map[string]func(c Context) Context{
"Any": func(c Context) Context {
return c.Any("k", fixtures.Interfaces[0])
},
"Bool": func(c Context) Context {
return c.Bool("k", fixtures.Bools[0])
},
"Bools": func(c Context) Context {
return c.Bools("k", fixtures.Bools)
},
"Bytes": func(c Context) Context {
return c.Bytes("k", fixtures.Bytes)
},
"Hex": func(c Context) Context {
return c.Hex("k", fixtures.Bytes)
},
"Int": func(c Context) Context {
return c.Int("k", fixtures.Ints[0])
},
"Ints": func(c Context) Context {
return c.Ints("k", fixtures.Ints)
},
"Float32": func(c Context) Context {
return c.Float32("k", fixtures.Floats32[0])
},
"Floats32": func(c Context) Context {
return c.Floats32("k", fixtures.Floats32)
},
"Float64": func(c Context) Context {
return c.Float64("k", fixtures.Floats64[0])
},
"Floats64": func(c Context) Context {
return c.Floats64("k", fixtures.Floats64)
},
"Str": func(c Context) Context {
return c.Str("k", fixtures.Strings[0])
},
"Strs": func(c Context) Context {
return c.Strs("k", fixtures.Strings)
},
"StrsV": func(c Context) Context {
return c.StrsV("k", fixtures.Strings...)
},
"Stringer": func(c Context) Context {
return c.Stringer("k", fixtures.Stringers[0])
},
"Stringers": func(c Context) Context {
return c.Stringers("k", fixtures.Stringers)
},
"StringersV": func(c Context) Context {
return c.StringersV("k", fixtures.Stringers...)
},
"Err": func(c Context) Context {
return c.Err(fixtures.Errs[0])
},
"Errs": func(c Context) Context {
return c.Errs("k", fixtures.Errs)
},
"Ctx": func(c Context) Context {
return c.Ctx(fixtures.Ctx)
},
"Time": func(c Context) Context {
return c.Time("k", fixtures.Times[0])
},
"Times": func(c Context) Context {
return c.Times("k", fixtures.Times)
},
"Dur": func(c Context) Context {
return c.Dur("k", fixtures.Durations[0])
},
"Durs": func(c Context) Context {
return c.Durs("k", fixtures.Durations)
},
"Interface": func(c Context) Context {
return c.Interface("k", fixtures.Interfaces[0])
},
"Interfaces": func(c Context) Context {
return c.Interface("k", fixtures.Interfaces)
},
"Interface(Object)": func(c Context) Context {
return c.Interface("k", fixtures.Objects[0])
},
"Interface(Objects)": func(c Context) Context {
return c.Interface("k", fixtures.Objects)
},
"Object": func(c Context) Context {
return c.Object("k", fixtures.Objects[0])
},
"Objects": func(c Context) Context {
return c.Objects("k", fixtures.Objects)
},
"ObjectsV": func(c Context) Context {
return c.ObjectsV("k", fixtures.Objects...)
},
"Timestamp": func(c Context) Context {
return c.Timestamp()
},
"IPAddr": func(c Context) Context {
return c.IPAddr("k", fixtures.IPAddrs[0])
},
"IPAddrs": func(c Context) Context {
return c.IPAddrs("k", fixtures.IPAddrs)
},
"IPPrefix": func(c Context) Context {
return c.IPPrefix("k", fixtures.IPPfxs[0])
},
"IPPrefixes": func(c Context) Context {
return c.IPPrefixes("k", fixtures.IPPfxs)
},
"MACAddr": func(c Context) Context {
return c.MACAddr("k", fixtures.MACAddr)
},
"Type": func(c Context) Context {
return c.Type("k", fixtures.Type)
},
}
logger := New(io.Discard)
b.ResetTimer()
for name := range types {
f := types[name]
b.Run(name, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l := f(logger.With()).Logger()
l.Info().Msg("")
}
})
})
}
}
+647
View File
@@ -0,0 +1,647 @@
//go:build binary_log
// +build binary_log
package zerolog
import (
"bytes"
"errors"
"fmt"
"net"
stdlog "log"
"time"
)
func ExampleLogger_With() {
dst := bytes.Buffer{}
log := New(&dst).
With().
Str("foo", "bar").
Logger()
log.Info().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"info","foo":"bar","message":"hello world"}
}
func ExampleLogger_Level() {
dst := bytes.Buffer{}
log := New(&dst).Level(WarnLevel)
log.Info().Msg("filtered out message")
log.Error().Msg("kept message")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"error","message":"kept message"}
}
func ExampleLogger_Sample() {
dst := bytes.Buffer{}
log := New(&dst).Sample(&BasicSampler{N: 2})
log.Info().Msg("message 1")
log.Info().Msg("message 2")
log.Info().Msg("message 3")
log.Info().Msg("message 4")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"info","message":"message 1"}
// {"level":"info","message":"message 3"}
}
type LevelNameHook1 struct{}
func (h LevelNameHook1) Run(e *Event, l Level, msg string) {
if l != NoLevel {
e.Str("level_name", l.String())
} else {
e.Str("level_name", "NoLevel")
}
}
type MessageHook string
func (h MessageHook) Run(e *Event, l Level, msg string) {
e.Str("the_message", msg)
}
func ExampleLogger_Hook() {
var levelNameHook LevelNameHook1
var messageHook MessageHook = "The message"
dst := bytes.Buffer{}
log := New(&dst).Hook(levelNameHook).Hook(messageHook)
log.Info().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"info","level_name":"info","the_message":"hello world","message":"hello world"}
}
func ExampleLogger_Print() {
dst := bytes.Buffer{}
log := New(&dst)
log.Print("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"debug","message":"hello world"}
}
func ExampleLogger_Printf() {
dst := bytes.Buffer{}
log := New(&dst)
log.Printf("hello %s", "world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"debug","message":"hello world"}
}
func ExampleLogger_Trace() {
dst := bytes.Buffer{}
log := New(&dst)
log.Trace().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Debug() {
dst := bytes.Buffer{}
log := New(&dst)
log.Debug().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"debug","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Info() {
dst := bytes.Buffer{}
log := New(&dst)
log.Info().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"info","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Warn() {
dst := bytes.Buffer{}
log := New(&dst)
log.Warn().
Str("foo", "bar").
Msg("a warning message")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"warn","foo":"bar","message":"a warning message"}
}
func ExampleLogger_Error() {
dst := bytes.Buffer{}
log := New(&dst)
log.Error().
Err(errors.New("some error")).
Msg("error doing something")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"error","error":"some error","message":"error doing something"}
}
func ExampleLogger_WithLevel() {
dst := bytes.Buffer{}
log := New(&dst)
log.WithLevel(InfoLevel).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"level":"info","message":"hello world"}
}
func ExampleLogger_Write() {
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Logger()
stdlog.SetFlags(0)
stdlog.SetOutput(log)
stdlog.Print("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","message":"hello world"}
}
func ExampleLogger_Log() {
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
Str("bar", "baz").
Msg("")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","bar":"baz"}
}
func ExampleEvent_Dict() {
dst := bytes.Buffer{}
log := New(&dst)
e := log.Log().
Str("foo", "bar")
e.Dict("dict", e.CreateDict().
Str("bar", "baz").
Int("n", 1),
).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
}
type User struct {
Name string
Age int
Created time.Time
}
func (u User) MarshalZerologObject(e *Event) {
e.Str("name", u.Name).
Int("age", u.Age).
Time("created", u.Created)
}
type Users []User
// User implements LogObjectMarshaler
func (uu Users) MarshalZerologArray(a *Array) {
for _, u := range uu {
a.Object(u)
}
}
func ExampleEvent_Array() {
dst := bytes.Buffer{}
log := New(&dst)
e := log.Log().
Str("foo", "bar")
e.Array("array", e.CreateArray().
Str("baz").
Int(1),
).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
}
func ExampleEvent_Array_object() {
dst := bytes.Buffer{}
log := New(&dst)
// Users implements LogArrayMarshaler
u := Users{
User{"John", 35, time.Time{}},
User{"Bob", 55, time.Time{}},
}
log.Log().
Str("foo", "bar").
Array("users", u).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleEvent_Object() {
dst := bytes.Buffer{}
log := New(&dst)
// User implements LogObjectMarshaler
u := User{"John", 35, time.Time{}}
log.Log().
Str("foo", "bar").
Object("user", u).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleContext_Objects() {
// In go, arrays are type invariant so even if you have a variable u of type []User array and User implements
// the LogObjectMarshaler interface, you cannot pass that to func that takes an []LogObjectMarshaler array in the
// Objects call. In 1.24+ it allows passing the variadic covariant slice (e.g. u...) but the unit test needs to
// work in earlier versions so we'll declare the array as []LogObjectMarshaler here.
u := []LogObjectMarshaler{User{"John", 35, time.Time{}}, User{"Bob", 55, time.Time{}}}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Objects("users", u).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleEvent_EmbedObject() {
price := Price{val: 6449, prec: 2, unit: "$"}
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
EmbedObject(price).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
func ExampleEvent_Interface() {
dst := bytes.Buffer{}
log := New(&dst)
obj := struct {
Name string `json:"name"`
}{
Name: "john",
}
log.Log().
Str("foo", "bar").
Interface("obj", obj).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
}
func ExampleEvent_Dur() {
d := time.Duration(10 * time.Second)
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
Dur("dur", d).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","dur":10000,"message":"hello world"}
}
func ExampleEvent_Durs() {
d := []time.Duration{
time.Duration(10 * time.Second),
time.Duration(20 * time.Second),
}
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
Durs("durs", d).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleEvent_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleEvent_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
dst := bytes.Buffer{}
log := New(&dst)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Dict() {
dst := bytes.Buffer{}
ctx := New(&dst).With().
Str("foo", "bar")
logger := ctx.Dict("dict", ctx.CreateDict().
Str("bar", "baz").
Int("n", 1),
).Logger()
logger.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
}
func ExampleContext_Array() {
dst := bytes.Buffer{}
ctx := New(&dst).With().
Str("foo", "bar")
logger := ctx.Array("array", ctx.CreateArray().
Str("baz").
Int(1),
).Logger()
logger.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
}
func ExampleContext_Array_object() {
// Users implements LogArrayMarshaler
u := Users{
User{"John", 35, time.Time{}},
User{"Bob", 55, time.Time{}},
}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Array("users", u).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bob","age":55,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
type Price struct {
val uint64
prec int
unit string
}
func (p Price) MarshalZerologObject(e *Event) {
denom := uint64(1)
for i := 0; i < p.prec; i++ {
denom *= 10
}
result := []byte(p.unit)
result = append(result, fmt.Sprintf("%d.%d", p.val/denom, p.val%denom)...)
e.Str("price", string(result))
}
func ExampleContext_EmbedObject() {
price := Price{val: 6449, prec: 2, unit: "$"}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
EmbedObject(price).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
func ExampleContext_Object() {
// User implements LogObjectMarshaler
u := User{"John", 35, time.Time{}}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Object("user", u).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleContext_Interface() {
obj := struct {
Name string `json:"name"`
}{
Name: "john",
}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Interface("obj", obj).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","obj":{"name":"john"},"message":"hello world"}
}
func ExampleContext_Dur() {
d := time.Duration(10 * time.Second)
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Dur("dur", d).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","dur":10000,"message":"hello world"}
}
func ExampleContext_Durs() {
d := []time.Duration{
time.Duration(10 * time.Second),
time.Duration(20 * time.Second),
}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Durs("durs", d).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleContext_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_IPAddr() {
ipV4 := net.IP{192, 168, 0, 1}
ipV6 := net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
IPAddr("v4", ipV4).
IPAddr("v6", ipV6).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","v4":"192.168.0.1","v6":"2001:db8:85a3::8a2e:370:7334","message":"hello world"}
}
func ExampleContext_IPPrefix() {
pfxV4 := net.IPNet{IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}
pfxV6 := net.IPNet{IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x00}, Mask: net.CIDRMask(64, 128)}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
IPPrefix("v4", pfxV4).
IPPrefix("v6", pfxV6).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","v4":"192.168.0.100/24","v6":"2001:db8:85a3::8a2e:370:7300/64","message":"hello world"}
}
func ExampleContext_MACAddr() {
mac := net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x90, 0xab}
dst := bytes.Buffer{}
log := New(&dst).With().
Str("foo", "bar").
MACAddr("mac", mac).
Logger()
log.Log().Msg("hello world")
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
// Output: {"foo":"bar","mac":"12:34:56:78:90:ab","message":"hello world"}
}
+39
View File
@@ -0,0 +1,39 @@
# Zerolog Lint
**DEPRECATED: In favor of https://github.com/ykadowak/zerologlint which is integrated with `go vet` and [golangci-lint](https://golangci-lint.run/).**
This is a basic linter that checks for missing log event finishers. Finds errors like: `log.Error().Int64("userID": 5)` - missing the `Msg`/`Msgf` finishers.
## Problem
When using zerolog it's easy to forget to finish the log event chain by calling a finisher - the `Msg` or `Msgf` function that will schedule the event for writing. The problem with this is that it doesn't warn/panic during compilation and it's not easily found by grep or other general tools. It's even prominently mentioned in the project's readme, that:
> It is very important to note that when using the **zerolog** chaining API, as shown above (`log.Info().Msg("hello world"`), the chain must have either the `Msg` or `Msgf` method call. If you forget to add either of these, the log will not occur and there is no compile time error to alert you of this.
## Solution
A basic linter like this one here that looks for method invocations on `zerolog.Event` can examine the last call in a method call chain and check if it is a finisher, thus pointing out these errors.
## Usage
Just compile this and then run it. Or just run it via `go run` command via something like `go run cmd/lint/lint.go`.
The command accepts only one argument - the package to be inspected - and 4 optional flags, all of which can occur multiple times. The standard synopsis of the command is:
`lint [-finisher value] [-ignoreFile value] [-ignorePkg value] [-ignorePkgRecursively value] package`
#### Flags
- finisher
- specify which finishers to accept, defaults to `Msg` and `Msgf`
- ignoreFile
- which files to ignore, either by full path or by go path (package/file.go)
- ignorePkg
- do not inspect the specified package if found in the dependency tree
- ignorePkgRecursively
- do not inspect the specified package or its subpackages if found in the dependency tree
## Drawbacks
As it is, linter can generate a false positives in a specific case. These false positives come from the fact that if you have a method that returns a `zerolog.Event` the linter will flag it because you are obviously not finishing the event. This will be solved in later release.
+5
View File
@@ -0,0 +1,5 @@
module github.com/rs/zerolog/cmd/lint
go 1.15
require golang.org/x/tools v0.1.8
+28
View File
@@ -0,0 +1,28 @@
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+175
View File
@@ -0,0 +1,175 @@
package main
import (
"flag"
"fmt"
"go/ast"
"go/token"
"go/types"
"os"
"path/filepath"
"strings"
"golang.org/x/tools/go/loader"
)
var (
recursivelyIgnoredPkgs arrayFlag
ignoredPkgs arrayFlag
ignoredFiles arrayFlag
allowedFinishers arrayFlag = []string{"Msg", "Msgf"}
rootPkg string
)
// parse input flags and args
func init() {
flag.Var(&recursivelyIgnoredPkgs, "ignorePkgRecursively", "ignore the specified package and all subpackages recursively")
flag.Var(&ignoredPkgs, "ignorePkg", "ignore the specified package")
flag.Var(&ignoredFiles, "ignoreFile", "ignore the specified file by its path and/or go path (package/file.go)")
flag.Var(&allowedFinishers, "finisher", "allowed finisher for the event chain")
flag.Parse()
// add zerolog to recursively ignored packages
recursivelyIgnoredPkgs = append(recursivelyIgnoredPkgs, "github.com/rs/zerolog")
args := flag.Args()
if len(args) != 1 {
fmt.Fprintln(os.Stderr, "you must provide exactly one package path")
os.Exit(1)
}
rootPkg = args[0]
}
func main() {
// load the package and all its dependencies
conf := loader.Config{}
conf.Import(rootPkg)
p, err := conf.Load()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: unable to load the root package. %s\n", err.Error())
os.Exit(1)
}
// get the github.com/rs/zerolog.Event type
event := getEvent(p)
if event == nil {
fmt.Fprintln(os.Stderr, "Error: github.com/rs/zerolog.Event declaration not found, maybe zerolog is not imported in the scanned package?")
os.Exit(1)
}
// get all selections (function calls) with the github.com/rs/zerolog.Event (or pointer) receiver
selections := getSelectionsWithReceiverType(p, event)
// print the violations (if any)
hasViolations := false
for _, s := range selections {
if hasBadFinisher(p, s) {
hasViolations = true
fmt.Printf("Error: missing or bad finisher for log chain, last call: %q at: %s:%v\n", s.fn.Name(), p.Fset.File(s.Pos()).Name(), p.Fset.Position(s.Pos()).Line)
}
}
// if no violations detected, return normally
if !hasViolations {
fmt.Println("No violations found")
return
}
// if violations were detected, return error code
os.Exit(1)
}
func getEvent(p *loader.Program) types.Type {
for _, pkg := range p.AllPackages {
if strings.HasSuffix(pkg.Pkg.Path(), "github.com/rs/zerolog") {
for _, d := range pkg.Defs {
if d != nil && d.Name() == "Event" {
return d.Type()
}
}
}
}
return nil
}
func getSelectionsWithReceiverType(p *loader.Program, targetType types.Type) map[token.Pos]selection {
selections := map[token.Pos]selection{}
for _, z := range p.AllPackages {
for i, t := range z.Selections {
switch o := t.Obj().(type) {
case *types.Func:
// this is not a bug, o.Type() is always *types.Signature, see docs
if vt := o.Type().(*types.Signature).Recv(); vt != nil {
typ := vt.Type()
if pointer, ok := typ.(*types.Pointer); ok {
typ = pointer.Elem()
}
if typ == targetType {
if s, ok := selections[i.Pos()]; !ok || i.End() > s.End() {
selections[i.Pos()] = selection{i, o, z.Pkg}
}
}
}
default:
// skip
}
}
}
return selections
}
func hasBadFinisher(p *loader.Program, s selection) bool {
pkgPath := strings.TrimPrefix(s.pkg.Path(), rootPkg+"/vendor/")
absoluteFilePath := strings.TrimPrefix(p.Fset.File(s.Pos()).Name(), rootPkg+"/vendor/")
goFilePath := pkgPath + "/" + filepath.Base(p.Fset.Position(s.Pos()).Filename)
for _, f := range allowedFinishers {
if f == s.fn.Name() {
return false
}
}
for _, ignoredPkg := range recursivelyIgnoredPkgs {
if strings.HasPrefix(pkgPath, ignoredPkg) {
return false
}
}
for _, ignoredPkg := range ignoredPkgs {
if pkgPath == ignoredPkg {
return false
}
}
for _, ignoredFile := range ignoredFiles {
if absoluteFilePath == ignoredFile {
return false
}
if goFilePath == ignoredFile {
return false
}
}
return true
}
type arrayFlag []string
func (i *arrayFlag) String() string {
return fmt.Sprintf("%v", []string(*i))
}
func (i *arrayFlag) Set(value string) error {
*i = append(*i, value)
return nil
}
type selection struct {
*ast.SelectorExpr
fn *types.Func
pkg *types.Package
}
+40
View File
@@ -0,0 +1,40 @@
# Zerolog PrettyLog
This is a basic CLI utility that will colorize and pretty print your structured JSON logs.
## Usage
You can compile it or run it directly. The only issue is that by default Zerolog does not output to `stdout`
but rather to `stderr` so we must pipe `stderr` stream to this CLI tool.
### Linux
These commands will redirect `stderr` to our `prettylog` tool and `stdout` will remain unaffected.
1. Compiled version
```shell
some_program_with_zerolog 2> >(prettylog)
```
2. Run it directly with `go run`
```shell
some_program_with_zerolog 2> >(go run cmd/prettylog/prettylog.go)
```
### Windows
These commands will redirect `stderr` to `stdout` and then pipe it to our `prettylog` tool.
1. Compiled version
```shell
some_program_with_zerolog 2>&1 | prettylog
```
2. Run it directly with `go run`
```shell
some_program_with_zerolog 2>&1 | go run cmd/prettylog/prettylog.go
```
+82
View File
@@ -0,0 +1,82 @@
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"os"
"time"
"github.com/rs/zerolog"
)
func isInputFromPipe() bool {
fileInfo, _ := os.Stdin.Stat()
return fileInfo.Mode()&os.ModeCharDevice == 0
}
func processInput(reader io.Reader, writer io.Writer) error {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
bytesToWrite := scanner.Bytes()
_, err := writer.Write(bytesToWrite)
if err != nil {
if errors.Is(err, io.EOF) {
break
}
fmt.Printf("%s\n", bytesToWrite)
}
}
return scanner.Err()
}
func main() {
timeFormats := map[string]string{
"default": time.Kitchen,
"full": time.RFC1123,
}
timeFormatFlag := flag.String(
"time-format",
"default",
"Time format, either 'default' or 'full'",
)
flag.Parse()
timeFormat, ok := timeFormats[*timeFormatFlag]
if !ok {
panic("Invalid time-format provided")
}
writer := zerolog.NewConsoleWriter()
writer.TimeFormat = timeFormat
if isInputFromPipe() {
_ = processInput(os.Stdin, writer)
} else if flag.NArg() >= 1 {
for _, filename := range flag.Args() {
// Scan each line from filename and write it into writer
reader, err := os.Open(filename)
if err != nil {
fmt.Printf("%s open: %v", filename, err)
os.Exit(1)
}
if err := processInput(reader, writer); err != nil {
fmt.Printf("%s scan: %v", filename, err)
os.Exit(1)
}
}
} else {
fmt.Println("Usage:")
fmt.Println(" app_with_zerolog | 2> >(prettylog)")
fmt.Println(" prettylog zerolog_output.jsonl")
os.Exit(1)
return
}
}
+535
View File
@@ -0,0 +1,535 @@
package zerolog
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/mattn/go-colorable"
)
const (
colorBlack = iota + 30
colorRed
colorGreen
colorYellow
colorBlue
colorMagenta
colorCyan
colorWhite
colorBold = 1
colorDarkGray = 90
unknownLevel = "???"
)
var (
consoleBufPool = sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 100))
},
}
)
const (
consoleDefaultTimeFormat = time.Kitchen
)
// Formatter transforms the input into a formatted string.
type Formatter func(interface{}) string
// FormatterByFieldName transforms the input into a formatted string,
// being able to differentiate formatting based on field name.
type FormatterByFieldName func(interface{}, string) string
// ConsoleWriter parses the JSON input and writes it in an
// (optionally) colorized, human-friendly format to Out.
type ConsoleWriter struct {
// Out is the output destination.
Out io.Writer
// NoColor disables the colorized output.
NoColor bool
// TimeFormat specifies the format for timestamp in output.
TimeFormat string
// TimeLocation tells ConsoleWriters default FormatTimestamp
// how to localize the time.
TimeLocation *time.Location
// PartsOrder defines the order of parts in output.
PartsOrder []string
// PartsExclude defines parts to not display in output.
PartsExclude []string
// FieldsOrder defines the order of contextual fields in output.
FieldsOrder []string
fieldIsOrdered map[string]int
// FieldsExclude defines contextual fields to not display in output.
FieldsExclude []string
FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
FormatMessage Formatter
FormatFieldName Formatter
FormatFieldValue Formatter
FormatErrFieldName Formatter
FormatErrFieldValue Formatter
// If this is configured it is used for "part" values and
// has precedence on FormatFieldValue
FormatPartValueByName FormatterByFieldName
FormatExtra func(map[string]interface{}, *bytes.Buffer) error
FormatPrepare func(map[string]interface{}) error
}
// NewConsoleWriter creates and initializes a new ConsoleWriter.
func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter {
w := ConsoleWriter{
Out: os.Stdout,
TimeFormat: consoleDefaultTimeFormat,
PartsOrder: consoleDefaultPartsOrder(),
}
for _, opt := range options {
opt(&w)
}
// Fix color on Windows
if w.Out == os.Stdout || w.Out == os.Stderr {
w.Out = colorable.NewColorable(w.Out.(*os.File))
}
return w
}
// Write transforms the JSON input with formatters and appends to w.Out.
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
// Fix color on Windows
if w.Out == os.Stdout || w.Out == os.Stderr {
w.Out = colorable.NewColorable(w.Out.(*os.File))
}
if w.PartsOrder == nil {
w.PartsOrder = consoleDefaultPartsOrder()
}
var buf = consoleBufPool.Get().(*bytes.Buffer)
defer func() {
buf.Reset()
consoleBufPool.Put(buf)
}()
var evt map[string]interface{}
p = decodeIfBinaryToBytes(p)
d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&evt)
if err != nil {
return n, fmt.Errorf("cannot decode event: %s", err)
}
if w.FormatPrepare != nil {
err = w.FormatPrepare(evt)
if err != nil {
return n, err
}
}
for _, p := range w.PartsOrder {
w.writePart(buf, evt, p)
}
w.writeFields(evt, buf)
if w.FormatExtra != nil {
err = w.FormatExtra(evt, buf)
if err != nil {
return n, err
}
}
err = buf.WriteByte('\n')
if err != nil {
return n, err
}
_, err = buf.WriteTo(w.Out)
return len(p), err
}
// Call the underlying writer's Close method if it is an io.Closer. Otherwise
// does nothing.
func (w ConsoleWriter) Close() error {
if closer, ok := w.Out.(io.Closer); ok {
return closer.Close()
}
return nil
}
// writeFields appends formatted key-value pairs to buf.
func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {
var fields = make([]string, 0, len(evt))
for field := range evt {
var isExcluded bool
for _, excluded := range w.FieldsExclude {
if field == excluded {
isExcluded = true
break
}
}
if isExcluded {
continue
}
switch field {
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
continue
}
fields = append(fields, field)
}
if len(w.FieldsOrder) > 0 {
w.orderFields(fields)
} else {
sort.Strings(fields)
}
// Write space only if something has already been written to the buffer, and if there are fields.
if buf.Len() > 0 && len(fields) > 0 {
buf.WriteByte(' ')
}
// Move the "error" field to the front
ei := sort.Search(len(fields), func(i int) bool { return fields[i] >= ErrorFieldName })
if ei < len(fields) && fields[ei] == ErrorFieldName {
fields[ei] = ""
fields = append([]string{ErrorFieldName}, fields...)
var xfields = make([]string, 0, len(fields))
for _, field := range fields {
if field == "" { // Skip empty fields
continue
}
xfields = append(xfields, field)
}
fields = xfields
}
for i, field := range fields {
var fn Formatter
var fv Formatter
if field == ErrorFieldName {
if w.FormatErrFieldName == nil {
fn = consoleDefaultFormatErrFieldName(w.NoColor)
} else {
fn = w.FormatErrFieldName
}
if w.FormatErrFieldValue == nil {
fv = consoleDefaultFormatErrFieldValue(w.NoColor)
} else {
fv = w.FormatErrFieldValue
}
} else {
if w.FormatFieldName == nil {
fn = consoleDefaultFormatFieldName(w.NoColor)
} else {
fn = w.FormatFieldName
}
if w.FormatFieldValue == nil {
fv = consoleDefaultFormatFieldValue
} else {
fv = w.FormatFieldValue
}
}
buf.WriteString(fn(field))
switch fValue := evt[field].(type) {
case string:
if needsQuote(fValue) {
buf.WriteString(fv(strconv.Quote(fValue)))
} else {
buf.WriteString(fv(fValue))
}
case json.Number:
buf.WriteString(fv(fValue))
default:
b, err := InterfaceMarshalFunc(fValue)
if err != nil {
fmt.Fprintf(buf, colorize("[error: %v]", colorRed, w.NoColor), err)
} else {
fmt.Fprint(buf, fv(b))
}
}
if i < len(fields)-1 { // Skip space for last field
buf.WriteByte(' ')
}
}
}
// writePart appends a formatted part to buf.
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
var f Formatter
var fvn FormatterByFieldName
if len(w.PartsExclude) > 0 {
for _, exclude := range w.PartsExclude {
if exclude == p {
return
}
}
}
switch p {
case LevelFieldName:
if w.FormatLevel == nil {
f = consoleDefaultFormatLevel(w.NoColor)
} else {
f = w.FormatLevel
}
case TimestampFieldName:
if w.FormatTimestamp == nil {
f = consoleDefaultFormatTimestamp(w.TimeFormat, w.TimeLocation, w.NoColor)
} else {
f = w.FormatTimestamp
}
case MessageFieldName:
if w.FormatMessage == nil {
f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
} else {
f = w.FormatMessage
}
case CallerFieldName:
if w.FormatCaller == nil {
f = consoleDefaultFormatCaller(w.NoColor)
} else {
f = w.FormatCaller
}
default:
if w.FormatPartValueByName != nil {
fvn = w.FormatPartValueByName
} else if w.FormatFieldValue != nil {
f = w.FormatFieldValue
} else {
f = consoleDefaultFormatFieldValue
}
}
var s string
if f == nil {
s = fvn(evt[p], p)
} else {
s = f(evt[p])
}
if len(s) > 0 {
if buf.Len() > 0 {
buf.WriteByte(' ') // Write space only if not the first part
}
buf.WriteString(s)
}
}
// orderFields takes an array of field names and an array representing field order
// and returns an array with any ordered fields at the beginning, in order,
// and the remaining fields after in their original order.
func (w ConsoleWriter) orderFields(fields []string) {
if w.fieldIsOrdered == nil {
w.fieldIsOrdered = make(map[string]int)
for i, fieldName := range w.FieldsOrder {
w.fieldIsOrdered[fieldName] = i
}
}
sort.Slice(fields, func(i, j int) bool {
ii, iOrdered := w.fieldIsOrdered[fields[i]]
jj, jOrdered := w.fieldIsOrdered[fields[j]]
if iOrdered && jOrdered {
return ii < jj
}
if iOrdered {
return true
}
if jOrdered {
return false
}
return fields[i] < fields[j]
})
}
// needsQuote returns true when the string s should be quoted in output.
func needsQuote(s string) bool {
for i := range s {
if s[i] < 0x20 || s[i] > 0x7e || s[i] == ' ' || s[i] == '\\' || s[i] == '"' {
return true
}
}
return false
}
// colorize returns the string s wrapped in ANSI code c, unless disabled is true or c is 0.
func colorize(s interface{}, c int, disabled bool) string {
e := os.Getenv("NO_COLOR")
if e != "" || c == 0 {
disabled = true
}
if disabled {
return fmt.Sprintf("%s", s)
}
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
}
// ----- DEFAULT FORMATTERS ---------------------------------------------------
func consoleDefaultPartsOrder() []string {
return []string{
TimestampFieldName,
LevelFieldName,
CallerFieldName,
MessageFieldName,
}
}
func consoleDefaultFormatTimestamp(timeFormat string, location *time.Location, noColor bool) Formatter {
if timeFormat == "" {
timeFormat = consoleDefaultTimeFormat
}
if location == nil {
location = time.Local
}
return func(i interface{}) string {
t := "<nil>"
switch tt := i.(type) {
case string:
ts, err := time.ParseInLocation(TimeFieldFormat, tt, location)
if err != nil {
t = tt
} else {
t = ts.In(location).Format(timeFormat)
}
case json.Number:
i, err := tt.Int64()
if err != nil {
t = tt.String()
} else {
var sec, nsec int64
switch TimeFieldFormat {
case TimeFormatUnixNano:
sec, nsec = 0, i
case TimeFormatUnixMicro:
sec, nsec = 0, int64(time.Duration(i)*time.Microsecond)
case TimeFormatUnixMs:
sec, nsec = 0, int64(time.Duration(i)*time.Millisecond)
default:
sec, nsec = i, 0
}
ts := time.Unix(sec, nsec)
t = ts.In(location).Format(timeFormat)
}
}
return colorize(t, colorDarkGray, noColor)
}
}
func stripLevel(ll string) string {
if len(ll) == 0 {
return unknownLevel
}
if len(ll) > 3 {
ll = ll[:3]
}
return strings.ToUpper(ll)
}
func consoleDefaultFormatLevel(noColor bool) Formatter {
return func(i interface{}) string {
if ll, ok := i.(string); ok {
level, _ := ParseLevel(ll)
fl, ok := FormattedLevels[level]
if ok {
return colorize(fl, LevelColors[level], noColor)
}
return stripLevel(ll)
}
if i == nil {
return unknownLevel
}
return stripLevel(fmt.Sprintf("%s", i))
}
}
func consoleDefaultFormatCaller(noColor bool) Formatter {
return func(i interface{}) string {
var c string
if cc, ok := i.(string); ok {
c = cc
}
if len(c) > 0 {
if cwd, err := os.Getwd(); err == nil {
if rel, err := filepath.Rel(cwd, c); err == nil {
c = rel
}
}
c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor)
}
return c
}
}
func consoleDefaultFormatMessage(noColor bool, level interface{}) Formatter {
return func(i interface{}) string {
if i == nil || i == "" {
return ""
}
switch level {
case LevelInfoValue, LevelWarnValue, LevelErrorValue, LevelFatalValue, LevelPanicValue:
return colorize(fmt.Sprintf("%s", i), colorBold, noColor)
default:
return fmt.Sprintf("%s", i)
}
}
}
func consoleDefaultFormatFieldName(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
}
}
func consoleDefaultFormatFieldValue(i interface{}) string {
return fmt.Sprintf("%s", i)
}
func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
}
}
func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(colorize(fmt.Sprintf("%s", i), colorBold, noColor), colorRed, noColor)
}
}
+654
View File
@@ -0,0 +1,654 @@
package zerolog_test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/rs/zerolog"
)
func ExampleConsoleWriter() {
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true})
log.Info().Str("foo", "bar").Msg("Hello World")
// Output: <nil> INF Hello World foo=bar
}
func ExampleConsoleWriter_customFormatters() {
out := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true}
out.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s|", i)) }
out.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) }
out.FormatFieldValue = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }
log := zerolog.New(out)
log.Info().Str("foo", "bar").Msg("Hello World")
// Output: <nil> INFO | Hello World foo:BAR
}
func ExampleConsoleWriter_partValueFormatter() {
out := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true,
PartsOrder: []string{"level", "one", "two", "three", "message"},
FieldsExclude: []string{"one", "two", "three"}}
out.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s", i)) }
out.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) }
out.FormatPartValueByName = func(i interface{}, s string) string {
var ret string
switch s {
case "one":
ret = strings.ToUpper(fmt.Sprintf("%s", i))
case "two":
ret = strings.ToLower(fmt.Sprintf("%s", i))
case "three":
ret = strings.ToLower(fmt.Sprintf("(%s)", i))
}
return ret
}
log := zerolog.New(out)
log.Info().Str("foo", "bar").
Str("two", "TEST_TWO").
Str("one", "test_one").
Str("three", "test_three").
Msg("Hello World")
// Output: INFO TEST_ONE test_two (test_three) Hello World foo:bar
}
func ExampleNewConsoleWriter() {
out := zerolog.NewConsoleWriter()
out.NoColor = true // For testing purposes only
log := zerolog.New(out)
log.Debug().Str("foo", "bar").Msg("Hello World")
// Output: <nil> DBG Hello World foo=bar
}
func ExampleNewConsoleWriter_customFormatters() {
out := zerolog.NewConsoleWriter(
func(w *zerolog.ConsoleWriter) {
// Customize time format
w.TimeFormat = time.RFC822
// Customize level formatting
w.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("[%-5s]", i)) }
},
)
out.NoColor = true // For testing purposes only
log := zerolog.New(out)
log.Info().Str("foo", "bar").Msg("Hello World")
// Output: <nil> [INFO ] Hello World foo=bar
}
func TestConsoleLogger(t *testing.T) {
t.Run("Numbers", func(t *testing.T) {
buf := &bytes.Buffer{}
log := zerolog.New(zerolog.ConsoleWriter{Out: buf, NoColor: true})
log.Info().
Float64("float", 1.23).
Uint64("small", 123).
Uint64("big", 1152921504606846976).
Msg("msg")
if got, want := strings.TrimSpace(buf.String()), "<nil> INF msg big=1152921504606846976 float=1.23 small=123"; got != want {
t.Errorf("\ngot:\n%s\nwant:\n%s", got, want)
}
})
}
func TestConsoleWriter(t *testing.T) {
t.Run("Default field formatter", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"foo"}}
_, err := w.Write([]byte(`{"foo": "DEFAULT"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "DEFAULT foo=DEFAULT\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Write colorized", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[33mWRN\x1b[0m \x1b[1mFoobar\x1b[0m\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("NO_COLOR = true", func(t *testing.T) {
os.Setenv("NO_COLOR", "anything")
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf}
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> WRN Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
os.Unsetenv("NO_COLOR")
})
t.Run("Write fields", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
ts := time.Unix(0, 0)
d := ts.UTC().Format(time.RFC3339)
_, err := w.Write([]byte(`{"time": "` + d + `", "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := ts.Format(time.Kitchen) + " DBG Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Unix timestamp input format", func(t *testing.T) {
of := zerolog.TimeFieldFormat
defer func() {
zerolog.TimeFieldFormat = of
}()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
_, err := w.Write([]byte(`{"time": 1234, "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := time.Unix(1234, 0).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Unix timestamp ms input format", func(t *testing.T) {
of := zerolog.TimeFieldFormat
defer func() {
zerolog.TimeFieldFormat = of
}()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
_, err := w.Write([]byte(`{"time": 1234567, "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := time.Unix(1234, 567000000).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Unix timestamp us input format", func(t *testing.T) {
of := zerolog.TimeFieldFormat
defer func() {
zerolog.TimeFieldFormat = of
}()
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, TimeFormat: time.StampMicro, NoColor: true}
_, err := w.Write([]byte(`{"time": 1234567891, "level": "debug", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := time.Unix(1234, 567891000).Format(time.StampMicro) + " DBG Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("No message field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
_, err := w.Write([]byte(`{"level": "debug", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> DBG foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("No level field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
_, err := w.Write([]byte(`{"message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> ??? Foobar foo=bar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Write colorized fields", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar", "foo": "bar"}`))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[33mWRN\x1b[0m \x1b[1mFoobar\x1b[0m \x1b[36mfoo=\x1b[0mbar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Write error field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
ts := time.Unix(0, 0)
d := ts.UTC().Format(time.RFC3339)
evt := `{"time": "` + d + `", "level": "error", "message": "Foobar", "aaa": "bbb", "error": "Error"}`
// t.Log(evt)
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := ts.Format(time.Kitchen) + " ERR Foobar error=Error aaa=bbb\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Write caller field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("Cannot get working directory: %s", err)
}
ts := time.Unix(0, 0)
d := ts.UTC().Format(time.RFC3339)
fields := map[string]interface{}{
"time": d,
"level": "debug",
"message": "Foobar",
"foo": "bar",
"caller": filepath.Join(cwd, "foo", "bar.go"),
}
evt, err := json.Marshal(fields)
if err != nil {
t.Fatalf("Cannot marshal fields: %s", err)
}
_, err = w.Write(evt)
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
// Define the expected output with forward slashes
expectedOutput := ts.Format(time.Kitchen) + " DBG foo/bar.go > Foobar foo=bar\n"
// Get the actual output and normalize path separators to forward slashes
actualOutput := buf.String()
actualOutput = strings.ReplaceAll(actualOutput, string(os.PathSeparator), "/")
// Compare the normalized actual output to the expected output
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Write JSON field", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
evt := `{"level": "debug", "message": "Foobar", "foo": [1, 2, 3], "bar": true}`
// t.Log(evt)
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> DBG Foobar bar=true foo=[1,2,3]\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("With an extra 'level' field", func(t *testing.T) {
t.Run("malformed string", func(t *testing.T) {
cases := []struct {
field string
output string
}{
{"", "<nil> ??? Hello World foo=bar\n"},
{"-", "<nil> - Hello World foo=bar\n"},
{"1", "<nil> " + zerolog.FormattedLevels[1] + " Hello World foo=bar\n"},
{"a", "<nil> A Hello World foo=bar\n"},
{"12", "<nil> 12 Hello World foo=bar\n"},
{"a2", "<nil> A2 Hello World foo=bar\n"},
{"2a", "<nil> 2A Hello World foo=bar\n"},
{"ab", "<nil> AB Hello World foo=bar\n"},
{"12a", "<nil> 12A Hello World foo=bar\n"},
{"a12", "<nil> A12 Hello World foo=bar\n"},
{"abc", "<nil> ABC Hello World foo=bar\n"},
{"123", "<nil> 123 Hello World foo=bar\n"},
{"abcd", "<nil> ABC Hello World foo=bar\n"},
{"1234", "<nil> 123 Hello World foo=bar\n"},
{"123d", "<nil> 123 Hello World foo=bar\n"},
{"01", "<nil> " + zerolog.FormattedLevels[1] + " Hello World foo=bar\n"},
{"001", "<nil> " + zerolog.FormattedLevels[1] + " Hello World foo=bar\n"},
{"0001", "<nil> " + zerolog.FormattedLevels[1] + " Hello World foo=bar\n"},
}
for i, c := range cases {
c := c
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
buf := &bytes.Buffer{}
out := zerolog.NewConsoleWriter()
out.NoColor = true
out.Out = buf
log := zerolog.New(out)
log.Debug().Str("level", c.field).Str("foo", "bar").Msg("Hello World")
actualOutput := buf.String()
if actualOutput != c.output {
t.Errorf("Unexpected output %q, want: %q", actualOutput, c.output)
}
})
}
})
t.Run("weird value", func(t *testing.T) {
cases := []struct {
field interface{}
output string
}{
{0, "<nil> 0 Hello World foo=bar\n"},
{1, "<nil> 1 Hello World foo=bar\n"},
{-1, "<nil> -1 Hello World foo=bar\n"},
{-3, "<nil> -3 Hello World foo=bar\n"},
{-32, "<nil> -32 Hello World foo=bar\n"},
{-321, "<nil> -32 Hello World foo=bar\n"},
{12, "<nil> 12 Hello World foo=bar\n"},
{123, "<nil> 123 Hello World foo=bar\n"},
{1234, "<nil> 123 Hello World foo=bar\n"},
}
for i, c := range cases {
c := c
t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
buf := &bytes.Buffer{}
out := zerolog.NewConsoleWriter()
out.NoColor = true
out.Out = buf
log := zerolog.New(out)
log.Debug().Interface("level", c.field).Str("foo", "bar").Msg("Hello World")
actualOutput := buf.String()
if actualOutput != c.output {
t.Errorf("Unexpected output %q, want: %q", actualOutput, c.output)
}
})
}
})
})
}
func TestConsoleWriterConfiguration(t *testing.T) {
t.Run("Sets TimeFormat", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: time.RFC3339}
ts := time.Unix(0, 0)
d := ts.UTC().Format(time.RFC3339)
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := ts.Format(time.RFC3339) + " INF Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets TimeFormat and TimeLocation", func(t *testing.T) {
locs := []*time.Location{time.Local, time.UTC}
for _, location := range locs {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{
Out: buf,
NoColor: true,
TimeFormat: time.RFC3339,
TimeLocation: location,
}
ts := time.Unix(0, 0)
d := ts.UTC().Format(time.RFC3339)
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := ts.In(location).Format(time.RFC3339) + " INF Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q (location=%s)", actualOutput, expectedOutput, location)
}
}
})
t.Run("Sets PartsOrder", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}
evt := `{"level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "Foobar INF\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets PartsExclude", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsExclude: []string{"time"}}
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "INF Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets FieldsOrder", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, FieldsOrder: []string{"zebra", "aardvark"}}
evt := `{"level": "info", "message": "Zoo", "aardvark": "Able", "mussel": "Mountain", "zebra": "Zulu"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> INF Zoo zebra=Zulu aardvark=Able mussel=Mountain\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets FieldsExclude", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, FieldsExclude: []string{"foo"}}
evt := `{"level": "info", "message": "Foobar", "foo":"bar", "baz":"quux"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "<nil> INF Foobar baz=quux\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets FormatExtra", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{
Out: buf, NoColor: true, PartsOrder: []string{"level", "message"},
FormatExtra: func(evt map[string]interface{}, buf *bytes.Buffer) error {
buf.WriteString("\nAdditional stacktrace")
return nil
},
}
evt := `{"level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "INF Foobar\nAdditional stacktrace\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Sets FormatPrepare", func(t *testing.T) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{
Out: buf, NoColor: true, PartsOrder: []string{"level", "message"},
FormatPrepare: func(evt map[string]interface{}) error {
evt["message"] = fmt.Sprintf("msg=%s", evt["message"])
return nil
},
}
evt := `{"level": "info", "message": "Foobar"}`
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
expectedOutput := "INF msg=Foobar\n"
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
t.Run("Uses local time for console writer without time zone", func(t *testing.T) {
// Regression test for issue #483 (check there for more details)
timeFormat := "2006-01-02 15:04:05"
expectedOutput := "2022-10-20 20:24:50 INF Foobar\n"
evt := `{"time": "2022-10-20 20:24:50", "level": "info", "message": "Foobar"}`
of := zerolog.TimeFieldFormat
defer func() {
zerolog.TimeFieldFormat = of
}()
zerolog.TimeFieldFormat = timeFormat
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: timeFormat}
_, err := w.Write([]byte(evt))
if err != nil {
t.Errorf("Unexpected error when writing output: %s", err)
}
actualOutput := buf.String()
if actualOutput != expectedOutput {
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
}
})
}
func BenchmarkConsoleWriter(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
var msg = []byte(`{"level": "info", "foo": "bar", "message": "HELLO", "time": "1990-01-01"}`)
w := zerolog.ConsoleWriter{Out: io.Discard, NoColor: false}
for i := 0; i < b.N; i++ {
w.Write(msg)
}
}
+312 -77
View File
@@ -1,10 +1,11 @@
package zerolog
import (
"io/ioutil"
"context"
"fmt"
"math"
"net"
"time"
"github.com/rs/zerolog/internal/json"
)
// Context configures a new sub-logger with contextual fields.
@@ -17,284 +18,518 @@ func (c Context) Logger() Logger {
return c.l
}
// Fields is a helper function to use a map to set fields using type assertion.
func (c Context) Fields(fields map[string]interface{}) Context {
c.l.context = appendFields(c.l.context, fields)
// Fields is a helper function to use a map or slice to set fields using type assertion.
// Only map[string]interface{} and []interface{} are accepted. []interface{} must
// alternate string keys and arbitrary values, and extraneous ones are ignored.
func (c Context) Fields(fields interface{}) Context {
c.l.context = appendFields(c.l.context, fields, c.l.stack, c.l.ctx, c.l.hooks)
return c
}
// Dict adds the field key with the dict to the logger context.
func (c Context) Dict(key string, dict *Event) Context {
dict.buf = append(dict.buf, '}')
c.l.context = append(json.AppendKey(c.l.context, key), dict.buf...)
eventPool.Put(dict)
dict.buf = enc.AppendEndMarker(dict.buf)
c.l.context = append(enc.AppendKey(c.l.context, key), dict.buf...)
putEvent(dict)
return c
}
// CreateDict creates an Event to be used with the Context.Dict method.
// It preserves the stack, hooks, and context from the logger.
// Call usual field methods like Str, Int etc to add fields to this
// event and give it as argument the Context.Dict method.
func (c Context) CreateDict() *Event {
return newEvent(nil, DebugLevel, c.l.stack, c.l.ctx, c.l.hooks)
}
// CreateArray creates an Array to be used with the Context.Array method.
// It preserves the stack, hooks, and context from the logger.
// Call usual field methods like Str, Int etc to add elements to this
// array and give it as argument the Context.Array method.
func (c Context) CreateArray() *Array {
a := Arr()
a.stack = c.l.stack
a.ctx = c.l.ctx
a.ch = c.l.hooks
return a
}
// Array adds the field key with an array to the event context.
// Use zerolog.Arr() to create the array or pass a type that
// Use c.CreateArray() to create the array or pass a type that
// implement the LogArrayMarshaler interface.
func (c Context) Array(key string, arr LogArrayMarshaler) Context {
c.l.context = json.AppendKey(c.l.context, key)
c.l.context = enc.AppendKey(c.l.context, key)
if arr, ok := arr.(*Array); ok {
c.l.context = arr.write(c.l.context)
return c
}
var a *Array
if aa, ok := arr.(*Array); ok {
a = aa
} else {
a = Arr()
arr.MarshalZerologArray(a)
}
a := c.CreateArray()
arr.MarshalZerologArray(a)
c.l.context = a.write(c.l.context)
return c
}
// Object marshals an object that implement the LogObjectMarshaler interface.
func (c Context) Object(key string, obj LogObjectMarshaler) Context {
e := newEvent(levelWriterAdapter{ioutil.Discard}, 0, true)
e := c.l.scratchEvent()
e.Object(key, obj)
e.buf[0] = ',' // A new event starts as an object, we want to embed it.
c.l.context = append(c.l.context, e.buf...)
eventPool.Put(e)
c.l.context = enc.AppendObjectData(c.l.context, e.buf)
putEvent(e)
return c
}
// Objects adds the field key with objs to the logger context as an array of
// objects that implement the LogObjectMarshaler interface.
//
// This is the array version that accepts a slice of LogObjectMarshaler objects.
func (c Context) Objects(key string, objs []LogObjectMarshaler) Context {
e := c.l.scratchEvent()
e.Objects(key, objs)
c.l.context = enc.AppendObjectData(c.l.context, e.buf)
putEvent(e)
return c
}
// ObjectsV adds the field key with objs to the logger context as an array of
// objects that implement the LogObjectMarshaler interface.
//
// This is a variadic version that accepts a list of individual LogObjectMarshaler objects.
func (c Context) ObjectsV(key string, objs ...LogObjectMarshaler) Context {
return c.Objects(key, objs)
}
// EmbedObject marshals and Embeds an object that implement the LogObjectMarshaler interface.
func (c Context) EmbedObject(obj LogObjectMarshaler) Context {
e := c.l.scratchEvent()
e.EmbedObject(obj)
c.l.context = enc.AppendObjectData(c.l.context, e.buf)
putEvent(e)
return c
}
// Str adds the field key with val as a string to the logger context.
func (c Context) Str(key, val string) Context {
c.l.context = json.AppendString(json.AppendKey(c.l.context, key), val)
c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val)
return c
}
// Strs adds the field key with val as a string to the logger context.
//
// This is the array version that accepts a slice of string values.
func (c Context) Strs(key string, vals []string) Context {
c.l.context = json.AppendStrings(json.AppendKey(c.l.context, key), vals)
c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals)
return c
}
// StrsV adds the field key with vals as a []string to the logger context.
//
// This is a variadic version that accepts a list of individual strings.
func (c Context) StrsV(key string, vals ...string) Context {
return c.Strs(key, vals)
}
// Stringer adds the field key with val.String() (or null if val is nil) to the logger context.
func (c Context) Stringer(key string, val fmt.Stringer) Context {
if val != nil {
c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val.String())
return c
}
c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), nil)
return c
}
// Stringers adds the field key with vals to the logger context where each
// individual val is added by calling val.String().
//
// This is the array version that accepts a slice of fmt.Stringer values.
func (c Context) Stringers(key string, vals []fmt.Stringer) Context {
c.l.context = enc.AppendStringers(enc.AppendKey(c.l.context, key), vals)
return c
}
// StringersV adds the field key with vals to the logger context where each
// individual val is added by calling val.String().
//
// This is a variadic version that accepts a list of individual
// fmt.Stringer values.
func (c Context) StringersV(key string, vals ...fmt.Stringer) Context {
return c.Stringers(key, vals)
}
// Bytes adds the field key with val as a []byte to the logger context.
func (c Context) Bytes(key string, val []byte) Context {
c.l.context = json.AppendBytes(json.AppendKey(c.l.context, key), val)
c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val)
return c
}
// AnErr adds the field key with err as a string to the logger context.
// Hex adds the field key with val as a hex string to the logger context.
func (c Context) Hex(key string, val []byte) Context {
c.l.context = enc.AppendHex(enc.AppendKey(c.l.context, key), val)
return c
}
// RawJSON adds already encoded JSON to context.
//
// No sanity check is performed on b; it must not contain carriage returns and
// be valid JSON.
func (c Context) RawJSON(key string, b []byte) Context {
c.l.context = appendJSON(enc.AppendKey(c.l.context, key), b)
return c
}
// AnErr adds the field key with serialized err to the logger context.
// If err is nil, no field is added.
func (c Context) AnErr(key string, err error) Context {
if err != nil {
c.l.context = json.AppendError(json.AppendKey(c.l.context, key), err)
switch m := ErrorMarshalFunc(err).(type) {
case nil:
return c
case LogObjectMarshaler:
return c.Object(key, m)
case error:
if isNilValue(m) {
return c
}
return c.Str(key, m.Error())
case string:
return c.Str(key, m)
default:
return c.Interface(key, m)
}
return c
}
// Errs adds the field key with errs as an array of strings to the logger context.
// Errs adds the field key with errs as an array of serialized errors to the
// logger context.
func (c Context) Errs(key string, errs []error) Context {
c.l.context = json.AppendErrors(json.AppendKey(c.l.context, key), errs)
return c
arr := c.CreateArray().Errs(errs)
return c.Array(key, arr)
}
// Err adds the field "error" with err as a string to the logger context.
// To customize the key name, change zerolog.ErrorFieldName.
// Err adds the field "error" with serialized err to the logger context.
func (c Context) Err(err error) Context {
if err != nil {
c.l.context = json.AppendError(json.AppendKey(c.l.context, ErrorFieldName), err)
if c.l.stack && ErrorStackMarshaler != nil {
switch m := ErrorStackMarshaler(err).(type) {
case nil:
return c // do nothing with nil errors
case LogObjectMarshaler:
c = c.Object(ErrorStackFieldName, m)
case error:
c = c.Str(ErrorStackFieldName, m.Error())
case string:
c = c.Str(ErrorStackFieldName, m)
default:
c = c.Interface(ErrorStackFieldName, m)
}
}
return c.AnErr(ErrorFieldName, err)
}
// Ctx adds the context.Context to the logger context. The context.Context is
// not rendered in the error message, but is made available for hooks to use.
// A typical use case is to extract tracing information from the
// context.Context.
func (c Context) Ctx(ctx context.Context) Context {
c.l.ctx = ctx
return c
}
// Bool adds the field key with val as a bool to the logger context.
func (c Context) Bool(key string, b bool) Context {
c.l.context = json.AppendBool(json.AppendKey(c.l.context, key), b)
c.l.context = enc.AppendBool(enc.AppendKey(c.l.context, key), b)
return c
}
// Bools adds the field key with val as a []bool to the logger context.
func (c Context) Bools(key string, b []bool) Context {
c.l.context = json.AppendBools(json.AppendKey(c.l.context, key), b)
c.l.context = enc.AppendBools(enc.AppendKey(c.l.context, key), b)
return c
}
// Int adds the field key with i as a int to the logger context.
func (c Context) Int(key string, i int) Context {
c.l.context = json.AppendInt(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInt(enc.AppendKey(c.l.context, key), i)
return c
}
// Ints adds the field key with i as a []int to the logger context.
func (c Context) Ints(key string, i []int) Context {
c.l.context = json.AppendInts(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInts(enc.AppendKey(c.l.context, key), i)
return c
}
// Int8 adds the field key with i as a int8 to the logger context.
func (c Context) Int8(key string, i int8) Context {
c.l.context = json.AppendInt8(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInt8(enc.AppendKey(c.l.context, key), i)
return c
}
// Ints8 adds the field key with i as a []int8 to the logger context.
func (c Context) Ints8(key string, i []int8) Context {
c.l.context = json.AppendInts8(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInts8(enc.AppendKey(c.l.context, key), i)
return c
}
// Int16 adds the field key with i as a int16 to the logger context.
func (c Context) Int16(key string, i int16) Context {
c.l.context = json.AppendInt16(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInt16(enc.AppendKey(c.l.context, key), i)
return c
}
// Ints16 adds the field key with i as a []int16 to the logger context.
func (c Context) Ints16(key string, i []int16) Context {
c.l.context = json.AppendInts16(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInts16(enc.AppendKey(c.l.context, key), i)
return c
}
// Int32 adds the field key with i as a int32 to the logger context.
func (c Context) Int32(key string, i int32) Context {
c.l.context = json.AppendInt32(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInt32(enc.AppendKey(c.l.context, key), i)
return c
}
// Ints32 adds the field key with i as a []int32 to the logger context.
func (c Context) Ints32(key string, i []int32) Context {
c.l.context = json.AppendInts32(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInts32(enc.AppendKey(c.l.context, key), i)
return c
}
// Int64 adds the field key with i as a int64 to the logger context.
func (c Context) Int64(key string, i int64) Context {
c.l.context = json.AppendInt64(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInt64(enc.AppendKey(c.l.context, key), i)
return c
}
// Ints64 adds the field key with i as a []int64 to the logger context.
func (c Context) Ints64(key string, i []int64) Context {
c.l.context = json.AppendInts64(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendInts64(enc.AppendKey(c.l.context, key), i)
return c
}
// Uint adds the field key with i as a uint to the logger context.
func (c Context) Uint(key string, i uint) Context {
c.l.context = json.AppendUint(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUint(enc.AppendKey(c.l.context, key), i)
return c
}
// Uints adds the field key with i as a []uint to the logger context.
func (c Context) Uints(key string, i []uint) Context {
c.l.context = json.AppendUints(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUints(enc.AppendKey(c.l.context, key), i)
return c
}
// Uint8 adds the field key with i as a uint8 to the logger context.
func (c Context) Uint8(key string, i uint8) Context {
c.l.context = json.AppendUint8(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUint8(enc.AppendKey(c.l.context, key), i)
return c
}
// Uints8 adds the field key with i as a []uint8 to the logger context.
func (c Context) Uints8(key string, i []uint8) Context {
c.l.context = json.AppendUints8(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUints8(enc.AppendKey(c.l.context, key), i)
return c
}
// Uint16 adds the field key with i as a uint16 to the logger context.
func (c Context) Uint16(key string, i uint16) Context {
c.l.context = json.AppendUint16(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUint16(enc.AppendKey(c.l.context, key), i)
return c
}
// Uints16 adds the field key with i as a []uint16 to the logger context.
func (c Context) Uints16(key string, i []uint16) Context {
c.l.context = json.AppendUints16(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUints16(enc.AppendKey(c.l.context, key), i)
return c
}
// Uint32 adds the field key with i as a uint32 to the logger context.
func (c Context) Uint32(key string, i uint32) Context {
c.l.context = json.AppendUint32(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUint32(enc.AppendKey(c.l.context, key), i)
return c
}
// Uints32 adds the field key with i as a []uint32 to the logger context.
func (c Context) Uints32(key string, i []uint32) Context {
c.l.context = json.AppendUints32(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUints32(enc.AppendKey(c.l.context, key), i)
return c
}
// Uint64 adds the field key with i as a uint64 to the logger context.
func (c Context) Uint64(key string, i uint64) Context {
c.l.context = json.AppendUint64(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUint64(enc.AppendKey(c.l.context, key), i)
return c
}
// Uints64 adds the field key with i as a []uint64 to the logger context.
func (c Context) Uints64(key string, i []uint64) Context {
c.l.context = json.AppendUints64(json.AppendKey(c.l.context, key), i)
c.l.context = enc.AppendUints64(enc.AppendKey(c.l.context, key), i)
return c
}
// Float32 adds the field key with f as a float32 to the logger context.
func (c Context) Float32(key string, f float32) Context {
c.l.context = json.AppendFloat32(json.AppendKey(c.l.context, key), f)
c.l.context = enc.AppendFloat32(enc.AppendKey(c.l.context, key), f, FloatingPointPrecision)
return c
}
// Floats32 adds the field key with f as a []float32 to the logger context.
func (c Context) Floats32(key string, f []float32) Context {
c.l.context = json.AppendFloats32(json.AppendKey(c.l.context, key), f)
c.l.context = enc.AppendFloats32(enc.AppendKey(c.l.context, key), f, FloatingPointPrecision)
return c
}
// Float64 adds the field key with f as a float64 to the logger context.
func (c Context) Float64(key string, f float64) Context {
c.l.context = json.AppendFloat64(json.AppendKey(c.l.context, key), f)
c.l.context = enc.AppendFloat64(enc.AppendKey(c.l.context, key), f, FloatingPointPrecision)
return c
}
// Floats64 adds the field key with f as a []float64 to the logger context.
func (c Context) Floats64(key string, f []float64) Context {
c.l.context = json.AppendFloats64(json.AppendKey(c.l.context, key), f)
c.l.context = enc.AppendFloats64(enc.AppendKey(c.l.context, key), f, FloatingPointPrecision)
return c
}
// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key.
type timestampHook struct{}
func (ts timestampHook) Run(e *Event, level Level, msg string) {
e.Timestamp()
}
var th = timestampHook{}
// Timestamp adds the current local time to the logger context with the "time" key, formatted using zerolog.TimeFieldFormat.
// To customize the key name, change zerolog.TimestampFieldName.
// To customize the time format, change zerolog.TimeFieldFormat.
//
// NOTE: It won't dedupe the "time" key if the *Context has one already.
func (c Context) Timestamp() Context {
if len(c.l.context) > 0 {
c.l.context[0] = 1
} else {
c.l.context = append(c.l.context, 1)
}
c.l = c.l.Hook(th)
return c
}
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
// Time adds the field key with t formatted as string using zerolog.TimeFieldFormat.
func (c Context) Time(key string, t time.Time) Context {
c.l.context = json.AppendTime(json.AppendKey(c.l.context, key), t, TimeFieldFormat)
c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
return c
}
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
// Times adds the field key with t formatted as string using zerolog.TimeFieldFormat.
func (c Context) Times(key string, t []time.Time) Context {
c.l.context = json.AppendTimes(json.AppendKey(c.l.context, key), t, TimeFieldFormat)
c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
return c
}
// Dur adds the fields key with d divided by unit and stored as a float.
// Dur adds the field key with d divided by unit and stored as a float.
func (c Context) Dur(key string, d time.Duration) Context {
c.l.context = json.AppendDuration(json.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return c
}
// Durs adds the fields key with d divided by unit and stored as a float.
// Durs adds the field key with d divided by unit and stored as a float.
func (c Context) Durs(key string, d []time.Duration) Context {
c.l.context = json.AppendDurations(json.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
return c
}
// Interface adds the field key with obj marshaled using reflection.
func (c Context) Interface(key string, i interface{}) Context {
c.l.context = json.AppendInterface(json.AppendKey(c.l.context, key), i)
if obj, ok := i.(LogObjectMarshaler); ok {
return c.Object(key, obj)
}
c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), i)
return c
}
// Type adds the field key with val's type using reflection.
func (c Context) Type(key string, val interface{}) Context {
c.l.context = enc.AppendType(enc.AppendKey(c.l.context, key), val)
return c
}
// Any is a wrapper around Context.Interface.
func (c Context) Any(key string, i interface{}) Context {
return c.Interface(key, i)
}
// Reset removes all the context fields.
func (c Context) Reset() Context {
c.l.context = enc.AppendBeginMarker(make([]byte, 0, 500))
return c
}
type callerHook struct {
callerSkipFrameCount int
}
func newCallerHook(skipFrameCount int) callerHook {
return callerHook{callerSkipFrameCount: skipFrameCount}
}
func (ch callerHook) Run(e *Event, level Level, msg string) {
switch ch.callerSkipFrameCount {
case useGlobalSkipFrameCount:
// Extra frames to skip (added by hook infra).
e.caller(CallerSkipFrameCount + contextCallerSkipFrameCount)
default:
// Extra frames to skip (added by hook infra).
e.caller(ch.callerSkipFrameCount + contextCallerSkipFrameCount)
}
}
// useGlobalSkipFrameCount acts as a flag to informat callerHook.Run
// to use the global CallerSkipFrameCount.
const useGlobalSkipFrameCount = math.MinInt32
// ch is the default caller hook using the global CallerSkipFrameCount.
var ch = newCallerHook(useGlobalSkipFrameCount)
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
func (c Context) Caller() Context {
c.l = c.l.Hook(ch)
return c
}
// CallerWithSkipFrameCount adds the file:line of the caller with the zerolog.CallerFieldName key.
// The specified skipFrameCount int will override the global CallerSkipFrameCount for this context's respective logger.
// If set to -1 the global CallerSkipFrameCount will be used.
func (c Context) CallerWithSkipFrameCount(skipFrameCount int) Context {
c.l = c.l.Hook(newCallerHook(skipFrameCount))
return c
}
// Stack enables stack trace printing for the error passed to Err().
func (c Context) Stack() Context {
c.l.stack = true
return c
}
// IPAddr adds adds the field key with ip as a net.IP IPv4 or IPv6 Address to the context
func (c Context) IPAddr(key string, ip net.IP) Context {
c.l.context = enc.AppendIPAddr(enc.AppendKey(c.l.context, key), ip)
return c
}
// IPAddrs adds the field key with ip as a []net.IP array of IPv4 or IPv6 Address to the context
func (c Context) IPAddrs(key string, ip []net.IP) Context {
c.l.context = enc.AppendIPAddrs(enc.AppendKey(c.l.context, key), ip)
return c
}
// IPPrefix adds adds the field key with pfx as a []net.IPNet IPv4 or IPv6 Prefix (address and mask) to the context
func (c Context) IPPrefix(key string, pfx net.IPNet) Context {
c.l.context = enc.AppendIPPrefix(enc.AppendKey(c.l.context, key), pfx)
return c
}
// IPPrefix adds adds the field key with pfx as a []net.IPNet array of IPv4 or IPv6 Prefix (address and mask) to the context
func (c Context) IPPrefixes(key string, pfx []net.IPNet) Context {
c.l.context = enc.AppendIPPrefixes(enc.AppendKey(c.l.context, key), pfx)
return c
}
// MACAddr adds adds the field key with ha as a net.HardwareAddr MAC address to the context
func (c Context) MACAddr(key string, ha net.HardwareAddr) Context {
c.l.context = enc.AppendMACAddr(enc.AppendKey(c.l.context, key), ha)
return c
}
+143
View File
@@ -0,0 +1,143 @@
package zerolog
import (
"bytes"
"errors"
"testing"
)
type myError struct{}
func (e *myError) Error() string { return "test" }
func TestContext_ErrWithStackMarshaler(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler
ErrorStackMarshaler = func(err error) interface{} {
return "stack-trace"
}
var buf bytes.Buffer
log := New(&buf).With().Stack().Err(errors.New("test error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","stack":"stack-trace","error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Context.Err() with stack marshaler = %q, want %q", got, want)
}
}
func TestContext_AnErrWithNilErrorMarshal(t *testing.T) {
// Save original
original := ErrorMarshalFunc
defer func() { ErrorMarshalFunc = original }()
// Set marshaler to return a nil error pointer
ErrorMarshalFunc = func(err error) interface{} {
return (*myError)(nil) // nil pointer of error type
}
var buf bytes.Buffer
log := New(&buf).With().AnErr("test", errors.New("some error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","message":"test message"}` + "\n" // No "test" field because isNilValue returned true
if got != want {
t.Errorf("Context.AnErr() with nil error marshal = %q, want %q", got, want)
}
}
func TestContext_ErrWithNilStackMarshaler(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set marshaler to return nil
ErrorStackMarshaler = func(err error) interface{} {
return nil
}
var buf bytes.Buffer
log := New(&buf).With().Stack().Err(errors.New("test error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","message":"test message"}` + "\n" // No stack or error field because stack marshaler returned nil
if got != want {
t.Errorf("Context.Err() with nil stack marshaler = %q, want %q", got, want)
}
}
func TestContext_ErrWithStackMarshalerObject(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns LogObjectMarshaler
ErrorStackMarshaler = func(err error) interface{} {
return logObjectMarshalerImpl{name: "user", age: 30}
}
var buf bytes.Buffer
log := New(&buf).With().Stack().Err(errors.New("test error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","stack":{"name":"user","age":-30},"error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Context.Err() with stack marshaler object = %q, want %q", got, want)
}
}
func TestContext_ErrWithStackMarshalerError(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an error
ErrorStackMarshaler = func(err error) interface{} {
return errors.New("stack error")
}
var buf bytes.Buffer
log := New(&buf).With().Stack().Err(errors.New("test error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","stack":"stack error","error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Context.Err() with stack marshaler error = %q, want %q", got, want)
}
}
func TestContext_ErrWithStackMarshalerInterface(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an int
ErrorStackMarshaler = func(err error) interface{} {
return 42
}
var buf bytes.Buffer
log := New(&buf).With().Stack().Err(errors.New("test error")).Logger()
log.Info().Msg("test message")
got := decodeIfBinaryToString(buf.Bytes())
want := `{"level":"info","stack":42,"error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Context.Err() with stack marshaler interface = %q, want %q", got, want)
}
}
+31 -9
View File
@@ -2,28 +2,50 @@ package zerolog
import (
"context"
"io/ioutil"
)
var disabledLogger = New(ioutil.Discard).Level(Disabled)
var disabledLogger *Logger
func init() {
SetGlobalLevel(TraceLevel)
l := Nop()
disabledLogger = &l
}
type ctxKey struct{}
// WithContext returns a copy of ctx with l associated.
// WithContext returns a copy of ctx with the receiver attached. The Logger
// attached to the provided Context (if any) will not be effected. If the
// receiver's log level is Disabled it will only be attached to the returned
// Context if the provided Context has a previously attached Logger. If the
// provided Context has no attached Logger, a Disabled Logger will not be
// attached.
//
// Note: to modify the existing Logger attached to a Context (instead of
// replacing it in a new Context), use UpdateContext with the following
// notation:
//
// ctx := r.Context()
// l := zerolog.Ctx(ctx)
// l.UpdateContext(func(c Context) Context {
// return c.Str("bar", "baz")
// })
func (l Logger) WithContext(ctx context.Context) context.Context {
if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
// Update existing pointer.
*lp = l
if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
// Do not store disabled logger.
return ctx
}
return context.WithValue(ctx, ctxKey{}, &l)
}
// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) Logger {
// is associated, DefaultContextLogger is returned, unless DefaultContextLogger
// is nil, in which case a disabled logger is returned.
func Ctx(ctx context.Context) *Logger {
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
return *l
return l
} else if l = DefaultContextLogger; l != nil {
return l
}
return disabledLogger
}
+77 -5
View File
@@ -1,17 +1,21 @@
package zerolog
import (
"bytes"
"context"
"io/ioutil"
"io"
"reflect"
"strings"
"testing"
"github.com/rs/zerolog/internal/cbor"
)
func TestCtx(t *testing.T) {
log := New(ioutil.Discard)
log := New(io.Discard)
ctx := log.WithContext(context.Background())
log2 := Ctx(ctx)
if !reflect.DeepEqual(log, log2) {
if !reflect.DeepEqual(log, *log2) {
t.Error("Ctx did not return the expected logger")
}
@@ -19,12 +23,80 @@ func TestCtx(t *testing.T) {
log = log.Level(InfoLevel)
ctx = log.WithContext(ctx)
log2 = Ctx(ctx)
if !reflect.DeepEqual(log, log2) {
if !reflect.DeepEqual(log, *log2) {
t.Error("Ctx did not return the expected logger")
}
log2 = Ctx(context.Background())
if !reflect.DeepEqual(log2, disabledLogger) {
if log2 != disabledLogger {
t.Error("Ctx did not return the expected logger")
}
DefaultContextLogger = &log
t.Cleanup(func() { DefaultContextLogger = nil })
log2 = Ctx(context.Background())
if log2 != &log {
t.Error("Ctx did not return the expected logger")
}
}
func TestCtxDisabled(t *testing.T) {
dl := New(io.Discard).Level(Disabled)
ctx := dl.WithContext(context.Background())
if ctx != context.Background() {
t.Error("WithContext stored a disabled logger")
}
l := New(io.Discard).With().Str("foo", "bar").Logger()
ctx = l.WithContext(ctx)
if !reflect.DeepEqual(Ctx(ctx), &l) {
t.Error("WithContext did not store logger")
}
l.UpdateContext(func(c Context) Context {
return c.Str("bar", "baz")
})
ctx = l.WithContext(ctx)
if !reflect.DeepEqual(Ctx(ctx), &l) {
t.Error("WithContext did not store updated logger")
}
l = l.Level(DebugLevel)
ctx = l.WithContext(ctx)
if !reflect.DeepEqual(Ctx(ctx), &l) {
t.Error("WithContext did not store copied logger")
}
ctx = dl.WithContext(ctx)
if !reflect.DeepEqual(Ctx(ctx), &dl) {
t.Error("WithContext did not override logger with a disabled logger")
}
}
type logObjectMarshalerImpl struct {
name string
age int
}
func (t logObjectMarshalerImpl) MarshalZerologObject(e *Event) {
e.Str("name", strings.ToLower(t.name)).Int("age", -t.age)
}
func Test_InterfaceLogObjectMarshaler(t *testing.T) {
var buf bytes.Buffer
log := New(&buf)
ctx := log.WithContext(context.Background())
log2 := Ctx(ctx)
withLog := log2.With().Interface("obj", &logObjectMarshalerImpl{
name: "FOO",
age: 29,
}).Logger()
withLog.Info().Msg("test")
if got, want := cbor.DecodeIfBinaryToString(buf.Bytes()), `{"level":"info","obj":{"name":"foo","age":-29},"message":"test"}`+"\n"; got != want {
t.Errorf("got %q, want %q", got, want)
}
}
+114
View File
@@ -0,0 +1,114 @@
// Package diode provides a thread-safe, lock-free, non-blocking io.Writer
// wrapper.
package diode
import (
"context"
"io"
"sync"
"time"
"github.com/rs/zerolog/diode/internal/diodes"
)
var bufPool = &sync.Pool{
New: func() interface{} {
return make([]byte, 0, 500)
},
}
type Alerter func(missed int)
type diodeFetcher interface {
diodes.Diode
Next() diodes.GenericDataType
}
// Writer is a io.Writer wrapper that uses a diode to make Write lock-free,
// non-blocking and thread safe.
type Writer struct {
w io.Writer
d diodeFetcher
c context.CancelFunc
done chan struct{}
}
// NewWriter creates a writer wrapping w with a many-to-one diode in order to
// never block log producers and drop events if the writer can't keep up with
// the flow of data.
//
// Use a diode.Writer when
//
// wr := diode.NewWriter(w, 1000, 0, func(missed int) {
// log.Printf("Dropped %d messages", missed)
// })
// log := zerolog.New(wr)
//
// If pollInterval is greater than 0, a poller is used otherwise a waiter is
// used.
//
// See code.cloudfoundry.org/go-diodes for more info on diode.
func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Writer {
ctx, cancel := context.WithCancel(context.Background())
dw := Writer{
w: w,
c: cancel,
done: make(chan struct{}),
}
if f == nil {
f = func(int) {}
}
d := diodes.NewManyToOne(size, diodes.AlertFunc(f))
if pollInterval > 0 {
dw.d = diodes.NewPoller(d,
diodes.WithPollingInterval(pollInterval),
diodes.WithPollingContext(ctx))
} else {
dw.d = diodes.NewWaiter(d,
diodes.WithWaiterContext(ctx))
}
go dw.poll()
return dw
}
func (dw Writer) Write(p []byte) (n int, err error) {
// p is pooled in zerolog so we can't hold it passed this call, hence the
// copy.
p = append(bufPool.Get().([]byte), p...)
dw.d.Set(diodes.GenericDataType(&p))
return len(p), nil
}
// Close releases the diode poller and call Close on the wrapped writer if
// io.Closer is implemented.
func (dw Writer) Close() error {
dw.c()
<-dw.done
if w, ok := dw.w.(io.Closer); ok {
return w.Close()
}
return nil
}
func (dw Writer) poll() {
defer close(dw.done)
for {
d := dw.d.Next()
if d == nil {
return
}
p := *(*[]byte)(d)
dw.w.Write(p)
// Proper usage of a sync.Pool requires each entry to have approximately
// the same memory cost. To obtain this property when the stored type
// contains a variably-sized buffer, we add a hard limit on the maximum buffer
// to place back in the pool.
//
// See https://golang.org/issue/23199
const maxSize = 1 << 16 // 64KiB
if cap(p) <= maxSize {
bufPool.Put(p[:0])
}
}
}
+23
View File
@@ -0,0 +1,23 @@
// +build !binary_log
package diode_test
import (
"fmt"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/diode"
)
func ExampleNewWriter() {
w := diode.NewWriter(os.Stdout, 1000, 0, func(missed int) {
fmt.Printf("Dropped %d messages\n", missed)
})
log := zerolog.New(w)
log.Print("test")
w.Close()
// Output: {"level":"debug","message":"test"}
}
+171
View File
@@ -0,0 +1,171 @@
package diode_test
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"sync"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/diode"
"github.com/rs/zerolog/internal/cbor"
)
func TestNewWriter(t *testing.T) {
buf := bytes.Buffer{}
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
fmt.Printf("Dropped %d messages\n", missed)
})
log := zerolog.New(w)
log.Print("test")
w.Close()
want := "{\"level\":\"debug\",\"message\":\"test\"}\n"
got := cbor.DecodeIfBinaryToString(buf.Bytes())
if got != want {
t.Errorf("Diode New Writer Test failed. got:%s, want:%s!", got, want)
}
}
func TestClose(t *testing.T) {
buf := bytes.Buffer{}
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {})
log := zerolog.New(w)
log.Print("test")
w.Close()
}
func TestFatal(t *testing.T) {
if os.Getenv("TEST_FATAL") == "1" {
w := diode.NewWriter(os.Stderr, 1000, 0, func(missed int) {
fmt.Printf("Dropped %d messages\n", missed)
})
defer w.Close()
log := zerolog.New(w)
log.Fatal().Msg("test")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
cmd.Env = append(os.Environ(), "TEST_FATAL=1")
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
var stderrBuf bytes.Buffer
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
if _, err := io.Copy(&stderrBuf, stderr); err != nil {
t.Errorf("failed to copy stderr: %v", err)
}
}()
err = cmd.Wait()
if err == nil {
t.Error("Expected log.Fatal to exit with non-zero status")
}
wg.Wait() // Wait for the goroutine to finish copying
slurp := stderrBuf.Bytes()
want := "{\"level\":\"fatal\",\"message\":\"test\"}\n"
got := cbor.DecodeIfBinaryToString(slurp)
if got != want {
t.Errorf("Diode Fatal Test failed. got:%s, want:%s!", got, want)
}
}
type SlowWriter struct{}
func (rw *SlowWriter) Write(p []byte) (n int, err error) {
time.Sleep(200 * time.Millisecond)
fmt.Print(string(p))
return len(p), nil
}
func TestFatalWithFilteredLevelWriter(t *testing.T) {
if os.Getenv("TEST_FATAL_SLOW") == "1" {
slowWriter := SlowWriter{}
diodeWriter := diode.NewWriter(&slowWriter, 500, 0, func(missed int) {
fmt.Printf("Missed %d logs\n", missed)
})
leveledDiodeWriter := zerolog.LevelWriterAdapter{
Writer: &diodeWriter,
}
filteredDiodeWriter := zerolog.FilteredLevelWriter{
Writer: &leveledDiodeWriter,
Level: zerolog.InfoLevel,
}
logger := zerolog.New(&filteredDiodeWriter)
logger.Fatal().Msg("test")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestFatalWithFilteredLevelWriter")
cmd.Env = append(os.Environ(), "TEST_FATAL_SLOW=1")
stdout, err := cmd.StdoutPipe()
if err != nil {
t.Fatal(err)
}
err = cmd.Start()
if err != nil {
t.Fatal(err)
}
var stdoutBuf bytes.Buffer
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
_, _ = io.Copy(&stdoutBuf, stdout)
}()
err = cmd.Wait()
if err == nil {
t.Error("Expected log.Fatal to exit with non-zero status")
}
wg.Wait() // Wait for the goroutine to finish copying
slurp := stdoutBuf.Bytes()
got := cbor.DecodeIfBinaryToString(slurp)
want := "{\"level\":\"fatal\",\"message\":\"test\"}\n"
if got != want {
t.Errorf("Expected output %q, got: %q", want, got)
}
}
func Benchmark(b *testing.B) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stderr)
benchs := map[string]time.Duration{
"Waiter": 0,
"Pooler": 10 * time.Millisecond,
}
for name, interval := range benchs {
b.Run(name, func(b *testing.B) {
w := diode.NewWriter(io.Discard, 100000, interval, nil)
log := zerolog.New(w)
defer w.Close()
b.SetParallelism(1000)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Print("test")
}
})
})
}
}
+1
View File
@@ -0,0 +1 @@
Copied from https://github.com/cloudfoundry/go-diodes to avoid test dependencies.
+130
View File
@@ -0,0 +1,130 @@
package diodes
import (
"log"
"sync/atomic"
"unsafe"
)
// ManyToOne diode is optimal for many writers (go-routines B-n) and a single
// reader (go-routine A). It is not thread safe for multiple readers.
type ManyToOne struct {
writeIndex uint64
readIndex uint64
buffer []unsafe.Pointer
alerter Alerter
}
// NewManyToOne creates a new diode (ring buffer). The ManyToOne diode
// is optimized for many writers (on go-routines B-n) and a single reader
// (on go-routine A). The alerter is invoked on the read's go-routine. It is
// called when it notices that the writer go-routine has passed it and wrote
// over data. A nil can be used to ignore alerts.
func NewManyToOne(size int, alerter Alerter) *ManyToOne {
if alerter == nil {
alerter = AlertFunc(func(int) {})
}
d := &ManyToOne{
buffer: make([]unsafe.Pointer, size),
alerter: alerter,
}
// Start write index at the value before 0
// to allow the first write to use AddUint64
// and still have a beginning index of 0
d.writeIndex = ^d.writeIndex
return d
}
// Set sets the data in the next slot of the ring buffer.
func (d *ManyToOne) Set(data GenericDataType) {
for {
writeIndex := atomic.AddUint64(&d.writeIndex, 1)
idx := writeIndex % uint64(len(d.buffer))
old := atomic.LoadPointer(&d.buffer[idx])
if old != nil &&
(*bucket)(old) != nil &&
(*bucket)(old).seq > writeIndex-uint64(len(d.buffer)) {
log.Println("Diode set collision: consider using a larger diode")
continue
}
newBucket := &bucket{
data: data,
seq: writeIndex,
}
if !atomic.CompareAndSwapPointer(&d.buffer[idx], old, unsafe.Pointer(newBucket)) {
log.Println("Diode set collision: consider using a larger diode")
continue
}
return
}
}
// TryNext will attempt to read from the next slot of the ring buffer.
// If there is no data available, it will return (nil, false).
func (d *ManyToOne) TryNext() (data GenericDataType, ok bool) {
// Read a value from the ring buffer based on the readIndex.
idx := d.readIndex % uint64(len(d.buffer))
result := (*bucket)(atomic.SwapPointer(&d.buffer[idx], nil))
// When the result is nil that means the writer has not had the
// opportunity to write a value into the diode. This value must be ignored
// and the read head must not increment.
if result == nil {
return nil, false
}
// When the seq value is less than the current read index that means a
// value was read from idx that was previously written but since has
// been dropped. This value must be ignored and the read head must not
// increment.
//
// The simulation for this scenario assumes the fast forward occurred as
// detailed below.
//
// 5. The reader reads again getting seq 5. It then reads again expecting
// seq 6 but gets seq 2. This is a read of a stale value that was
// effectively "dropped" so the read fails and the read head stays put.
// `| 4 | 5 | 2 | 3 |` r: 7, w: 6
//
if result.seq < d.readIndex {
return nil, false
}
// When the seq value is greater than the current read index that means a
// value was read from idx that overwrote the value that was expected to
// be at this idx. This happens when the writer has lapped the reader. The
// reader needs to catch up to the writer so it moves its write head to
// the new seq, effectively dropping the messages that were not read in
// between the two values.
//
// Here is a simulation of this scenario:
//
// 1. Both the read and write heads start at 0.
// `| nil | nil | nil | nil |` r: 0, w: 0
// 2. The writer fills the buffer.
// `| 0 | 1 | 2 | 3 |` r: 0, w: 4
// 3. The writer laps the read head.
// `| 4 | 5 | 2 | 3 |` r: 0, w: 6
// 4. The reader reads the first value, expecting a seq of 0 but reads 4,
// this forces the reader to fast forward to 5.
// `| 4 | 5 | 2 | 3 |` r: 5, w: 6
//
if result.seq > d.readIndex {
dropped := result.seq - d.readIndex
d.readIndex = result.seq
d.alerter.Alert(int(dropped))
}
// Only increment read index if a regular read occurred (where seq was
// equal to readIndex) or a value was read that caused a fast forward
// (where seq was greater than readIndex).
//
d.readIndex++
return result.data, true
}
+129
View File
@@ -0,0 +1,129 @@
package diodes
import (
"sync/atomic"
"unsafe"
)
// GenericDataType is the data type the diodes operate on.
type GenericDataType unsafe.Pointer
// Alerter is used to report how many values were overwritten since the
// last write.
type Alerter interface {
Alert(missed int)
}
// AlertFunc type is an adapter to allow the use of ordinary functions as
// Alert handlers.
type AlertFunc func(missed int)
// Alert calls f(missed)
func (f AlertFunc) Alert(missed int) {
f(missed)
}
type bucket struct {
data GenericDataType
seq uint64 // seq is the recorded write index at the time of writing
}
// OneToOne diode is meant to be used by a single reader and a single writer.
// It is not thread safe if used otherwise.
type OneToOne struct {
writeIndex uint64
readIndex uint64
buffer []unsafe.Pointer
alerter Alerter
}
// NewOneToOne creates a new diode is meant to be used by a single reader and
// a single writer. The alerter is invoked on the read's go-routine. It is
// called when it notices that the writer go-routine has passed it and wrote
// over data. A nil can be used to ignore alerts.
func NewOneToOne(size int, alerter Alerter) *OneToOne {
if alerter == nil {
alerter = AlertFunc(func(int) {})
}
return &OneToOne{
buffer: make([]unsafe.Pointer, size),
alerter: alerter,
}
}
// Set sets the data in the next slot of the ring buffer.
func (d *OneToOne) Set(data GenericDataType) {
idx := d.writeIndex % uint64(len(d.buffer))
newBucket := &bucket{
data: data,
seq: d.writeIndex,
}
d.writeIndex++
atomic.StorePointer(&d.buffer[idx], unsafe.Pointer(newBucket))
}
// TryNext will attempt to read from the next slot of the ring buffer.
// If there is no data available, it will return (nil, false).
func (d *OneToOne) TryNext() (data GenericDataType, ok bool) {
// Read a value from the ring buffer based on the readIndex.
idx := d.readIndex % uint64(len(d.buffer))
result := (*bucket)(atomic.SwapPointer(&d.buffer[idx], nil))
// When the result is nil that means the writer has not had the
// opportunity to write a value into the diode. This value must be ignored
// and the read head must not increment.
if result == nil {
return nil, false
}
// When the seq value is less than the current read index that means a
// value was read from idx that was previously written but since has
// been dropped. This value must be ignored and the read head must not
// increment.
//
// The simulation for this scenario assumes the fast forward occurred as
// detailed below.
//
// 5. The reader reads again getting seq 5. It then reads again expecting
// seq 6 but gets seq 2. This is a read of a stale value that was
// effectively "dropped" so the read fails and the read head stays put.
// `| 4 | 5 | 2 | 3 |` r: 7, w: 6
//
if result.seq < d.readIndex {
return nil, false
}
// When the seq value is greater than the current read index that means a
// value was read from idx that overwrote the value that was expected to
// be at this idx. This happens when the writer has lapped the reader. The
// reader needs to catch up to the writer so it moves its write head to
// the new seq, effectively dropping the messages that were not read in
// between the two values.
//
// Here is a simulation of this scenario:
//
// 1. Both the read and write heads start at 0.
// `| nil | nil | nil | nil |` r: 0, w: 0
// 2. The writer fills the buffer.
// `| 0 | 1 | 2 | 3 |` r: 0, w: 4
// 3. The writer laps the read head.
// `| 4 | 5 | 2 | 3 |` r: 0, w: 6
// 4. The reader reads the first value, expecting a seq of 0 but reads 4,
// this forces the reader to fast forward to 5.
// `| 4 | 5 | 2 | 3 |` r: 5, w: 6
//
if result.seq > d.readIndex {
dropped := result.seq - d.readIndex
d.readIndex = result.seq
d.alerter.Alert(int(dropped))
}
// Only increment read index if a regular read occurred (where seq was
// equal to readIndex) or a value was read that caused a fast forward
// (where seq was greater than readIndex).
d.readIndex++
return result.data, true
}
+80
View File
@@ -0,0 +1,80 @@
package diodes
import (
"context"
"time"
)
// Diode is any implementation of a diode.
type Diode interface {
Set(GenericDataType)
TryNext() (GenericDataType, bool)
}
// Poller will poll a diode until a value is available.
type Poller struct {
Diode
interval time.Duration
ctx context.Context
}
// PollerConfigOption can be used to setup the poller.
type PollerConfigOption func(*Poller)
// WithPollingInterval sets the interval at which the diode is queried
// for new data. The default is 10ms.
func WithPollingInterval(interval time.Duration) PollerConfigOption {
return func(c *Poller) {
c.interval = interval
}
}
// WithPollingContext sets the context to cancel any retrieval (Next()). It
// will not change any results for adding data (Set()). Default is
// context.Background().
func WithPollingContext(ctx context.Context) PollerConfigOption {
return func(c *Poller) {
c.ctx = ctx
}
}
// NewPoller returns a new Poller that wraps the given diode.
func NewPoller(d Diode, opts ...PollerConfigOption) *Poller {
p := &Poller{
Diode: d,
interval: 10 * time.Millisecond,
ctx: context.Background(),
}
for _, o := range opts {
o(p)
}
return p
}
// Next polls the diode until data is available or until the context is done.
// If the context is done, then nil will be returned.
func (p *Poller) Next() GenericDataType {
for {
data, ok := p.Diode.TryNext()
if !ok {
if p.isDone() {
return nil
}
time.Sleep(p.interval)
continue
}
return data
}
}
func (p *Poller) isDone() bool {
select {
case <-p.ctx.Done():
return true
default:
return false
}
}
+88
View File
@@ -0,0 +1,88 @@
package diodes
import (
"context"
"sync"
)
// Waiter will use a conditional mutex to alert the reader to when data is
// available.
type Waiter struct {
Diode
mu sync.Mutex
c *sync.Cond
ctx context.Context
}
// WaiterConfigOption can be used to setup the waiter.
type WaiterConfigOption func(*Waiter)
// WithWaiterContext sets the context to cancel any retrieval (Next()). It
// will not change any results for adding data (Set()). Default is
// context.Background().
func WithWaiterContext(ctx context.Context) WaiterConfigOption {
return func(c *Waiter) {
c.ctx = ctx
}
}
// NewWaiter returns a new Waiter that wraps the given diode.
func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter {
w := new(Waiter)
w.Diode = d
w.c = sync.NewCond(&w.mu)
w.ctx = context.Background()
for _, opt := range opts {
opt(w)
}
go func() {
<-w.ctx.Done()
// Mutex is strictly necessary here to avoid a race in Next() (between
// w.isDone() and w.c.Wait()) and w.c.Broadcast() here.
w.mu.Lock()
w.c.Broadcast()
w.mu.Unlock()
}()
return w
}
// Set invokes the wrapped diode's Set with the given data and uses Broadcast
// to wake up any readers.
func (w *Waiter) Set(data GenericDataType) {
w.Diode.Set(data)
w.c.Broadcast()
}
// Next returns the next data point on the wrapped diode. If there is not any
// new data, it will Wait for set to be called or the context to be done.
// If the context is done, then nil will be returned.
func (w *Waiter) Next() GenericDataType {
w.mu.Lock()
defer w.mu.Unlock()
for {
data, ok := w.Diode.TryNext()
if !ok {
if w.isDone() {
return nil
}
w.c.Wait()
continue
}
return data
}
}
func (w *Waiter) isDone() bool {
select {
case <-w.ctx.Done():
return true
default:
return false
}
}
+56
View File
@@ -0,0 +1,56 @@
package zerolog
import (
"net"
"time"
)
type encoder interface {
AppendArrayDelim(dst []byte) []byte
AppendArrayEnd(dst []byte) []byte
AppendArrayStart(dst []byte) []byte
AppendBeginMarker(dst []byte) []byte
AppendBool(dst []byte, val bool) []byte
AppendBools(dst []byte, vals []bool) []byte
AppendBytes(dst, s []byte) []byte
AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte
AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte
AppendEndMarker(dst []byte) []byte
AppendFloat32(dst []byte, val float32, precision int) []byte
AppendFloat64(dst []byte, val float64, precision int) []byte
AppendFloats32(dst []byte, vals []float32, precision int) []byte
AppendFloats64(dst []byte, vals []float64, precision int) []byte
AppendHex(dst, s []byte) []byte
AppendIPAddr(dst []byte, ip net.IP) []byte
AppendIPPrefix(dst []byte, pfx net.IPNet) []byte
AppendInt(dst []byte, val int) []byte
AppendInt16(dst []byte, val int16) []byte
AppendInt32(dst []byte, val int32) []byte
AppendInt64(dst []byte, val int64) []byte
AppendInt8(dst []byte, val int8) []byte
AppendInterface(dst []byte, i interface{}) []byte
AppendInts(dst []byte, vals []int) []byte
AppendInts16(dst []byte, vals []int16) []byte
AppendInts32(dst []byte, vals []int32) []byte
AppendInts64(dst []byte, vals []int64) []byte
AppendInts8(dst []byte, vals []int8) []byte
AppendKey(dst []byte, key string) []byte
AppendLineBreak(dst []byte) []byte
AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte
AppendNil(dst []byte) []byte
AppendObjectData(dst []byte, o []byte) []byte
AppendString(dst []byte, s string) []byte
AppendStrings(dst []byte, vals []string) []byte
AppendTime(dst []byte, t time.Time, format string) []byte
AppendTimes(dst []byte, vals []time.Time, format string) []byte
AppendUint(dst []byte, val uint) []byte
AppendUint16(dst []byte, val uint16) []byte
AppendUint32(dst []byte, val uint32) []byte
AppendUint64(dst []byte, val uint64) []byte
AppendUint8(dst []byte, val uint8) []byte
AppendUints(dst []byte, vals []uint) []byte
AppendUints16(dst []byte, vals []uint16) []byte
AppendUints32(dst []byte, vals []uint32) []byte
AppendUints64(dst []byte, vals []uint64) []byte
AppendUints8(dst []byte, vals []uint8) []byte
}
+45
View File
@@ -0,0 +1,45 @@
// +build binary_log
package zerolog
// This file contains bindings to do binary encoding.
import (
"github.com/rs/zerolog/internal/cbor"
)
var (
_ encoder = (*cbor.Encoder)(nil)
enc = cbor.Encoder{}
)
func init() {
// using closure to reflect the changes at runtime.
cbor.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return InterfaceMarshalFunc(v)
}
}
func appendJSON(dst []byte, j []byte) []byte {
return cbor.AppendEmbeddedJSON(dst, j)
}
func appendCBOR(dst []byte, c []byte) []byte {
return cbor.AppendEmbeddedCBOR(dst, c)
}
// decodeIfBinaryToString - converts a binary formatted log msg to a
// JSON formatted String Log message.
func decodeIfBinaryToString(in []byte) string {
return cbor.DecodeIfBinaryToString(in)
}
func decodeObjectToStr(in []byte) string {
return cbor.DecodeObjectToStr(in)
}
// decodeIfBinaryToBytes - converts a binary formatted log msg to a
// JSON formatted Bytes Log message.
func decodeIfBinaryToBytes(in []byte) []byte {
return cbor.DecodeIfBinaryToBytes(in)
}
+51
View File
@@ -0,0 +1,51 @@
// +build !binary_log
package zerolog
// encoder_json.go file contains bindings to generate
// JSON encoded byte stream.
import (
"encoding/base64"
"github.com/rs/zerolog/internal/json"
)
var (
_ encoder = (*json.Encoder)(nil)
enc = json.Encoder{}
)
func init() {
// using closure to reflect the changes at runtime.
json.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return InterfaceMarshalFunc(v)
}
}
func appendJSON(dst []byte, j []byte) []byte {
return append(dst, j...)
}
func appendCBOR(dst []byte, cbor []byte) []byte {
dst = append(dst, []byte("\"data:application/cbor;base64,")...)
l := len(dst)
enc := base64.StdEncoding
n := enc.EncodedLen(len(cbor))
for i := 0; i < n; i++ {
dst = append(dst, '.')
}
enc.Encode(dst[l:], cbor)
return append(dst, '"')
}
func decodeIfBinaryToString(in []byte) string {
return string(in)
}
func decodeObjectToStr(in []byte) string {
return string(in)
}
func decodeIfBinaryToBytes(in []byte) []byte {
return in
}
+224
View File
@@ -0,0 +1,224 @@
package zerolog
import (
"bytes"
"fmt"
"strings"
"testing"
)
type loggableError struct {
error
}
func (l loggableError) MarshalZerologObject(e *Event) {
if l.error == nil {
return
}
e.Str("l", strings.ToUpper(l.error.Error()))
}
type nonLoggableError struct {
error
line int
}
type wrappedError struct {
error
msg string
}
func (w wrappedError) Error() string {
if w.error == nil {
return w.msg
}
return w.error.Error() + ": " + w.msg
}
type interfaceError struct {
val string
}
func TestArrayErrorMarshalFunc(t *testing.T) {
prefixed := func(s, prefix string) string {
if s == "null" {
return ""
}
return prefix + s + `,`
}
errs := []error{
nil,
fmt.Errorf("failure"),
loggableError{fmt.Errorf("whoops")},
nonLoggableError{fmt.Errorf("oops"), 402},
}
type testCase struct {
name string
marshal func(err error) interface{}
want []string
}
testCases := []testCase{
{
name: "default",
marshal: nil,
want: []string{`null`, `"failure"`, `{"l":"WHOOPS"}`, `"oops"`},
},
{
name: "string",
marshal: func(err error) interface{} {
if err == nil {
return nil
}
return err.Error()
},
want: []string{`null`, `"failure"`, `"whoops"`, `"oops"`},
},
{
name: "loggable",
marshal: func(err error) interface{} {
if err == nil {
return nil
}
return loggableError{err}
},
want: []string{`null`, `{"l":"FAILURE"}`, `{"l":"WHOOPS"}`, `{"l":"OOPS"}`},
},
{
name: "non-loggable",
marshal: func(err error) interface{} {
if err == nil {
return nil
}
return nonLoggableError{err, 404}
},
want: []string{`null`, `"failure"`, `"whoops"`, `"oops"`},
},
{
name: "interface",
marshal: func(err error) interface{} {
var some interfaceError
if err != nil {
some.val = err.Error()
}
var interfaceErr interface{} = some
return interfaceErr
},
want: []string{`{}`, `{}`, `{}`, `{}`},
},
{
name: "nilError",
marshal: func(err error) interface{} {
var errNil error = nil
return errNil
},
want: []string{`null`, `null`, `null`, `null`},
},
{
name: "wrapped error",
marshal: func(err error) interface{} {
if err == nil {
return nil
} else if we, ok := err.(wrappedError); ok {
return we
} else {
return wrappedError{err, "addendum"}
}
},
want: []string{`null`, `"failure: addendum"`, `"whoops: addendum"`, `"oops: addendum"`},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
originalErrorMarshalFunc := ErrorMarshalFunc
defer func() {
ErrorMarshalFunc = originalErrorMarshalFunc
}()
if tc.marshal != nil {
ErrorMarshalFunc = tc.marshal
}
t.Run("Err", func(t *testing.T) {
for i, err := range errs {
want := tc.want[i]
t.Run("Arr", func(t *testing.T) {
wants := `[` + want + `]`
a := Arr().Err(err)
if got := decodeObjectToStr(a.write([]byte{})); got != wants {
t.Errorf("%s %d Array.Err(%v)\ngot: %s\nwant: %s", tc.name, i, err, got, wants)
}
})
t.Run("Ctx", func(t *testing.T) {
wants := `{` + prefixed(want, `"error":`) + `"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out).With().Err(err).Logger()
logger.Log().Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s %d Ctx.Err(%v)\ngot: %v\nwant: %v", tc.name, i, err, got, wants)
}
})
t.Run("Event", func(t *testing.T) {
wants := `{` + prefixed(want, `"error":`) + `"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out)
logger.Log().Err(err).Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s %d Event.Err(%v)\ngot: %v\nwant: %v", tc.name, i, err, got, wants)
}
})
t.Run("Fields", func(t *testing.T) {
if i == 0 && tc.want[i] == "{}" {
want = `null`
}
wants := `{"err":` + want + `,"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out)
logger.Log().Fields(map[string]interface{}{"err": err}).Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s %d Event.Fields(%v)\ngot: %v\nwant: %v", tc.name, i, err, got, wants)
}
})
}
})
t.Run("Errs", func(t *testing.T) {
want := `[` + strings.Join(tc.want, ",") + `]`
t.Run("Arr", func(t *testing.T) {
a := Arr().Errs(errs)
if got := decodeObjectToStr(a.write([]byte{})); got != want {
t.Errorf("%s Array.Errs()\ngot: %s\nwant: %s", tc.name, got, want)
}
})
t.Run("Ctx", func(t *testing.T) {
wants := `{"e":` + want + `,"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out).With().Errs("e", errs).Logger()
logger.Log().Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s Ctx.Errs()\ngot: %v\nwant: %v", tc.name, got, wants)
}
})
t.Run("Event", func(t *testing.T) {
wants := `{"e":` + want + `,"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out)
logger.Log().Errs("e", errs).Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s Ctx.Errs()\ngot: %v\nwant: %v", tc.name, got, wants)
}
})
t.Run("Fields", func(t *testing.T) {
wants := `{"e":` + want + `,"message":"msg"}` + "\n"
out := &bytes.Buffer{}
logger := New(out)
logger.Log().Fields(map[string]interface{}{"e": errs}).Msg("msg")
if got := decodeIfBinaryToString(out.Bytes()); got != wants {
t.Errorf("%s Ctx.Errs()\ngot: %v\nwant: %v", tc.name, got, wants)
}
})
})
})
}
}
+520 -156
View File
File diff suppressed because it is too large Load Diff
+733
View File
@@ -0,0 +1,733 @@
//go:build !binary_log
// +build !binary_log
package zerolog
import (
"bytes"
"context"
"errors"
"io"
"os"
"strings"
"testing"
)
type nilError struct{}
func (nilError) Error() string {
return "nope"
}
func TestEvent_AnErr(t *testing.T) {
tests := []struct {
name string
err error
want string
}{
{"nil", nil, `{}`},
{"error", errors.New("test"), `{"err":"test"}`},
{"nil interface", func() *nilError { return nil }(), `{}`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
e = e.AnErr("err", tt.err)
err := e.write()
if err != nil {
t.Errorf("Event.AnErr() error: %v", err)
}
if got, want := strings.TrimSpace(buf.String()), tt.want; got != want {
t.Errorf("Event.AnErr() = %v, want %v", got, want)
}
})
}
}
func TestEvent_writeWithNil(t *testing.T) {
var e *Event = nil
got := e.write()
var want *Event = nil
if got != nil {
t.Errorf("Event.write() = %v, want %v", got, want)
}
}
type loggableObject struct {
member string
}
func (o loggableObject) MarshalZerologObject(e *Event) {
e.Str("member", o.member)
}
func TestEvent_Object(t *testing.T) {
t.Run("ObjectWithNil", func(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
e = e.Object("obj", nil)
err := e.write()
if err != nil {
t.Errorf("Event.Object() error: %v", err)
}
want := `{"obj":null}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.Object()\ngot: %s\nwant: %s", got, want)
}
})
t.Run("EmbedObjectWithNil", func(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
e = e.EmbedObject(nil)
err := e.write()
if err != nil {
t.Errorf("Event.EmbedObject() error: %v", err)
}
want := "{}"
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.EmbedObject()\ngot: %s\nwant: %s", got, want)
}
})
type contextKeyType struct{}
var contextKey = contextKeyType{}
called := false
ctxHook := HookFunc(func(e *Event, level Level, message string) {
called = true
ctx := e.GetCtx()
if ctx == nil {
t.Errorf("expected context to be set in Event")
}
val := ctx.Value(contextKey)
if val == nil {
t.Errorf("expected context value, got %v", val)
}
e.Str("ctxValue", val.(string))
e.Bool("stackValue", e.stack)
})
t.Run("ObjectWithFullContext", func(t *testing.T) {
called = false
ctx := context.WithValue(context.Background(), contextKey, "ctx-object")
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, true, ctx, []Hook{ctxHook})
e = e.Object("obj", loggableObject{member: "object-value"})
e.Msg("hello")
if !called {
t.Errorf("hook was not called")
}
want := `{"obj":{"member":"object-value"},"ctxValue":"ctx-object","stackValue":true,"message":"hello"}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.EmbedObject()\ngot: %s\nwant: %s", got, want)
}
})
t.Run("EmbedObjectWithFullContext", func(t *testing.T) {
called = false
ctx := context.WithValue(context.Background(), contextKey, "ctx-embed")
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, ctx, []Hook{ctxHook})
e = e.EmbedObject(loggableObject{member: "embedded-value"})
e.Msg("hello")
if !called {
t.Errorf("hook was not called")
}
want := `{"member":"embedded-value","ctxValue":"ctx-embed","stackValue":false,"message":"hello"}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.EmbedObject()\ngot: %s\nwant: %s", got, want)
}
})
}
func TestEvent_WithNilEvent(t *testing.T) {
// coverage for nil Event receiver for all types
var e *Event = nil
fixtures := makeFieldFixtures()
types := map[string]func() *Event{
"Array": func() *Event {
arr := e.CreateArray()
return e.Array("k", arr)
},
"Bool": func() *Event {
return e.Bool("k", fixtures.Bools[0])
},
"Bools": func() *Event {
return e.Bools("k", fixtures.Bools)
},
"Fields": func() *Event {
return e.Fields(fixtures)
},
"Int": func() *Event {
return e.Int("k", fixtures.Ints[0])
},
"Ints": func() *Event {
return e.Ints("k", fixtures.Ints)
},
"Int8": func() *Event {
return e.Int8("k", fixtures.Ints8[0])
},
"Ints8": func() *Event {
return e.Ints8("k", fixtures.Ints8)
},
"Int16": func() *Event {
return e.Int16("k", fixtures.Ints16[0])
},
"Ints16": func() *Event {
return e.Ints16("k", fixtures.Ints16)
},
"Int32": func() *Event {
return e.Int32("k", fixtures.Ints32[0])
},
"Ints32": func() *Event {
return e.Ints32("k", fixtures.Ints32)
},
"Int64": func() *Event {
return e.Int64("k", fixtures.Ints64[0])
},
"Ints64": func() *Event {
return e.Ints64("k", fixtures.Ints64)
},
"Uint": func() *Event {
return e.Uint("k", fixtures.Uints[0])
},
"Uints": func() *Event {
return e.Uints("k", fixtures.Uints)
},
"Uint8": func() *Event {
return e.Uint8("k", fixtures.Uints8[0])
},
"Uints8": func() *Event {
return e.Uints8("k", fixtures.Uints8)
},
"Uint16": func() *Event {
return e.Uint16("k", fixtures.Uints16[0])
},
"Uints16": func() *Event {
return e.Uints16("k", fixtures.Uints16)
},
"Uint32": func() *Event {
return e.Uint32("k", fixtures.Uints32[0])
},
"Uints32": func() *Event {
return e.Uints32("k", fixtures.Uints32)
},
"Uint64": func() *Event {
return e.Uint64("k", fixtures.Uints64[0])
},
"Uints64": func() *Event {
return e.Uints64("k", fixtures.Uints64)
},
"Float64": func() *Event {
return e.Float64("k", fixtures.Floats64[0])
},
"Floats64": func() *Event {
return e.Floats64("k", fixtures.Floats64)
},
"Float32": func() *Event {
return e.Float32("k", fixtures.Floats32[0])
},
"Floats32": func() *Event {
return e.Floats32("k", fixtures.Floats32)
},
"RawCBOR": func() *Event {
return e.RawCBOR("k", fixtures.RawCBOR)
},
"RawJSON": func() *Event {
return e.RawJSON("k", fixtures.RawJSONs[0])
},
"Str": func() *Event {
return e.Str("k", fixtures.Strings[0])
},
"Strs": func() *Event {
return e.Strs("k", fixtures.Strings)
},
"StrsV": func() *Event {
return e.StrsV("k", fixtures.Strings...)
},
"Stringers": func() *Event {
return e.Stringers("k", fixtures.Stringers)
},
"StringersV": func() *Event {
return e.StringersV("k", fixtures.Stringers...)
},
"Err": func() *Event {
return e.Err(fixtures.Errs[0])
},
"Errs": func() *Event {
return e.Errs("k", fixtures.Errs)
},
"Ctx": func() *Event {
return e.Ctx(fixtures.Ctx)
},
"Time": func() *Event {
return e.Time("k", fixtures.Times[0])
},
"Times": func() *Event {
return e.Times("k", fixtures.Times)
},
"Dict": func() *Event {
d := e.CreateDict()
d.Str("greeting", "hello")
return e.Dict("k", d)
},
"Dur": func() *Event {
return e.Dur("k", fixtures.Durations[0])
},
"Durs": func() *Event {
return e.Durs("k", fixtures.Durations)
},
"Interface": func() *Event {
return e.Interface("k", fixtures.Interfaces[0])
},
"Interfaces": func() *Event {
return e.Interface("k", fixtures.Interfaces)
},
"Interface(Object)": func() *Event {
return e.Interface("k", fixtures.Objects[0])
},
"Interface(Objects)": func() *Event {
return e.Interface("k", fixtures.Objects)
},
"Object": func() *Event {
return e.Object("k", fixtures.Objects[0])
},
"Objects": func() *Event {
return e.Objects("k", fixtures.Objects)
},
"ObjectsV": func() *Event {
return e.ObjectsV("k", fixtures.Objects...)
},
"EmbedObject": func() *Event {
return e.EmbedObject(fixtures.Objects[0])
},
"Timestamp": func() *Event {
return e.Timestamp()
},
"IPAddr": func() *Event {
return e.IPAddr("k", fixtures.IPAddrs[0])
},
"IPAddrs": func() *Event {
return e.IPAddrs("k", fixtures.IPAddrs)
},
"IPPrefix": func() *Event {
return e.IPPrefix("k", fixtures.IPPfxs[0])
},
"IPPrefixes": func() *Event {
return e.IPPrefixes("k", fixtures.IPPfxs)
},
"MACAddr": func() *Event {
return e.MACAddr("k", fixtures.MACAddr)
},
"Type": func() *Event {
return e.Type("k", fixtures.Type)
},
"Caller": func() *Event {
return e.Caller(1)
},
"CallerSkip": func() *Event {
return e.CallerSkipFrame(2)
},
"Stack": func() *Event {
return e.Stack()
},
}
for name := range types {
f := types[name]
if got := f(); got != nil {
t.Errorf("Event.Bool() = %v, want %v", got, nil)
}
}
e.Send()
e.Msg("nothing")
e.Msgf("what %s", "nothing")
got := e.write()
if got != nil {
t.Errorf("Event.write() = %v, want %v", got, e)
}
called := false
e.MsgFunc(func() string {
called = true
return "called"
})
if called {
t.Errorf("Event.MsgFunc() should not be called on nil Event")
}
}
func TestEvent_MsgFunc(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
called := false
e.MsgFunc(func() string {
called = true
return "called"
})
if !called {
t.Errorf("Event.MsgFunc() was not called on non-nil Event")
}
want := `{"message":"called"}`
got := strings.TrimSpace(buf.String())
if got != want {
t.Errorf("Event.MsgFunc() = %q, want %q", got, want)
}
}
func TestEvent_CallerRuntimeFail(t *testing.T) {
var buf bytes.Buffer
e := newEvent(LevelWriterAdapter{&buf}, DebugLevel, false, nil, nil)
// Set a very large skipFrame to make runtime.Caller fail
e.CallerSkipFrame(1000)
e.Caller()
e.Msg("test")
got := strings.TrimSpace(buf.String())
want := `{"message":"test"}` // No caller field because runtime.Caller failed
if got != want {
t.Errorf("Event.Caller() with failed runtime.Caller = %q, want %q", got, want)
}
}
func TestEvent_DoneHandler(t *testing.T) {
e := newEvent(nil, InfoLevel, false, nil, nil)
// Set up a done handler to capture calls
var called bool
var capturedMsg string
e.done = func(msg string) {
called = true
capturedMsg = msg
}
// Trigger msg via Msg
e.Msg("test message")
// Assert the handler was called with the correct message
if !called {
t.Error("Done handler was not called")
}
if capturedMsg != "test message" {
t.Errorf("Expected message 'test message', got '%s'", capturedMsg)
}
}
type badLevelWriter struct {
err error
}
func (w *badLevelWriter) WriteLevel(level Level, p []byte) (n int, err error) {
return 0, w.err
}
func (w *badLevelWriter) Write(p []byte) (n int, err error) {
return 0, w.err
}
func TestEvent_Msg_ErrorHandlerNil(t *testing.T) {
// Save original ErrorHandler and restore after test
originalErrorHandler := ErrorHandler
ErrorHandler = nil
defer func() { ErrorHandler = originalErrorHandler }()
// Create a LevelWriter that always returns an error
mockWriter := &badLevelWriter{err: errors.New("write error")}
e := newEvent(mockWriter, InfoLevel, false, nil, nil)
if e == nil {
t.Fatal("Event should not be nil")
}
// Capture stderr
oldStderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
os.Stderr = w
// Call Msg to trigger write error
e.Msg("test message")
// Restore stderr and read captured output
w.Close()
os.Stderr = oldStderr
captured, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
// Assert the error message was printed to stderr
expected := "zerolog: could not write event: write error\n"
if string(captured) != expected {
t.Errorf("Expected stderr output %q, got %q", expected, string(captured))
}
}
type mockLogObjectMarshaler struct {
data string
}
func (m mockLogObjectMarshaler) MarshalZerologObject(e *Event) {
e.Str("stack_func", m.data)
}
func TestEvent_ErrWithStackMarshaler(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler
ErrorStackMarshaler = func(err error) interface{} {
return "stack-trace"
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Err(err).Msg("test message")
got := buf.String()
want := `{"stack":"stack-trace","error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Err() with stack marshaler = %q, want %q", got, want)
}
}
func TestEvent_FieldsWithErrorAndStackMarshaler(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler
ErrorStackMarshaler = func(err error) interface{} {
return "stack-trace"
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("test message")
got := buf.String()
want := `{"error":"test error","stack":"stack-trace","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Fields() with error and stack marshaler = %q, want %q", got, want)
}
}
func TestEvent_FieldsWithErrorAndStackMarshalerObject(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns LogObjectMarshaler
ErrorStackMarshaler = func(err error) interface{} {
return mockLogObjectMarshaler{data: "stack-data"}
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("test message")
got := buf.String()
want := `{"error":"test error","stack":{"stack_func":"stack-data"},"message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Fields() with error and stack marshaler object = %q, want %q", got, want)
}
}
func TestEvent_FieldsWithErrorAndStackMarshalerError(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an error
ErrorStackMarshaler = func(err error) interface{} {
return errors.New("stack error")
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("test message")
got := buf.String()
want := `{"error":"test error","stack":"stack error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Fields() with error and stack marshaler error = %q, want %q", got, want)
}
}
func TestEvent_FieldsWithErrorAndStackMarshalerInterface(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an int
ErrorStackMarshaler = func(err error) interface{} {
return 42
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("test message")
got := buf.String()
want := `{"error":"test error","stack":42,"message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Fields() with error and stack marshaler interface = %q, want %q", got, want)
}
}
func TestEvent_FieldsWithErrorAndStackMarshalerNil(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set marshaler to return nil
ErrorStackMarshaler = func(err error) interface{} {
return nil
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("test message")
got := buf.String()
want := `{"error":"test error","message":"test message"}` + "\n" // No stack field because marshaler returned nil
if got != want {
t.Errorf("Event.Fields() with error and nil stack marshaler = %q, want %q", got, want)
}
}
func TestEvent_ErrWithStackMarshalerObject(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns LogObjectMarshaler
ErrorStackMarshaler = func(err error) interface{} {
return mockLogObjectMarshaler{data: "stack-data"}
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Err(err).Msg("test message")
got := buf.String()
want := `{"stack":{"stack_func":"stack-data"},"error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Err() with stack marshaler object = %q, want %q", got, want)
}
}
func TestEvent_ErrWithStackMarshalerError(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an error
ErrorStackMarshaler = func(err error) interface{} {
return errors.New("stack error")
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Err(err).Msg("test message")
got := buf.String()
want := `{"stack":"stack error","error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Err() with stack marshaler error = %q, want %q", got, want)
}
}
func TestEvent_ErrWithStackMarshalerInterface(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set a mock marshaler that returns an int
ErrorStackMarshaler = func(err error) interface{} {
return 42
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Err(err).Msg("test message")
got := buf.String()
want := `{"stack":42,"error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Err() with stack marshaler interface = %q, want %q", got, want)
}
}
func TestEvent_ErrWithStackMarshalerNil(t *testing.T) {
// Save original
original := ErrorStackMarshaler
defer func() { ErrorStackMarshaler = original }()
// Set marshaler to return nil
ErrorStackMarshaler = func(err error) interface{} {
return nil
}
var buf bytes.Buffer
log := New(&buf)
err := errors.New("test error")
log.Log().Stack().Err(err).Msg("test message")
got := buf.String()
// When ErrorStackMarshaler returns nil (no stack trace available for this
// error), Err() must still log the error value via AnErr. Without a stack
// field, the output is the same as if Stack() had not been called.
// Regression test for https://github.com/rs/zerolog/issues/762:
// commit f6fbd33 introduced a `case nil: return e` branch that silently
// swallowed the error instead of falling through to AnErr.
want := `{"error":"test error","message":"test message"}` + "\n"
if got != want {
t.Errorf("Event.Err() with nil stack marshaler = %q, want %q", got, want)
}
}
+7
View File
@@ -0,0 +1,7 @@
{"time":"5:41PM","level":"info","message":"Starting listener","listen":":8080","pid":37556}
{"time":"5:41PM","level":"debug","message":"Access","database":"myapp","host":"localhost:4962","pid":37556}
{"time":"5:41PM","level":"info","message":"Access","method":"GET","path":"/users","pid":37556,"resp_time":23}
{"time":"5:41PM","level":"info","message":"Access","method":"POST","path":"/posts","pid":37556,"resp_time":532}
{"time":"5:41PM","level":"warn","message":"Slow request","method":"POST","path":"/posts","pid":37556,"resp_time":532}
{"time":"5:41PM","level":"info","message":"Access","method":"GET","path":"/users","pid":37556,"resp_time":10}
{"time":"5:41PM","level":"error","message":"Database connection lost","database":"myapp","pid":37556,"error":"connection reset by peer"}
+300 -84
View File
@@ -1,95 +1,311 @@
package zerolog
import (
"context"
"encoding/json"
"io"
"net"
"reflect"
"sort"
"time"
"github.com/rs/zerolog/internal/json"
)
func appendFields(dst []byte, fields map[string]interface{}) []byte {
keys := make([]string, 0, len(fields))
for key := range fields {
keys = append(keys, key)
func isNilValue(e error) bool {
switch reflect.TypeOf(e).Kind() {
case reflect.Ptr:
return reflect.ValueOf(e).IsNil()
default:
return false
}
sort.Strings(keys)
for _, key := range keys {
dst = json.AppendKey(dst, key)
switch val := fields[key].(type) {
case string:
dst = json.AppendString(dst, val)
case []byte:
dst = json.AppendBytes(dst, val)
case error:
dst = json.AppendError(dst, val)
case []error:
dst = json.AppendErrors(dst, val)
case bool:
dst = json.AppendBool(dst, val)
case int:
dst = json.AppendInt(dst, val)
case int8:
dst = json.AppendInt8(dst, val)
case int16:
dst = json.AppendInt16(dst, val)
case int32:
dst = json.AppendInt32(dst, val)
case int64:
dst = json.AppendInt64(dst, val)
case uint:
dst = json.AppendUint(dst, val)
case uint8:
dst = json.AppendUint8(dst, val)
case uint16:
dst = json.AppendUint16(dst, val)
case uint32:
dst = json.AppendUint32(dst, val)
case uint64:
dst = json.AppendUint64(dst, val)
case float32:
dst = json.AppendFloat32(dst, val)
case float64:
dst = json.AppendFloat64(dst, val)
case time.Time:
dst = json.AppendTime(dst, val, TimeFieldFormat)
case time.Duration:
dst = json.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
case []string:
dst = json.AppendStrings(dst, val)
case []bool:
dst = json.AppendBools(dst, val)
case []int:
dst = json.AppendInts(dst, val)
case []int8:
dst = json.AppendInts8(dst, val)
case []int16:
dst = json.AppendInts16(dst, val)
case []int32:
dst = json.AppendInts32(dst, val)
case []int64:
dst = json.AppendInts64(dst, val)
case []uint:
dst = json.AppendUints(dst, val)
// case []uint8:
// dst = appendUints8(dst, val)
case []uint16:
dst = json.AppendUints16(dst, val)
case []uint32:
dst = json.AppendUints32(dst, val)
case []uint64:
dst = json.AppendUints64(dst, val)
case []float32:
dst = json.AppendFloats32(dst, val)
case []float64:
dst = json.AppendFloats64(dst, val)
case []time.Time:
dst = json.AppendTimes(dst, val, TimeFieldFormat)
case []time.Duration:
dst = json.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
case nil:
dst = append(dst, "null"...)
default:
dst = json.AppendInterface(dst, val)
}
func appendFields(dst []byte, fields interface{}, stack bool, ctx context.Context, hooks []Hook) []byte {
switch fields := fields.(type) {
case []interface{}:
if n := len(fields); n&0x1 == 1 { // odd number
fields = fields[:n-1]
}
dst = appendFieldList(dst, fields, stack, ctx, hooks)
case map[string]interface{}:
keys := make([]string, 0, len(fields))
for key := range fields {
keys = append(keys, key)
}
sort.Strings(keys)
kv := make([]interface{}, 2)
for _, key := range keys {
kv[0], kv[1] = key, fields[key]
dst = appendFieldList(dst, kv, stack, ctx, hooks)
}
}
return dst
}
func appendObject(dst []byte, obj LogObjectMarshaler, stack bool, ctx context.Context, hooks []Hook) []byte {
e := newEvent(LevelWriterAdapter{io.Discard}, DebugLevel, stack, ctx, hooks)
e.buf = e.buf[:0] // discard the beginning marker added by newEvent
e.appendObject(obj)
dst = append(dst, e.buf...)
putEvent(e)
return dst
}
func appendFieldList(dst []byte, kvList []interface{}, stack bool, ctx context.Context, hooks []Hook) []byte {
for i, n := 0, len(kvList); i < n; i += 2 {
key, val := kvList[i], kvList[i+1]
if key, ok := key.(string); ok {
dst = enc.AppendKey(dst, key)
} else {
continue
}
switch val := val.(type) {
case string:
dst = enc.AppendString(dst, val)
case []byte:
dst = enc.AppendBytes(dst, val)
case error:
switch m := ErrorMarshalFunc(val).(type) {
case nil:
dst = enc.AppendNil(dst)
case LogObjectMarshaler:
dst = appendObject(dst, m, stack, ctx, hooks)
case error:
if !isNilValue(m) {
dst = enc.AppendString(dst, m.Error())
}
case string:
dst = enc.AppendString(dst, m)
default:
dst = enc.AppendInterface(dst, m)
}
if stack && ErrorStackMarshaler != nil {
switch m := ErrorStackMarshaler(val).(type) {
case nil:
return dst // do nothing with nil errors
case LogObjectMarshaler:
dst = enc.AppendKey(dst, ErrorStackFieldName)
dst = appendObject(dst, m, stack, ctx, hooks)
case error:
dst = enc.AppendKey(dst, ErrorStackFieldName)
dst = enc.AppendString(dst, m.Error())
case string:
dst = enc.AppendKey(dst, ErrorStackFieldName)
dst = enc.AppendString(dst, m)
default:
dst = enc.AppendKey(dst, ErrorStackFieldName)
dst = enc.AppendInterface(dst, m)
}
}
case []error:
dst = enc.AppendArrayStart(dst)
for i, err := range val {
switch m := ErrorMarshalFunc(err).(type) {
case nil:
dst = enc.AppendNil(dst)
case LogObjectMarshaler:
dst = appendObject(dst, m, stack, ctx, hooks)
case error:
if !isNilValue(m) {
dst = enc.AppendString(dst, m.Error())
}
case string:
dst = enc.AppendString(dst, m)
default:
dst = enc.AppendInterface(dst, m)
}
if i < (len(val) - 1) {
dst = enc.AppendArrayDelim(dst)
}
}
dst = enc.AppendArrayEnd(dst)
case []LogObjectMarshaler:
dst = enc.AppendArrayStart(dst)
for i, obj := range val {
dst = appendObject(dst, obj, stack, ctx, hooks)
if i < (len(val) - 1) {
dst = enc.AppendArrayDelim(dst)
}
}
dst = enc.AppendArrayEnd(dst)
case bool:
dst = enc.AppendBool(dst, val)
case int:
dst = enc.AppendInt(dst, val)
case int8:
dst = enc.AppendInt8(dst, val)
case int16:
dst = enc.AppendInt16(dst, val)
case int32:
dst = enc.AppendInt32(dst, val)
case int64:
dst = enc.AppendInt64(dst, val)
case uint:
dst = enc.AppendUint(dst, val)
case uint8:
dst = enc.AppendUint8(dst, val)
case uint16:
dst = enc.AppendUint16(dst, val)
case uint32:
dst = enc.AppendUint32(dst, val)
case uint64:
dst = enc.AppendUint64(dst, val)
case float32:
dst = enc.AppendFloat32(dst, val, FloatingPointPrecision)
case float64:
dst = enc.AppendFloat64(dst, val, FloatingPointPrecision)
case time.Time:
dst = enc.AppendTime(dst, val, TimeFieldFormat)
case time.Duration:
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
case *string:
if val != nil {
dst = enc.AppendString(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *bool:
if val != nil {
dst = enc.AppendBool(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *int:
if val != nil {
dst = enc.AppendInt(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *int8:
if val != nil {
dst = enc.AppendInt8(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *int16:
if val != nil {
dst = enc.AppendInt16(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *int32:
if val != nil {
dst = enc.AppendInt32(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *int64:
if val != nil {
dst = enc.AppendInt64(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *uint:
if val != nil {
dst = enc.AppendUint(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *uint8:
if val != nil {
dst = enc.AppendUint8(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *uint16:
if val != nil {
dst = enc.AppendUint16(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *uint32:
if val != nil {
dst = enc.AppendUint32(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *uint64:
if val != nil {
dst = enc.AppendUint64(dst, *val)
} else {
dst = enc.AppendNil(dst)
}
case *float32:
if val != nil {
dst = enc.AppendFloat32(dst, *val, FloatingPointPrecision)
} else {
dst = enc.AppendNil(dst)
}
case *float64:
if val != nil {
dst = enc.AppendFloat64(dst, *val, FloatingPointPrecision)
} else {
dst = enc.AppendNil(dst)
}
case *time.Time:
if val != nil {
dst = enc.AppendTime(dst, *val, TimeFieldFormat)
} else {
dst = enc.AppendNil(dst)
}
case *time.Duration:
if val != nil {
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
} else {
dst = enc.AppendNil(dst)
}
case []string:
dst = enc.AppendStrings(dst, val)
case []bool:
dst = enc.AppendBools(dst, val)
case []int:
dst = enc.AppendInts(dst, val)
case []int8:
dst = enc.AppendInts8(dst, val)
case []int16:
dst = enc.AppendInts16(dst, val)
case []int32:
dst = enc.AppendInts32(dst, val)
case []int64:
dst = enc.AppendInts64(dst, val)
case []uint:
dst = enc.AppendUints(dst, val)
// case []uint8: is handled as []byte above
case []uint16:
dst = enc.AppendUints16(dst, val)
case []uint32:
dst = enc.AppendUints32(dst, val)
case []uint64:
dst = enc.AppendUints64(dst, val)
case []float32:
dst = enc.AppendFloats32(dst, val, FloatingPointPrecision)
case []float64:
dst = enc.AppendFloats64(dst, val, FloatingPointPrecision)
case []time.Time:
dst = enc.AppendTimes(dst, val, TimeFieldFormat)
case []time.Duration:
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldFormat, DurationFieldInteger, FloatingPointPrecision)
case nil:
dst = enc.AppendNil(dst)
case net.IP:
dst = enc.AppendIPAddr(dst, val)
case []net.IP:
dst = enc.AppendIPAddrs(dst, val)
case net.IPNet:
dst = enc.AppendIPPrefix(dst, val)
case []net.IPNet:
dst = enc.AppendIPPrefixes(dst, val)
case net.HardwareAddr:
dst = enc.AppendMACAddr(dst, val)
case json.RawMessage:
dst = appendJSON(dst, val)
default:
if lom, ok := val.(LogObjectMarshaler); ok {
dst = appendObject(dst, lom, stack, ctx, hooks)
} else {
dst = enc.AppendInterface(dst, val)
}
}
}
return dst
+159
View File
@@ -0,0 +1,159 @@
package zerolog
import (
"context"
"errors"
"fmt"
"net"
"reflect"
"time"
)
type fixtureObj struct {
Pub string
Tag string `json:"tag"`
priv int
}
func (o fixtureObj) MarshalZerologObject(e *Event) {
e.Str("Pub", o.Pub).
Str("Tag", o.Tag).
Int("priv", o.priv)
}
type fieldFixtures struct {
Bools []bool
Bytes []byte
Ctx context.Context
Durations []time.Duration
Errs []error
Floats32 []float32
Floats64 []float64
Interfaces []struct {
Pub string
Tag string `json:"tag"`
priv int
}
Ints []int
Ints8 []int8
Ints16 []int16
Ints32 []int32
Ints64 []int64
Uints []uint
Uints8 []uint8
Uints16 []uint16
Uints32 []uint32
Uints64 []uint64
IPAddrs []net.IP
IPPfxs []net.IPNet
MACAddr net.HardwareAddr
Objects []LogObjectMarshaler
RawCBOR []byte
RawJSONs [][]byte
Stringers []fmt.Stringer
Strings []string
Times []time.Time
Type reflect.Type
}
func makeFieldFixtures() *fieldFixtures {
bools := []bool{true, false, true, false, true, false, true, false, true, false}
bytes := []byte(`abcdef`)
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
ints8 := []int8{-8, 8}
ints16 := []int16{-16, 16}
ints32 := []int32{-32, 32}
ints64 := []int64{-64, 64}
uints := []uint{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
uints8 := []uint8{8, uint8(^uint8(0))}
uints16 := []uint16{16, uint16(^uint16(0))}
uints32 := []uint32{32, uint32(^uint32(0))}
uints64 := []uint64{64, uint64(^uint64(0))}
floats32 := []float32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
floats64 := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
strings := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
durations := []time.Duration{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
times := []time.Time{
time.Unix(0, 0),
time.Unix(1, 0),
time.Unix(2, 0),
time.Unix(3, 0),
time.Unix(4, 0),
time.Unix(5, 0),
time.Unix(6, 0),
time.Unix(7, 0),
time.Unix(8, 0),
time.Unix(9, 0),
}
interfaces := []struct {
Pub string
Tag string `json:"tag"`
priv int
}{
{"A", "j", -5},
{"B", "i", -4},
{"C", "h", -3},
{"D", "g", -2},
{"E", "f", -1},
{"F", "e", 0},
{"G", "d", 1},
{"H", "c", 2},
{"I", "b", 3},
{"J", "a", 4},
}
objects := []LogObjectMarshaler{
fixtureObj{"a", "z", 1},
fixtureObj{"b", "y", 2},
fixtureObj{"c", "x", 3},
fixtureObj{"d", "w", 4},
fixtureObj{"e", "v", 5},
fixtureObj{"f", "u", 6},
fixtureObj{"g", "t", 7},
fixtureObj{"h", "s", 8},
fixtureObj{"i", "r", 9},
fixtureObj{"j", "q", 10},
}
ipAddrV4 := net.IP{192, 168, 0, 1}
ipAddrV6 := net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}
ipAddrs := []net.IP{ipAddrV4, ipAddrV6, ipAddrV4, ipAddrV6, ipAddrV4, ipAddrV6, ipAddrV4, ipAddrV6, ipAddrV4, ipAddrV6}
ipPfxV4 := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
ipPfxV6 := net.IPNet{IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x00}, Mask: net.CIDRMask(64, 128)}
ipPfxs := []net.IPNet{ipPfxV4, ipPfxV6, ipPfxV4, ipPfxV6, ipPfxV4, ipPfxV6, ipPfxV4, ipPfxV6, ipPfxV4, ipPfxV6}
macAddr := net.HardwareAddr{0x00, 0x1A, 0x2B, 0x3C, 0x4D, 0x5E}
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e"), nil, loggableError{fmt.Errorf("oops")}}
ctx := context.Background()
stringers := []fmt.Stringer{ipAddrs[0], durations[0]}
rawJSONs := [][]byte{[]byte(`{"some":"json"}`), []byte(`{"longer":[1111,2222,3333,4444,5555]}`)}
rawCBOR := []byte{0xA1, 0x64, 0x73, 0x6F, 0x6D, 0x65, 0x64, 0x61, 0x74, 0x61} // {"some":"data"}
return &fieldFixtures{
Bools: bools,
Bytes: bytes,
Ctx: ctx,
Durations: durations,
Errs: errs,
Floats32: floats32,
Floats64: floats64,
Interfaces: interfaces,
Ints: ints,
Ints8: ints8,
Ints16: ints16,
Ints32: ints32,
Ints64: ints64,
Uints: uints,
Uints8: uints8,
Uints16: uints16,
Uints32: uints32,
Uints64: uints64,
IPAddrs: ipAddrs,
IPPfxs: ipPfxs,
MACAddr: macAddr,
Objects: objects,
RawCBOR: rawCBOR,
RawJSONs: rawJSONs,
Stringers: stringers,
Strings: strings,
Times: times,
Type: reflect.TypeOf(12345),
}
}
+156 -15
View File
@@ -1,7 +1,40 @@
package zerolog
import "time"
import "sync/atomic"
import (
"bytes"
"encoding/json"
"strconv"
"sync/atomic"
"time"
)
const (
// TimeFormatUnix defines a time format that makes time fields to be
// serialized as Unix timestamp integers.
TimeFormatUnix = ""
// TimeFormatUnixMs defines a time format that makes time fields to be
// serialized as Unix timestamp integers in milliseconds.
TimeFormatUnixMs = "UNIXMS"
// TimeFormatUnixMicro defines a time format that makes time fields to be
// serialized as Unix timestamp integers in microseconds.
TimeFormatUnixMicro = "UNIXMICRO"
// TimeFormatUnixNano defines a time format that makes time fields to be
// serialized as Unix timestamp integers in nanoseconds.
TimeFormatUnixNano = "UNIXNANO"
// DurationFormatFloat defines a format for Duration fields that makes duration fields to be
// serialized as floating point numbers.
DurationFormatFloat = "float"
// DurationFormatInt defines a format for Duration fields that makes duration fields to be
// serialized as integers.
DurationFormatInt = "int"
// DurationFormatString defines a format for Duration fields that makes duration fields to be
// serialized as string.
DurationFormatString = "string"
)
var (
// TimestampFieldName is the field name used for the timestamp field.
@@ -10,35 +43,142 @@ var (
// LevelFieldName is the field name used for the level field.
LevelFieldName = "level"
// LevelTraceValue is the value used for the trace level field.
LevelTraceValue = "trace"
// LevelDebugValue is the value used for the debug level field.
LevelDebugValue = "debug"
// LevelInfoValue is the value used for the info level field.
LevelInfoValue = "info"
// LevelWarnValue is the value used for the warn level field.
LevelWarnValue = "warn"
// LevelErrorValue is the value used for the error level field.
LevelErrorValue = "error"
// LevelFatalValue is the value used for the fatal level field.
LevelFatalValue = "fatal"
// LevelPanicValue is the value used for the panic level field.
LevelPanicValue = "panic"
// LevelFieldMarshalFunc allows customization of global level field marshaling.
LevelFieldMarshalFunc = func(l Level) string {
return l.String()
}
// MessageFieldName is the field name used for the message field.
MessageFieldName = "message"
// ErrorFieldName is the field name used for error fields.
ErrorFieldName = "error"
// SampleFieldName is the name of the field used to report sampling.
SampleFieldName = "sample"
// CallerFieldName is the field name used for caller field.
CallerFieldName = "caller"
// TimeFieldFormat defines the time format of the Time field type.
// If set to an empty string, the time is formatted as an UNIX timestamp
// as integer.
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
CallerSkipFrameCount = 2
// CallerMarshalFunc allows customization of global caller marshaling
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
return file + ":" + strconv.Itoa(line)
}
// ErrorStackFieldName is the field name used for error stacks.
ErrorStackFieldName = "stack"
// ErrorStackMarshaler extract the stack from err if any.
ErrorStackMarshaler func(err error) interface{}
// ErrorMarshalFunc allows customization of global error marshaling
ErrorMarshalFunc = func(err error) interface{} {
return err
}
// InterfaceMarshalFunc allows customization of interface marshaling.
// Default: "encoding/json.Marshal" with disabled HTML escaping
InterfaceMarshalFunc = func(v interface{}) ([]byte, error) {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
b := buf.Bytes()
if len(b) > 0 {
// Remove trailing \n which is added by Encode.
return b[:len(b)-1], nil
}
return b, nil
}
// TimeFieldFormat defines the time format of the Time field type. If set to
// TimeFormatUnix, TimeFormatUnixMs, TimeFormatUnixMicro or TimeFormatUnixNano, the time is formatted as a UNIX
// timestamp as integer.
TimeFieldFormat = time.RFC3339
// TimestampFunc defines the function called to generate a timestamp.
TimestampFunc = time.Now
// DurationFieldFormat defines the format of the Duration field type.
DurationFieldFormat = DurationFormatFloat
// DurationFieldUnit defines the unit for time.Duration type fields added
// using the Dur method.
DurationFieldUnit = time.Millisecond
// DurationFieldInteger renders Dur fields as integer instead of float if
// set to true.
// Deprecated: use DurationFieldFormat with DurationFormatInt instead.
DurationFieldInteger = false
// ErrorHandler is called whenever zerolog fails to write an event on its
// output. If not set, an error is printed on the stderr. This handler must
// be thread safe and non-blocking.
ErrorHandler func(err error)
// FatalExitFunc is called by log.Fatal() instead of os.Exit(1). If not set,
// os.Exit(1) is called.
FatalExitFunc func()
// DefaultContextLogger is returned from Ctx() if there is no logger associated
// with the context.
DefaultContextLogger *Logger
// LevelColors are used by ConsoleWriter's consoleDefaultFormatLevel to color
// log levels.
LevelColors = map[Level]int{
TraceLevel: colorBlue,
DebugLevel: 0,
InfoLevel: colorGreen,
WarnLevel: colorYellow,
ErrorLevel: colorRed,
FatalLevel: colorRed,
PanicLevel: colorRed,
}
// FormattedLevels are used by ConsoleWriter's consoleDefaultFormatLevel
// for a short level name.
FormattedLevels = map[Level]string{
TraceLevel: "TRC",
DebugLevel: "DBG",
InfoLevel: "INF",
WarnLevel: "WRN",
ErrorLevel: "ERR",
FatalLevel: "FTL",
PanicLevel: "PNC",
}
// TriggerLevelWriterBufferReuseLimit is a limit in bytes that a buffer is dropped
// from the TriggerLevelWriter buffer pool if the buffer grows above the limit.
TriggerLevelWriterBufferReuseLimit = 64 * 1024
// FloatingPointPrecision, if set to a value other than -1, controls the number
// of digits when formatting float numbers in JSON. See strconv.FormatFloat for
// more details.
FloatingPointPrecision = -1
)
var (
gLevel = new(uint32)
disableSampling = new(uint32)
gLevel = new(int32)
disableSampling = new(int32)
)
// SetGlobalLevel sets the global override for log level. If this
@@ -46,22 +186,23 @@ var (
//
// To globally disable logs, set GlobalLevel to Disabled.
func SetGlobalLevel(l Level) {
atomic.StoreUint32(gLevel, uint32(l))
atomic.StoreInt32(gLevel, int32(l))
}
func globalLevel() Level {
return Level(atomic.LoadUint32(gLevel))
// GlobalLevel returns the current global log level
func GlobalLevel() Level {
return Level(atomic.LoadInt32(gLevel))
}
// DisableSampling will disable sampling in all Loggers if true.
func DisableSampling(v bool) {
var i uint32
var i int32
if v {
i = 1
}
atomic.StoreUint32(disableSampling, i)
atomic.StoreInt32(disableSampling, i)
}
func samplingDisabled() bool {
return atomic.LoadUint32(disableSampling) == 1
return atomic.LoadInt32(disableSampling) == 1
}
+30
View File
@@ -0,0 +1,30 @@
//go:build go1.18
// +build go1.18
package zerolog
import (
"fmt"
)
func AsLogObjectMarshalers[T LogObjectMarshaler](objs []T) []LogObjectMarshaler {
if objs == nil {
return nil
}
s := make([]LogObjectMarshaler, len(objs))
for i, v := range objs {
s[i] = v
}
return s
}
func AsStringers[T fmt.Stringer](objs []T) []fmt.Stringer {
if objs == nil {
return nil
}
s := make([]fmt.Stringer, len(objs))
for i, v := range objs {
s[i] = v
}
return s
}
+15
View File
@@ -0,0 +1,15 @@
module github.com/rs/zerolog
go 1.23
require (
github.com/coreos/go-systemd/v22 v22.7.0
github.com/mattn/go-colorable v0.1.14
github.com/pkg/errors v0.9.1
github.com/rs/xid v1.6.0
)
require (
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.29.0 // indirect
)
+13
View File
@@ -0,0 +1,13 @@
github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA=
github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+7
View File
@@ -0,0 +1,7 @@
// +build go1.12
package zerolog
// Since go 1.12, some auto generated init functions are hidden from
// runtime.Caller.
const contextCallerSkipFrameCount = 2
+181 -23
View File
@@ -5,17 +5,18 @@ import (
"context"
"net"
"net/http"
"strings"
"time"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog/internal/mutil"
"github.com/rs/zerolog/log"
"github.com/zenazn/goji/web/mutil"
)
// FromRequest gets the logger in the request's context.
// This is a shortcut for log.Ctx(r.Context())
func FromRequest(r *http.Request) zerolog.Logger {
func FromRequest(r *http.Request) *zerolog.Logger {
return log.Ctx(r.Context())
}
@@ -23,7 +24,10 @@ func FromRequest(r *http.Request) zerolog.Logger {
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r = r.WithContext(log.WithContext(r.Context()))
// Create a copy of the logger (including internal context slice)
// to prevent data race when using UpdateContext.
l := log.With().Logger()
r = r.WithContext(l.WithContext(r.Context()))
next.ServeHTTP(w, r)
})
}
@@ -35,8 +39,9 @@ func URLHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, r.URL.String()).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.URL.String())
})
next.ServeHTTP(w, r)
})
}
@@ -48,8 +53,9 @@ func MethodHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, r.Method).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.Method)
})
next.ServeHTTP(w, r)
})
}
@@ -61,8 +67,9 @@ func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, r.Method+" "+r.URL.String()).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.Method+" "+r.URL.String())
})
next.ServeHTTP(w, r)
})
}
@@ -73,10 +80,40 @@ func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
if r.RemoteAddr != "" {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, host).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.RemoteAddr)
})
}
next.ServeHTTP(w, r)
})
}
}
func getHost(hostPort string) string {
if hostPort == "" {
return ""
}
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return hostPort
}
return host
}
// RemoteIPHandler is similar to RemoteAddrHandler, but logs only
// an IP, not a port.
func RemoteIPHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ip := getHost(r.RemoteAddr)
if ip != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ip)
})
}
next.ServeHTTP(w, r)
})
@@ -90,8 +127,9 @@ func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ua := r.Header.Get("User-Agent"); ua != "" {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, ua).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ua)
})
}
next.ServeHTTP(w, r)
})
@@ -105,25 +143,65 @@ func RefererHandler(fieldKey string) func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if ref := r.Header.Get("Referer"); ref != "" {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, ref).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, ref)
})
}
next.ServeHTTP(w, r)
})
}
}
// ProtoHandler adds the requests protocol version as a field to the context logger
// using fieldKey as field Key.
func ProtoHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, r.Proto)
})
next.ServeHTTP(w, r)
})
}
}
// HTTPVersionHandler is similar to ProtoHandler, but it does not store the "HTTP/"
// prefix in the protocol name.
func HTTPVersionHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
proto := strings.TrimPrefix(r.Proto, "HTTP/")
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, proto)
})
next.ServeHTTP(w, r)
})
}
}
type idKey struct{}
// IDFromRequest returns the unique id accociated to the request if any.
// IDFromRequest returns the unique id associated to the request if any.
func IDFromRequest(r *http.Request) (id xid.ID, ok bool) {
if r == nil {
return
}
id, ok = r.Context().Value(idKey{}).(xid.ID)
return IDFromCtx(r.Context())
}
// IDFromCtx returns the unique id associated to the context if any.
func IDFromCtx(ctx context.Context) (id xid.ID, ok bool) {
id, ok = ctx.Value(idKey{}).(xid.ID)
return
}
// CtxWithID adds the given xid.ID to the context
func CtxWithID(ctx context.Context, id xid.ID) context.Context {
return context.WithValue(ctx, idKey{}, id)
}
// RequestIDHandler returns a handler setting a unique id to the request which can
// be gathered using IDFromRequest(req). This generated id is added as a field to the
// logger using the passed fieldKey as field name. The id is also added as a response
@@ -136,16 +214,18 @@ func IDFromRequest(r *http.Request) (id xid.ID, ok bool) {
func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
id, ok := IDFromRequest(r)
if !ok {
id = xid.New()
ctx := context.WithValue(r.Context(), idKey{}, id)
ctx = CtxWithID(ctx, id)
r = r.WithContext(ctx)
}
if fieldKey != "" {
log := zerolog.Ctx(r.Context())
log = log.With().Str(fieldKey, id.String()).Logger()
r = r.WithContext(log.WithContext(r.Context()))
log := zerolog.Ctx(ctx)
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, id.String())
})
}
if headerName != "" {
w.Header().Set(headerName, id.String())
@@ -155,14 +235,92 @@ func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.
}
}
// CustomHeaderHandler adds given header from request's header as a field to
// the context's logger using fieldKey as field key.
func CustomHeaderHandler(fieldKey, header string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if val := r.Header.Get(header); val != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, val)
})
}
next.ServeHTTP(w, r)
})
}
}
// EtagHandler adds Etag header from response's header as a field to
// the context's logger using fieldKey as field key.
func EtagHandler(fieldKey string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
etag := w.Header().Get("Etag")
if etag != "" {
etag = strings.ReplaceAll(etag, `"`, "")
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, etag)
})
}
}()
next.ServeHTTP(w, r)
})
}
}
func ResponseHeaderHandler(fieldKey, headerName string) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
value := w.Header().Get(headerName)
if value != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, value)
})
}
}()
next.ServeHTTP(w, r)
})
}
}
// AccessHandler returns a handler that call f after each request.
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
lw := mutil.WrapWriter(w)
defer func() {
f(r, lw.Status(), lw.BytesWritten(), time.Since(start))
}()
next.ServeHTTP(lw, r)
f(r, lw.Status(), lw.BytesWritten(), time.Since(start))
})
}
}
// HostHandler adds the request's host as a field to the context's logger
// using fieldKey as field key. If trimPort is set to true, then port is
// removed from the host.
func HostHandler(fieldKey string, trimPort ...bool) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var host string
if len(trimPort) > 0 && trimPort[0] {
host = getHost(r.Host)
} else {
host = r.Host
}
if host != "" {
log := zerolog.Ctx(r.Context())
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str(fieldKey, host)
})
}
next.ServeHTTP(w, r)
})
}
}
+6 -4
View File
@@ -1,3 +1,5 @@
// +build !binary_log
package hlog_test
import (
@@ -46,8 +48,8 @@ func Example_handler() {
// Install the logger handler with default output on the console
c = c.Append(hlog.NewHandler(log))
// Install some provided extra handler to set some request's context fields.
// Thanks to those handler, all our logs will come with some pre-populated fields.
// Install some provided extra handlers to set some request's context fields.
// Thanks to those handlers, all our logs will come with some pre-populated fields.
c = c.Append(hlog.RemoteAddrHandler("ip"))
c = c.Append(hlog.UserAgentHandler("user_agent"))
c = c.Append(hlog.RefererHandler("referer"))
@@ -61,11 +63,11 @@ func Example_handler() {
hlog.FromRequest(r).Info().
Str("user", "current user").
Str("status", "ok").
Msg("Something happend")
Msg("Something happened")
}))
http.Handle("/", h)
h.ServeHTTP(httptest.NewRecorder(), &http.Request{})
// Output: {"time":"2001-02-03T04:05:06Z","level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","message":"Something happend"}
// Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happened"}
}
+289 -27
View File
@@ -1,21 +1,32 @@
//go:build go1.7
// +build go1.7
package hlog
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"reflect"
"net/http/httptest"
"github.com/rs/xid"
"github.com/rs/zerolog"
"github.com/rs/zerolog/internal/cbor"
)
func decodeIfBinary(out *bytes.Buffer) string {
p := out.Bytes()
if len(p) == 0 || p[0] < 0x7F {
return out.String()
}
return cbor.DecodeObjectToStr(p) + "\n"
}
func TestNewHandler(t *testing.T) {
log := zerolog.New(nil).With().
Str("foo", "bar").
@@ -23,7 +34,7 @@ func TestNewHandler(t *testing.T) {
lh := NewHandler(log)
h := lh(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
if !reflect.DeepEqual(l, log) {
if !reflect.DeepEqual(*l, log) {
t.Fail()
}
}))
@@ -38,12 +49,12 @@ func TestURLHandler(t *testing.T) {
h := URLHandler("url")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"url":"/path?foo=bar"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestMethodHandler(t *testing.T) {
@@ -54,12 +65,12 @@ func TestMethodHandler(t *testing.T) {
h := MethodHandler("method")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"method":"POST"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"method":"POST"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRequestHandler(t *testing.T) {
@@ -71,12 +82,12 @@ func TestRequestHandler(t *testing.T) {
h := RequestHandler("request")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"request":"POST /path?foo=bar"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"request":"POST /path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRemoteAddrHandler(t *testing.T) {
@@ -87,12 +98,12 @@ func TestRemoteAddrHandler(t *testing.T) {
h := RemoteAddrHandler("ip")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"ip":"1.2.3.4"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"ip":"1.2.3.4:1234"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRemoteAddrHandlerIPv6(t *testing.T) {
@@ -103,12 +114,44 @@ func TestRemoteAddrHandlerIPv6(t *testing.T) {
h := RemoteAddrHandler("ip")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"ip":"2001:db8:a0b:12f0::1"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"ip":"[2001:db8:a0b:12f0::1]:1234"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRemoteIPHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
RemoteAddr: "1.2.3.4:1234",
}
h := RemoteIPHandler("ip")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"ip":"1.2.3.4"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRemoteIPHandlerIPv6(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
RemoteAddr: "[2001:db8:a0b:12f0::1]:1234",
}
h := RemoteIPHandler("ip")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"ip":"2001:db8:a0b:12f0::1"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestUserAgentHandler(t *testing.T) {
@@ -121,12 +164,12 @@ func TestUserAgentHandler(t *testing.T) {
h := UserAgentHandler("ua")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"ua":"some user agent string"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"ua":"some user agent string"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRefererHandler(t *testing.T) {
@@ -139,12 +182,12 @@ func TestRefererHandler(t *testing.T) {
h := RefererHandler("referer")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
if want, got := `{"referer":"http://foo.com/bar"}`+"\n", out.String(); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"referer":"http://foo.com/bar"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestRequestIDHandler(t *testing.T) {
@@ -164,10 +207,229 @@ func TestRequestIDHandler(t *testing.T) {
}
l := FromRequest(r)
l.Log().Msg("")
if want, got := fmt.Sprintf(`{"id":"%s"}`+"\n", id), out.String(); want != got {
if want, got := fmt.Sprintf(`{"id":"%s"}`+"\n", id), decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(httptest.NewRecorder(), r)
}
func TestCustomHeaderHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
Header: http.Header{
"X-Request-Id": []string{"514bbe5bb5251c92bd07a9846f4a1ab6"},
},
}
h := CustomHeaderHandler("reqID", "X-Request-Id")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"reqID":"514bbe5bb5251c92bd07a9846f4a1ab6"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestEtagHandler(t *testing.T) {
out := &bytes.Buffer{}
w := httptest.NewRecorder()
r := &http.Request{}
h := EtagHandler("etag")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Etag", `"abcdef"`)
w.WriteHeader(http.StatusOK)
}))
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
l := FromRequest(r)
l.Log().Msg("")
})
h3 := NewHandler(zerolog.New(out))(h2)
h3.ServeHTTP(w, r)
if want, got := `{"etag":"abcdef"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestResponseHeaderHandler(t *testing.T) {
out := &bytes.Buffer{}
w := httptest.NewRecorder()
r := &http.Request{}
h := ResponseHeaderHandler("encoding", "Content-Encoding")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Encoding", `gzip`)
w.WriteHeader(http.StatusOK)
}))
h2 := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
l := FromRequest(r)
l.Log().Msg("")
})
h3 := NewHandler(zerolog.New(out))(h2)
h3.ServeHTTP(w, r)
if want, got := `{"encoding":"gzip"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestProtoHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
Proto: "test",
}
h := ProtoHandler("proto")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"proto":"test"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestHTTPVersionHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
Proto: "HTTP/1.1",
}
h := HTTPVersionHandler("proto")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"proto":"1.1"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestCombinedHandlers(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{
Method: "POST",
URL: &url.URL{Path: "/path", RawQuery: "foo=bar"},
}
h := MethodHandler("method")(RequestHandler("request")(URLHandler("url")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"method":"POST","request":"POST /path?foo=bar","url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func BenchmarkHandlers(b *testing.B) {
r := &http.Request{
Method: "POST",
URL: &url.URL{Path: "/path", RawQuery: "foo=bar"},
}
h1 := URLHandler("url")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h2 := MethodHandler("method")(RequestHandler("request")(h1))
handlers := map[string]http.Handler{
"Single": NewHandler(zerolog.New(io.Discard))(h1),
"Combined": NewHandler(zerolog.New(io.Discard))(h2),
"SingleDisabled": NewHandler(zerolog.New(io.Discard).Level(zerolog.Disabled))(h1),
"CombinedDisabled": NewHandler(zerolog.New(io.Discard).Level(zerolog.Disabled))(h2),
}
for name := range handlers {
h := handlers[name]
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
h.ServeHTTP(nil, r)
}
})
}
}
func BenchmarkDataRace(b *testing.B) {
log := zerolog.New(nil).With().
Str("foo", "bar").
Logger()
lh := NewHandler(log)
h := lh(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str("bar", "baz")
})
l.Log().Msg("")
}))
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
h.ServeHTTP(nil, &http.Request{})
}
})
}
func TestCtxWithID(t *testing.T) {
ctx := context.Background()
id, _ := xid.FromString(`c0umremcie6smuu506pg`)
want := context.Background()
want = context.WithValue(want, idKey{}, id)
if got := CtxWithID(ctx, id); !reflect.DeepEqual(got, want) {
t.Errorf("CtxWithID() = %v, want %v", got, want)
}
}
func TestHostHandler(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{Host: "example.com:8080"}
h := HostHandler("host")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"host":"example.com:8080"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestHostHandlerWithoutPort(t *testing.T) {
out := &bytes.Buffer{}
r := &http.Request{Host: "example.com:8080"}
h := HostHandler("host", true)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
l := FromRequest(r)
l.Log().Msg("")
}))
h = NewHandler(zerolog.New(out))(h)
h.ServeHTTP(nil, r)
if want, got := `{"host":"example.com"}`+"\n", decodeIfBinary(out); want != got {
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
}
}
func TestGetHost(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"example.com:8080", "example.com"},
{"example.com", "example.com"},
{"invalid", "invalid"},
{"192.168.0.1:8080", "192.168.0.1"},
{"[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"},
{"こんにちは.com:8080", "こんにちは.com"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.input, func(t *testing.T) {
result := getHost(tt.input)
if tt.expected != result {
t.Errorf("Invalid log output, got: %s, want: %s", result, tt.expected)
}
})
}
}
+20
View File
@@ -0,0 +1,20 @@
Copyright (c) 2014, 2015, 2016 Carl Jackson (carl@avtok.com)
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+6
View File
@@ -0,0 +1,6 @@
// Package mutil contains various functions that are helpful when writing http
// middleware.
//
// It has been vendored from Goji v1.0, with the exception of the code for Go 1.8:
// https://github.com/zenazn/goji/
package mutil
+154
View File
@@ -0,0 +1,154 @@
package mutil
import (
"bufio"
"io"
"net"
"net/http"
)
// WriterProxy is a proxy around an http.ResponseWriter that allows you to hook
// into various parts of the response process.
type WriterProxy interface {
http.ResponseWriter
// Status returns the HTTP status of the request, or 0 if one has not
// yet been sent.
Status() int
// BytesWritten returns the total number of bytes sent to the client.
BytesWritten() int
// Tee causes the response body to be written to the given io.Writer in
// addition to proxying the writes through. Only one io.Writer can be
// tee'd to at once: setting a second one will overwrite the first.
// Writes will be sent to the proxy before being written to this
// io.Writer. It is illegal for the tee'd writer to be modified
// concurrently with writes.
Tee(io.Writer)
// Unwrap returns the original proxied target.
Unwrap() http.ResponseWriter
}
// WrapWriter wraps an http.ResponseWriter, returning a proxy that allows you to
// hook into various parts of the response process.
func WrapWriter(w http.ResponseWriter) WriterProxy {
_, cn := w.(http.CloseNotifier)
_, fl := w.(http.Flusher)
_, hj := w.(http.Hijacker)
_, rf := w.(io.ReaderFrom)
bw := basicWriter{ResponseWriter: w}
if cn && fl && hj && rf {
return &fancyWriter{bw}
}
if fl {
return &flushWriter{bw}
}
return &bw
}
// basicWriter wraps a http.ResponseWriter that implements the minimal
// http.ResponseWriter interface.
type basicWriter struct {
http.ResponseWriter
wroteHeader bool
code int
bytes int
tee io.Writer
}
func (b *basicWriter) WriteHeader(code int) {
if !b.wroteHeader {
b.code = code
b.wroteHeader = true
b.ResponseWriter.WriteHeader(code)
}
}
func (b *basicWriter) Write(buf []byte) (int, error) {
b.WriteHeader(http.StatusOK)
n, err := b.ResponseWriter.Write(buf)
if b.tee != nil {
_, err2 := b.tee.Write(buf[:n])
// Prefer errors generated by the proxied writer.
if err == nil {
err = err2
}
}
b.bytes += n
return n, err
}
func (b *basicWriter) maybeWriteHeader() {
if !b.wroteHeader {
b.WriteHeader(http.StatusOK)
}
}
func (b *basicWriter) Status() int {
return b.code
}
func (b *basicWriter) BytesWritten() int {
return b.bytes
}
func (b *basicWriter) Tee(w io.Writer) {
b.tee = w
}
func (b *basicWriter) Unwrap() http.ResponseWriter {
return b.ResponseWriter
}
// fancyWriter is a writer that additionally satisfies http.CloseNotifier,
// http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
// of wrapping the http.ResponseWriter that package http gives you, in order to
// make the proxied object support the full method set of the proxied object.
type fancyWriter struct {
basicWriter
}
func (f *fancyWriter) CloseNotify() <-chan bool {
cn := f.basicWriter.ResponseWriter.(http.CloseNotifier)
return cn.CloseNotify()
}
func (f *fancyWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}
func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
return hj.Hijack()
}
func (f *fancyWriter) ReadFrom(r io.Reader) (int64, error) {
if f.basicWriter.tee != nil {
n, err := io.Copy(&f.basicWriter, r)
f.bytes += int(n)
return n, err
}
rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
f.basicWriter.maybeWriteHeader()
n, err := rf.ReadFrom(r)
f.bytes += int(n)
return n, err
}
type flushWriter struct {
basicWriter
}
func (f *flushWriter) Flush() {
fl := f.basicWriter.ResponseWriter.(http.Flusher)
fl.Flush()
}
var (
_ http.CloseNotifier = &fancyWriter{}
_ http.Flusher = &fancyWriter{}
_ http.Hijacker = &fancyWriter{}
_ io.ReaderFrom = &fancyWriter{}
_ http.Flusher = &flushWriter{}
)
+64
View File
@@ -0,0 +1,64 @@
package zerolog
// Hook defines an interface to a log hook.
type Hook interface {
// Run runs the hook with the event.
Run(e *Event, level Level, message string)
}
// HookFunc is an adaptor to allow the use of an ordinary function
// as a Hook.
type HookFunc func(e *Event, level Level, message string)
// Run implements the Hook interface.
func (h HookFunc) Run(e *Event, level Level, message string) {
h(e, level, message)
}
// LevelHook applies a different hook for each level.
type LevelHook struct {
NoLevelHook, TraceHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
}
// Run implements the Hook interface.
func (h LevelHook) Run(e *Event, level Level, message string) {
switch level {
case TraceLevel:
if h.TraceHook != nil {
h.TraceHook.Run(e, level, message)
}
case DebugLevel:
if h.DebugHook != nil {
h.DebugHook.Run(e, level, message)
}
case InfoLevel:
if h.InfoHook != nil {
h.InfoHook.Run(e, level, message)
}
case WarnLevel:
if h.WarnHook != nil {
h.WarnHook.Run(e, level, message)
}
case ErrorLevel:
if h.ErrorHook != nil {
h.ErrorHook.Run(e, level, message)
}
case FatalLevel:
if h.FatalHook != nil {
h.FatalHook.Run(e, level, message)
}
case PanicLevel:
if h.PanicHook != nil {
h.PanicHook.Run(e, level, message)
}
case NoLevel:
if h.NoLevelHook != nil {
h.NoLevelHook.Run(e, level, message)
}
}
}
// NewLevelHook returns a new LevelHook.
func NewLevelHook() LevelHook {
return LevelHook{}
}
+298
View File
@@ -0,0 +1,298 @@
package zerolog
import (
"bytes"
"context"
"io"
"testing"
)
type contextKeyType int
var contextKey contextKeyType
var (
levelNameHook = HookFunc(func(e *Event, level Level, msg string) {
levelName := level.String()
if level == NoLevel {
levelName = "nolevel"
}
e.Str("level_name", levelName)
})
simpleHook = HookFunc(func(e *Event, level Level, msg string) {
e.Bool("has_level", level != NoLevel)
e.Str("test", "logged")
})
copyHook = HookFunc(func(e *Event, level Level, msg string) {
hasLevel := level != NoLevel
e.Bool("copy_has_level", hasLevel)
if hasLevel {
e.Str("copy_level", level.String())
}
e.Str("copy_msg", msg)
})
nopHook = HookFunc(func(e *Event, level Level, message string) {
})
discardHook = HookFunc(func(e *Event, level Level, message string) {
e.Discard()
})
contextHook = HookFunc(func(e *Event, level Level, message string) {
contextData, ok := e.GetCtx().Value(contextKey).(string)
if ok {
e.Str("context-data", contextData)
}
})
)
func TestHook(t *testing.T) {
tests := []struct {
name string
want string
test func(log Logger)
}{
{"Message", `{"message":"test message"}` + "\n", func(log Logger) {
log = log.Hook()
log.Log().Msg("test message")
}},
{"Message", `{"level_name":"nolevel","message":"test message"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook)
log.Log().Msg("test message")
}},
{"NoLevel", `{"level_name":"nolevel"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook)
log.Log().Msg("")
}},
{"Print", `{"level":"debug","level_name":"debug"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook)
log.Print("")
}},
{"Error", `{"level":"error","level_name":"error"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook)
log.Error().Msg("")
}},
{"Copy/1", `{"copy_has_level":false,"copy_msg":""}` + "\n", func(log Logger) {
log = log.Hook(copyHook)
log.Log().Msg("")
}},
{"Copy/2", `{"level":"info","copy_has_level":true,"copy_level":"info","copy_msg":"a message","message":"a message"}` + "\n", func(log Logger) {
log = log.Hook(copyHook)
log.Info().Msg("a message")
}},
{"Multi", `{"level":"error","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook).Hook(simpleHook)
log.Error().Msg("")
}},
{"Multi/Message", `{"level":"error","level_name":"error","has_level":true,"test":"logged","message":"a message"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook).Hook(simpleHook)
log.Error().Msg("a message")
}},
{"Output/single/pre", `{"level":"error","level_name":"error"}` + "\n", func(log Logger) {
ignored := &bytes.Buffer{}
log = New(ignored).Hook(levelNameHook).Output(log.w)
log.Error().Msg("")
}},
{"Output/single/post", `{"level":"error","level_name":"error"}` + "\n", func(log Logger) {
ignored := &bytes.Buffer{}
log = New(ignored).Output(log.w).Hook(levelNameHook)
log.Error().Msg("")
}},
{"Output/multi/pre", `{"level":"error","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
ignored := &bytes.Buffer{}
log = New(ignored).Hook(levelNameHook).Hook(simpleHook).Output(log.w)
log.Error().Msg("")
}},
{"Output/multi/post", `{"level":"error","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
ignored := &bytes.Buffer{}
log = New(ignored).Output(log.w).Hook(levelNameHook).Hook(simpleHook)
log.Error().Msg("")
}},
{"Output/mixed", `{"level":"error","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
ignored := &bytes.Buffer{}
log = New(ignored).Hook(levelNameHook).Output(log.w).Hook(simpleHook)
log.Error().Msg("")
}},
{"With/single/pre", `{"level":"error","with":"pre","level_name":"error"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook).With().Str("with", "pre").Logger()
log.Error().Msg("")
}},
{"With/single/post", `{"level":"error","with":"post","level_name":"error"}` + "\n", func(log Logger) {
log = log.With().Str("with", "post").Logger().Hook(levelNameHook)
log.Error().Msg("")
}},
{"With/multi/pre", `{"level":"error","with":"pre","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook).Hook(simpleHook).With().Str("with", "pre").Logger()
log.Error().Msg("")
}},
{"With/multi/post", `{"level":"error","with":"post","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
log = log.With().Str("with", "post").Logger().Hook(levelNameHook).Hook(simpleHook)
log.Error().Msg("")
}},
{"With/mixed", `{"level":"error","with":"mixed","level_name":"error","has_level":true,"test":"logged"}` + "\n", func(log Logger) {
log = log.Hook(levelNameHook).With().Str("with", "mixed").Logger().Hook(simpleHook)
log.Error().Msg("")
}},
{"Discard", "", func(log Logger) {
log = log.Hook(discardHook)
log.Log().Msg("test message")
}},
{"Context/Background", `{"level":"info","message":"test message"}` + "\n", func(log Logger) {
log = log.Hook(contextHook)
log.Info().Ctx(context.Background()).Msg("test message")
}},
{"Context/nil", `{"level":"info","message":"test message"}` + "\n", func(log Logger) {
// passing `nil` where a context is wanted is against
// the rules, but people still do it.
log = log.Hook(contextHook)
log.Info().Ctx(nil).Msg("test message") // nolint
}},
{"Context/valid", `{"level":"info","context-data":"12345abcdef","message":"test message"}` + "\n", func(log Logger) {
ctx := context.Background()
ctx = context.WithValue(ctx, contextKey, "12345abcdef")
log = log.Hook(contextHook)
log.Info().Ctx(ctx).Msg("test message")
}},
{"Context/With/valid", `{"level":"info","context-data":"12345abcdef","message":"test message"}` + "\n", func(log Logger) {
ctx := context.Background()
ctx = context.WithValue(ctx, contextKey, "12345abcdef")
log = log.Hook(contextHook)
log = log.With().Ctx(ctx).Logger()
log.Info().Msg("test message")
}},
{"None", `{"level":"error"}` + "\n", func(log Logger) {
log.Error().Msg("")
}},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
out := &bytes.Buffer{}
log := New(out)
tt.test(log)
if got, want := decodeIfBinaryToString(out.Bytes()), tt.want; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
})
}
}
func TestLevelHook(t *testing.T) {
var called []string
traceHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "trace")
})
debugHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "debug")
})
infoHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "info")
})
warnHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "warn")
})
errorHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "error")
})
fatalHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "fatal")
})
panicHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "panic")
})
noLevelHook := HookFunc(func(e *Event, level Level, msg string) {
called = append(called, "nolevel")
})
hook := LevelHook{
TraceHook: traceHook,
DebugHook: debugHook,
InfoHook: infoHook,
WarnHook: warnHook,
ErrorHook: errorHook,
FatalHook: fatalHook,
PanicHook: panicHook,
NoLevelHook: noLevelHook,
}
e := &Event{}
// Test each level
hook.Run(e, TraceLevel, "")
if len(called) != 1 || called[0] != "trace" {
t.Errorf("TraceLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, DebugLevel, "")
if len(called) != 1 || called[0] != "debug" {
t.Errorf("DebugLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, InfoLevel, "")
if len(called) != 1 || called[0] != "info" {
t.Errorf("InfoLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, WarnLevel, "")
if len(called) != 1 || called[0] != "warn" {
t.Errorf("WarnLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, ErrorLevel, "")
if len(called) != 1 || called[0] != "error" {
t.Errorf("ErrorLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, FatalLevel, "")
if len(called) != 1 || called[0] != "fatal" {
t.Errorf("FatalLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, PanicLevel, "")
if len(called) != 1 || called[0] != "panic" {
t.Errorf("PanicLevel hook not called correctly: %v", called)
}
called = nil
hook.Run(e, NoLevel, "")
if len(called) != 1 || called[0] != "nolevel" {
t.Errorf("NoLevel hook not called correctly: %v", called)
}
// Test NewLevelHook
_ = NewLevelHook()
}
func BenchmarkHooks(b *testing.B) {
logger := New(io.Discard)
b.ResetTimer()
b.Run("Nop/Single", func(b *testing.B) {
log := logger.Hook(nopHook)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Log().Msg("")
}
})
})
b.Run("Nop/Multi", func(b *testing.B) {
log := logger.Hook(nopHook).Hook(nopHook)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Log().Msg("")
}
})
})
b.Run("Simple", func(b *testing.B) {
log := logger.Hook(simpleHook)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
log.Log().Msg("")
}
})
})
}
+56
View File
@@ -0,0 +1,56 @@
## Reference:
CBOR Encoding is described in [RFC7049](https://tools.ietf.org/html/rfc7049)
## Comparison of JSON vs CBOR
Two main areas of reduction are:
1. CPU usage to write a log msg
2. Size (in bytes) of log messages.
CPU Usage savings are below:
```
name JSON time/op CBOR time/op delta
Info-32 15.3ns ± 1% 11.7ns ± 3% -23.78% (p=0.000 n=9+10)
ContextFields-32 16.2ns ± 2% 12.3ns ± 3% -23.97% (p=0.000 n=9+9)
ContextAppend-32 6.70ns ± 0% 6.20ns ± 0% -7.44% (p=0.000 n=9+9)
LogFields-32 66.4ns ± 0% 24.6ns ± 2% -62.89% (p=0.000 n=10+9)
LogArrayObject-32 911ns ±11% 768ns ± 6% -15.64% (p=0.000 n=10+10)
LogFieldType/Floats-32 70.3ns ± 2% 29.5ns ± 1% -57.98% (p=0.000 n=10+10)
LogFieldType/Err-32 14.0ns ± 3% 12.1ns ± 8% -13.20% (p=0.000 n=8+10)
LogFieldType/Dur-32 17.2ns ± 2% 13.1ns ± 1% -24.27% (p=0.000 n=10+9)
LogFieldType/Object-32 54.3ns ±11% 52.3ns ± 7% ~ (p=0.239 n=10+10)
LogFieldType/Ints-32 20.3ns ± 2% 15.1ns ± 2% -25.50% (p=0.000 n=9+10)
LogFieldType/Interfaces-32 642ns ±11% 621ns ± 9% ~ (p=0.118 n=10+10)
LogFieldType/Interface(Objects)-32 635ns ±13% 632ns ± 9% ~ (p=0.592 n=10+10)
LogFieldType/Times-32 294ns ± 0% 27ns ± 1% -90.71% (p=0.000 n=10+9)
LogFieldType/Durs-32 121ns ± 0% 33ns ± 2% -72.44% (p=0.000 n=9+9)
LogFieldType/Interface(Object)-32 56.6ns ± 8% 52.3ns ± 8% -7.54% (p=0.007 n=10+10)
LogFieldType/Errs-32 17.8ns ± 3% 16.1ns ± 2% -9.71% (p=0.000 n=10+9)
LogFieldType/Time-32 40.5ns ± 1% 12.7ns ± 6% -68.66% (p=0.000 n=8+9)
LogFieldType/Bool-32 12.0ns ± 5% 10.2ns ± 2% -15.18% (p=0.000 n=10+8)
LogFieldType/Bools-32 17.2ns ± 2% 12.6ns ± 4% -26.63% (p=0.000 n=10+10)
LogFieldType/Int-32 12.3ns ± 2% 11.2ns ± 4% -9.27% (p=0.000 n=9+10)
LogFieldType/Float-32 16.7ns ± 1% 12.6ns ± 2% -24.42% (p=0.000 n=7+9)
LogFieldType/Str-32 12.7ns ± 7% 11.3ns ± 7% -10.88% (p=0.000 n=10+9)
LogFieldType/Strs-32 20.3ns ± 3% 18.2ns ± 3% -10.25% (p=0.000 n=9+10)
LogFieldType/Interface-32 183ns ±12% 175ns ± 9% ~ (p=0.078 n=10+10)
```
Log message size savings is greatly dependent on the number and type of fields in the log message.
Assuming this log message (with an Integer, timestamp and string, in addition to level).
`{"level":"error","Fault":41650,"time":"2018-04-01T15:18:19-07:00","message":"Some Message"}`
Two measurements were done for the log file sizes - one without any compression, second
using [compress/zlib](https://golang.org/pkg/compress/zlib/).
Results for 10,000 log messages:
| Log Format | Plain File Size (in KB) | Compressed File Size (in KB) |
| :--- | :---: | :---: |
| JSON | 920 | 28 |
| CBOR | 550 | 28 |
The example used to calculate the above data is available in [Examples](examples).
+19
View File
@@ -0,0 +1,19 @@
package cbor
// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice.
// Making it package level instead of embedded in Encoder brings
// some extra efforts at importing, but avoids value copy when the functions
// of Encoder being invoked.
// DO REMEMBER to set this variable at importing, or
// you might get a nil pointer dereference panic at runtime.
var JSONMarshalFunc func(v interface{}) ([]byte, error)
type Encoder struct{}
// AppendKey adds a key (string) to the binary encoded log message
func (e Encoder) AppendKey(dst []byte, key string) []byte {
if len(dst) < 1 {
dst = e.AppendBeginMarker(dst)
}
return e.AppendString(dst, key)
}
+22
View File
@@ -0,0 +1,22 @@
package cbor
import (
"bytes"
"encoding/hex"
"testing"
)
func TestAppendKey(t *testing.T) {
want := make([]byte, 0)
want = append(want, 0xbf) // start string
want = append(want, 0x63) // length 3
want = append(want, []byte("key")...)
got := enc.AppendKey([]byte{}, "key")
if !bytes.Equal(got, want) {
t.Errorf("AppendKey(%v)\ngot: 0x%s\nwant: 0x%s",
"key",
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
+102
View File
@@ -0,0 +1,102 @@
// Package cbor provides primitives for storing different data
// in the CBOR (binary) format. CBOR is defined in RFC7049.
package cbor
import "time"
const (
majorOffset = 5
additionalMax = 23
// Non Values.
additionalTypeBoolFalse byte = 20
additionalTypeBoolTrue byte = 21
additionalTypeNull byte = 22
// Integer (+ve and -ve) Sub-types.
additionalTypeIntUint8 byte = 24
additionalTypeIntUint16 byte = 25
additionalTypeIntUint32 byte = 26
additionalTypeIntUint64 byte = 27
// Float Sub-types.
additionalTypeFloat16 byte = 25
additionalTypeFloat32 byte = 26
additionalTypeFloat64 byte = 27
additionalTypeBreak byte = 31
// Tag Sub-types.
additionalTypeTimestamp byte = 01
additionalTypeEmbeddedCBOR byte = 63
// Extended Tags - from https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
additionalTypeTagNetworkAddr uint16 = 260
additionalTypeTagNetworkPrefix uint16 = 261
additionalTypeEmbeddedJSON uint16 = 262
additionalTypeTagHexString uint16 = 263
// Unspecified number of elements.
additionalTypeInfiniteCount byte = 31
)
const (
majorTypeUnsignedInt byte = iota << majorOffset // Major type 0
majorTypeNegativeInt // Major type 1
majorTypeByteString // Major type 2
majorTypeUtf8String // Major type 3
majorTypeArray // Major type 4
majorTypeMap // Major type 5
majorTypeTags // Major type 6
majorTypeSimpleAndFloat // Major type 7
)
const (
maskOutAdditionalType byte = (7 << majorOffset)
maskOutMajorType byte = 31
)
const (
float32Nan = "\x7f\xc0\x00\x00"
float32PosInfinity = "\x7f\x80\x00\x00"
float32NegInfinity = "\xff\x80\x00\x00"
float64Nan = "\x7f\xf8\x00\x00\x00\x00\x00\x00"
float64PosInfinity = "\x7f\xf0\x00\x00\x00\x00\x00\x00"
float64NegInfinity = "\xff\xf0\x00\x00\x00\x00\x00\x00"
)
// IntegerTimeFieldFormat indicates the format of timestamp decoded
// from an integer (time in seconds).
var IntegerTimeFieldFormat = time.RFC3339
// NanoTimeFieldFormat indicates the format of timestamp decoded
// from a float value (time in seconds and nanoseconds).
var NanoTimeFieldFormat = time.RFC3339Nano
func appendCborTypePrefix(dst []byte, major byte, number uint64) []byte {
var byteCount int
var minor byte
switch {
case number < 256:
byteCount = 1
minor = additionalTypeIntUint8
case number < 65536:
byteCount = 2
minor = additionalTypeIntUint16
case number < 4294967296:
byteCount = 4
minor = additionalTypeIntUint32
default:
byteCount = 8
minor = additionalTypeIntUint64
}
dst = append(dst, major|minor)
byteCount--
for ; byteCount >= 0; byteCount-- {
dst = append(dst, byte(number>>(uint(byteCount)*8)))
}
return dst
}
+654
View File
@@ -0,0 +1,654 @@
package cbor
// This file contains code to decode a stream of CBOR Data into JSON.
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
"math"
"net"
"runtime"
"strconv"
"strings"
"time"
"unicode/utf8"
)
var decodeTimeZone *time.Location
const hexTable = "0123456789abcdef"
const isFloat32 = 4
const isFloat64 = 8
func readNBytes(src *bufio.Reader, n int) []byte {
ret := make([]byte, n)
for i := 0; i < n; i++ {
ch, e := src.ReadByte()
if e != nil {
panic(fmt.Errorf("Tried to Read %d Bytes.. But hit end of file", n))
}
ret[i] = ch
}
return ret
}
func readByte(src *bufio.Reader) byte {
b, e := src.ReadByte()
if e != nil {
panic(fmt.Errorf("Tried to Read 1 Byte.. But hit end of file"))
}
return b
}
func decodeIntAdditionalType(src *bufio.Reader, minor byte) int64 {
val := int64(0)
if minor <= 23 {
val = int64(minor)
} else {
bytesToRead := 0
switch minor {
case additionalTypeIntUint8:
bytesToRead = 1
case additionalTypeIntUint16:
bytesToRead = 2
case additionalTypeIntUint32:
bytesToRead = 4
case additionalTypeIntUint64:
bytesToRead = 8
default:
panic(fmt.Errorf("Invalid Additional Type: %d in decodeInteger (expected <28)", minor))
}
pb := readNBytes(src, bytesToRead)
for i := 0; i < bytesToRead; i++ {
val = val * 256
val += int64(pb[i])
}
}
return val
}
func decodeInteger(src *bufio.Reader) int64 {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeUnsignedInt && major != majorTypeNegativeInt {
panic(fmt.Errorf("Major type is: %d in decodeInteger!! (expected 0 or 1)", major))
}
val := decodeIntAdditionalType(src, minor)
if major == 0 {
return val
}
return (-1 - val)
}
func decodeFloat(src *bufio.Reader) (float64, int) {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeSimpleAndFloat {
panic(fmt.Errorf("Incorrect Major type is: %d in decodeFloat", major))
}
switch minor {
case additionalTypeFloat16:
panic(fmt.Errorf("float16 is not supported in decodeFloat"))
case additionalTypeFloat32:
pb := readNBytes(src, 4)
switch string(pb) {
case float32Nan:
return math.NaN(), isFloat32
case float32PosInfinity:
return math.Inf(0), isFloat32
case float32NegInfinity:
return math.Inf(-1), isFloat32
}
n := uint32(0)
for i := 0; i < 4; i++ {
n = n * 256
n += uint32(pb[i])
}
val := math.Float32frombits(n)
return float64(val), isFloat32
case additionalTypeFloat64:
pb := readNBytes(src, 8)
switch string(pb) {
case float64Nan:
return math.NaN(), isFloat64
case float64PosInfinity:
return math.Inf(0), isFloat64
case float64NegInfinity:
return math.Inf(-1), isFloat64
}
n := uint64(0)
for i := 0; i < 8; i++ {
n = n * 256
n += uint64(pb[i])
}
val := math.Float64frombits(n)
return val, isFloat64
}
panic(fmt.Errorf("Invalid Additional Type: %d in decodeFloat", minor))
}
func decodeStringComplex(dst []byte, s string, pos uint) []byte {
i := int(pos)
start := 0
for i < len(s) {
b := s[i]
if b >= utf8.RuneSelf {
r, size := utf8.DecodeRuneInString(s[i:])
if r == utf8.RuneError && size == 1 {
// In case of error, first append previous simple characters to
// the byte slice if any and append a replacement character code
// in place of the invalid sequence.
if start < i {
dst = append(dst, s[start:i]...)
}
dst = append(dst, `\ufffd`...)
i += size
start = i
continue
}
i += size
continue
}
if b >= 0x20 && b <= 0x7e && b != '\\' && b != '"' {
i++
continue
}
// We encountered a character that needs to be encoded.
// Let's append the previous simple characters to the byte slice
// and switch our operation to read and encode the remainder
// characters byte-by-byte.
if start < i {
dst = append(dst, s[start:i]...)
}
switch b {
case '"', '\\':
dst = append(dst, '\\', b)
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
dst = append(dst, '\\', 'u', '0', '0', hexTable[b>>4], hexTable[b&0xF])
}
i++
start = i
}
if start < len(s) {
dst = append(dst, s[start:]...)
}
return dst
}
func decodeString(src *bufio.Reader, noQuotes bool) []byte {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeByteString {
panic(fmt.Errorf("Major type is: %d in decodeString", major))
}
result := []byte{}
if !noQuotes {
result = append(result, '"')
}
length := decodeIntAdditionalType(src, minor)
len := int(length)
pbs := readNBytes(src, len)
result = append(result, pbs...)
if noQuotes {
return result
}
return append(result, '"')
}
func decodeStringToDataUrl(src *bufio.Reader, mimeType string) []byte {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeByteString {
panic(fmt.Errorf("Major type is: %d in decodeString", major))
}
length := decodeIntAdditionalType(src, minor)
l := int(length)
enc := base64.StdEncoding
lEnc := enc.EncodedLen(l)
result := make([]byte, len("\"data:;base64,\"")+len(mimeType)+lEnc)
dest := result
u := copy(dest, "\"data:")
dest = dest[u:]
u = copy(dest, mimeType)
dest = dest[u:]
u = copy(dest, ";base64,")
dest = dest[u:]
pbs := readNBytes(src, l)
enc.Encode(dest, pbs)
dest = dest[lEnc:]
dest[0] = '"'
return result
}
func decodeUTF8String(src *bufio.Reader) []byte {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeUtf8String {
panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major))
}
result := []byte{'"'}
length := decodeIntAdditionalType(src, minor)
len := int(length)
pbs := readNBytes(src, len)
for i := 0; i < len; i++ {
// Check if the character needs encoding. Control characters, slashes,
// and the double quote need json encoding. Bytes above the ascii
// boundary needs utf8 encoding.
if pbs[i] < 0x20 || pbs[i] > 0x7e || pbs[i] == '\\' || pbs[i] == '"' {
// We encountered a character that needs to be encoded. Switch
// to complex version of the algorithm.
dst := []byte{'"'}
dst = decodeStringComplex(dst, string(pbs), uint(i))
return append(dst, '"')
}
}
// The string has no need for encoding and therefore is directly
// appended to the byte slice.
result = append(result, pbs...)
return append(result, '"')
}
func array2Json(src *bufio.Reader, dst io.Writer) {
dst.Write([]byte{'['})
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeArray {
panic(fmt.Errorf("Major type is: %d in array2Json", major))
}
len := 0
unSpecifiedCount := false
if minor == additionalTypeInfiniteCount {
unSpecifiedCount = true
} else {
length := decodeIntAdditionalType(src, minor)
len = int(length)
}
for i := 0; unSpecifiedCount || i < len; i++ {
if unSpecifiedCount {
pb, e := src.Peek(1)
if e != nil {
panic(e)
}
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
readByte(src)
break
}
}
cbor2JsonOneObject(src, dst)
if unSpecifiedCount {
pb, e := src.Peek(1)
if e != nil {
panic(e)
}
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
readByte(src)
break
}
dst.Write([]byte{','})
} else if i+1 < len {
dst.Write([]byte{','})
}
}
dst.Write([]byte{']'})
}
func map2Json(src *bufio.Reader, dst io.Writer) {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeMap {
panic(fmt.Errorf("Major type is: %d in map2Json", major))
}
len := 0
unSpecifiedCount := false
if minor == additionalTypeInfiniteCount {
unSpecifiedCount = true
} else {
length := decodeIntAdditionalType(src, minor)
len = int(length)
}
dst.Write([]byte{'{'})
for i := 0; unSpecifiedCount || i < len; i++ {
if unSpecifiedCount {
pb, e := src.Peek(1)
if e != nil {
panic(e)
}
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
readByte(src)
break
}
}
cbor2JsonOneObject(src, dst)
if i%2 == 0 {
// Even position values are keys.
dst.Write([]byte{':'})
} else {
if unSpecifiedCount {
pb, e := src.Peek(1)
if e != nil {
panic(e)
}
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
readByte(src)
break
}
dst.Write([]byte{','})
} else if i+1 < len {
dst.Write([]byte{','})
}
}
}
dst.Write([]byte{'}'})
}
func decodeTagData(src *bufio.Reader) []byte {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeTags {
panic(fmt.Errorf("Major type is: %d in decodeTagData", major))
}
switch minor {
case additionalTypeTimestamp:
return decodeTimeStamp(src)
case additionalTypeIntUint8:
val := decodeIntAdditionalType(src, minor)
switch byte(val) {
case additionalTypeEmbeddedCBOR:
pb := readByte(src)
dataMajor := pb & maskOutAdditionalType
if dataMajor != majorTypeByteString {
panic(fmt.Errorf("Unsupported embedded Type: %d in decodeEmbeddedCBOR", dataMajor))
}
src.UnreadByte()
return decodeStringToDataUrl(src, "application/cbor")
default:
panic(fmt.Errorf("Unsupported Additional Tag Type: %d in decodeTagData", val))
}
// Tag value is larger than 256 (so uint16).
case additionalTypeIntUint16:
val := decodeIntAdditionalType(src, minor)
switch uint16(val) {
case additionalTypeEmbeddedJSON:
pb := readByte(src)
dataMajor := pb & maskOutAdditionalType
if dataMajor != majorTypeByteString {
panic(fmt.Errorf("Unsupported embedded Type: %d in decodeEmbeddedJSON", dataMajor))
}
src.UnreadByte()
return decodeString(src, true)
case additionalTypeTagNetworkAddr:
octets := decodeString(src, true)
ss := []byte{'"'}
switch len(octets) {
case 6: // MAC address.
ha := net.HardwareAddr(octets)
ss = append(append(ss, ha.String()...), '"')
case 4: // IPv4 address.
fallthrough
case 16: // IPv6 address.
ip := net.IP(octets)
ss = append(append(ss, ip.String()...), '"')
default:
panic(fmt.Errorf("Unexpected Network Address length: %d (expected 4,6,16)", len(octets)))
}
return ss
case additionalTypeTagNetworkPrefix:
pb := readByte(src)
if pb != majorTypeMap|0x1 {
panic(fmt.Errorf("IP Prefix is NOT of MAP of 1 elements as expected"))
}
octets := decodeString(src, true)
val := decodeInteger(src)
ip := net.IP(octets)
var mask net.IPMask
pfxLen := int(val)
if len(octets) == 4 {
mask = net.CIDRMask(pfxLen, 32)
} else {
mask = net.CIDRMask(pfxLen, 128)
}
ipPfx := net.IPNet{IP: ip, Mask: mask}
ss := []byte{'"'}
ss = append(append(ss, ipPfx.String()...), '"')
return ss
case additionalTypeTagHexString:
octets := decodeString(src, true)
ss := []byte{'"'}
for _, v := range octets {
ss = append(ss, hexTable[v>>4], hexTable[v&0x0f])
}
return append(ss, '"')
default:
panic(fmt.Errorf("Unsupported Additional Tag Type: %d in decodeTagData", val))
}
}
panic(fmt.Errorf("Unsupported Additional Type: %d in decodeTagData", minor))
}
func decodeTimeStamp(src *bufio.Reader) []byte {
pb := readByte(src)
src.UnreadByte()
tsMajor := pb & maskOutAdditionalType
if tsMajor == majorTypeUnsignedInt || tsMajor == majorTypeNegativeInt {
n := decodeInteger(src)
t := time.Unix(n, 0)
if decodeTimeZone != nil {
t = t.In(decodeTimeZone)
} else {
t = t.In(time.UTC)
}
tsb := []byte{}
tsb = append(tsb, '"')
tsb = t.AppendFormat(tsb, IntegerTimeFieldFormat)
tsb = append(tsb, '"')
return tsb
} else if tsMajor == majorTypeSimpleAndFloat {
n, _ := decodeFloat(src)
secs := int64(n)
n -= float64(secs)
n *= float64(1e9)
t := time.Unix(secs, int64(n))
if decodeTimeZone != nil {
t = t.In(decodeTimeZone)
} else {
t = t.In(time.UTC)
}
tsb := []byte{}
tsb = append(tsb, '"')
tsb = t.AppendFormat(tsb, NanoTimeFieldFormat)
tsb = append(tsb, '"')
return tsb
}
panic(fmt.Errorf("TS format is neither int nor float: %d", tsMajor))
}
func decodeSimpleFloat(src *bufio.Reader) []byte {
pb := readByte(src)
major := pb & maskOutAdditionalType
minor := pb & maskOutMajorType
if major != majorTypeSimpleAndFloat {
panic(fmt.Errorf("Major type is: %d in decodeSimpleFloat", major))
}
switch minor {
case additionalTypeBoolTrue:
return []byte("true")
case additionalTypeBoolFalse:
return []byte("false")
case additionalTypeNull:
return []byte("null")
case additionalTypeFloat16:
fallthrough
case additionalTypeFloat32:
fallthrough
case additionalTypeFloat64:
src.UnreadByte()
v, bc := decodeFloat(src)
ba := []byte{}
switch {
case math.IsNaN(v):
return []byte("\"NaN\"")
case math.IsInf(v, 1):
return []byte("\"+Inf\"")
case math.IsInf(v, -1):
return []byte("\"-Inf\"")
}
if bc == isFloat32 {
ba = strconv.AppendFloat(ba, v, 'f', -1, 32)
} else if bc == isFloat64 {
ba = strconv.AppendFloat(ba, v, 'f', -1, 64)
} else {
panic(fmt.Errorf("Invalid Float precision from decodeFloat: %d", bc))
}
return ba
default:
panic(fmt.Errorf("Invalid Additional Type: %d in decodeSimpleFloat", minor))
}
}
func cbor2JsonOneObject(src *bufio.Reader, dst io.Writer) {
pb, e := src.Peek(1)
if e != nil {
panic(e)
}
major := (pb[0] & maskOutAdditionalType)
switch major {
case majorTypeUnsignedInt:
fallthrough
case majorTypeNegativeInt:
n := decodeInteger(src)
dst.Write([]byte(strconv.Itoa(int(n))))
case majorTypeByteString:
s := decodeString(src, false)
dst.Write(s)
case majorTypeUtf8String:
s := decodeUTF8String(src)
dst.Write(s)
case majorTypeArray:
array2Json(src, dst)
case majorTypeMap:
map2Json(src, dst)
case majorTypeTags:
s := decodeTagData(src)
dst.Write(s)
case majorTypeSimpleAndFloat:
s := decodeSimpleFloat(src)
dst.Write(s)
}
}
func moreBytesToRead(src *bufio.Reader) bool {
_, e := src.ReadByte()
if e == nil {
src.UnreadByte()
return true
}
return false
}
// Cbor2JsonManyObjects decodes all the CBOR Objects read from src
// reader. It keeps on decoding until reader returns EOF (error when reading).
// Decoded string is written to the dst. At the end of every CBOR Object
// newline is written to the output stream.
//
// Returns error (if any) that was encountered during decode.
// The child functions will generate a panic when error is encountered and
// this function will recover non-runtime Errors and return the reason as error.
func Cbor2JsonManyObjects(src io.Reader, dst io.Writer) (err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
err = r.(error)
}
}()
bufRdr := bufio.NewReader(src)
for moreBytesToRead(bufRdr) {
cbor2JsonOneObject(bufRdr, dst)
dst.Write([]byte("\n"))
}
return nil
}
// Detect if the bytes to be printed is Binary or not.
func binaryFmt(p []byte) bool {
if len(p) > 0 && p[0] > 0x7F {
return true
}
return false
}
func getReader(str string) *bufio.Reader {
return bufio.NewReader(strings.NewReader(str))
}
// DecodeIfBinaryToString converts a binary formatted log msg to a
// JSON formatted String Log message - suitable for printing to Console/Syslog.
func DecodeIfBinaryToString(in []byte) string {
if binaryFmt(in) {
var b bytes.Buffer
Cbor2JsonManyObjects(strings.NewReader(string(in)), &b)
return b.String()
}
return string(in)
}
// DecodeObjectToStr checks if the input is a binary format, if so,
// it will decode a single Object and return the decoded string.
func DecodeObjectToStr(in []byte) string {
if binaryFmt(in) {
var b bytes.Buffer
cbor2JsonOneObject(getReader(string(in)), &b)
return b.String()
}
return string(in)
}
// DecodeIfBinaryToBytes checks if the input is a binary format, if so,
// it will decode all Objects and return the decoded string as byte array.
func DecodeIfBinaryToBytes(in []byte) []byte {
if binaryFmt(in) {
var b bytes.Buffer
Cbor2JsonManyObjects(bytes.NewReader(in), &b)
return b.Bytes()
}
return in
}
+489
View File
@@ -0,0 +1,489 @@
package cbor
import (
"bytes"
"encoding/hex"
"math"
"testing"
"time"
"github.com/rs/zerolog/internal"
)
func TestDecodeInteger(t *testing.T) {
for _, tc := range internal.IntegerTestCases {
gotv := decodeInteger(getReader(tc.Binary))
if gotv != int64(tc.Val) {
t.Errorf("decodeInteger(0x%s)=0x%d, want: 0x%d",
hex.EncodeToString([]byte(tc.Binary)), gotv, tc.Val)
}
}
}
func TestDecodeString(t *testing.T) {
for _, tt := range encodeStringTests {
got := decodeUTF8String(getReader(tt.binary))
if string(got) != "\""+tt.json+"\"" {
t.Errorf("DecodeString(0x%s)=%s, want:\"%s\"\n", hex.EncodeToString([]byte(tt.binary)), string(got),
hex.EncodeToString([]byte(tt.json)))
}
}
}
func TestDecodeArray(t *testing.T) {
for _, tc := range internal.IntegerArrayTestCases {
buf := bytes.NewBuffer([]byte{})
array2Json(getReader(tc.Binary), buf)
if buf.String() != tc.Json {
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.Binary)), buf.String(), tc.Json)
}
}
//Unspecified Length Array
var infiniteArrayTestCases = []struct {
in string
out string
}{
{"\x9f\x20\x00\x18\xc8\x14\xff", "[-1,0,200,20]"},
{"\x9f\x38\xc7\x29\x18\xc8\x19\x01\x90\xff", "[-200,-10,200,400]"},
{"\x9f\x01\x02\x03\xff", "[1,2,3]"},
{"\x9f\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x18\x18\x19\xff",
"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]"},
}
for _, tc := range infiniteArrayTestCases {
buf := bytes.NewBuffer([]byte{})
array2Json(getReader(tc.in), buf)
if buf.String() != tc.out {
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.out)), buf.String(), tc.out)
}
}
for _, tc := range internal.BooleanArrayTestCases {
buf := bytes.NewBuffer([]byte{})
array2Json(getReader(tc.Binary), buf)
if buf.String() != tc.Json {
t.Errorf("array2Json(0x%s)=%s, want: %s", hex.EncodeToString([]byte(tc.Binary)), buf.String(), tc.Json)
}
}
//TODO add cases for arrays of other types
}
var infiniteMapDecodeTestCases = []struct {
Bin []byte
Json string
}{
{[]byte("\xbf\x64IETF\x20\xff"), "{\"IETF\":-1}"},
{[]byte("\xbf\x65Array\x84\x20\x00\x18\xc8\x14\xff"), "{\"Array\":[-1,0,200,20]}"},
}
var mapDecodeTestCases = []struct {
Bin []byte
Json string
}{
{[]byte("\xa2\x64IETF\x20"), "{\"IETF\":-1}"},
{[]byte("\xa2\x65Array\x84\x20\x00\x18\xc8\x14"), "{\"Array\":[-1,0,200,20]}"},
{[]byte("\xa6\x61\x61\x01\x61\x62\x02\x61\x63\x03"), "{\"a\":1,\"b\":2,\"c\":3}"},
{[]byte("\xbf\x61a\x01\x61b\x02\xff"), "{\"a\":1,\"b\":2}"},
}
func TestDecodeMap(t *testing.T) {
for _, tc := range mapDecodeTestCases {
buf := bytes.NewBuffer([]byte{})
map2Json(getReader(string(tc.Bin)), buf)
if buf.String() != tc.Json {
t.Errorf("map2Json(0x%s)=%s, want: %s", hex.EncodeToString(tc.Bin), buf.String(), tc.Json)
}
}
for _, tc := range infiniteMapDecodeTestCases {
buf := bytes.NewBuffer([]byte{})
map2Json(getReader(string(tc.Bin)), buf)
if buf.String() != tc.Json {
t.Errorf("map2Json(0x%s)=%s, want: %s", hex.EncodeToString(tc.Bin), buf.String(), tc.Json)
}
}
}
func TestDecodeBool(t *testing.T) {
for _, tc := range internal.BooleanTestCases {
got := decodeSimpleFloat(getReader(tc.Binary))
if string(got) != tc.Json {
t.Errorf("decodeSimpleFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), string(got), tc.Json)
}
}
}
func TestDecodeFloat(t *testing.T) {
for _, tc := range internal.Float32TestCases {
got, _ := decodeFloat(getReader(tc.Binary))
if got != float64(tc.Val) && math.IsNaN(got) != math.IsNaN(float64(tc.Val)) {
t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.Binary)), got, tc.Val)
}
}
for _, tc := range internal.Float64TestCases {
got, _ := decodeFloat(getReader(tc.Binary))
if got != tc.Val && math.IsNaN(got) != math.IsNaN(tc.Val) {
t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.Binary)), got, tc.Val)
}
}
// Test float64 special values with correct CBOR encoding
float64Tests := []struct {
name string
input string
want float64
}{
{"float64 NaN", "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00", math.NaN()},
{"float64 +Inf", "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00", math.Inf(0)},
{"float64 -Inf", "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00", math.Inf(-1)},
{"float64 1.0", "\xfb\x3f\xf0\x00\x00\x00\x00\x00\x00", 1.0},
}
for _, tt := range float64Tests {
t.Run(tt.name, func(t *testing.T) {
got, _ := decodeFloat(getReader(tt.input))
if math.IsNaN(tt.want) {
if !math.IsNaN(got) {
t.Errorf("decodeFloat(%q) = %f, want NaN", tt.input, got)
}
} else if math.IsInf(tt.want, 0) {
if !math.IsInf(got, 0) {
t.Errorf("decodeFloat(%q) = %f, want +Inf", tt.input, got)
}
} else if math.IsInf(tt.want, -1) {
if !math.IsInf(got, -1) {
t.Errorf("decodeFloat(%q) = %f, want -Inf", tt.input, got)
}
} else if got != tt.want {
t.Errorf("decodeFloat(%q) = %f, want %f", tt.input, got, tt.want)
}
})
}
}
func TestDecodeTimestamp(t *testing.T) {
decodeTimeZone, _ = time.LoadLocation("UTC")
for _, tc := range internal.TimeIntegerTestcases {
tm := decodeTagData(getReader(tc.Binary))
if string(tm) != "\""+tc.RfcStr+"\"" {
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), tm, tc.RfcStr)
}
}
for _, tc := range internal.TimeFloatTestcases {
tm := decodeTagData(getReader(tc.Out))
//Since we convert to float and back - it may be slightly off - so
//we cannot check for exact equality instead, we'll check it is
//very close to each other Less than a Microsecond (lets not yet do nanosec)
got, _ := time.Parse(string(tm), string(tm))
want, _ := time.Parse(tc.RfcStr, tc.RfcStr)
if got.Sub(want) > time.Microsecond {
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Out)), tm, tc.RfcStr)
}
}
// Test with decodeTimeZone = nil to cover the else branches
oldTimeZone := decodeTimeZone
decodeTimeZone = nil
defer func() { decodeTimeZone = oldTimeZone }()
for _, tc := range internal.TimeIntegerTestcases {
tm := decodeTagData(getReader(tc.Binary))
if string(tm) != "\""+tc.RfcStr+"\"" {
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), tm, tc.RfcStr)
}
}
for _, tc := range internal.TimeFloatTestcases {
tm := decodeTagData(getReader(tc.Out))
got, _ := time.Parse(string(tm), string(tm))
want, _ := time.Parse(tc.RfcStr, tc.RfcStr)
if got.Sub(want) > time.Microsecond {
t.Errorf("decodeFloat(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Out)), tm, tc.RfcStr)
}
}
}
func TestDecodeNetworkAddr(t *testing.T) {
for _, tc := range internal.IpAddrTestCases {
d1 := decodeTagData(getReader(tc.Binary))
if string(d1) != tc.Text {
t.Errorf("decodeNetworkAddr(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
}
}
}
func TestDecodeMACAddr(t *testing.T) {
for _, tc := range internal.MacAddrTestCases {
d1 := decodeTagData(getReader(tc.Binary))
if string(d1) != tc.Text {
t.Errorf("decodeNetworkAddr(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
}
}
}
func TestDecodeIPPrefix(t *testing.T) {
for _, tc := range internal.IPPrefixTestCases {
d1 := decodeTagData(getReader(tc.Binary))
if string(d1) != tc.Text {
t.Errorf("decodeIPPrefix(0x%s)=%s, want:%s", hex.EncodeToString([]byte(tc.Binary)), d1, tc.Text)
}
}
}
var compositeCborTestCases = []struct {
Binary []byte
Json string
}{
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14\xff\xff"), "{\"IETF\":-1,\"Array\":[-1,0,200,20]}\n"},
{[]byte("\xbf\x64IETF\x64YES!\x65Array\x9f\x20\x00\x18\xc8\x14\xff\xff"), "{\"IETF\":\"YES!\",\"Array\":[-1,0,200,20]}\n"},
{[]byte("\xbf\x61a\x01\x61b\x02\x61c\x03\xff"), "{\"a\":1,\"b\":2,\"c\":3}\n"},
{[]byte("\xc1\x1a\x51\x0f\x30\xd8"), "\"2013-02-04T03:54:00Z\"\n"},
}
func TestDecodeCbor2Json(t *testing.T) {
for _, tc := range compositeCborTestCases {
buf := bytes.NewBuffer([]byte{})
err := Cbor2JsonManyObjects(getReader(string(tc.Binary)), buf)
if buf.String() != tc.Json || err != nil {
t.Errorf("cbor2JsonManyObjects(0x%s)=%s, want: %s, err:%s", hex.EncodeToString(tc.Binary), buf.String(), tc.Json, err.Error())
}
}
}
var negativeCborTestCases = []struct {
Binary []byte
errStr string
}{
{[]byte("\xb9\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "Tried to Read 18 Bytes.. But hit end of file"},
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "EOF"},
{[]byte("\xbf\x14IETF\x20\x65Array\x9f\x20\x00\x18\xc8\x14"), "Tried to Read 40736 Bytes.. But hit end of file"},
{[]byte("\xbf\x64IETF"), "EOF"},
{[]byte("\xbf\x64IETF\x20\x65Array\x9f\x20\x00\x18\xc8\xff\xff\xff"), "Invalid Additional Type: 31 in decodeSimpleFloat"},
{[]byte("\xbf\x64IETF\x20\x65Array"), "EOF"},
{[]byte("\xbf\x64"), "Tried to Read 4 Bytes.. But hit end of file"},
}
func TestDecodeNegativeCbor2Json(t *testing.T) {
for _, tc := range negativeCborTestCases {
buf := bytes.NewBuffer([]byte{})
err := Cbor2JsonManyObjects(getReader(string(tc.Binary)), buf)
if err == nil || err.Error() != tc.errStr {
t.Errorf("Expected error got:%s, want:%s", err, tc.errStr)
}
}
}
func TestBinaryFmt(t *testing.T) {
tests := []struct {
input []byte
want bool
}{
{[]byte{}, false},
{[]byte{0x00}, false},
{[]byte{0x7F}, false},
{[]byte{0x80}, true},
{[]byte{0xFF}, true},
{[]byte{0x00, 0x80}, false}, // Only checks first byte
}
for _, tt := range tests {
got := binaryFmt(tt.input)
if got != tt.want {
t.Errorf("binaryFmt(%v) = %v, want %v", tt.input, got, tt.want)
}
}
}
func TestDecodeIfBinaryToString(t *testing.T) {
tests := []struct {
name string
input []byte
want string
}{
{
name: "non-binary input",
input: []byte(`{"key":"value"}`),
want: `{"key":"value"}`,
},
{
name: "binary input - simple object",
input: []byte("\xbf\x64IETF\x20\xff"), // {"IETF": -1} in indefinite length CBOR
want: "{\"IETF\":-1}\n",
},
{
name: "binary input - multiple objects",
input: []byte("\xbf\x64IETF\x20\xff\xbf\x65Array\x84\x20\x00\x18\xc8\x14\xff"), // Two objects
want: "{\"IETF\":-1}\n{\"Array\":[-1,0,200,20]}\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := DecodeIfBinaryToString(tt.input)
if got != tt.want {
t.Errorf("DecodeIfBinaryToString() = %q, want %q", got, tt.want)
}
})
}
}
func TestDecodeObjectToStr(t *testing.T) {
tests := []struct {
name string
input []byte
want string
}{
{
name: "non-binary input",
input: []byte(`{"key":"value"}`),
want: `{"key":"value"}`,
},
{
name: "binary input - simple object",
input: []byte("\xbf\x64IETF\x20\xff"), // {"IETF": -1} in indefinite length CBOR
want: "{\"IETF\":-1}",
},
{
name: "binary input - array",
input: []byte("\x84\x20\x00\x18\xc8\x14"), // [-1, 0, 200, 20]
want: "[-1,0,200,20]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := DecodeObjectToStr(tt.input)
if got != tt.want {
t.Errorf("DecodeObjectToStr() = %q, want %q", got, tt.want)
}
})
}
}
func TestDecodeIfBinaryToBytes(t *testing.T) {
tests := []struct {
name string
input []byte
want []byte
}{
{
name: "non-binary input",
input: []byte(`{"key":"value"}`),
want: []byte(`{"key":"value"}`),
},
{
name: "binary input - simple object",
input: []byte("\xbf\x64IETF\x20\xff"), // {"IETF": -1} in indefinite length CBOR
want: []byte("{\"IETF\":-1}\n"),
},
{
name: "binary input - multiple objects",
input: []byte("\xbf\x64IETF\x20\xff\xbf\x65Array\x84\x20\x00\x18\xc8\x14\xff"), // Two objects
want: []byte("{\"IETF\":-1}\n{\"Array\":[-1,0,200,20]}\n"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := DecodeIfBinaryToBytes(tt.input)
if !bytes.Equal(got, tt.want) {
t.Errorf("DecodeIfBinaryToBytes() = %q, want %q", string(got), string(tt.want))
}
})
}
}
func TestDecodeEmbeddedCBOR(t *testing.T) {
// Test embedded CBOR tag: 0xD8 0x3F (tag 63) followed by byte string
// 0xD8 = major type 6 (tags) + additional type 24 (uint8 follows)
// 0x3F = 63 (additionalTypeEmbeddedCBOR)
// 0x43 = major type 2 (byte string) + length 3
// 0x01 0x02 0x03 = the embedded CBOR data
embeddedCBOR := []byte("\xd8\x3f\x43\x01\x02\x03")
expected := "\"data:application/cbor;base64,AQID\""
got := decodeTagData(getReader(string(embeddedCBOR)))
if string(got) != expected {
t.Errorf("decodeTagData(embedded CBOR) = %q, want %q", string(got), expected)
}
}
func TestDecodeEmbeddedJSON(t *testing.T) {
t.Run("valid embedded JSON", func(t *testing.T) {
// Test embedded JSON tag: 0xD9 0x01 0x06 (tag 262) followed by byte string.
// 0xD9 = major type 6 (tags) + additional type 25 (uint16 follows)
// 0x01 0x06 = 262 (additionalTypeEmbeddedJSON)
// 0x47 = major type 2 (byte string) + length 7
// {"a":1} = embedded JSON payload (no surrounding quotes expected)
embeddedJSON := []byte("\xd9\x01\x06\x47{\"a\":1}")
expected := "{\"a\":1}"
got := decodeTagData(getReader(string(embeddedJSON)))
if string(got) != expected {
t.Errorf("decodeTagData(embedded JSON) = %q, want %q", string(got), expected)
}
})
t.Run("unsupported embedded type panics", func(t *testing.T) {
// Same embedded JSON tag, but followed by a UTF-8 string instead of a byte string.
// This should hit the "Unsupported embedded Type" panic branch.
bad := []byte("\xd9\x01\x06\x61x")
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected panic, got none")
}
}()
_ = decodeTagData(getReader(string(bad)))
})
}
func TestDecodeHexString(t *testing.T) {
// Test hex string tag: 0xD9 0x01 0x07 (tag 263) followed by byte string
// 0xD9 = major type 6 (tags) + additional type 25 (uint16 follows)
// 0x01 0x07 = 263 (additionalTypeTagHexString)
// 0x43 = major type 2 (byte string) + length 3
// 0x01 0x02 0x03 = the byte data to hex encode
hexString := []byte("\xd9\x01\x07\x43\x01\x02\x03")
expected := "\"010203\""
got := decodeTagData(getReader(string(hexString)))
if string(got) != expected {
t.Errorf("decodeTagData(hex string) = %q, want %q", string(got), expected)
}
}
func TestDecodeSimpleFloat(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
// Boolean and null cases (already covered)
{"true", "\xf5", "true"},
{"false", "\xf4", "false"},
{"null", "\xf6", "null"},
// Float32 cases
{"float32 1.0", "\xfa\x3f\x80\x00\x00", "1"},
{"float32 1.5", "\xfa\x3f\xc0\x00\x00", "1.5"},
{"float32 +Inf", "\xfa\x7f\x80\x00\x00", "\"+Inf\""},
{"float32 -Inf", "\xfa\xff\x80\x00\x00", "\"-Inf\""},
{"float32 NaN", "\xfa\x7f\xc0\x00\x00", "\"NaN\""},
// Float64 cases
{"float64 1.0", "\xfb\x3f\xf0\x00\x00\x00\x00\x00\x00", "1"},
{"float64 +Inf", "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00", "\"+Inf\""},
{"float64 -Inf", "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00", "\"-Inf\""},
{"float64 NaN", "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00", "\"NaN\""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := decodeSimpleFloat(getReader(tt.input))
if string(got) != tt.want {
t.Errorf("decodeSimpleFloat(%q) = %q, want %q", tt.input, string(got), tt.want)
}
})
}
}
+55
View File
@@ -0,0 +1,55 @@
package main
import (
"compress/zlib"
"flag"
"io"
"log"
"os"
"time"
"github.com/rs/zerolog"
)
func writeLog(fname string, count int, useCompress bool) {
opFile := os.Stdout
if fname != "<stdout>" {
fil, _ := os.Create(fname)
opFile = fil
defer func() {
if err := fil.Close(); err != nil {
log.Fatal(err)
}
}()
}
var f io.WriteCloser = opFile
if useCompress {
f = zlib.NewWriter(f)
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
}
zerolog.TimestampFunc = func() time.Time { return time.Now().Round(time.Second) }
log := zerolog.New(f).With().
Timestamp().
Logger()
for i := 0; i < count; i++ {
log.Error().
Int("Fault", 41650+i).Msg("Some Message")
}
}
func main() {
outFile := flag.String("out", "<stdout>", "Output File to which logs will be written to (WILL overwrite if already present).")
numLogs := flag.Int("num", 10, "Number of log messages to generate.")
doCompress := flag.Bool("compress", false, "Enable inline compressed writer")
flag.Parse()
writeLog(*outFile, *numLogs, *doCompress)
}
+10
View File
@@ -0,0 +1,10 @@
all: genLogJSON genLogCBOR
genLogJSON: genLog.go
go build -o genLogJSON genLog.go
genLogCBOR: genLog.go
go build -tags binary_log -o genLogCBOR genLog.go
clean:
rm -f genLogJSON genLogCBOR
+117
View File
@@ -0,0 +1,117 @@
package cbor
import "fmt"
// AppendStrings encodes and adds an array of strings to the dst byte array.
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
major := majorTypeArray
l := len(vals)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendString(dst, v)
}
return dst
}
// AppendString encodes and adds a string to the dst byte array.
func (Encoder) AppendString(dst []byte, s string) []byte {
major := majorTypeUtf8String
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l))
}
return append(dst, s...)
}
// AppendStringers encodes and adds an array of Stringer values
// to the dst byte array.
func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte {
if vals == nil || len(vals) == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
dst = e.AppendArrayStart(dst)
dst = e.AppendStringer(dst, vals[0])
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = e.AppendStringer(dst, val)
}
}
return e.AppendArrayEnd(dst)
}
// AppendStringer encodes and adds the Stringer value to the dst
// byte array.
func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte {
if val == nil {
return e.AppendNil(dst)
}
return e.AppendString(dst, val.String())
}
// AppendBytes encodes and adds an array of bytes to the dst byte array.
func (Encoder) AppendBytes(dst, s []byte) []byte {
major := majorTypeByteString
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
return append(dst, s...)
}
// AppendEmbeddedJSON adds a tag and embeds input JSON as such.
func AppendEmbeddedJSON(dst, s []byte) []byte {
major := majorTypeTags
minor := additionalTypeEmbeddedJSON
// Append the TAG to indicate this is Embedded JSON.
dst = append(dst, major|additionalTypeIntUint16)
dst = append(dst, byte(minor>>8))
dst = append(dst, byte(minor&0xff))
// Append the JSON Object as Byte String.
major = majorTypeByteString
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
return append(dst, s...)
}
// AppendEmbeddedCBOR adds a tag and embeds input CBOR as such.
func AppendEmbeddedCBOR(dst, s []byte) []byte {
major := majorTypeTags
minor := additionalTypeEmbeddedCBOR
// Append the TAG to indicate this is Embedded JSON.
dst = append(dst, major|additionalTypeIntUint8)
dst = append(dst, minor)
// Append the CBOR Object as Byte String.
major = majorTypeByteString
l := len(s)
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
return append(dst, s...)
}
+283
View File
@@ -0,0 +1,283 @@
package cbor
import (
"bytes"
"encoding/hex"
"testing"
"github.com/rs/zerolog/internal"
)
var encodeStringTests = []struct {
plain string
binary string
json string //begin and end quotes are implied
}{
{"", "\x60", ""},
{"\\", "\x61\x5c", "\\\\"},
{"\"", "\x61\x22", "\\\""},
{"\b", "\x61\x08", "\\b"},
{"\f", "\x61\x0c", "\\f"},
{"\n", "\x61\x0a", "\\n"},
{"\r", "\x61\x0d", "\\r"},
{"\t", "\x61\x09", "\\t"},
{"Hi\t", "\x63Hi\x09", "Hi\\t"},
{"\x00", "\x61\x00", "\\u0000"},
{"\x01", "\x61\x01", "\\u0001"},
{"\x02", "\x61\x02", "\\u0002"},
{"\x03", "\x61\x03", "\\u0003"},
{"\x04", "\x61\x04", "\\u0004"},
{"*", "\x61*", "*"},
{"a", "\x61a", "a"},
{"IETF", "\x64IETF", "IETF"},
{"abcdefghijklmnopqrstuvwxyzABCD", "\x78\x1eabcdefghijklmnopqrstuvwxyzABCD", "abcdefghijklmnopqrstuvwxyzABCD"},
{"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->",
"\x79\x01\x2c<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->",
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->"},
{"emoji \u2764\ufe0f!", "\x6demoji ❤️!", "emoji \u2764\ufe0f!"},
{"invalid utf8 \xff", "\x6einvalid utf8 \xff", "invalid utf8 \\ufffd"},
}
var encodeByteTests = []struct {
plain []byte
binary string
}{
{[]byte{}, "\x40"},
{[]byte("\\"), "\x41\x5c"},
{[]byte("\x00"), "\x41\x00"},
{[]byte("\x01"), "\x41\x01"},
{[]byte("\x02"), "\x41\x02"},
{[]byte("\x03"), "\x41\x03"},
{[]byte("\x04"), "\x41\x04"},
{[]byte("\f"), "\x41\x0C"},
{[]byte("\n"), "\x41\x0A"},
{[]byte("\r"), "\x41\x0D"},
{[]byte("*"), "\x41*"},
{[]byte("a"), "\x41a"},
{[]byte("IETF"), "\x44IETF"},
{[]byte("abcdefghijklmnopqrstuvwxyzABCD"), "\x58\x1eabcdefghijklmnopqrstuvwxyzABCD"},
{[]byte("<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->"),
"\x59\x01\x2c<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->" +
"<------------------------------------ This is a 100 character string ----------------------------->"},
{[]byte("emoji \u2764\ufe0f!"), "\x4demoji ❤️!"},
}
func TestAppendString(t *testing.T) {
for _, tt := range encodeStringTests {
b := enc.AppendString([]byte{}, tt.plain)
if got, want := string(b), tt.binary; got != want {
t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want)
}
}
//Test a large string > 65535 length
var buffer bytes.Buffer
for i := 0; i < 0x00011170; i++ { //70,000 character string
buffer.WriteString("a")
}
inp := buffer.String()
want := "\x7a\x00\x01\x11\x70" + inp
b := enc.AppendString([]byte{}, inp)
if got := string(b); got != want {
t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
}
}
func TestAppendStrings(t *testing.T) {
array := []string{}
for _, tt := range encodeStringTests {
array = append(array, tt.plain)
}
want := make([]byte, 0)
want = append(want, 0x95) // start array
for _, tt := range encodeStringTests {
want = append(want, []byte(tt.binary)...)
}
got := enc.AppendStrings([]byte{}, array)
if !bytes.Equal(got, want) {
t.Errorf("AppendStrings(%v)\ngot: 0x%s\nwant: 0x%s",
array,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now empty array case
array = make([]string, 0)
want = make([]byte, 0)
want = append(want, 0x80) // start an empty string array
got = enc.AppendStrings([]byte{}, array)
if !bytes.Equal(got, want) {
t.Errorf("AppendStrings(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now large array case
array = make([]string, 24)
want = make([]byte, 0)
want = append(want, 0x98) // start a large array
want = append(want, 0x18) // of length 24
for i := 0; i < len(array); i++ {
array[i] = "test"
want = append(want, []byte("\x64test")...)
}
got = enc.AppendStrings([]byte{}, array)
if !bytes.Equal(got, want) {
t.Errorf("AppendStrings(%v)\ngot: %s\nwant: %s",
array,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
func TestAppendStringer(t *testing.T) {
oldJSONMarshalFunc := JSONMarshalFunc
defer func() {
JSONMarshalFunc = oldJSONMarshalFunc
}()
JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return internal.InterfaceMarshalFunc(v)
}
for _, tt := range internal.EncodeStringerTests {
got := enc.AppendStringer([]byte{}, tt.In)
want := []byte(tt.Binary)
if !bytes.Equal(got, want) {
t.Errorf("AppendStrings(%v)\ngot: %s\nwant: %s",
tt.In,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
}
func TestAppendStringers(t *testing.T) {
for _, tt := range internal.EncodeStringersTests {
want := make([]byte, 0)
want = append(want, []byte(tt.Binary)...)
got := enc.AppendStringers([]byte{}, tt.In)
if !bytes.Equal(got, want) {
t.Errorf("AppendStrings(%v)\ngot: %s\nwant: %s",
tt,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
}
func TestAppendBytes(t *testing.T) {
for _, tt := range encodeByteTests {
b := enc.AppendBytes([]byte{}, tt.plain)
if got, want := string(b), tt.binary; got != want {
t.Errorf("appendString(%q) = %#q, want %#q", tt.plain, got, want)
}
}
//Test a large string > 65535 length
inp := []byte{}
for i := 0; i < 0x00011170; i++ { //70,000 character string
inp = append(inp, byte('a'))
}
want := "\x5a\x00\x01\x11\x70" + string(inp)
b := enc.AppendBytes([]byte{}, inp)
if got := string(b); got != want {
t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
}
}
func BenchmarkAppendString(b *testing.B) {
tests := map[string]string{
"NoEncoding": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingFirst": `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa"aaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`,
"MultiBytesFirst": `❤️aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa❤️aaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa❤️`,
}
for name, str := range tests {
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 120)
for i := 0; i < b.N; i++ {
_ = enc.AppendString(buf, str)
}
})
}
}
func TestAppendEmbeddedJSON(t *testing.T) {
tests := []struct {
name string
input []byte
want string
}{
{
name: "empty JSON",
input: []byte{},
want: "\xd9\x01\x06@", // tag 0xd9 + empty byte string
},
{
name: "small JSON",
input: []byte(`{"key":"value"}`),
want: "\xd9\x01\x06O{\"key\":\"value\"}", // tag 0xd9 + byte string with content
},
{
name: "large JSON (>23 bytes)",
input: []byte(`{"key":"this is a very long value that exceeds the 23 byte limit for direct encoding"}`),
want: "\xd9\x01\x06XV{\"key\":\"this is a very long value that exceeds the 23 byte limit for direct encoding\"}",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AppendEmbeddedJSON([]byte{}, tt.input)
if string(got) != tt.want {
t.Errorf("AppendEmbeddedJSON() = %q, want %q", string(got), tt.want)
}
})
}
}
func TestAppendEmbeddedCBOR(t *testing.T) {
tests := []struct {
name string
input []byte
want string
}{
{
name: "empty CBOR",
input: []byte{},
want: "\xd8?@", // tag 0xd8 + empty byte string
},
{
name: "small CBOR",
input: []byte{0x01, 0x02, 0x03},
want: "\xd8?C\x01\x02\x03", // tag 0xd8 + byte string with 3 bytes
},
{
name: "large CBOR (>23 bytes)",
input: make([]byte, 30), // 30 bytes of zeros
want: "\xd8?X\x1e" + string(make([]byte, 30)), // tag 0xd8 + byte string with length prefix
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := AppendEmbeddedCBOR([]byte{}, tt.input)
if string(got) != tt.want {
t.Errorf("AppendEmbeddedCBOR() = %q, want %q", string(got), tt.want)
}
})
}
}
+111
View File
@@ -0,0 +1,111 @@
package cbor
import (
"time"
)
const (
// Import from zerolog/global.go
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
timeFormatUnixMicro = "UNIXMICRO"
timeFormatUnixNano = "UNIXNANO"
durationFormatFloat = "float"
durationFormatInt = "int"
durationFormatString = "string"
)
func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
major := majorTypeTags
minor := additionalTypeTimestamp
dst = append(dst, major|minor)
secs := t.Unix()
var val uint64
if secs < 0 {
major = majorTypeNegativeInt
val = uint64(-secs - 1)
} else {
major = majorTypeUnsignedInt
val = uint64(secs)
}
dst = appendCborTypePrefix(dst, major, val)
return dst
}
func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte {
major := majorTypeTags
minor := additionalTypeTimestamp
dst = append(dst, major|minor)
secs := t.Unix()
nanos := t.Nanosecond()
val := float64(secs)*1.0 + float64(nanos)*1e-9
return e.AppendFloat64(dst, val, -1)
}
// AppendTime encodes and adds a timestamp to the dst byte array.
func (e Encoder) AppendTime(dst []byte, t time.Time, unused string) []byte {
utc := t.UTC()
if utc.Nanosecond() == 0 {
return appendIntegerTimestamp(dst, utc)
}
return e.appendFloatTimestamp(dst, utc)
}
// AppendTimes encodes and adds an array of timestamps to the dst byte array.
func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, t := range vals {
dst = e.AppendTime(dst, t, unused)
}
return dst
}
// AppendDuration encodes and adds a duration to the dst byte array.
// useInt field indicates whether to store the duration as seconds (integer) or
// as seconds+nanoseconds (float).
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, unused int) []byte {
if useInt {
return e.AppendInt64(dst, int64(d/unit))
}
switch format {
case durationFormatFloat:
return e.AppendFloat64(dst, float64(d)/float64(unit), unused)
case durationFormatInt:
return e.AppendInt64(dst, int64(d/unit))
case durationFormatString:
return e.AppendString(dst, d.String())
}
return e.AppendFloat64(dst, float64(d)/float64(unit), unused)
}
// AppendDurations encodes and adds an array of durations to the dst byte array.
// useInt field indicates whether to store the duration as seconds (integer) or
// as seconds+nanoseconds (float).
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, unused int) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, d := range vals {
dst = e.AppendDuration(dst, d, unit, format, useInt, unused)
}
return dst
}
+348
View File
@@ -0,0 +1,348 @@
package cbor
import (
"bytes"
"encoding/hex"
"fmt"
"math"
"reflect"
"testing"
"time"
"github.com/rs/zerolog/internal"
)
func TestEncoder_AppendDuration(t *testing.T) {
type args struct {
dst []byte
d time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
d: 1234567890,
unit: time.Second,
useInt: true,
},
want: []byte{1},
},
{
name: "formatFloat",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{251, 63, 243, 192, 202, 66, 131, 222, 27},
},
{
name: "formatInt",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatInt,
},
want: []byte{1},
},
{
name: "formatString",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatString,
},
want: []byte{107, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115},
},
{
name: "formatBlank",
args: args{
d: 1234567890,
unit: time.Second,
},
want: []byte{251, 63, 243, 192, 202, 66, 131, 222, 27},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDuration(tt.args.dst, tt.args.d, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestEncoder_AppendDurations(t *testing.T) {
type args struct {
dst []byte
vals []time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
useInt: true,
},
want: []byte{129, 1},
},
{
name: "formatFloat",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{129, 251, 63, 243, 192, 202, 66, 131, 222, 27},
},
{
name: "formatInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatInt,
},
want: []byte{129, 1},
},
{
name: "formatString",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatString,
},
want: []byte{129, 107, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115},
},
{
name: "formatBlank",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
},
want: []byte{129, 251, 63, 243, 192, 202, 66, 131, 222, 27},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDurations(tt.args.dst, tt.args.vals, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDurations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAppendTimeNow(t *testing.T) {
tm := time.Now()
s := enc.AppendTime([]byte{}, tm, "unused")
got := string(s)
tm1 := float64(tm.Unix()) + float64(tm.Nanosecond())*1e-9
tm2 := math.Float64bits(tm1)
var tm3 [8]byte
for i := uint(0); i < 8; i++ {
tm3[i] = byte(tm2 >> ((8 - i - 1) * 8))
}
want := append([]byte{0xc1, 0xfb}, tm3[:]...)
if got != string(want) {
t.Errorf("Appendtime(%s)=0x%s, want: 0x%s",
"time.Now()", hex.EncodeToString(s),
hex.EncodeToString(want))
}
}
func TestAppendTimePastPresentInteger(t *testing.T) {
for _, tt := range internal.TimeIntegerTestcases {
tin, err := time.Parse(time.RFC3339, tt.Txt)
if err != nil {
fmt.Println("Cannot parse input", tt.Txt, ".. Skipping!", err)
continue
}
b := enc.AppendTime([]byte{}, tin, "unused")
if got, want := string(b), tt.Binary; got != want {
t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.Txt,
hex.EncodeToString(b),
hex.EncodeToString([]byte(want)))
}
}
}
func TestAppendTimePastPresentFloat(t *testing.T) {
const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
for _, tt := range internal.TimeFloatTestcases {
tin, err := time.Parse(timeFloatFmt, tt.RfcStr)
if err != nil {
fmt.Println("Cannot parse input", tt.RfcStr, ".. Skipping!")
continue
}
b := enc.AppendTime([]byte{}, tin, "unused")
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendString(%s) = 0x%s, want 0x%s", tt.RfcStr,
hex.EncodeToString(b),
hex.EncodeToString([]byte(want)))
}
}
}
func TestAppendTimes(t *testing.T) {
const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
array := make([]time.Time, len(internal.TimeFloatTestcases))
want := make([]byte, 0)
want = append(want, 0x82) // start small array
for i, tt := range internal.TimeFloatTestcases {
array[i], _ = time.Parse(timeFloatFmt, tt.RfcStr)
want = append(want, []byte(tt.Out)...)
}
got := enc.AppendTimes([]byte{}, array, "unused")
if !bytes.Equal(got, want) {
t.Errorf("AppendTimes(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now empty array case
array = make([]time.Time, 0)
want = make([]byte, 0)
want = append(want, 0x9f) // start and end array
want = append(want, 0xff) // for empty array
got = enc.AppendTimes([]byte{}, array, "unused")
if !bytes.Equal(got, want) {
t.Errorf("AppendTimes(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now large array case
testtime, _ := time.Parse(timeFloatFmt, internal.TimeFloatTestcases[0].RfcStr)
outbytes := internal.TimeFloatTestcases[0].Out
array = make([]time.Time, 24)
want = make([]byte, 0)
want = append(want, 0x98) // start a large array
want = append(want, 0x18) // of length 24
for i := 0; i < len(array); i++ {
array[i] = testtime
want = append(want, []byte(outbytes)...)
}
got = enc.AppendTimes([]byte{}, array, "unused")
if !bytes.Equal(got, want) {
t.Errorf("AppendTimes(%v)\ngot: 0x%s\nwant: 0x%s",
array,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
func TestAppendDurationFloat(t *testing.T) {
for _, tt := range internal.DurTestcases {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(tt.FloatOut)...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: 0x%s\nwant: 0x%s",
dur,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
}
func TestAppendDurationInteger(t *testing.T) {
for _, tt := range internal.DurTestcases {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(tt.IntegerOut)...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", true, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: 0x%s\nwant: 0x%s",
dur,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
}
func TestAppendDurations(t *testing.T) {
array := make([]time.Duration, len(internal.DurTestcases))
want := make([]byte, 0)
want = append(want, 0x83) // start 3 element array
for i, tt := range internal.DurTestcases {
array[i] = tt.Duration
want = append(want, []byte(tt.FloatOut)...)
}
got := enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now empty array case
array = make([]time.Duration, 0)
want = make([]byte, 0)
want = append(want, 0x9f) // start and end array
want = append(want, 0xff) // for empty array
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array, hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now large array case
testtime := internal.DurTestcases[0].Duration
outbytes := internal.DurTestcases[0].FloatOut
array = make([]time.Duration, 24)
want = make([]byte, 0)
want = append(want, 0x98) // start a large array
want = append(want, 0x18) // of length 24
for i := 0; i < len(array); i++ {
array[i] = testtime
want = append(want, []byte(outbytes)...)
}
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: 0x%s\nwant: 0x%s",
array,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
}
func BenchmarkAppendTime(b *testing.B) {
tests := map[string]string{
"Integer": "Feb 3, 2013 at 7:54pm (PST)",
"Float": "2006-01-02T15:04:05.999999-08:00",
}
const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
for name, str := range tests {
t, err := time.Parse(time.RFC3339, str)
if err != nil {
t, _ = time.Parse(timeFloatFmt, str)
}
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 100)
for i := 0; i < b.N; i++ {
_ = enc.AppendTime(buf, t, "unused")
}
})
}
}
+524
View File
@@ -0,0 +1,524 @@
package cbor
import (
"fmt"
"math"
"net"
"reflect"
)
// AppendNil inserts a 'Nil' object into the dst byte array.
func (Encoder) AppendNil(dst []byte) []byte {
return append(dst, majorTypeSimpleAndFloat|additionalTypeNull)
}
// AppendBeginMarker inserts a map start into the dst byte array.
func (Encoder) AppendBeginMarker(dst []byte) []byte {
return append(dst, majorTypeMap|additionalTypeInfiniteCount)
}
// AppendEndMarker inserts a map end into the dst byte array.
func (Encoder) AppendEndMarker(dst []byte) []byte {
return append(dst, majorTypeSimpleAndFloat|additionalTypeBreak)
}
// AppendObjectData takes an object in form of a byte array and appends to dst.
func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
// BeginMarker is present in the dst, which
// should not be copied when appending to existing data.
return append(dst, o[1:]...)
}
// AppendArrayStart adds markers to indicate the start of an array.
func (Encoder) AppendArrayStart(dst []byte) []byte {
return append(dst, majorTypeArray|additionalTypeInfiniteCount)
}
// AppendArrayEnd adds markers to indicate the end of an array.
func (Encoder) AppendArrayEnd(dst []byte) []byte {
return append(dst, majorTypeSimpleAndFloat|additionalTypeBreak)
}
// AppendArrayDelim adds markers to indicate end of a particular array element.
func (Encoder) AppendArrayDelim(dst []byte) []byte {
//No delimiters needed in cbor
return dst
}
// AppendLineBreak is a noop that keep API compat with json encoder.
func (Encoder) AppendLineBreak(dst []byte) []byte {
// No line breaks needed in binary format.
return dst
}
// AppendBool encodes and inserts a boolean value into the dst byte array.
func (Encoder) AppendBool(dst []byte, val bool) []byte {
b := additionalTypeBoolFalse
if val {
b = additionalTypeBoolTrue
}
return append(dst, majorTypeSimpleAndFloat|b)
}
// AppendBools encodes and inserts an array of boolean values into the dst byte array.
func (e Encoder) AppendBools(dst []byte, vals []bool) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendBool(dst, v)
}
return dst
}
// AppendInt encodes and inserts an integer value into the dst byte array.
func (Encoder) AppendInt(dst []byte, val int) []byte {
major := majorTypeUnsignedInt
contentVal := val
if val < 0 {
major = majorTypeNegativeInt
contentVal = -val - 1
}
if contentVal <= additionalMax {
lb := byte(contentVal)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
}
return dst
}
// AppendInts encodes and inserts an array of integer values into the dst byte array.
func (e Encoder) AppendInts(dst []byte, vals []int) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendInt(dst, v)
}
return dst
}
// AppendInt8 encodes and inserts an int8 value into the dst byte array.
func (e Encoder) AppendInt8(dst []byte, val int8) []byte {
return e.AppendInt(dst, int(val))
}
// AppendInts8 encodes and inserts an array of integer values into the dst byte array.
func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendInt(dst, int(v))
}
return dst
}
// AppendInt16 encodes and inserts a int16 value into the dst byte array.
func (e Encoder) AppendInt16(dst []byte, val int16) []byte {
return e.AppendInt(dst, int(val))
}
// AppendInts16 encodes and inserts an array of int16 values into the dst byte array.
func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendInt(dst, int(v))
}
return dst
}
// AppendInt32 encodes and inserts a int32 value into the dst byte array.
func (e Encoder) AppendInt32(dst []byte, val int32) []byte {
return e.AppendInt(dst, int(val))
}
// AppendInts32 encodes and inserts an array of int32 values into the dst byte array.
func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendInt(dst, int(v))
}
return dst
}
// AppendInt64 encodes and inserts a int64 value into the dst byte array.
func (Encoder) AppendInt64(dst []byte, val int64) []byte {
major := majorTypeUnsignedInt
contentVal := val
if val < 0 {
major = majorTypeNegativeInt
contentVal = -val - 1
}
if contentVal <= additionalMax {
lb := byte(contentVal)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
}
return dst
}
// AppendInts64 encodes and inserts an array of int64 values into the dst byte array.
func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendInt64(dst, v)
}
return dst
}
// AppendUint encodes and inserts an unsigned integer value into the dst byte array.
func (e Encoder) AppendUint(dst []byte, val uint) []byte {
return e.AppendInt64(dst, int64(val))
}
// AppendUints encodes and inserts an array of unsigned integer values into the dst byte array.
func (e Encoder) AppendUints(dst []byte, vals []uint) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendUint(dst, v)
}
return dst
}
// AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array.
func (e Encoder) AppendUint8(dst []byte, val uint8) []byte {
return e.AppendUint(dst, uint(val))
}
// AppendUints8 encodes and inserts an array of uint8 values into the dst byte array.
func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendUint8(dst, v)
}
return dst
}
// AppendUint16 encodes and inserts a uint16 value into the dst byte array.
func (e Encoder) AppendUint16(dst []byte, val uint16) []byte {
return e.AppendUint(dst, uint(val))
}
// AppendUints16 encodes and inserts an array of uint16 values into the dst byte array.
func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendUint16(dst, v)
}
return dst
}
// AppendUint32 encodes and inserts a uint32 value into the dst byte array.
func (e Encoder) AppendUint32(dst []byte, val uint32) []byte {
return e.AppendUint(dst, uint(val))
}
// AppendUints32 encodes and inserts an array of uint32 values into the dst byte array.
func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendUint32(dst, v)
}
return dst
}
// AppendUint64 encodes and inserts a uint64 value into the dst byte array.
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
major := majorTypeUnsignedInt
contentVal := val
if contentVal <= additionalMax {
lb := byte(contentVal)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, contentVal)
}
return dst
}
// AppendUints64 encodes and inserts an array of uint64 values into the dst byte array.
func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendUint64(dst, v)
}
return dst
}
// AppendFloat32 encodes and inserts a single precision float value into the dst byte array.
func (Encoder) AppendFloat32(dst []byte, val float32, unused int) []byte {
switch {
case math.IsNaN(float64(val)):
return append(dst, "\xfa\x7f\xc0\x00\x00"...)
case math.IsInf(float64(val), 1):
return append(dst, "\xfa\x7f\x80\x00\x00"...)
case math.IsInf(float64(val), -1):
return append(dst, "\xfa\xff\x80\x00\x00"...)
}
major := majorTypeSimpleAndFloat
subType := additionalTypeFloat32
n := math.Float32bits(val)
var buf [4]byte
for i := uint(0); i < 4; i++ {
buf[i] = byte(n >> ((3 - i) * 8))
}
return append(append(dst, major|subType), buf[0], buf[1], buf[2], buf[3])
}
// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.
func (e Encoder) AppendFloats32(dst []byte, vals []float32, unused int) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendFloat32(dst, v, unused)
}
return dst
}
// AppendFloat64 encodes and inserts a double precision float value into the dst byte array.
func (Encoder) AppendFloat64(dst []byte, val float64, unused int) []byte {
switch {
case math.IsNaN(val):
return append(dst, "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"...)
case math.IsInf(val, 1):
return append(dst, "\xfb\x7f\xf0\x00\x00\x00\x00\x00\x00"...)
case math.IsInf(val, -1):
return append(dst, "\xfb\xff\xf0\x00\x00\x00\x00\x00\x00"...)
}
major := majorTypeSimpleAndFloat
subType := additionalTypeFloat64
n := math.Float64bits(val)
dst = append(dst, major|subType)
for i := uint(1); i <= 8; i++ {
b := byte(n >> ((8 - i) * 8))
dst = append(dst, b)
}
return dst
}
// AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array.
func (e Encoder) AppendFloats64(dst []byte, vals []float64, unused int) []byte {
major := majorTypeArray
l := len(vals)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range vals {
dst = e.AppendFloat64(dst, v, unused)
}
return dst
}
// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
marshaled, err := JSONMarshalFunc(i)
if err != nil {
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
}
return AppendEmbeddedJSON(dst, marshaled)
}
// AppendType appends the parameter type (as a string) to the input byte slice.
func (e Encoder) AppendType(dst []byte, i interface{}) []byte {
if i == nil {
return e.AppendString(dst, "<nil>")
}
return e.AppendString(dst, reflect.TypeOf(i).String())
}
// AppendIPAddr adds a net.IP IPv4 or IPv6 address into the dst byte array.
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
return e.AppendBytes(dst, ip)
}
// AppendIPAddrs adds a []net.IP array of IPv4 or IPv6 address into the dst byte array.
func (e Encoder) AppendIPAddrs(dst []byte, ips []net.IP) []byte {
major := majorTypeArray
l := len(ips)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range ips {
dst = e.AppendIPAddr(dst, v)
}
return dst
}
// AppendIPPrefix adds a net.IPNet IPv4 or IPv6 Prefix (address & mask) into the dst byte array.
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8))
dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff))
// Prefix is a tuple (aka MAP of 1 pair of elements) -
// first element is prefix, second is mask length.
dst = append(dst, majorTypeMap|0x1)
dst = e.AppendBytes(dst, pfx.IP)
maskLen, _ := pfx.Mask.Size()
return e.AppendUint8(dst, uint8(maskLen))
}
// AppendIPPrefixes adds a []net.IPNet array of IPv4 or IPv6 Prefix (address & mask) into the dst byte array.
func (e Encoder) AppendIPPrefixes(dst []byte, pfxs []net.IPNet) []byte {
major := majorTypeArray
l := len(pfxs)
if l == 0 {
return e.AppendArrayEnd(e.AppendArrayStart(dst))
}
if l <= additionalMax {
lb := byte(l)
dst = append(dst, major|lb)
} else {
dst = appendCborTypePrefix(dst, major, uint64(l))
}
for _, v := range pfxs {
dst = e.AppendIPPrefix(dst, v)
}
return dst
}
// AppendMACAddr encodes and inserts a Hardware (MAC) address.
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
return e.AppendBytes(dst, ha)
}
// AppendHex adds a TAG and inserts a hex bytes as a string.
func (e Encoder) AppendHex(dst []byte, val []byte) []byte {
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
dst = append(dst, byte(additionalTypeTagHexString>>8))
dst = append(dst, byte(additionalTypeTagHexString&0xff))
return e.AppendBytes(dst, val)
}
+34
View File
@@ -0,0 +1,34 @@
// +build !386
package cbor
import (
"encoding/hex"
"testing"
)
var enc2 = Encoder{}
var integerTestCases_64bit = []struct {
val int
binary string
}{
// Value in 8 bytes.
{0xabcd100000000, "\x1b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
{1000000000000, "\x1b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
// Value in 8 bytes.
{-0xabcd100000001, "\x3b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
{-1000000000001, "\x3b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
}
func TestAppendInt_64bit(t *testing.T) {
for _, tc := range integerTestCases_64bit {
s := enc2.AppendInt([]byte{}, tc.val)
got := string(s)
if got != tc.binary {
t.Errorf("AppendInt(0x%x)=0x%s, want: 0x%s",
tc.val, hex.EncodeToString(s),
hex.EncodeToString([]byte(tc.binary)))
}
}
}
File diff suppressed because it is too large Load Diff
+14 -34
View File
@@ -1,39 +1,19 @@
package json
func AppendKey(dst []byte, key string) []byte {
if len(dst) > 1 {
// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice.
// Making it package level instead of embedded in Encoder brings
// some extra efforts at importing, but avoids value copy when the functions
// of Encoder being invoked.
// DO REMEMBER to set this variable at importing, or
// you might get a nil pointer dereference panic at runtime.
var JSONMarshalFunc func(v interface{}) ([]byte, error)
type Encoder struct{}
// AppendKey appends a new key to the output JSON.
func (e Encoder) AppendKey(dst []byte, key string) []byte {
if dst[len(dst)-1] != '{' {
dst = append(dst, ',')
}
dst = AppendString(dst, key)
return append(dst, ':')
}
func AppendError(dst []byte, err error) []byte {
if err == nil {
return append(dst, `null`...)
}
return AppendString(dst, err.Error())
}
func AppendErrors(dst []byte, errs []error) []byte {
if len(errs) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
if errs[0] != nil {
dst = AppendString(dst, errs[0].Error())
} else {
dst = append(dst, "null"...)
}
if len(errs) > 1 {
for _, err := range errs[1:] {
if err == nil {
dst = append(dst, ",null"...)
continue
}
dst = AppendString(append(dst, ','), err.Error())
}
}
dst = append(dst, ']')
return dst
return append(e.AppendString(dst, key), ':')
}
+30
View File
@@ -0,0 +1,30 @@
package json
import (
"bytes"
"testing"
)
func TestAppendKey(t *testing.T) {
want := make([]byte, 0)
want = append(want, []byte("{\"key\":")...)
got := enc.AppendKey([]byte("{"), "key") // test with empty object
if !bytes.Equal(got, want) {
t.Errorf("AppendKey(%v)\ngot: %s\nwant: %s",
"key",
string(got),
string(want))
}
want = make([]byte, 0)
want = append(want, []byte("},\"key\":")...) // test with non-empty object
got = enc.AppendKey([]byte("}"), "key")
if !bytes.Equal(got, want) {
t.Errorf("AppendKey(%v)\ngot: %s\nwant: %s",
"key",
string(got),
string(want))
}
}
+85
View File
@@ -0,0 +1,85 @@
package json
import "unicode/utf8"
// AppendBytes is a mirror of appendString with []byte arg
func (Encoder) AppendBytes(dst, s []byte) []byte {
dst = append(dst, '"')
for i := 0; i < len(s); i++ {
if !noEscapeTable[s[i]] {
dst = appendBytesComplex(dst, s, i)
return append(dst, '"')
}
}
dst = append(dst, s...)
return append(dst, '"')
}
// AppendHex encodes the input bytes to a hex string and appends
// the encoded string to the input byte slice.
//
// The operation loops though each byte and encodes it as hex using
// the hex lookup table.
func (Encoder) AppendHex(dst, s []byte) []byte {
dst = append(dst, '"')
for _, v := range s {
dst = append(dst, hexCharacters[v>>4], hexCharacters[v&0x0f])
}
return append(dst, '"')
}
// appendBytesComplex is a mirror of the appendStringComplex
// with []byte arg
func appendBytesComplex(dst, s []byte, i int) []byte {
start := 0
for i < len(s) {
b := s[i]
if b >= utf8.RuneSelf {
r, size := utf8.DecodeRune(s[i:])
if r == utf8.RuneError && size == 1 {
if start < i {
dst = append(dst, s[start:i]...)
}
dst = append(dst, `\ufffd`...)
i += size
start = i
continue
}
i += size
continue
}
if noEscapeTable[b] {
i++
continue
}
// We encountered a character that needs to be encoded.
// Let's append the previous simple characters to the byte slice
// and switch our operation to read and encode the remainder
// characters byte-by-byte.
if start < i {
dst = append(dst, s[start:i]...)
}
switch b {
case '"', '\\':
dst = append(dst, '\\', b)
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
dst = append(dst, '\\', 'u', '0', '0', hexCharacters[b>>4], hexCharacters[b&0xF])
}
i++
start = i
}
if start < len(s) {
dst = append(dst, s[start:]...)
}
return dst
}
+86
View File
@@ -0,0 +1,86 @@
package json
import (
"testing"
"unicode"
"github.com/rs/zerolog/internal"
)
var enc = Encoder{}
func TestAppendBytes(t *testing.T) {
for _, tt := range internal.EncodeStringTests {
b := enc.AppendBytes([]byte{}, []byte(tt.In))
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendBytes(%q) = %#q, want %#q", tt.In, got, want)
}
}
}
func TestAppendHex(t *testing.T) {
for _, tt := range internal.EncodeHexTests {
b := enc.AppendHex([]byte{}, []byte{tt.In})
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendHex(%x) = %s, want %s", tt.In, got, want)
}
}
}
func TestStringBytes(t *testing.T) {
t.Parallel()
// Test that encodeState.stringBytes and encodeState.string use the same encoding.
var r []rune
for i := '\u0000'; i <= unicode.MaxRune; i++ {
r = append(r, i)
}
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
encStr := string(enc.AppendString([]byte{}, s))
encBytes := string(enc.AppendBytes([]byte{}, []byte(s)))
if encStr != encBytes {
i := 0
for i < len(encStr) && i < len(encBytes) && encStr[i] == encBytes[i] {
i++
}
encStr = encStr[i:]
encBytes = encBytes[i:]
i = 0
for i < len(encStr) && i < len(encBytes) && encStr[len(encStr)-i-1] == encBytes[len(encBytes)-i-1] {
i++
}
encStr = encStr[:len(encStr)-i]
encBytes = encBytes[:len(encBytes)-i]
if len(encStr) > 20 {
encStr = encStr[:20] + "..."
}
if len(encBytes) > 20 {
encBytes = encBytes[:20] + "..."
}
t.Errorf("encodings differ at %#q vs %#q", encStr, encBytes)
}
}
func BenchmarkAppendBytes(b *testing.B) {
tests := map[string]string{
"NoEncoding": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingFirst": `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa"aaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`,
"MultiBytesFirst": `❤️aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa❤️aaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa❤️`,
}
for name, str := range tests {
byt := []byte(str)
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 100)
for i := 0; i < b.N; i++ {
_ = enc.AppendBytes(buf, byt)
}
})
}
}
+433
View File
@@ -0,0 +1,433 @@
package json
import (
"bytes"
"encoding/json"
"fmt"
"math"
"math/rand"
"testing"
"github.com/rs/zerolog/internal"
)
var float64Tests = []struct {
Name string
Val float64
Want string
}{
{
Name: "Positive integer",
Val: 1234.0,
Want: "1234",
},
{
Name: "Negative integer",
Val: -5678.0,
Want: "-5678",
},
{
Name: "Positive decimal",
Val: 12.3456,
Want: "12.3456",
},
{
Name: "Negative decimal",
Val: -78.9012,
Want: "-78.9012",
},
{
Name: "Large positive number",
Val: 123456789.0,
Want: "123456789",
},
{
Name: "Large negative number",
Val: -987654321.0,
Want: "-987654321",
},
{
Name: "Zero",
Val: 0.0,
Want: "0",
},
{
Name: "Smallest positive value",
Val: math.SmallestNonzeroFloat64,
Want: "5e-324",
},
{
Name: "Largest positive value",
Val: math.MaxFloat64,
Want: "1.7976931348623157e+308",
},
{
Name: "Smallest negative value",
Val: -math.SmallestNonzeroFloat64,
Want: "-5e-324",
},
{
Name: "Largest negative value",
Val: -math.MaxFloat64,
Want: "-1.7976931348623157e+308",
},
{
Name: "NaN",
Val: math.NaN(),
Want: `"NaN"`,
},
{
Name: "+Inf",
Val: math.Inf(1),
Want: `"+Inf"`,
},
{
Name: "-Inf",
Val: math.Inf(-1),
Want: `"-Inf"`,
},
{
Name: "Clean up e-09 to e-9 case 1",
Val: 1e-9,
Want: "1e-9",
},
{
Name: "Clean up e-09 to e-9 case 2",
Val: -2.236734e-9,
Want: "-2.236734e-9",
},
}
func TestEncoder_AppendFloat64(t *testing.T) {
for _, tc := range float64Tests {
t.Run(tc.Name, func(t *testing.T) {
var b []byte
b = (Encoder{}).AppendFloat64(b, tc.Val, -1)
if s := string(b); tc.Want != s {
t.Errorf("%q", s)
}
})
}
}
func FuzzEncoder_AppendFloat64(f *testing.F) {
for _, tc := range float64Tests {
f.Add(tc.Val)
}
f.Fuzz(func(t *testing.T, val float64) {
actual := (Encoder{}).AppendFloat64(nil, val, -1)
if len(actual) == 0 {
t.Fatal("empty buffer")
}
if actual[0] == '"' {
switch string(actual) {
case `"NaN"`:
if !math.IsNaN(val) {
t.Fatalf("expected %v got NaN", val)
}
case `"+Inf"`:
if !math.IsInf(val, 1) {
t.Fatalf("expected %v got +Inf", val)
}
case `"-Inf"`:
if !math.IsInf(val, -1) {
t.Fatalf("expected %v got -Inf", val)
}
default:
t.Fatalf("unexpected string: %s", actual)
}
return
}
if expected, err := json.Marshal(val); err != nil {
t.Error(err)
} else if string(actual) != string(expected) {
t.Errorf("expected %s, got %s", expected, actual)
}
var parsed float64
if err := json.Unmarshal(actual, &parsed); err != nil {
t.Fatal(err)
}
if parsed != val {
t.Fatalf("expected %v, got %v", val, parsed)
}
})
}
var float32Tests = []struct {
Name string
Val float32
Want string
}{
{
Name: "Positive integer",
Val: 1234.0,
Want: "1234",
},
{
Name: "Negative integer",
Val: -5678.0,
Want: "-5678",
},
{
Name: "Positive decimal",
Val: 12.3456,
Want: "12.3456",
},
{
Name: "Negative decimal",
Val: -78.9012,
Want: "-78.9012",
},
{
Name: "Large positive number",
Val: 123456789.0,
Want: "123456790",
},
{
Name: "Large negative number",
Val: -987654321.0,
Want: "-987654340",
},
{
Name: "Zero",
Val: 0.0,
Want: "0",
},
{
Name: "Smallest positive value",
Val: math.SmallestNonzeroFloat32,
Want: "1e-45",
},
{
Name: "Largest positive value",
Val: math.MaxFloat32,
Want: "3.4028235e+38",
},
{
Name: "Smallest negative value",
Val: -math.SmallestNonzeroFloat32,
Want: "-1e-45",
},
{
Name: "Largest negative value",
Val: -math.MaxFloat32,
Want: "-3.4028235e+38",
},
{
Name: "NaN",
Val: float32(math.NaN()),
Want: `"NaN"`,
},
{
Name: "+Inf",
Val: float32(math.Inf(1)),
Want: `"+Inf"`,
},
{
Name: "-Inf",
Val: float32(math.Inf(-1)),
Want: `"-Inf"`,
},
{
Name: "Clean up e-09 to e-9 case 1",
Val: 1e-9,
Want: "1e-9",
},
{
Name: "Clean up e-09 to e-9 case 2",
Val: -2.236734e-9,
Want: "-2.236734e-9",
},
}
func TestEncoder_AppendFloat32(t *testing.T) {
for _, tc := range float32Tests {
t.Run(tc.Name, func(t *testing.T) {
var b []byte
b = (Encoder{}).AppendFloat32(b, tc.Val, -1)
if s := string(b); tc.Want != s {
t.Errorf("%q", s)
}
})
}
}
func FuzzEncoder_AppendFloat32(f *testing.F) {
for _, tc := range float32Tests {
f.Add(tc.Val)
}
f.Fuzz(func(t *testing.T, val float32) {
actual := (Encoder{}).AppendFloat32(nil, val, -1)
if len(actual) == 0 {
t.Fatal("empty buffer")
}
if actual[0] == '"' {
val := float64(val)
switch string(actual) {
case `"NaN"`:
if !math.IsNaN(val) {
t.Fatalf("expected %v got NaN", val)
}
case `"+Inf"`:
if !math.IsInf(val, 1) {
t.Fatalf("expected %v got +Inf", val)
}
case `"-Inf"`:
if !math.IsInf(val, -1) {
t.Fatalf("expected %v got -Inf", val)
}
default:
t.Fatalf("unexpected string: %s", actual)
}
return
}
if expected, err := json.Marshal(val); err != nil {
t.Error(err)
} else if string(actual) != string(expected) {
t.Errorf("expected %s, got %s", expected, actual)
}
var parsed float32
if err := json.Unmarshal(actual, &parsed); err != nil {
t.Fatal(err)
}
if parsed != val {
t.Fatalf("expected %v, got %v", val, parsed)
}
})
}
func generateFloat32s(n int) []float32 {
floats := make([]float32, n)
for i := 0; i < n; i++ {
floats[i] = rand.Float32()
}
return floats
}
func generateFloat64s(n int) []float64 {
floats := make([]float64, n)
for i := 0; i < n; i++ {
floats[i] = rand.Float64()
}
return floats
}
func TestAppendFloats32(t *testing.T) {
doOne := func(vals []float32) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
if math.IsNaN(float64(val)) {
want = append(want, []byte(`"NaN"`)...)
} else if math.IsInf(float64(val), 1) {
want = append(want, []byte(`"+Inf"`)...)
} else if math.IsInf(float64(val), -1) {
want = append(want, []byte(`"-Inf"`)...)
} else {
want = append(want, []byte(fmt.Sprintf("%v", float32(val)))...)
}
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendFloats32([]byte{}, vals, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendFloats32(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]float32, 0)
for _, tc := range internal.Float32TestCases {
if tc.Val > 0 && tc.Val < 1e-4 {
continue // we want to ignore very small numbers for this test
}
array = append(array, float32(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendFloats64(t *testing.T) {
doOne := func(vals []float64) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
if math.IsNaN(val) {
want = append(want, []byte(`"NaN"`)...)
} else if math.IsInf(val, 1) {
want = append(want, []byte(`"+Inf"`)...)
} else if math.IsInf(val, -1) {
want = append(want, []byte(`"-Inf"`)...)
} else {
want = append(want, []byte(fmt.Sprintf("%v", val))...)
}
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendFloats64([]byte{}, vals, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendFloats64(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]float64, 0)
for _, tc := range internal.Float64TestCases {
if tc.Val > 0 && tc.Val < 1e-4 {
continue // we want to ignore very small numbers for this test
}
array = append(array, tc.Val)
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
// this is really just for the memory allocation characteristics
func BenchmarkEncoder_AppendFloat32(b *testing.B) {
floats := append(generateFloat32s(5000), float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)))
dst := make([]byte, 0, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, f := range floats {
dst = (Encoder{}).AppendFloat32(dst[:0], f, -1)
}
}
}
// this is really just for the memory allocation characteristics
func BenchmarkEncoder_AppendFloat64(b *testing.B) {
floats := append(generateFloat64s(5000), math.NaN(), math.Inf(1), math.Inf(-1))
dst := make([]byte, 0, 128)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, f := range floats {
dst = (Encoder{}).AppendFloat64(dst[:0], f, -1)
}
}
}
+356
View File
@@ -0,0 +1,356 @@
package json
import (
"bytes"
"fmt"
"math"
"testing"
"github.com/rs/zerolog/internal"
)
func TestAppendInts8(t *testing.T) {
doOne := func(vals []int8) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", int8(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendInts8([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendInts8(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]int8, 0)
for _, tc := range internal.IntegerTestCases {
if (tc.Val < math.MinInt8) || (tc.Val > math.MaxInt8) {
continue
}
array = append(array, int8(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendUints8(t *testing.T) {
doOne := func(vals []uint8) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%v", uint8(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendUints8([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendUints8(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]uint8, 0)
for _, tc := range internal.UnsignedIntegerTestCases {
if tc.Val > math.MaxUint8 {
continue
}
array = append(array, uint8(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendInts16(t *testing.T) {
doOne := func(vals []int16) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", int16(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendInts16([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendInts16(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]int16, 0)
for _, tc := range internal.IntegerTestCases {
if (tc.Val < math.MinInt16) || (tc.Val > math.MaxInt16) {
continue
}
array = append(array, int16(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendUints16(t *testing.T) {
doOne := func(vals []uint16) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", uint16(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendUints16([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendUints16(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]uint16, 0)
for _, tc := range internal.UnsignedIntegerTestCases {
if tc.Val > math.MaxUint16 {
continue
}
array = append(array, uint16(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendInts32(t *testing.T) {
doOne := func(vals []int32) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", int32(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendInts32([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendInts32(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]int32, 0)
for _, tc := range internal.IntegerTestCases {
if (tc.Val < math.MinInt32) || (tc.Val > math.MaxInt32) {
continue
}
array = append(array, int32(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendUints32(t *testing.T) {
doOne := func(vals []uint32) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", uint32(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendUints32([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendUints32(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]uint32, 0)
for _, tc := range internal.UnsignedIntegerTestCases {
if tc.Val > math.MaxUint32 {
continue
}
array = append(array, uint32(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendInt64(t *testing.T) {
doOne := func(vals []int64) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", int64(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendInts64([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendInts64(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]int64, 0)
for _, tc := range internal.IntegerTestCases {
array = append(array, int64(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendUints64(t *testing.T) {
doOne := func(vals []uint64) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", uint64(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendUints64([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendUints64(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]uint64, 0)
for _, tc := range internal.UnsignedIntegerTestCases {
array = append(array, uint64(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendInt(t *testing.T) {
for _, tc := range internal.IntegerTestCases {
want := []byte(fmt.Sprintf("%d", tc.Val))
got := enc.AppendInt([]byte{}, tc.Val)
if !bytes.Equal(got, want) {
t.Errorf("AppendInt(0x%x)\ngot: %s\nwant: %s",
tc.Val,
string(got),
string(want))
}
}
}
func TestAppendUint(t *testing.T) {
for _, tc := range internal.UnsignedIntegerTestCases {
want := []byte(fmt.Sprintf("%d", tc.Val))
got := enc.AppendUint([]byte{}, tc.Val)
if !bytes.Equal(got, want) {
t.Errorf("AppendUint(0x%x)\ngot: %s\nwant: %s",
tc.Val,
string(got),
string(want))
}
}
}
func TestAppendInts(t *testing.T) {
doOne := func(vals []int) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", int(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendInts([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendInts(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]int, 0)
for _, tc := range internal.IntegerTestCases {
array = append(array, int(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
func TestAppendUints(t *testing.T) {
doOne := func(vals []uint) {
want := make([]byte, 0)
want = append(want, '[')
for i, val := range vals {
want = append(want, []byte(fmt.Sprintf("%d", uint(val)))...)
if i < len(vals)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendUints([]byte{}, vals)
if !bytes.Equal(got, want) {
t.Errorf("AppendUints(%v)\ngot: %s\nwant: %s",
vals,
string(got),
string(want))
}
}
array := make([]uint, 0)
for _, tc := range internal.UnsignedIntegerTestCases {
array = append(array, uint(tc.Val))
}
doOne(array)
doOne(array[:1]) // single element
doOne(array[:0]) // edge case of zero length
}
+50 -81
View File
@@ -1,18 +1,31 @@
package json
import "unicode/utf8"
import (
"fmt"
"unicode/utf8"
)
const hex = "0123456789abcdef"
const hexCharacters = "0123456789abcdef"
func AppendStrings(dst []byte, vals []string) []byte {
var noEscapeTable = [256]bool{}
func init() {
for i := 0; i <= 0x7e; i++ {
noEscapeTable[i] = i >= 0x20 && i != '\\' && i != '"'
}
}
// AppendStrings encodes the input strings to json and
// appends the encoded string list to the input byte slice.
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = AppendString(dst, vals[0])
dst = e.AppendString(dst, vals[0])
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = AppendString(append(dst, ','), val)
dst = e.AppendString(append(dst, ','), val)
}
}
dst = append(dst, ']')
@@ -24,11 +37,11 @@ func AppendStrings(dst []byte, vals []string) []byte {
//
// The operation loops though each byte in the string looking
// for characters that need json or utf8 encoding. If the string
// does not need encoding, then the string is appended in it's
// does not need encoding, then the string is appended in its
// entirety to the byte slice.
// If we encounter a byte that does need encoding, switch up
// the operation and perform a byte-by-byte read-encode-append.
func AppendString(dst []byte, s string) []byte {
func (Encoder) AppendString(dst []byte, s string) []byte {
// Start with a double quote.
dst = append(dst, '"')
// Loop through each character in the string.
@@ -36,20 +49,45 @@ func AppendString(dst []byte, s string) []byte {
// Check if the character needs encoding. Control characters, slashes,
// and the double quote need json encoding. Bytes above the ascii
// boundary needs utf8 encoding.
if s[i] < 0x20 || s[i] > 0x7e || s[i] == '\\' || s[i] == '"' {
if !noEscapeTable[s[i]] {
// We encountered a character that needs to be encoded. Switch
// to complex version of the algorithm.
dst = appendStringComplex(dst, s, i)
return append(dst, '"')
}
}
// The string has no need for encoding an therefore is directly
// The string has no need for encoding and therefore is directly
// appended to the byte slice.
dst = append(dst, s...)
// End with a double quote
return append(dst, '"')
}
// AppendStringers encodes the provided Stringer list to json and
// appends the encoded Stringer list to the input byte slice.
func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte {
if vals == nil || len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = e.AppendStringer(dst, vals[0])
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = e.AppendStringer(append(dst, ','), val)
}
}
return append(dst, ']')
}
// AppendStringer encodes the input Stringer to json and appends the
// encoded Stringer value to the input byte slice.
func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte {
if val == nil {
return e.AppendInterface(dst, nil)
}
return e.AppendString(dst, val.String())
}
// appendStringComplex is used by appendString to take over an in
// progress JSON string encoding that encountered a character that needs
// to be encoded.
@@ -61,7 +99,7 @@ func appendStringComplex(dst []byte, s string, i int) []byte {
r, size := utf8.DecodeRuneInString(s[i:])
if r == utf8.RuneError && size == 1 {
// In case of error, first append previous simple characters to
// the byte slice if any and append a remplacement character code
// the byte slice if any and append a replacement character code
// in place of the invalid sequence.
if start < i {
dst = append(dst, s[start:i]...)
@@ -74,7 +112,7 @@ func appendStringComplex(dst []byte, s string, i int) []byte {
i += size
continue
}
if b >= 0x20 && b <= 0x7e && b != '\\' && b != '"' {
if noEscapeTable[b] {
i++
continue
}
@@ -99,76 +137,7 @@ func appendStringComplex(dst []byte, s string, i int) []byte {
case '\t':
dst = append(dst, '\\', 't')
default:
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
}
i++
start = i
}
if start < len(s) {
dst = append(dst, s[start:]...)
}
return dst
}
// AppendBytes is a mirror of appendString with []byte arg
func AppendBytes(dst, s []byte) []byte {
dst = append(dst, '"')
for i := 0; i < len(s); i++ {
if s[i] < 0x20 || s[i] > 0x7e || s[i] == '\\' || s[i] == '"' {
dst = appendBytesComplex(dst, s, i)
return append(dst, '"')
}
}
dst = append(dst, s...)
return append(dst, '"')
}
// appendBytesComplex is a mirror of the appendStringComplex
// with []byte arg
func appendBytesComplex(dst, s []byte, i int) []byte {
start := 0
for i < len(s) {
b := s[i]
if b >= utf8.RuneSelf {
r, size := utf8.DecodeRune(s[i:])
if r == utf8.RuneError && size == 1 {
if start < i {
dst = append(dst, s[start:i]...)
}
dst = append(dst, `\ufffd`...)
i += size
start = i
continue
}
i += size
continue
}
if b >= 0x20 && b <= 0x7e && b != '\\' && b != '"' {
i++
continue
}
// We encountered a character that needs to be encoded.
// Let's append the previous simple characters to the byte slice
// and switch our operation to read and encode the remainder
// characters byte-by-byte.
if start < i {
dst = append(dst, s[start:i]...)
}
switch b {
case '"', '\\':
dst = append(dst, '\\', b)
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
dst = append(dst, '\\', 'u', '0', '0', hexCharacters[b>>4], hexCharacters[b&0xF])
}
i++
start = i
+35 -113
View File
@@ -2,113 +2,56 @@ package json
import (
"testing"
"unicode"
"github.com/rs/zerolog/internal"
)
var encodeStringTests = []struct {
in string
out string
}{
{"", `""`},
{"\\", `"\\"`},
{"\x00", `"\u0000"`},
{"\x01", `"\u0001"`},
{"\x02", `"\u0002"`},
{"\x03", `"\u0003"`},
{"\x04", `"\u0004"`},
{"\x05", `"\u0005"`},
{"\x06", `"\u0006"`},
{"\x07", `"\u0007"`},
{"\x08", `"\b"`},
{"\x09", `"\t"`},
{"\x0a", `"\n"`},
{"\x0b", `"\u000b"`},
{"\x0c", `"\f"`},
{"\x0d", `"\r"`},
{"\x0e", `"\u000e"`},
{"\x0f", `"\u000f"`},
{"\x10", `"\u0010"`},
{"\x11", `"\u0011"`},
{"\x12", `"\u0012"`},
{"\x13", `"\u0013"`},
{"\x14", `"\u0014"`},
{"\x15", `"\u0015"`},
{"\x16", `"\u0016"`},
{"\x17", `"\u0017"`},
{"\x18", `"\u0018"`},
{"\x19", `"\u0019"`},
{"\x1a", `"\u001a"`},
{"\x1b", `"\u001b"`},
{"\x1c", `"\u001c"`},
{"\x1d", `"\u001d"`},
{"\x1e", `"\u001e"`},
{"\x1f", `"\u001f"`},
{"✭", `"✭"`},
{"foo\xc2\x7fbar", `"foo\ufffd\u007fbar"`}, // invalid sequence
{"ascii", `"ascii"`},
{"\"a", `"\"a"`},
{"\x1fa", `"\u001fa"`},
{"foo\"bar\"baz", `"foo\"bar\"baz"`},
{"\x1ffoo\x1fbar\x1fbaz", `"\u001ffoo\u001fbar\u001fbaz"`},
{"emoji \u2764\ufe0f!", `"emoji ❤️!"`},
}
func TestappendString(t *testing.T) {
for _, tt := range encodeStringTests {
b := AppendString([]byte{}, tt.in)
if got, want := string(b), tt.out; got != want {
t.Errorf("appendString(%q) = %#q, want %#q", tt.in, got, want)
func TestAppendString(t *testing.T) {
for _, tt := range internal.EncodeStringTests {
b := enc.AppendString([]byte{}, tt.In)
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendString(%q) = %#q, want %#q", tt.In, got, want)
}
}
}
func TestappendBytes(t *testing.T) {
for _, tt := range encodeStringTests {
b := AppendBytes([]byte{}, []byte(tt.in))
if got, want := string(b), tt.out; got != want {
t.Errorf("appendBytes(%q) = %#q, want %#q", tt.in, got, want)
func TestAppendStrings(t *testing.T) {
for _, tt := range internal.EncodeStringsTests {
b := enc.AppendStrings([]byte{}, tt.In)
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendStrings(%q) = %#q, want %#q", tt.In, got, want)
}
}
}
func TestStringBytes(t *testing.T) {
t.Parallel()
// Test that encodeState.stringBytes and encodeState.string use the same encoding.
var r []rune
for i := '\u0000'; i <= unicode.MaxRune; i++ {
r = append(r, i)
func TestAppendStringer(t *testing.T) {
oldJSONMarshalFunc := JSONMarshalFunc
defer func() {
JSONMarshalFunc = oldJSONMarshalFunc
}()
JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return internal.InterfaceMarshalFunc(v)
}
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
enc := string(AppendString([]byte{}, s))
encBytes := string(AppendBytes([]byte{}, []byte(s)))
if enc != encBytes {
i := 0
for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
i++
for _, tt := range internal.EncodeStringerTests {
b := enc.AppendStringer([]byte{}, tt.In)
if got, want := string(b), tt.Out; got != want {
t.Errorf("AppendStringer(%q)\ngot: %#q, want: %#q", tt.In, got, want)
}
enc = enc[i:]
encBytes = encBytes[i:]
i = 0
for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
i++
}
enc = enc[:len(enc)-i]
encBytes = encBytes[:len(encBytes)-i]
if len(enc) > 20 {
enc = enc[:20] + "..."
}
if len(encBytes) > 20 {
encBytes = encBytes[:20] + "..."
}
t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
}
}
func BenchmarkappendString(b *testing.B) {
func TestAppendStringers(t *testing.T) {
for _, tt := range internal.EncodeStringersTests {
b := enc.AppendStringers([]byte{}, tt.In)
if got, want := string(b), tt.Out; got != want {
t.Errorf("appendStrings(%q) = %#q, want %#q", tt.In, got, want)
}
}
}
func BenchmarkAppendString(b *testing.B) {
tests := map[string]string{
"NoEncoding": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingFirst": `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
@@ -122,28 +65,7 @@ func BenchmarkappendString(b *testing.B) {
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 100)
for i := 0; i < b.N; i++ {
_ = AppendString(buf, str)
}
})
}
}
func BenchmarkappendBytes(b *testing.B) {
tests := map[string]string{
"NoEncoding": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingFirst": `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa"aaaaaaaaaaaaaaaaaaaaaaaa`,
"EncodingLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"`,
"MultiBytesFirst": `❤️aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesMiddle": `aaaaaaaaaaaaaaaaaaaaaaaaa❤️aaaaaaaaaaaaaaaaaaaaaaaa`,
"MultiBytesLast": `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa❤️`,
}
for name, str := range tests {
byt := []byte(str)
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 100)
for i := 0; i < b.N; i++ {
_ = AppendBytes(buf, byt)
_ = enc.AppendString(buf, str)
}
})
}
+73 -17
View File
@@ -5,16 +5,45 @@ import (
"time"
)
func AppendTime(dst []byte, t time.Time, format string) []byte {
if format == "" {
return AppendInt64(dst, t.Unix())
const (
// Import from zerolog/global.go
timeFormatUnix = ""
timeFormatUnixMs = "UNIXMS"
timeFormatUnixMicro = "UNIXMICRO"
timeFormatUnixNano = "UNIXNANO"
durationFormatFloat = "float"
durationFormatInt = "int"
durationFormatString = "string"
)
// AppendTime formats the input time with the given format
// and appends the encoded string to the input byte slice.
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
switch format {
case timeFormatUnix:
return e.AppendInt64(dst, t.Unix())
case timeFormatUnixMs:
return e.AppendInt64(dst, t.UnixNano()/1000000)
case timeFormatUnixMicro:
return e.AppendInt64(dst, t.UnixNano()/1000)
case timeFormatUnixNano:
return e.AppendInt64(dst, t.UnixNano())
}
return append(t.AppendFormat(append(dst, '"'), format), '"')
}
func AppendTimes(dst []byte, vals []time.Time, format string) []byte {
if format == "" {
// AppendTimes converts the input times with the given format
// and appends the encoded string list to the input byte slice.
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
switch format {
case timeFormatUnix:
return appendUnixTimes(dst, vals)
case timeFormatUnixMs:
return appendUnixNanoTimes(dst, vals, 1000000)
case timeFormatUnixMicro:
return appendUnixNanoTimes(dst, vals, 1000)
case timeFormatUnixNano:
return appendUnixNanoTimes(dst, vals, 1)
}
if len(vals) == 0 {
return append(dst, '[', ']')
@@ -38,29 +67,56 @@ func appendUnixTimes(dst []byte, vals []time.Time) []byte {
dst = strconv.AppendInt(dst, vals[0].Unix(), 10)
if len(vals) > 1 {
for _, t := range vals[1:] {
dst = strconv.AppendInt(dst, t.Unix(), 10)
dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10)
}
}
dst = append(dst, ']')
return dst
}
func AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
if useInt {
return strconv.AppendInt(dst, int64(d/unit), 10)
}
return AppendFloat64(dst, float64(d)/float64(unit))
}
func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = AppendDuration(dst, vals[0], unit, useInt)
dst = strconv.AppendInt(dst, vals[0].UnixNano()/div, 10)
if len(vals) > 1 {
for _, d := range vals[1:] {
dst = AppendDuration(append(dst, ','), d, unit, useInt)
for _, t := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/div, 10)
}
}
dst = append(dst, ']')
return dst
}
// AppendDuration formats the input duration with the given unit & format
// and appends the encoded string to the input byte slice.
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
if useInt {
return strconv.AppendInt(dst, int64(d/unit), 10)
}
switch format {
case durationFormatFloat:
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
case durationFormatInt:
return e.AppendInt64(dst, int64(d/unit))
case durationFormatString:
return e.AppendString(dst, d.String())
}
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
}
// AppendDurations formats the input durations with the given unit & format
// and appends the encoded string list to the input byte slice.
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = e.AppendDuration(dst, vals[0], unit, format, useInt, precision)
if len(vals) > 1 {
for _, d := range vals[1:] {
dst = e.AppendDuration(append(dst, ','), d, unit, format, useInt, precision)
}
}
dst = append(dst, ']')
+373
View File
@@ -0,0 +1,373 @@
package json
import (
"bytes"
"fmt"
"reflect"
"testing"
"time"
"github.com/rs/zerolog/internal"
)
func TestEncoder_AppendDuration(t *testing.T) {
type args struct {
dst []byte
d time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
d: 1234567890,
unit: time.Second,
useInt: true,
},
want: []byte{49},
},
{
name: "formatFloat",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{49},
},
{
name: "formatInt",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatInt,
},
want: []byte{49},
},
{
name: "formatString",
args: args{
d: 1234567890,
unit: time.Second,
format: durationFormatString,
},
want: []byte{34, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115, 34},
},
{
name: "formatBlank",
args: args{
d: 1234567890,
unit: time.Second,
},
want: []byte{49},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDuration(tt.args.dst, tt.args.d, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDuration() = %v, want %v", got, tt.want)
}
})
}
}
func TestEncoder_AppendDurations(t *testing.T) {
type args struct {
dst []byte
vals []time.Duration
unit time.Duration
format string
useInt bool
unused int
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "useInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
useInt: true,
},
want: []byte{91, 49, 93},
},
{
name: "formatFloat",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatFloat,
},
want: []byte{91, 49, 93},
},
{
name: "formatInt",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatInt,
},
want: []byte{91, 49, 93},
},
{
name: "formatString",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
format: durationFormatString,
},
want: []byte{91, 34, 49, 46, 50, 51, 52, 53, 54, 55, 56, 57, 115, 34, 93},
},
{
name: "formatBlank",
args: args{
vals: []time.Duration{1234567890},
unit: time.Second,
},
want: []byte{91, 49, 93},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := Encoder{}
if got := e.AppendDurations(tt.args.dst, tt.args.vals, tt.args.unit, tt.args.format, tt.args.useInt, tt.args.unused); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendDurations() = %v, want %v", got, tt.want)
}
})
}
}
func TestAppendTimeNow(t *testing.T) {
tm := time.Now()
got := enc.AppendTime([]byte{}, tm, time.RFC3339)
want := tm.AppendFormat([]byte{'"'}, time.RFC3339)
want = append(want, '"')
if !bytes.Equal(got, want) {
t.Errorf("AppendTime(%s)\ngot: %s\nwant: %s",
"time.Now()",
string(got),
string(want))
}
}
func TestAppendTimePastPresentInteger(t *testing.T) {
for _, tt := range internal.TimeIntegerTestcases {
tin, err := time.Parse(time.RFC3339, tt.Txt)
if err != nil {
fmt.Println("Cannot parse input", tt.Txt, ".. Skipping!", err)
continue
}
got := enc.AppendTime([]byte{}, tin, timeFormatUnix)
want := []byte(fmt.Sprintf("%d", tt.UnixInt))
if !bytes.Equal(got, want) {
t.Errorf("appendString(%s)\ngot: %s\nwant: %s",
tt.Txt,
string(got),
string(want))
}
got = enc.AppendTime([]byte{}, tin, timeFormatUnixMs)
want = []byte(fmt.Sprintf("%d", tt.UnixInt*1000))
if !bytes.Equal(got, want) {
t.Errorf("appendString(%s)\ngot: %s\nwant: %s",
tt.Txt,
string(got),
string(want))
}
got = enc.AppendTime([]byte{}, tin, timeFormatUnixMicro)
want = []byte(fmt.Sprintf("%d", tt.UnixInt*1000000))
if !bytes.Equal(got, want) {
t.Errorf("appendString(%s)\ngot: %s\nwant: %s",
tt.Txt,
string(got),
string(want))
}
got = enc.AppendTime([]byte{}, tin, timeFormatUnixNano)
want = []byte(fmt.Sprintf("%d", tt.UnixInt*1000000000))
if !bytes.Equal(got, want) {
t.Errorf("appendString(%s)\ngot: %s\nwant: %s",
tt.Txt,
string(got),
string(want))
}
}
}
func TestAppendTimePastPresentFloat(t *testing.T) {
const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
for _, tt := range internal.TimeFloatTestcases {
tin, err := time.Parse(timeFloatFmt, tt.RfcStr)
if err != nil {
fmt.Println("Cannot parse input", tt.RfcStr, ".. Skipping!")
continue
}
got := enc.AppendTime([]byte{}, tin, timeFormatUnix)
want := []byte(fmt.Sprintf("%d", tt.UnixInt))
if !bytes.Equal(got, want) {
t.Errorf("appendString(%s)\ngot: %s\nwant: %s",
tt.RfcStr,
string(got),
string(want))
}
}
}
func TestAppendTimes(t *testing.T) {
doOne := func(multiplier int, format string) {
array := make([]time.Time, 0)
want := append([]byte{}, '[')
want = append(want, ']')
got := enc.AppendTimes([]byte{}, array, format)
if !bytes.Equal(got, want) {
t.Errorf("AppendTimes(%v)\ngot: %s\nwant: %s",
array,
string(got),
string(want))
}
array = make([]time.Time, len(internal.TimeIntegerTestcases))
want = append([]byte{}, '[')
for i, tt := range internal.TimeIntegerTestcases {
if tin, err := time.Parse(time.RFC3339, tt.RfcStr); err != nil {
fmt.Println("Cannot parse input", tt.RfcStr, ".. Skipping!")
continue
} else {
array[i] = tin
}
if multiplier == 0 {
want = append(want, '"')
formatted := array[i].Format(format)
want = append(want, []byte(fmt.Sprintf("%v", formatted))...)
want = append(want, '"')
} else {
scaled := tt.UnixInt * multiplier
want = append(want, []byte(fmt.Sprintf("%d", scaled))...)
}
if i < len(internal.TimeIntegerTestcases)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got = enc.AppendTimes([]byte{}, array, format)
if !bytes.Equal(got, want) {
t.Errorf("AppendTimes(%v) %d %s\ngot: %s\nwant: %s",
array, multiplier, format,
string(got),
string(want))
}
}
doOne(0, time.RFC3339)
doOne(1, timeFormatUnix)
doOne(1000, timeFormatUnixMs)
doOne(1000000, timeFormatUnixMicro)
doOne(1000000000, timeFormatUnixNano)
}
func TestAppendDurationFloat(t *testing.T) {
for _, tt := range internal.DurTestcases {
dur := tt.Duration
want := []byte{}
want = append(want, []byte(fmt.Sprintf("%v", dur.Microseconds()))...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
string(got),
string(want))
}
want = []byte{}
fraction := float64(dur) / float64(time.Millisecond)
want = append(want, []byte(fmt.Sprintf("%v", fraction))...)
got = enc.AppendDuration([]byte{}, dur, time.Millisecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
string(got),
string(want))
}
}
}
func TestAppendDurationInteger(t *testing.T) {
for _, tt := range internal.DurTestcases {
dur := tt.Duration
want := []byte{}
whole := int(dur) / int(time.Microsecond)
want = append(want, []byte(fmt.Sprintf("%v", whole))...)
got := enc.AppendDuration([]byte{}, dur, time.Microsecond, "", true, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDuration(%v)=\ngot: %s\nwant: %s",
dur,
string(got),
string(want))
}
}
}
func TestAppendDurations(t *testing.T) {
array := make([]time.Duration, len(internal.DurTestcases))
want := make([]byte, 0)
want = append(want, '[')
for i, tt := range internal.DurTestcases {
array[i] = tt.Duration
whole := int(tt.Duration) / int(time.Microsecond)
want = append(want, []byte(fmt.Sprintf("%v", whole))...)
if i < len(internal.DurTestcases)-1 {
want = append(want, ',')
}
}
want = append(want, ']')
got := enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: %s\nwant: %s",
array,
string(got),
string(want))
}
// now empty array case
array = make([]time.Duration, 0)
want = make([]byte, 0)
want = append(want, '[')
want = append(want, ']')
got = enc.AppendDurations([]byte{}, array, time.Microsecond, "", false, -1)
if !bytes.Equal(got, want) {
t.Errorf("AppendDurations(%v)\ngot: %s\nwant: %s",
array,
string(got),
string(want))
}
}
func BenchmarkAppendTime(b *testing.B) {
tests := map[string]string{
"Integer": "Feb 3, 2013 at 7:54pm (PST)",
"Float": "2006-01-02T15:04:05.999999-08:00",
}
const timeFloatFmt = "2006-01-02T15:04:05.999999-07:00"
for name, str := range tests {
t, err := time.Parse(time.RFC3339, str)
if err != nil {
t, _ = time.Parse(timeFloatFmt, str)
}
b.Run(name, func(b *testing.B) {
buf := make([]byte, 0, 100)
for i := 0; i < b.N; i++ {
_ = enc.AppendTime(buf, t, "unused")
}
})
}
}
+228 -40
View File
@@ -1,17 +1,60 @@
package json
import (
"encoding/json"
"fmt"
"math"
"net"
"reflect"
"strconv"
)
func AppendBool(dst []byte, val bool) []byte {
// AppendNil inserts a 'Nil' object into the dst byte array.
func (Encoder) AppendNil(dst []byte) []byte {
return append(dst, "null"...)
}
// AppendBeginMarker inserts a map start into the dst byte array.
func (Encoder) AppendBeginMarker(dst []byte) []byte {
return append(dst, '{')
}
// AppendEndMarker inserts a map end into the dst byte array.
func (Encoder) AppendEndMarker(dst []byte) []byte {
return append(dst, '}')
}
// AppendLineBreak appends a line break.
func (Encoder) AppendLineBreak(dst []byte) []byte {
return append(dst, '\n')
}
// AppendArrayStart adds markers to indicate the start of an array.
func (Encoder) AppendArrayStart(dst []byte) []byte {
return append(dst, '[')
}
// AppendArrayEnd adds markers to indicate the end of an array.
func (Encoder) AppendArrayEnd(dst []byte) []byte {
return append(dst, ']')
}
// AppendArrayDelim adds markers to indicate end of a particular array element.
func (Encoder) AppendArrayDelim(dst []byte) []byte {
if len(dst) > 0 {
return append(dst, ',')
}
return dst
}
// AppendBool converts the input bool to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendBool(dst []byte, val bool) []byte {
return strconv.AppendBool(dst, val)
}
func AppendBools(dst []byte, vals []bool) []byte {
// AppendBools encodes the input bools to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendBools(dst []byte, vals []bool) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -26,11 +69,15 @@ func AppendBools(dst []byte, vals []bool) []byte {
return dst
}
func AppendInt(dst []byte, val int) []byte {
// AppendInt converts the input int to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendInt(dst []byte, val int) []byte {
return strconv.AppendInt(dst, int64(val), 10)
}
func AppendInts(dst []byte, vals []int) []byte {
// AppendInts encodes the input ints to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendInts(dst []byte, vals []int) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -45,11 +92,15 @@ func AppendInts(dst []byte, vals []int) []byte {
return dst
}
func AppendInt8(dst []byte, val int8) []byte {
// AppendInt8 converts the input []int8 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendInt8(dst []byte, val int8) []byte {
return strconv.AppendInt(dst, int64(val), 10)
}
func AppendInts8(dst []byte, vals []int8) []byte {
// AppendInts8 encodes the input int8s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendInts8(dst []byte, vals []int8) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -64,11 +115,15 @@ func AppendInts8(dst []byte, vals []int8) []byte {
return dst
}
func AppendInt16(dst []byte, val int16) []byte {
// AppendInt16 converts the input int16 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendInt16(dst []byte, val int16) []byte {
return strconv.AppendInt(dst, int64(val), 10)
}
func AppendInts16(dst []byte, vals []int16) []byte {
// AppendInts16 encodes the input int16s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendInts16(dst []byte, vals []int16) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -83,11 +138,15 @@ func AppendInts16(dst []byte, vals []int16) []byte {
return dst
}
func AppendInt32(dst []byte, val int32) []byte {
// AppendInt32 converts the input int32 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendInt32(dst []byte, val int32) []byte {
return strconv.AppendInt(dst, int64(val), 10)
}
func AppendInts32(dst []byte, vals []int32) []byte {
// AppendInts32 encodes the input int32s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendInts32(dst []byte, vals []int32) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -102,11 +161,15 @@ func AppendInts32(dst []byte, vals []int32) []byte {
return dst
}
func AppendInt64(dst []byte, val int64) []byte {
// AppendInt64 converts the input int64 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendInt64(dst []byte, val int64) []byte {
return strconv.AppendInt(dst, val, 10)
}
func AppendInts64(dst []byte, vals []int64) []byte {
// AppendInts64 encodes the input int64s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendInts64(dst []byte, vals []int64) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -121,11 +184,15 @@ func AppendInts64(dst []byte, vals []int64) []byte {
return dst
}
func AppendUint(dst []byte, val uint) []byte {
// AppendUint converts the input uint to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendUint(dst []byte, val uint) []byte {
return strconv.AppendUint(dst, uint64(val), 10)
}
func AppendUints(dst []byte, vals []uint) []byte {
// AppendUints encodes the input uints to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendUints(dst []byte, vals []uint) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -140,11 +207,15 @@ func AppendUints(dst []byte, vals []uint) []byte {
return dst
}
func AppendUint8(dst []byte, val uint8) []byte {
// AppendUint8 converts the input uint8 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendUint8(dst []byte, val uint8) []byte {
return strconv.AppendUint(dst, uint64(val), 10)
}
func AppendUints8(dst []byte, vals []uint8) []byte {
// AppendUints8 encodes the input uint8s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -159,11 +230,15 @@ func AppendUints8(dst []byte, vals []uint8) []byte {
return dst
}
func AppendUint16(dst []byte, val uint16) []byte {
// AppendUint16 converts the input uint16 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendUint16(dst []byte, val uint16) []byte {
return strconv.AppendUint(dst, uint64(val), 10)
}
func AppendUints16(dst []byte, vals []uint16) []byte {
// AppendUints16 encodes the input uint16s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -178,11 +253,15 @@ func AppendUints16(dst []byte, vals []uint16) []byte {
return dst
}
func AppendUint32(dst []byte, val uint32) []byte {
// AppendUint32 converts the input uint32 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendUint32(dst []byte, val uint32) []byte {
return strconv.AppendUint(dst, uint64(val), 10)
}
func AppendUints32(dst []byte, vals []uint32) []byte {
// AppendUints32 encodes the input uint32s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -197,11 +276,15 @@ func AppendUints32(dst []byte, vals []uint32) []byte {
return dst
}
func AppendUint64(dst []byte, val uint64) []byte {
return strconv.AppendUint(dst, uint64(val), 10)
// AppendUint64 converts the input uint64 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
return strconv.AppendUint(dst, val, 10)
}
func AppendUints64(dst []byte, vals []uint64) []byte {
// AppendUints64 encodes the input uint64s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
@@ -216,9 +299,9 @@ func AppendUints64(dst []byte, vals []uint64) []byte {
return dst
}
func AppendFloat(dst []byte, val float64, bitSize int) []byte {
func appendFloat(dst []byte, val float64, bitSize, precision int) []byte {
// JSON does not permit NaN or Infinity. A typical JSON encoder would fail
// with an error, but a logging library wants the data to get thru so we
// with an error, but a logging library wants the data to get through so we
// make a tradeoff and store those types as string.
switch {
case math.IsNaN(val):
@@ -228,51 +311,156 @@ func AppendFloat(dst []byte, val float64, bitSize int) []byte {
case math.IsInf(val, -1):
return append(dst, `"-Inf"`...)
}
return strconv.AppendFloat(dst, val, 'f', -1, bitSize)
// convert as if by es6 number to string conversion
// see also https://cs.opensource.google/go/go/+/refs/tags/go1.20.3:src/encoding/json/encode.go;l=573
strFmt := byte('f')
// If precision is set to a value other than -1, we always just format the float using that precision.
if precision == -1 {
// Use float32 comparisons for underlying float32 value to get precise cutoffs right.
if abs := math.Abs(val); abs != 0 {
if bitSize == 64 && (abs < 1e-6 || abs >= 1e21) || bitSize == 32 && (float32(abs) < 1e-6 || float32(abs) >= 1e21) {
strFmt = 'e'
}
}
}
dst = strconv.AppendFloat(dst, val, strFmt, precision, bitSize)
if strFmt == 'e' {
// Clean up e-09 to e-9
n := len(dst)
if n >= 4 && dst[n-4] == 'e' && dst[n-3] == '-' && dst[n-2] == '0' {
dst[n-2] = dst[n-1]
dst = dst[:n-1]
}
}
return dst
}
func AppendFloat32(dst []byte, val float32) []byte {
return AppendFloat(dst, float64(val), 32)
// AppendFloat32 converts the input float32 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendFloat32(dst []byte, val float32, precision int) []byte {
return appendFloat(dst, float64(val), 32, precision)
}
func AppendFloats32(dst []byte, vals []float32) []byte {
// AppendFloats32 encodes the input float32s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendFloats32(dst []byte, vals []float32, precision int) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = AppendFloat(dst, float64(vals[0]), 32)
dst = appendFloat(dst, float64(vals[0]), 32, precision)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = AppendFloat(append(dst, ','), float64(val), 32)
dst = appendFloat(append(dst, ','), float64(val), 32, precision)
}
}
dst = append(dst, ']')
return dst
}
func AppendFloat64(dst []byte, val float64) []byte {
return AppendFloat(dst, val, 64)
// AppendFloat64 converts the input float64 to a string and
// appends the encoded string to the input byte slice.
func (Encoder) AppendFloat64(dst []byte, val float64, precision int) []byte {
return appendFloat(dst, val, 64, precision)
}
func AppendFloats64(dst []byte, vals []float64) []byte {
// AppendFloats64 encodes the input float64s to json and
// appends the encoded string list to the input byte slice.
func (Encoder) AppendFloats64(dst []byte, vals []float64, precision int) []byte {
if len(vals) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = AppendFloat(dst, vals[0], 32)
dst = appendFloat(dst, vals[0], 64, precision)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = AppendFloat(append(dst, ','), val, 64)
dst = appendFloat(append(dst, ','), val, 64, precision)
}
}
dst = append(dst, ']')
return dst
}
func AppendInterface(dst []byte, i interface{}) []byte {
marshaled, err := json.Marshal(i)
// AppendInterface marshals the input interface to a string and
// appends the encoded string to the input byte slice.
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
marshaled, err := JSONMarshalFunc(i)
if err != nil {
return AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
}
return append(dst, marshaled...)
}
// AppendType appends the parameter type (as a string) to the input byte slice.
func (e Encoder) AppendType(dst []byte, i interface{}) []byte {
if i == nil {
return e.AppendString(dst, "<nil>")
}
return e.AppendString(dst, reflect.TypeOf(i).String())
}
// AppendObjectData takes in an object that is already in a byte array
// and adds it to the dst.
func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
// Three conditions apply here:
// 1. new content starts with '{' - which should be dropped OR
// 2. new content starts with '{' - which should be replaced with ','
// to separate with existing content OR
// 3. existing content has already other fields
if o[0] == '{' {
if len(dst) > 1 {
dst = append(dst, ',')
}
o = o[1:]
} else if len(dst) > 1 {
dst = append(dst, ',')
}
return append(dst, o...)
}
// AppendIPAddr adds a net.IP IPv4 or IPv6 address to dst.
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
return e.AppendString(dst, ip.String())
}
// AppendIPAddrs adds a []net.IP array of IPv4 or IPv6 address to dst.
func (e Encoder) AppendIPAddrs(dst []byte, ips []net.IP) []byte {
if len(ips) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = e.AppendString(dst, ips[0].String())
if len(ips) > 1 {
for _, ip := range ips[1:] {
dst = e.AppendString(append(dst, ','), ip.String())
}
}
dst = append(dst, ']')
return dst
}
// AppendIPPrefix adds a net.IPNet IPv4 or IPv6 Prefix (address & mask) to dst.
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
return e.AppendString(dst, pfx.String())
}
// AppendIPPrefixes adds a []net.IPNet array of IPv4 or IPv6 Prefix (address & mask) to dst.
func (e Encoder) AppendIPPrefixes(dst []byte, pfxs []net.IPNet) []byte {
if len(pfxs) == 0 {
return append(dst, '[', ']')
}
dst = append(dst, '[')
dst = e.AppendString(dst, pfxs[0].String())
if len(pfxs) > 1 {
for _, pfx := range pfxs[1:] {
dst = e.AppendString(append(dst, ','), pfx.String())
}
}
dst = append(dst, ']')
return dst
}
// AppendMACAddr adds a net.HardwareAddr MAC address to dst.
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
return e.AppendString(dst, ha.String())
}
+425 -14
View File
@@ -1,32 +1,443 @@
package json
import (
"bytes"
"encoding/hex"
"encoding/json"
"errors"
"math"
"net"
"reflect"
"testing"
"github.com/rs/zerolog/internal"
)
func Test_appendFloat64(t *testing.T) {
func TestAppendNil(t *testing.T) {
got := enc.AppendNil([]byte{})
want := []byte(`null`)
if !bytes.Equal(got, want) {
t.Errorf("AppendNil() = %s, want: %s",
string(got),
string(want))
}
}
func TestAppendBeginMarker(t *testing.T) {
got := enc.AppendBeginMarker([]byte{})
want := []byte(`{`)
if !bytes.Equal(got, want) {
t.Errorf("AppendBeginMarker()\ngot: %s, want: %s",
string(got),
string(want))
}
}
func TestAppendEndMarker(t *testing.T) {
got := enc.AppendEndMarker([]byte{})
want := []byte(`}`)
if !bytes.Equal(got, want) {
t.Errorf("AppendEndMarker()\ngot: %s, want: %s",
string(got),
string(want))
}
}
func TestAppendArrayStart(t *testing.T) {
got := enc.AppendArrayStart([]byte{})
want := []byte("[")
if !bytes.Equal(got, want) {
t.Errorf("AppendArrayStart() = %s, want: %s",
string(got),
string(want))
}
}
func TestAppendArrayEnd(t *testing.T) {
got := enc.AppendArrayEnd([]byte{})
want := []byte("]")
if !bytes.Equal(got, want) {
t.Errorf("AppendArrayEnd() = %s, want: %s",
string(got),
string(want))
}
}
func TestAppendArrayDelim(t *testing.T) {
got := enc.AppendArrayDelim([]byte{})
want := []byte("")
if !bytes.Equal(got, want) {
t.Errorf("AppendArrayDelim() = 0x%s, want: 0x%s",
string(got),
string(want))
}
got = enc.AppendArrayDelim([]byte("a"))
want = []byte("a,")
if !bytes.Equal(got, want) {
t.Errorf("AppendArrayDelim() = 0x%s, want: 0x%s",
string(got),
string(want))
}
}
func TestAppendLineBreak(t *testing.T) {
got := enc.AppendLineBreak([]byte{})
want := []byte("\n")
if !bytes.Equal(got, want) {
t.Errorf("AppendLineBreak() = 0x%s, want: 0x%s",
string(got),
string(want))
}
}
// inline copy from globals.go of InterfaceMarshalFunc used in tests to avoid import cycle
func interfaceMarshalFunc(v interface{}) ([]byte, error) {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
b := buf.Bytes()
if len(b) > 0 {
// Remove trailing \n which is added by Encode.
return b[:len(b)-1], nil
}
return b, nil
}
func TestAppendInterface(t *testing.T) {
oldJSONMarshalFunc := JSONMarshalFunc
defer func() {
JSONMarshalFunc = oldJSONMarshalFunc
}()
JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return interfaceMarshalFunc(v)
}
var i int = 17
got := enc.AppendInterface([]byte{}, i)
want := make([]byte, 0)
want = append(want, []byte("17")...) // of type interface, two characters
if !bytes.Equal(got, want) {
t.Errorf("AppendInterface\ngot: 0x%s\nwant: 0x%s",
string(got),
string(want))
}
JSONMarshalFunc = func(v interface{}) ([]byte, error) {
return nil, errors.New("test")
}
got = enc.AppendInterface([]byte{}, nil)
want = make([]byte, 0)
want = append(want, []byte("\"marshaling error: test\"")...) // of type interface, two characters
if !bytes.Equal(got, want) {
t.Errorf("AppendInterface\ngot: 0x%s\nwant: 0x%s",
string(got),
string(want))
}
}
func TestAppendType(t *testing.T) {
w := map[string]func(interface{}) []byte{
"AppendInt": func(v interface{}) []byte { return enc.AppendInt([]byte{}, v.(int)) },
"AppendInt8": func(v interface{}) []byte { return enc.AppendInt8([]byte{}, v.(int8)) },
"AppendInt16": func(v interface{}) []byte { return enc.AppendInt16([]byte{}, v.(int16)) },
"AppendInt32": func(v interface{}) []byte { return enc.AppendInt32([]byte{}, v.(int32)) },
"AppendInt64": func(v interface{}) []byte { return enc.AppendInt64([]byte{}, v.(int64)) },
"AppendUint": func(v interface{}) []byte { return enc.AppendUint([]byte{}, v.(uint)) },
"AppendUint8": func(v interface{}) []byte { return enc.AppendUint8([]byte{}, v.(uint8)) },
"AppendUint16": func(v interface{}) []byte { return enc.AppendUint16([]byte{}, v.(uint16)) },
"AppendUint32": func(v interface{}) []byte { return enc.AppendUint32([]byte{}, v.(uint32)) },
"AppendUint64": func(v interface{}) []byte { return enc.AppendUint64([]byte{}, v.(uint64)) },
"AppendFloat32": func(v interface{}) []byte { return enc.AppendFloat32([]byte{}, v.(float32), -1) },
"AppendFloat64": func(v interface{}) []byte { return enc.AppendFloat64([]byte{}, v.(float64), -1) },
"AppendFloat32SmallPrecision": func(v interface{}) []byte { return enc.AppendFloat32([]byte{}, v.(float32), 1) },
"AppendFloat64SmallPrecision": func(v interface{}) []byte { return enc.AppendFloat64([]byte{}, v.(float64), 1) },
}
tests := []struct {
name string
input float64
fn string
input interface{}
want []byte
}{
{"-Inf", math.Inf(-1), []byte(`"-Inf"`)},
{"+Inf", math.Inf(1), []byte(`"+Inf"`)},
{"NaN", math.NaN(), []byte(`"NaN"`)},
{"0", 0, []byte(`0`)},
{"-1.1", -1.1, []byte(`-1.1`)},
{"1e20", 1e20, []byte(`100000000000000000000`)},
{"1e21", 1e21, []byte(`1000000000000000000000`)},
{"AppendInt8(math.MaxInt8)", "AppendInt8", int8(math.MaxInt8), []byte("127")},
{"AppendInt16(math.MaxInt16)", "AppendInt16", int16(math.MaxInt16), []byte("32767")},
{"AppendInt32(math.MaxInt32)", "AppendInt32", int32(math.MaxInt32), []byte("2147483647")},
{"AppendInt64(math.MaxInt64)", "AppendInt64", int64(math.MaxInt64), []byte("9223372036854775807")},
{"AppendUint8(math.MaxUint8)", "AppendUint8", uint8(math.MaxUint8), []byte("255")},
{"AppendUint16(math.MaxUint16)", "AppendUint16", uint16(math.MaxUint16), []byte("65535")},
{"AppendUint32(math.MaxUint32)", "AppendUint32", uint32(math.MaxUint32), []byte("4294967295")},
{"AppendUint64(math.MaxUint64)", "AppendUint64", uint64(math.MaxUint64), []byte("18446744073709551615")},
{"AppendFloat32(-Inf)", "AppendFloat32", float32(math.Inf(-1)), []byte(`"-Inf"`)},
{"AppendFloat32(+Inf)", "AppendFloat32", float32(math.Inf(1)), []byte(`"+Inf"`)},
{"AppendFloat32(NaN)", "AppendFloat32", float32(math.NaN()), []byte(`"NaN"`)},
{"AppendFloat32(0)", "AppendFloat32", float32(0), []byte(`0`)},
{"AppendFloat32(-1.1)", "AppendFloat32", float32(-1.1), []byte(`-1.1`)},
{"AppendFloat32(1e20)", "AppendFloat32", float32(1e20), []byte(`100000000000000000000`)},
{"AppendFloat32(1e21)", "AppendFloat32", float32(1e21), []byte(`1e+21`)},
{"AppendFloat64(-Inf)", "AppendFloat64", float64(math.Inf(-1)), []byte(`"-Inf"`)},
{"AppendFloat64(+Inf)", "AppendFloat64", float64(math.Inf(1)), []byte(`"+Inf"`)},
{"AppendFloat64(NaN)", "AppendFloat64", float64(math.NaN()), []byte(`"NaN"`)},
{"AppendFloat64(0)", "AppendFloat64", float64(0), []byte(`0`)},
{"AppendFloat64(-1.1)", "AppendFloat64", float64(-1.1), []byte(`-1.1`)},
{"AppendFloat64(1e20)", "AppendFloat64", float64(1e20), []byte(`100000000000000000000`)},
{"AppendFloat64(1e21)", "AppendFloat64", float64(1e21), []byte(`1e+21`)},
{"AppendFloat32SmallPrecision(-1.123)", "AppendFloat32SmallPrecision", float32(-1.123), []byte(`-1.1`)},
{"AppendFloat64SmallPrecision(-1.123)", "AppendFloat64SmallPrecision", float64(-1.123), []byte(`-1.1`)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := AppendFloat32([]byte{}, float32(tt.input)); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendFloat32() = %s, want %s", got, tt.want)
}
if got := AppendFloat64([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendFloat32() = %s, want %s", got, tt.want)
if got := w[tt.fn](tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("got %s, want %s", got, tt.want)
}
})
}
}
func TestAppendMAC(t *testing.T) {
MACtests := []struct {
input string
want []byte
}{
{"01:23:45:67:89:ab", []byte(`"01:23:45:67:89:ab"`)},
{"cd:ef:11:22:33:44", []byte(`"cd:ef:11:22:33:44"`)},
}
for _, tt := range MACtests {
t.Run("MAC", func(t *testing.T) {
ha, _ := net.ParseMAC(tt.input)
if got := enc.AppendMACAddr([]byte{}, ha); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendMACAddr() = %s, want %s", got, tt.want)
}
})
}
}
func TestAppendBool(t *testing.T) {
for _, tc := range internal.BooleanTestCases {
s := enc.AppendBool([]byte{}, tc.Val)
got := string(s)
if got != tc.Json {
t.Errorf("AppendBool(%s)=0x%s, want: 0x%s",
tc.Json,
string(s),
string([]byte(tc.Binary)))
}
}
}
func TestAppendBoolArray(t *testing.T) {
for _, tc := range internal.BooleanArrayTestCases {
s := enc.AppendBools([]byte{}, tc.Val)
got := string(s)
if got != tc.Json {
t.Errorf("AppendBools(%s)=0x%s, want: 0x%s",
tc.Json,
hex.EncodeToString(s),
hex.EncodeToString([]byte(tc.Binary)))
}
}
// now empty array case
array := make([]bool, 0)
want := make([]byte, 0)
want = append(want, []byte("[]")...) // start and end array
got := enc.AppendBools([]byte{}, array)
if !bytes.Equal(got, want) {
t.Errorf("AppendBools(%v)\ngot: 0x%s\nwant: 0x%s",
array,
hex.EncodeToString(got),
hex.EncodeToString(want))
}
// now a large array case
array = make([]bool, 24)
want = make([]byte, 0)
want = append(want, []byte("[")...) // start a large array
for i := 0; i < 24; i++ {
array[i] = bool(i%2 == 1)
if array[i] {
want = append(want, []byte("true")...)
} else {
want = append(want, []byte("false")...)
}
if (i + 1) < 24 {
want = append(want, []byte(",")...)
}
}
want = append(want, []byte("]")...) // end a large array
got = enc.AppendBools([]byte{}, array)
if !bytes.Equal(got, want) {
t.Errorf("AppendBools(%v)\ngot: 0x%s\nwant: 0x%s",
array,
string(got),
string(want))
}
}
func TestAppendIP(t *testing.T) {
IPv4tests := []struct {
input net.IP
want []byte
}{
{net.IP{0, 0, 0, 0}, []byte(`"0.0.0.0"`)},
{net.IP{192, 0, 2, 200}, []byte(`"192.0.2.200"`)},
}
for _, tt := range IPv4tests {
t.Run("IPv4", func(t *testing.T) {
if got := enc.AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPAddr() = %s, want %s", got, tt.want)
}
})
}
IPv6tests := []struct {
input net.IP
want []byte
}{
{net.IPv6zero, []byte(`"::"`)},
{net.IPv6linklocalallnodes, []byte(`"ff02::1"`)},
{net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}, []byte(`"2001:db8:85a3::8a2e:370:7334"`)},
}
for _, tt := range IPv6tests {
t.Run("IPv6", func(t *testing.T) {
if got := enc.AppendIPAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPAddr() = %s, want %s", got, tt.want)
}
})
}
}
var IPAddrArrayTestCases = []struct {
input []net.IP
want []byte
}{
{[]net.IP{}, []byte(`[]`)},
{[]net.IP{{127, 0, 0, 0}}, []byte(`["127.0.0.0"]`)},
{[]net.IP{{0, 0, 0, 0}, {192, 168, 0, 100}}, []byte(`["0.0.0.0","192.168.0.100"]`)},
}
func TestAppendIPAddrs(t *testing.T) {
for _, tt := range IPAddrArrayTestCases {
t.Run("IPAddrs", func(t *testing.T) {
if got := enc.AppendIPAddrs([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPAddr() = %s, want %s", got, tt.want)
}
})
}
}
var IPPrefixArrayTestCases = []struct {
input []net.IPNet
want []byte
}{
{[]net.IPNet{}, []byte(`[]`)},
{[]net.IPNet{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(24, 32)}}, []byte(`["127.0.0.0/24"]`)},
{[]net.IPNet{{IP: net.IP{0, 0, 0, 0}, Mask: net.CIDRMask(0, 32)}, {IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}}, []byte(`["0.0.0.0/0","192.168.0.100/24"]`)},
}
func TestAppendIPPrefixes(t *testing.T) {
for _, tt := range IPPrefixArrayTestCases {
t.Run("IPPrefixes", func(t *testing.T) {
if got := enc.AppendIPPrefixes([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPAddr() = %s, want %s", got, tt.want)
}
})
}
}
func TestAppendIPPrefix(t *testing.T) {
IPv4Prefixtests := []struct {
input net.IPNet
want []byte
}{
{net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.IPv4Mask(0, 0, 0, 0)}, []byte(`"0.0.0.0/0"`)},
{net.IPNet{IP: net.IP{192, 0, 2, 200}, Mask: net.IPv4Mask(255, 255, 255, 0)}, []byte(`"192.0.2.200/24"`)},
}
for _, tt := range IPv4Prefixtests {
t.Run("IPv4", func(t *testing.T) {
if got := enc.AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPPrefix() = %s, want %s", got, tt.want)
}
})
}
IPv6Prefixtests := []struct {
input net.IPNet
want []byte
}{
{net.IPNet{IP: net.IPv6zero, Mask: net.CIDRMask(0, 128)}, []byte(`"::/0"`)},
{net.IPNet{IP: net.IPv6linklocalallnodes, Mask: net.CIDRMask(128, 128)}, []byte(`"ff02::1/128"`)},
{net.IPNet{IP: net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
Mask: net.CIDRMask(64, 128)},
[]byte(`"2001:db8:85a3::8a2e:370:7334/64"`)},
}
for _, tt := range IPv6Prefixtests {
t.Run("IPv6", func(t *testing.T) {
if got := enc.AppendIPPrefix([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendIPPrefix() = %s, want %s", got, tt.want)
}
})
}
}
func TestAppendMACAddr(t *testing.T) {
MACtests := []struct {
input net.HardwareAddr
want []byte
}{
{net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x90, 0xab}, []byte(`"12:34:56:78:90:ab"`)},
{net.HardwareAddr{0x12, 0x34, 0x00, 0x00, 0x90, 0xab}, []byte(`"12:34:00:00:90:ab"`)},
}
for _, tt := range MACtests {
t.Run("MAC", func(t *testing.T) {
if got := enc.AppendMACAddr([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendMAC() = %s, want %s", got, tt.want)
}
})
}
}
func TestAppendType2(t *testing.T) {
typeTests := []struct {
label string
input interface{}
want []byte
}{
{"int", 42, []byte(`"int"`)},
{"MAC", net.HardwareAddr{0x12, 0x34, 0x00, 0x00, 0x90, 0xab}, []byte(`"net.HardwareAddr"`)},
{"float64", float64(2.50), []byte(`"float64"`)},
{"nil", nil, []byte(`"<nil>"`)},
{"bool", true, []byte(`"bool"`)},
}
for _, tt := range typeTests {
t.Run(tt.label, func(t *testing.T) {
if got := enc.AppendType([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendType() = %s, want %s", got, tt.want)
}
})
}
}
func TestAppendObjectData(t *testing.T) {
tests := []struct {
dst []byte
obj []byte
want []byte
}{
{[]byte{}, []byte(`{"foo":"bar"}`), []byte(`"foo":"bar"}`)},
{[]byte(`{"qux":"quz"`), []byte(`{"foo":"bar"}`), []byte(`{"qux":"quz","foo":"bar"}`)},
{[]byte{}, []byte(`"foo":"bar"`), []byte(`"foo":"bar"`)},
{[]byte(`{"qux":"quz"`), []byte(`"foo":"bar"`), []byte(`{"qux":"quz","foo":"bar"`)},
}
for _, tt := range tests {
t.Run("ObjectData", func(t *testing.T) {
if got := enc.AppendObjectData(tt.dst, tt.obj); !reflect.DeepEqual(got, tt.want) {
t.Errorf("appendObjectData() = %s, want %s", got, tt.want)
}
})
}
+377
View File
@@ -0,0 +1,377 @@
package internal
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net"
"time"
)
var BooleanTestCases = []struct {
Val bool
Binary string
Json string
}{
{true, "\xf5", "true"},
{false, "\xf4", "false"},
}
var BooleanArrayTestCases = []struct {
Val []bool
Binary string
Json string
}{
{[]bool{}, "\x9f\xff", "[]"},
{[]bool{false}, "\x81\xf4", "[false]"},
{[]bool{true, false, true}, "\x83\xf5\xf4\xf5", "[true,false,true]"},
{[]bool{true, false, false, true, false, true}, "\x86\xf5\xf4\xf4\xf5\xf4\xf5", "[true,false,false,true,false,true]"},
}
var IntegerTestCases = []struct {
Val int
Binary string
}{
// Value included in the type.
{0, "\x00"},
{1, "\x01"},
{2, "\x02"},
{3, "\x03"},
{8, "\x08"},
{9, "\x09"},
{10, "\x0a"},
{22, "\x16"},
{23, "\x17"},
// Value in 1 byte.
{24, "\x18\x18"},
{25, "\x18\x19"},
{26, "\x18\x1a"},
{127, "\x18\x7f"},
{254, "\x18\xfe"},
{255, "\x18\xff"},
// Value in 2 bytes.
{256, "\x19\x01\x00"},
{257, "\x19\x01\x01"},
{1000, "\x19\x03\xe8"},
{0xFFFF, "\x19\xff\xff"},
// Value in 4 bytes.
{0x10000, "\x1a\x00\x01\x00\x00"},
{0x7FFFFFFE, "\x1a\x7f\xff\xff\xfe"},
{1000000, "\x1a\x00\x0f\x42\x40"},
// Negative number test cases.
// Value included in the type.
{-1, "\x20"},
{-2, "\x21"},
{-3, "\x22"},
{-10, "\x29"},
{-21, "\x34"},
{-22, "\x35"},
{-23, "\x36"},
{-24, "\x37"},
// Value in 1 byte.
{-25, "\x38\x18"},
{-26, "\x38\x19"},
{-100, "\x38\x63"},
{-128, "\x38\x7f"},
{-254, "\x38\xfd"},
{-255, "\x38\xfe"},
{-256, "\x38\xff"},
// Value in 2 bytes.
{-257, "\x39\x01\x00"},
{-258, "\x39\x01\x01"},
{-1000, "\x39\x03\xe7"},
// Value in 4 bytes.
{-0x10001, "\x3a\x00\x01\x00\x00"},
{-0x7FFFFFFE, "\x3a\x7f\xff\xff\xfd"},
{-1000000, "\x3a\x00\x0f\x42\x3f"},
//Constants
{math.MaxInt8, "\x18\x7f"},
{math.MinInt8, "\x38\x7f"},
{math.MaxInt16, "\x19\x7f\xff"},
{math.MinInt16, "\x39\x7f\xff"},
{math.MaxInt32, "\x1a\x7f\xff\xff\xff"},
{math.MinInt32, "\x3a\x7f\xff\xff\xff"},
{math.MaxInt64, "\x1b\x7f\xff\xff\xff\xff\xff\xff\xff"},
{math.MinInt64, "\x3b\x7f\xff\xff\xff\xff\xff\xff\xff"},
}
type UnsignedIntTestCase struct {
Val uint
Binary string
Bigbinary string
}
var AdditionalUnsignedIntegerTestCases = []UnsignedIntTestCase{
{0x7FFFFFFF, "\x18\xff", "\x1a\x7f\xff\xff\xff"},
{0x80000000, "\x19\xff\xff", "\x1a\x80\x00\x00\x00"},
{1000000, "\x1b\x80\x00\x00\x00\x00\x00\x00\x00", "\x1a\x00\x0f\x42\x40"},
//Constants
{math.MaxUint8, "\x18\xff", "\x18\xff"},
{math.MaxUint16, "\x19\xff\xff", "\x19\xff\xff"},
{math.MaxUint32, "\x1a\xff\xff\xff\xff", "\x1a\xff\xff\xff\xff"},
{math.MaxUint64, "\x1b\xff\xff\xff\xff\xff\xff\xff\xff", "\x1b\xff\xff\xff\xff\xff\xff\xff\xff"},
}
func unsignedIntegerTestCases() []UnsignedIntTestCase {
size := len(IntegerTestCases) + len(AdditionalUnsignedIntegerTestCases)
cases := make([]UnsignedIntTestCase, 0, size)
cases = append(cases, AdditionalUnsignedIntegerTestCases...)
for _, itc := range IntegerTestCases {
if itc.Val < 0 {
continue
}
cases = append(cases, UnsignedIntTestCase{Val: uint(itc.Val), Binary: itc.Binary, Bigbinary: itc.Binary})
}
return cases
}
var UnsignedIntegerTestCases = unsignedIntegerTestCases()
var Float32TestCases = []struct {
Val float32
Binary string
}{
{0.0, "\xfa\x00\x00\x00\x00"},
{-0.0, "\xfa\x00\x00\x00\x00"},
{1.0, "\xfa\x3f\x80\x00\x00"},
{1.5, "\xfa\x3f\xc0\x00\x00"},
{65504.0, "\xfa\x47\x7f\xe0\x00"},
{-4.0, "\xfa\xc0\x80\x00\x00"},
{0.00006103515625, "\xfa\x38\x80\x00\x00"},
{float32(math.Inf(0)), "\xfa\x7f\x80\x00\x00"},
{float32(math.Inf(-1)), "\xfa\xff\x80\x00\x00"},
{float32(math.NaN()), "\xfa\x7f\xc0\x00\x00"},
{math.SmallestNonzeroFloat32, "\xfa\x00\x00\x00\x01"},
{math.MaxFloat32, "\xfa\x7f\x7f\xff\xff"},
}
var Float64TestCases = []struct {
Val float64
Binary string
}{
{0.0, "\xfa\x00\x00\x00\x00"},
{-0.0, "\xfa\x00\x00\x00\x00"},
{1.0, "\xfa\x3f\x80\x00\x00"},
{1.5, "\xfa\x3f\xc0\x00\x00"},
{65504.0, "\xfa\x47\x7f\xe0\x00"},
{-4.0, "\xfa\xc0\x80\x00\x00"},
{0.00006103515625, "\xfa\x38\x80\x00\x00"},
{math.Inf(0), "\xfa\x7f\x80\x00\x00\x00\x00\x00\x00"},
{math.Inf(-1), "\xfa\xff\x80\x00\x00\x00\x00\x00\x00"},
{math.NaN(), "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"},
{math.SmallestNonzeroFloat64, "\xfa\x00\x00\x00\x00\x00\x00\x00\x01"},
{math.MaxFloat64, "\xfa\x7f\x7f\xff\xff"},
}
var IntegerArrayTestCases = []struct {
Val []int
Binary string
Json string
}{
{[]int{}, "\x9f\xff", "[]"},
{[]int{32768}, "\x81\x19\x80\x00", "[32768]"},
{[]int{-1, 0, 200, 20}, "\x84\x20\x00\x18\xc8\x14", "[-1,0,200,20]"},
{[]int{-200, -10, 200, 400}, "\x84\x38\xc7\x29\x18\xc8\x19\x01\x90", "[-200,-10,200,400]"},
{[]int{1, 2, 3}, "\x83\x01\x02\x03", "[1,2,3]"},
{[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25},
"\x98\x19\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x18\x18\x19",
"[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]"},
}
var IpAddrTestCases = []struct {
Ipaddr net.IP
Text string
Binary string
}{
{net.IP{10, 0, 0, 1}, "\"10.0.0.1\"", "\xd9\x01\x04\x44\x0a\x00\x00\x01"},
{net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x0, 0x0, 0x0, 0x0, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
"\"2001:db8:85a3::8a2e:370:7334\"",
"\xd9\x01\x04\x50\x20\x01\x0d\xb8\x85\xa3\x00\x00\x00\x00\x8a\x2e\x03\x70\x73\x34"},
}
var IPAddrArrayTestCases = []struct {
Val []net.IP
Binary string
Json string
}{
{[]net.IP{}, "\x9f\xff", "[]"},
{[]net.IP{{127, 0, 0, 0}}, "\x81\xd9\x01\x04\x44\x7f\x00\x00\x00", "[127.0.0.0]"},
{[]net.IP{{0, 0, 0, 0}, {192, 168, 0, 100}}, "\x82\xd9\x01\x04\x44\x00\x00\x00\x00\xd9\x01\x04\x44\xc0\xa8\x00\x64", "[0.0.0.0,192.168.0.100]"},
}
var IPPrefixTestCases = []struct {
Pfx net.IPNet
Text string // ASCII representation of pfx
Binary string // CBOR representation of pfx
}{
{net.IPNet{IP: net.IP{0, 0, 0, 0}, Mask: net.CIDRMask(0, 32)}, "\"0.0.0.0/0\"", "\xd9\x01\x05\xa1\x44\x00\x00\x00\x00\x00"},
{net.IPNet{IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}, "\"192.168.0.100/24\"",
"\xd9\x01\x05\xa1\x44\xc0\xa8\x00\x64\x18\x18"},
{net.IPNet{IP: net.IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, Mask: net.CIDRMask(128, 128)}, "\"::1/128\"",
"\xd9\x01\x05\xa1\x50\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x18\x80"},
}
var IPPrefixArrayTestCases = []struct {
Val []net.IPNet
Binary string
Json string
}{
{[]net.IPNet{}, "\x9f\xff", "[]"},
{[]net.IPNet{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(24, 32)}}, "\x81\xd9\x01\x05\xa1\x44\x7f\x00\x00\x00\x18\x18", "[127.0.0.0/24]"},
{[]net.IPNet{{IP: net.IP{0, 0, 0, 0}, Mask: net.CIDRMask(0, 32)}, {IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}}, "\x82\xd9\x01\x05\xa1\x44\x00\x00\x00\x00\x00\xd9\x01\x05\xa1\x44\xc0\xa8\x00\x64\x18\x18", "[0.0.0.0/0,192.168.0.100/24]"},
}
var MacAddrTestCases = []struct {
Macaddr net.HardwareAddr
Text string // ASCII representation of macaddr
Binary string // CBOR representation of macaddr
}{
{net.HardwareAddr{0x12, 0x34, 0x56, 0x78, 0x90, 0xab}, "\"12:34:56:78:90:ab\"", "\xd9\x01\x04\x46\x12\x34\x56\x78\x90\xab"},
{net.HardwareAddr{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3}, "\"20:01:0d:b8:85:a3\"", "\xd9\x01\x04\x46\x20\x01\x0d\xb8\x85\xa3"},
}
var EncodeHexTests = []struct {
In byte
Out string
}{
{0x00, `"00"`},
{0x0f, `"0f"`},
{0x10, `"10"`},
{0xf0, `"f0"`},
{0xff, `"ff"`},
}
var EncodeStringTests = []struct {
In string
Out string
}{
{"", `""`},
{"\\", `"\\"`},
{"\x00", `"\u0000"`},
{"\x01", `"\u0001"`},
{"\x02", `"\u0002"`},
{"\x03", `"\u0003"`},
{"\x04", `"\u0004"`},
{"\x05", `"\u0005"`},
{"\x06", `"\u0006"`},
{"\x07", `"\u0007"`},
{"\x08", `"\b"`},
{"\x09", `"\t"`},
{"\x0a", `"\n"`},
{"\x0b", `"\u000b"`},
{"\x0c", `"\f"`},
{"\x0d", `"\r"`},
{"\x0e", `"\u000e"`},
{"\x0f", `"\u000f"`},
{"\x10", `"\u0010"`},
{"\x11", `"\u0011"`},
{"\x12", `"\u0012"`},
{"\x13", `"\u0013"`},
{"\x14", `"\u0014"`},
{"\x15", `"\u0015"`},
{"\x16", `"\u0016"`},
{"\x17", `"\u0017"`},
{"\x18", `"\u0018"`},
{"\x19", `"\u0019"`},
{"\x1a", `"\u001a"`},
{"\x1b", `"\u001b"`},
{"\x1c", `"\u001c"`},
{"\x1d", `"\u001d"`},
{"\x1e", `"\u001e"`},
{"\x1f", `"\u001f"`},
{"✭", `"✭"`},
{"foo\xc2\x7fbar", `"foo\ufffd\u007fbar"`}, // invalid sequence
{"ascii", `"ascii"`},
{"\"a", `"\"a"`},
{"\x1fa", `"\u001fa"`},
{"foo\"bar\"baz", `"foo\"bar\"baz"`},
{"\x1ffoo\x1fbar\x1fbaz", `"\u001ffoo\u001fbar\u001fbaz"`},
{"emoji \u2764\ufe0f!", `"emoji ❤️!"`},
}
var EncodeStringsTests = []struct {
In []string
Out string
}{
{nil, `[]`},
{[]string{}, `[]`},
{[]string{"A"}, `["A"]`},
{[]string{"A", "B"}, `["A","B"]`},
}
var EncodeStringerTests = []struct {
In fmt.Stringer
Out string
Binary string
}{
{nil, `null`, "\xf6"},
{fmt.Stringer(nil), `null`, "\xf6"},
{net.IPv4bcast, `"255.255.255.255"`, "\x6f\x32\x35\x35\x2e\x32\x35\x35\x2e\x32\x35\x35\x2e\x32\x35\x35"},
}
var EncodeStringersTests = []struct {
In []fmt.Stringer
Out string
Binary string
}{
{nil, `[]`, "\x9f\xff"},
{[]fmt.Stringer{}, `[]`, "\x9f\xff"},
{[]fmt.Stringer{net.IPv4bcast}, `["255.255.255.255"]`, "\x9f\x6f255.255.255.255\xff"},
{[]fmt.Stringer{net.IPv4allsys, net.IPv4allrouter}, `["224.0.0.1","224.0.0.2"]`, "\x9f\x69224.0.0.1\x69224.0.0.2\xff"},
}
var TimeIntegerTestcases = []struct {
Txt string
Binary string
RfcStr string
UnixInt int
}{
{"2013-02-03T19:54:00-08:00", "\xc1\x1a\x51\x0f\x30\xd8", "2013-02-04T03:54:00Z", 1359950040},
{"1950-02-03T19:54:00-08:00", "\xc1\x3a\x25\x71\x93\xa7", "1950-02-04T03:54:00Z", -628200360},
}
var TimeFloatTestcases = []struct {
RfcStr string
Out string
UnixInt int
}{
{"2006-01-02T15:04:05.999999-08:00", "\xc1\xfb\x41\xd0\xee\x6c\x59\x7f\xff\xfc", 1136243045},
{"1956-01-02T15:04:05.999999-08:00", "\xc1\xfb\xc1\xba\x53\x81\x1a\x00\x00\x11", -441680155},
}
var DurTestcases = []struct {
Duration time.Duration
FloatOut string
IntegerOut string
}{
{1000, "\xfb\x3f\xf0\x00\x00\x00\x00\x00\x00", "\x01"},
{2000, "\xfb\x40\x00\x00\x00\x00\x00\x00\x00", "\x02"},
{200000, "\xfb\x40\x69\x00\x00\x00\x00\x00\x00", "\x18\xc8"},
}
// inline copy from globals.go of InterfaceMarshalFunc used in tests to avoid import cycle
func InterfaceMarshalFunc(v interface{}) ([]byte, error) {
var buf bytes.Buffer
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
if err != nil {
return nil, err
}
b := buf.Bytes()
if len(b) > 0 {
// Remove trailing \n which is added by Encode.
return b[:len(b)-1], nil
}
return b, nil
}
+150
View File
@@ -0,0 +1,150 @@
//go:build !windows
// +build !windows
// Package journald provides a io.Writer to send the logs
// to journalD component of systemd.
package journald
// This file provides a zerolog writer so that logs printed
// using zerolog library can be sent to a journalD.
// Zerolog's Top level key/Value Pairs are translated to
// journald's args - all Values are sent to journald as strings.
// And all key strings are converted to uppercase and sanitized
// by replacing any characters not in [A-Z0-9_] with '_' before
// sending to journald (as required by journald).
// In addition, entire log message (all Key Value Pairs), is also
// sent to journald under the key "JSON".
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/coreos/go-systemd/v22/journal"
"github.com/rs/zerolog"
"github.com/rs/zerolog/internal/cbor"
)
const defaultJournalDPrio = journal.PriNotice
// SendFunc is the function used to send logs to journald.
// It can be replaced in tests for mocking. If nil, journal.Send is used directly.
// This variable should only be modified in tests and must not be changed while the
// writer is in use. Tests that modify this variable should not use t.Parallel().
var SendFunc func(string, journal.Priority, map[string]string) error
// NewJournalDWriter returns a zerolog log destination
// to be used as parameter to New() calls. Writing logs
// to this writer will send the log messages to journalD
// running in this system.
func NewJournalDWriter() io.Writer {
return journalWriter{}
}
type journalWriter struct {
}
// levelToJPrio converts zerolog Level string into
// journalD's priority values. JournalD has more
// priorities than zerolog.
func levelToJPrio(zLevel string) journal.Priority {
lvl, _ := zerolog.ParseLevel(zLevel)
switch lvl {
case zerolog.TraceLevel:
return journal.PriDebug
case zerolog.DebugLevel:
return journal.PriDebug
case zerolog.InfoLevel:
return journal.PriInfo
case zerolog.WarnLevel:
return journal.PriWarning
case zerolog.ErrorLevel:
return journal.PriErr
case zerolog.FatalLevel:
return journal.PriCrit
case zerolog.PanicLevel:
return journal.PriEmerg
case zerolog.NoLevel:
return journal.PriNotice
}
return defaultJournalDPrio
}
// sanitizeKey converts a key to uppercase and replaces invalid characters with '_'
// JournalD requires keys start with A-Z and contain only A-Z, 0-9, or _
func sanitizeKey(key string) string {
sanitized := strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' {
return r - 'a' + 'A'
} else if (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' {
return r
} else {
return '_'
}
}, key)
if len(sanitized) == 0 || sanitized[0] >= '0' && sanitized[0] <= '9' || sanitized[0] == '_' {
sanitized = "X" + sanitized
}
return sanitized
}
func (w journalWriter) Write(p []byte) (n int, err error) {
var event map[string]interface{}
origPLen := len(p)
p = cbor.DecodeIfBinaryToBytes(p)
d := json.NewDecoder(bytes.NewReader(p))
d.UseNumber()
err = d.Decode(&event)
jPrio := defaultJournalDPrio
args := make(map[string]string)
if err != nil {
return
}
if l, ok := event[zerolog.LevelFieldName].(string); ok {
jPrio = levelToJPrio(l)
}
msg := ""
for key, value := range event {
jKey := sanitizeKey(key)
switch key {
case zerolog.LevelFieldName, zerolog.TimestampFieldName:
continue
case zerolog.MessageFieldName:
msg, _ = value.(string)
continue
}
switch v := value.(type) {
case string:
args[jKey] = v
case json.Number:
args[jKey] = fmt.Sprint(value)
default:
b, err := zerolog.InterfaceMarshalFunc(value)
if err != nil {
args[jKey] = fmt.Sprintf("[error: %v]", err)
} else {
args[jKey] = string(b)
}
}
}
args["JSON"] = string(p)
if SendFunc != nil {
err = SendFunc(msg, jPrio, args)
} else {
err = journal.Send(msg, jPrio, args)
}
if err == nil {
n = origPLen
}
return
}
+282
View File
@@ -0,0 +1,282 @@
//go:build linux
// +build linux
package journald
import (
"bytes"
"fmt"
"io"
"strings"
"testing"
"github.com/coreos/go-systemd/v22/journal"
"github.com/rs/zerolog"
)
func ExampleNewJournalDWriter() {
log := zerolog.New(NewJournalDWriter())
log.Info().Str("foo", "bar").Uint64("small", 123).Float64("float", 3.14).Uint64("big", 1152921504606846976).Msg("Journal Test")
// Output:
}
/*
There is no automated way to verify the output - since the output is sent
to journald process and method to retrieve is journalctl. Will find a way
to automate the process and fix this test.
$ journalctl -o verbose -f
Thu 2018-04-26 22:30:20.768136 PDT [s=3284d695bde946e4b5017c77a399237f;i=329f0;b=98c0dca0debc4b98a5b9534e910e7dd6;m=7a702e35dd4;t=56acdccd2ed0a;x=4690034cf0348614]
PRIORITY=6
_AUDIT_LOGINUID=1000
_BOOT_ID=98c0dca0debc4b98a5b9534e910e7dd6
_MACHINE_ID=926ed67eb4744580948de70fb474975e
_HOSTNAME=sprint
_UID=1000
_GID=1000
_CAP_EFFECTIVE=0
_SYSTEMD_SLICE=-.slice
_TRANSPORT=journal
_SYSTEMD_CGROUP=/
_AUDIT_SESSION=2945
MESSAGE=Journal Test
FOO=bar
BIG=1152921504606846976
_COMM=journald.test
SMALL=123
FLOAT=3.14
JSON={"level":"info","foo":"bar","small":123,"float":3.14,"big":1152921504606846976,"message":"Journal Test"}
_PID=27103
_SOURCE_REALTIME_TIMESTAMP=1524807020768136
*/
func TestSanitizeKey(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"test", "TEST"},
{"Test", "TEST"},
{"test-key", "TEST_KEY"},
{"Test.Key", "TEST_KEY"},
{"test_key123", "TEST_KEY123"},
{"invalid@key!", "INVALID_KEY_"},
{"a1B2_c3D4", "A1B2_C3D4"},
{"_", "X_"},
{"", "X"},
{"123", "X123"},
{"a-b.c_d", "A_B_C_D"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
result := sanitizeKey(tt.input)
if result != tt.expected {
t.Errorf("sanitizeKey(%q) = %q; want %q", tt.input, result, tt.expected)
}
})
}
}
func TestWriteReturnsNoOfWrittenBytes(t *testing.T) {
input := []byte(`{"level":"info","time":1570912626,"message":"Starting..."}`)
wr := NewJournalDWriter()
want := len(input)
got, err := wr.Write(input)
if err != nil {
t.Errorf("Unexpected error %v", err)
}
if want != got {
t.Errorf("Expected %d bytes to be written got %d", want, got)
}
}
func TestMultiWrite(t *testing.T) {
var (
w1 = new(bytes.Buffer)
w2 = new(bytes.Buffer)
w3 = NewJournalDWriter()
)
zerolog.ErrorHandler = func(err error) {
if err == io.ErrShortWrite {
t.Errorf("Unexpected ShortWriteError")
t.FailNow()
}
}
log := zerolog.New(io.MultiWriter(w1, w2, w3)).With().Logger()
for i := 0; i < 10; i++ {
log.Info().Msg("Tick!")
}
}
func TestWriteWithVariousTypes(t *testing.T) {
mock := &mockSend{}
oldSend := SendFunc
SendFunc = mock.send
defer func() { SendFunc = oldSend }()
wr := NewJournalDWriter()
log := zerolog.New(wr)
// This should cover the default case in the switch for value types
log.Info().Bool("flag", true).Str("foo", "bar").Uint64("small", 123).Float64("float", 3.14).Uint64("big", 1152921504606846976).Interface("data", map[string]int{"a": 1}).Msg("Test various types")
// Verify the call
if len(mock.calls) != 1 {
t.Fatalf("Expected 1 call, got %d", len(mock.calls))
}
call := mock.calls[0]
// Check that flag is sanitized to FLAG and value is "true"
if call.args["FLAG"] != "true" {
t.Errorf("Expected FLAG=true, got %s", call.args["FLAG"])
}
// Check that data is marshaled (should be a JSON string)
expectedData := `{"a":1}`
if call.args["DATA"] != expectedData {
t.Errorf("Expected DATA=%q, got %q", expectedData, call.args["DATA"])
}
}
func TestWriteWithAllLevels(t *testing.T) {
wr := NewJournalDWriter()
// Save original FatalExitFunc
oldFatalExitFunc := zerolog.FatalExitFunc
defer func() { zerolog.FatalExitFunc = oldFatalExitFunc }()
// Set FatalExitFunc to prevent actual exit
zerolog.FatalExitFunc = func() {}
log := zerolog.New(wr)
// Test all zerolog levels to cover levelToJPrio switch cases
log.Trace().Msg("Trace level")
log.Debug().Msg("Debug level")
log.Info().Msg("Info level")
log.Warn().Msg("Warn level")
log.Error().Msg("Error level")
log.Log().Msg("No level")
// For Fatal, it will call FatalExitFunc instead of exiting
log.Fatal().Msg("Fatal level")
// For Panic, use recover to catch the panic, do last because it will stop of this test execution
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic from Panic level")
}
}()
log.Panic().Msg("Panic level")
}
func TestWriteOutputs(t *testing.T) {
mock := &mockSend{}
oldSend := SendFunc
SendFunc = mock.send
defer func() { SendFunc = oldSend }()
wr := NewJournalDWriter()
log := zerolog.New(wr)
// Log a message with various fields
log.Info().Str("test-key", "value").Int("number", 42).Msg("Test message")
// Check that SendFunc was called
if len(mock.calls) != 1 {
t.Fatalf("Expected 1 call to SendFunc, got %d", len(mock.calls))
}
call := mock.calls[0]
// Check message
if call.msg != "Test message" {
t.Errorf("Expected msg 'Test message', got %q", call.msg)
}
// Check priority
if call.prio != journal.PriInfo {
t.Errorf("Expected prio %d (PriInfo), got %d", journal.PriInfo, call.prio)
}
// Check args
expectedArgs := map[string]string{
"TEST_KEY": "value",
"NUMBER": "42",
"JSON": `{"level":"info","test-key":"value","number":42,"message":"Test message"}` + "\n",
}
for k, v := range expectedArgs {
if call.args[k] != v {
t.Errorf("Expected args[%q] = %q, got %q", k, v, call.args[k])
}
}
// Check that LEVEL is not in args (since it's skipped)
if _, ok := call.args["LEVEL"]; ok {
t.Error("LEVEL should not be in args")
}
}
func TestWriteWithMarshalError(t *testing.T) {
mock := &mockSend{}
oldSend := SendFunc
SendFunc = mock.send
defer func() { SendFunc = oldSend }()
// Save original marshal func
originalMarshal := zerolog.InterfaceMarshalFunc
defer func() { zerolog.InterfaceMarshalFunc = originalMarshal }()
// Set marshal func to fail
zerolog.InterfaceMarshalFunc = func(v interface{}) ([]byte, error) {
return nil, fmt.Errorf("fake error")
}
wr := NewJournalDWriter()
log := zerolog.New(wr)
// This should trigger the error handling in the default case
log.Info().Interface("data", map[string]int{"a": 1}).Msg("Test with error")
// Verify the call
if len(mock.calls) != 1 {
t.Fatalf("Expected 1 call, got %d", len(mock.calls))
}
call := mock.calls[0]
// Check that data has the error message
got := call.args["DATA"]
want := "error: fake error"
if !strings.Contains(got, want) {
t.Errorf("Expected DATA to contain %q, got %q", want, got)
}
}
type mockSend struct {
calls []struct {
msg string
prio journal.Priority
args map[string]string
}
}
func (m *mockSend) send(msg string, prio journal.Priority, args map[string]string) error {
m.calls = append(m.calls, struct {
msg string
prio journal.Priority
args map[string]string
}{msg, prio, args})
return nil
}
+324 -143
View File
@@ -2,83 +2,129 @@
//
// A global Logger can be use for simple logging:
//
// import "github.com/rs/zerolog/log"
// import "github.com/rs/zerolog/log"
//
// log.Info().Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world"}
// log.Info().Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world"}
//
// NOTE: To import the global logger, import the "log" subpackage "github.com/rs/zerolog/log".
//
// Fields can be added to log messages:
//
// log.Info().Str("foo", "bar").Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
// log.Info().Str("foo", "bar").Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
//
// Create logger instance to manage different outputs:
//
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// logger.Info().
// Str("foo", "bar").
// Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// logger.Info().
// Str("foo", "bar").
// Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
//
// Sub-loggers let you chain loggers with additional context:
//
// sublogger := log.With().Str("component": "foo").Logger()
// sublogger.Info().Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","component":"foo"}
// sublogger := log.With().Str("component", "foo").Logger()
// sublogger.Info().Msg("hello world")
// // Output: {"time":1494567715,"level":"info","message":"hello world","component":"foo"}
//
// Level logging
//
// zerolog.SetGlobalLevel(zerolog.InfoLevel)
// zerolog.SetGlobalLevel(zerolog.InfoLevel)
//
// log.Debug().Msg("filtered out message")
// log.Info().Msg("routed message")
// log.Debug().Msg("filtered out message")
// log.Info().Msg("routed message")
//
// if e := log.Debug(); e.Enabled() {
// // Compute log output only if enabled.
// value := compute()
// e.Str("foo": value).Msg("some debug message")
// }
// // Output: {"level":"info","time":1494567715,"routed message"}
// if e := log.Debug(); e.Enabled() {
// // Compute log output only if enabled.
// value := compute()
// e.Str("foo": value).Msg("some debug message")
// }
// // Output: {"level":"info","time":1494567715,"routed message"}
//
// Customize automatic field names:
//
// log.TimestampFieldName = "t"
// log.LevelFieldName = "p"
// log.MessageFieldName = "m"
// log.TimestampFieldName = "t"
// log.LevelFieldName = "p"
// log.MessageFieldName = "m"
//
// log.Info().Msg("hello world")
// // Output: {"t":1494567715,"p":"info","m":"hello world"}
// log.Info().Msg("hello world")
// // Output: {"t":1494567715,"p":"info","m":"hello world"}
//
// Log with no level and message:
//
// log.Log().Str("foo","bar").Msg("")
// // Output: {"time":1494567715,"foo":"bar"}
// log.Log().Str("foo","bar").Msg("")
// // Output: {"time":1494567715,"foo":"bar"}
//
// Add contextual fields to global Logger:
//
// log.Logger = log.With().Str("foo", "bar").Logger()
// log.Logger = log.With().Str("foo", "bar").Logger()
//
// Sample logs:
//
// sampled := log.Sample(10)
// sampled.Info().Msg("will be logged every 10 messages")
// sampled := log.Sample(&zerolog.BasicSampler{N: 10})
// sampled.Info().Msg("will be logged every 10 messages")
//
// Log with contextual hooks:
//
// // Create the hook:
// type SeverityHook struct{}
//
// func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
// if level != zerolog.NoLevel {
// e.Str("severity", level.String())
// }
// }
//
// // And use it:
// var h SeverityHook
// log := zerolog.New(os.Stdout).Hook(h)
// log.Warn().Msg("")
// // Output: {"level":"warn","severity":"warn"}
//
// # Caveats
//
// Field duplication:
//
// There is no fields deduplication out-of-the-box.
// Using the same key multiple times creates new key in final JSON each time.
//
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
// logger.Info().
// Timestamp().
// Msg("dup")
// // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
//
// In this case, many consumers will take the last value,
// but this is not guaranteed; check yours if in doubt.
//
// Concurrency safety:
//
// Be careful when calling UpdateContext. It is not concurrency safe. Use the With method to create a child logger:
//
// func handler(w http.ResponseWriter, r *http.Request) {
// // Create a child logger for concurrency safety
// logger := log.Logger.With().Logger()
//
// // Add context fields, for example User-Agent from HTTP headers
// logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
// ...
// })
// }
package zerolog
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"sync/atomic"
"github.com/rs/zerolog/internal/json"
"strings"
)
// Level defines log levels.
type Level uint8
type Level int8
const (
// DebugLevel defines debug log level.
@@ -93,50 +139,101 @@ const (
FatalLevel
// PanicLevel defines panic log level.
PanicLevel
// NoLevel defines an absent log level.
NoLevel
// Disabled disables the logger.
Disabled
// TraceLevel defines trace log level.
TraceLevel Level = -1
// Values less than TraceLevel are handled as numbers.
)
func (l Level) String() string {
switch l {
case TraceLevel:
return LevelTraceValue
case DebugLevel:
return "debug"
return LevelDebugValue
case InfoLevel:
return "info"
return LevelInfoValue
case WarnLevel:
return "warn"
return LevelWarnValue
case ErrorLevel:
return "error"
return LevelErrorValue
case FatalLevel:
return "fatal"
return LevelFatalValue
case PanicLevel:
return "panic"
return LevelPanicValue
case Disabled:
return "disabled"
case NoLevel:
return ""
}
return ""
return strconv.Itoa(int(l))
}
const (
// Often samples log every 10 events.
Often = 10
// Sometimes samples log every 100 events.
Sometimes = 100
// Rarely samples log every 1000 events.
Rarely = 1000
)
// ParseLevel converts a level string into a zerolog Level value.
// returns an error if the input string does not match known values.
func ParseLevel(levelStr string) (Level, error) {
switch {
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(TraceLevel)):
return TraceLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(DebugLevel)):
return DebugLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(InfoLevel)):
return InfoLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(WarnLevel)):
return WarnLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(ErrorLevel)):
return ErrorLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(FatalLevel)):
return FatalLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(PanicLevel)):
return PanicLevel, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(Disabled)):
return Disabled, nil
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(NoLevel)):
return NoLevel, nil
}
i, err := strconv.Atoi(levelStr)
if err != nil {
return NoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
}
if i > 127 || i < -128 {
return NoLevel, fmt.Errorf("Out-Of-Bounds Level: '%d', defaulting to NoLevel", i)
}
return Level(i), nil
}
var disabledEvent = newEvent(levelWriterAdapter{ioutil.Discard}, 0, false)
// UnmarshalText implements encoding.TextUnmarshaler to allow for easy reading from toml/yaml/json formats
func (l *Level) UnmarshalText(text []byte) error {
if l == nil {
return errors.New("can't unmarshal a nil *Level")
}
var err error
*l, err = ParseLevel(string(text))
return err
}
// MarshalText implements encoding.TextMarshaler to allow for easy writing into toml/yaml/json formats
func (l Level) MarshalText() ([]byte, error) {
return []byte(LevelFieldMarshalFunc(l)), nil
}
// A Logger represents an active logging object that generates lines
// of JSON output to an io.Writer. Each logging operation makes a single
// call to the Writer's Write method. There is no guaranty on access
// call to the Writer's Write method. There is no guarantee on access
// serialization to the Writer. If your Writer is not thread safe,
// you may consider a sync wrapper.
type Logger struct {
w LevelWriter
level Level
sample uint32
counter *uint32
sampler Sampler
context []byte
hooks []Hook
stack bool
ctx context.Context
}
// New creates a root logger with given output writer. If the output writer implements
@@ -144,17 +241,17 @@ type Logger struct {
// one.
//
// Each logging operation makes a single call to the Writer's Write method. There is no
// guaranty on access serialization to the Writer. If your Writer is not thread safe,
// guarantee on access serialization to the Writer. If your Writer is not thread safe,
// you may consider using sync wrapper.
func New(w io.Writer) Logger {
if w == nil {
w = ioutil.Discard
w = io.Discard
}
lw, ok := w.(LevelWriter)
if !ok {
lw = levelWriterAdapter{w}
lw = LevelWriterAdapter{w}
}
return Logger{w: lw}
return Logger{w: lw, level: TraceLevel}
}
// Nop returns a disabled logger for which all operation are no-op.
@@ -166,8 +263,15 @@ func Nop() Logger {
func (l Logger) Output(w io.Writer) Logger {
l2 := New(w)
l2.level = l.level
l2.sample = l.sample
copy(l2.context, l.context)
l2.sampler = l.sampler
l2.stack = l.stack
if len(l.hooks) > 0 {
l2.hooks = append(l2.hooks, l.hooks...)
}
if l.context != nil {
l2.context = make([]byte, len(l.context), cap(l.context))
copy(l2.context, l.context)
}
return l2
}
@@ -178,91 +282,144 @@ func (l Logger) With() Context {
if context != nil {
l.context = append(l.context, context...)
} else {
// first byte of context is presence of timestamp or not
l.context = append(l.context, 0)
// This is needed for AppendKey to not check len of input
// thus making it inlinable
l.context = enc.AppendBeginMarker(l.context)
}
return Context{l}
}
// Level creates a child logger with the minimum accepted level set to level.
func (l Logger) Level(lvl Level) Logger {
return Logger{
w: l.w,
level: lvl,
sample: l.sample,
counter: l.counter,
context: l.context,
// UpdateContext updates the internal logger's context.
//
// Caution: This method is not concurrency safe.
// Use the With method to create a child logger before modifying the context from concurrent goroutines.
func (l *Logger) UpdateContext(update func(c Context) Context) {
if l.disabled() {
return
}
if cap(l.context) == 0 {
l.context = make([]byte, 0, 500)
}
if len(l.context) == 0 {
l.context = enc.AppendBeginMarker(l.context)
}
c := update(Context{*l})
l.context = c.l.context
}
// Sample returns a logger that only let one message out of every to pass thru.
func (l Logger) Sample(every int) Logger {
if every == 0 {
// Create a child with no sampling.
return Logger{
w: l.w,
level: l.level,
context: l.context,
}
}
return Logger{
w: l.w,
level: l.level,
sample: uint32(every),
counter: new(uint32),
context: l.context,
// Level creates a child logger with the minimum accepted level set to level.
func (l Logger) Level(lvl Level) Logger {
l.level = lvl
return l
}
// GetLevel returns the current Level of l.
func (l Logger) GetLevel() Level {
return l.level
}
// Sample returns a logger with the s sampler.
func (l Logger) Sample(s Sampler) Logger {
l.sampler = s
return l
}
// Hook returns a logger with the h Hook.
func (l Logger) Hook(hooks ...Hook) Logger {
if len(hooks) == 0 {
return l
}
newHooks := make([]Hook, len(l.hooks), len(l.hooks)+len(hooks))
copy(newHooks, l.hooks)
l.hooks = append(newHooks, hooks...)
return l
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) Trace() *Event {
return l.newEvent(TraceLevel, nil)
}
// Debug starts a new message with debug level.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Debug() *Event {
return l.newEvent(DebugLevel, true, nil)
func (l *Logger) Debug() *Event {
return l.newEvent(DebugLevel, nil)
}
// Info starts a new message with info level.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Info() *Event {
return l.newEvent(InfoLevel, true, nil)
func (l *Logger) Info() *Event {
return l.newEvent(InfoLevel, nil)
}
// Warn starts a new message with warn level.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Warn() *Event {
return l.newEvent(WarnLevel, true, nil)
func (l *Logger) Warn() *Event {
return l.newEvent(WarnLevel, nil)
}
// Error starts a new message with error level.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Error() *Event {
return l.newEvent(ErrorLevel, true, nil)
func (l *Logger) Error() *Event {
return l.newEvent(ErrorLevel, nil)
}
// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Fatal() *Event {
return l.newEvent(FatalLevel, true, func(msg string) { os.Exit(1) })
func (l *Logger) Err(err error) *Event {
if err != nil {
return l.Error().Err(err)
}
return l.Info()
}
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
// Fatal starts a new message with fatal level. The FatalExitFunc interceptor function
// is called by the Msg method, which by default terminates the program immediately
// using os.Exit(1), any desired behavior can be implemented by setting FatalExitFunc.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Panic() *Event {
return l.newEvent(PanicLevel, true, func(msg string) { panic(msg) })
func (l *Logger) Fatal() *Event {
return l.newEvent(FatalLevel, func(msg string) {
if closer, ok := l.w.(io.Closer); ok {
// Close the writer to flush any buffered message. Otherwise the message
// could be lost if FatalExitFunc() terminates the program immediately or
// os.Exit(1) is called if not FatalExitFunc isn't set (default).
closer.Close()
}
if FatalExitFunc != nil {
FatalExitFunc()
} else {
os.Exit(1) // untestable: terminates the program, cannot be covered
}
})
}
// WithLevel starts a new message with level.
// Panic starts a new message with panic level. The panic() function
// is called by the Msg method, which stops the ordinary flow of a goroutine.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) WithLevel(level Level) *Event {
func (l *Logger) Panic() *Event {
return l.newEvent(PanicLevel, func(msg string) { panic(msg) })
}
// WithLevel starts a new message with level. Unlike Fatal and Panic
// methods, WithLevel does not terminate the program or stop the ordinary
// flow of a goroutine when used with their respective levels.
//
// You must call Msg on the returned event in order to send the event.
func (l *Logger) WithLevel(level Level) *Event {
switch level {
case TraceLevel:
return l.Trace()
case DebugLevel:
return l.Debug()
case InfoLevel:
@@ -272,13 +429,15 @@ func (l Logger) WithLevel(level Level) *Event {
case ErrorLevel:
return l.Error()
case FatalLevel:
return l.Fatal()
return l.newEvent(FatalLevel, nil)
case PanicLevel:
return l.Panic()
return l.newEvent(PanicLevel, nil)
case NoLevel:
return l.Log()
case Disabled:
return disabledEvent
return nil
default:
panic("zerolog: WithLevel(): invalid level: " + strconv.Itoa(int(level)))
return l.newEvent(level, nil)
}
}
@@ -286,10 +445,32 @@ func (l Logger) WithLevel(level Level) *Event {
// will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func (l Logger) Log() *Event {
// We use panic level with addLevelField=false to make Log passthrough all
// levels except Disabled.
return l.newEvent(PanicLevel, false, nil)
func (l *Logger) Log() *Event {
return l.newEvent(NoLevel, nil)
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func (l *Logger) Print(v ...interface{}) {
if e := l.Debug(); e.Enabled() {
e.CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func (l *Logger) Printf(format string, v ...interface{}) {
if e := l.Debug(); e.Enabled() {
e.CallerSkipFrame(1).Msg(fmt.Sprintf(format, v...))
}
}
// Println sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Println.
func (l *Logger) Println(v ...interface{}) {
if e := l.Debug(); e.Enabled() {
e.CallerSkipFrame(1).Msg(fmt.Sprintln(v...))
}
}
// Write implements the io.Writer interface. This is useful to set as a writer
@@ -300,48 +481,48 @@ func (l Logger) Write(p []byte) (n int, err error) {
// Trim CR added by stdlog.
p = p[0 : n-1]
}
l.Log().Msg(string(p))
l.Log().CallerSkipFrame(1).Msg(string(p))
return
}
func (l Logger) newEvent(level Level, addLevelField bool, done func(string)) *Event {
func (l *Logger) newEvent(level Level, done func(string)) *Event {
enabled := l.should(level)
if !enabled {
return disabledEvent
}
lvl := InfoLevel
if addLevelField {
lvl = level
}
e := newEvent(l.w, lvl, enabled)
e.done = done
if l.context != nil && len(l.context) > 0 && l.context[0] > 0 {
// first byte of context is ts flag
e.buf = json.AppendTime(json.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
}
if addLevelField {
e.Str(LevelFieldName, level.String())
}
if l.sample > 0 && SampleFieldName != "" {
e.Uint32(SampleFieldName, l.sample)
}
if l.context != nil && len(l.context) > 1 {
if len(e.buf) > 1 {
e.buf = append(e.buf, ',')
if done != nil {
done("")
}
e.buf = append(e.buf, l.context[1:]...)
return nil
}
e := newEvent(l.w, level, l.stack, l.ctx, l.hooks)
e.done = done
if level != NoLevel && LevelFieldName != "" {
e.Str(LevelFieldName, LevelFieldMarshalFunc(level))
}
if len(l.context) > 1 {
e.buf = enc.AppendObjectData(e.buf, l.context)
}
return e
}
func (l *Logger) scratchEvent() *Event {
return newEvent(LevelWriterAdapter{io.Discard}, DebugLevel, l.stack, l.ctx, l.hooks)
}
// disabled returns true if the logger is a disabled or nop logger.
func (l *Logger) disabled() bool {
return l.w == nil || l.level == Disabled
}
// should returns true if the log event should be logged.
func (l Logger) should(lvl Level) bool {
if lvl < l.level || lvl < globalLevel() {
func (l *Logger) should(lvl Level) bool {
if l.disabled() {
return false
}
if l.sample > 0 && l.counter != nil && !samplingDisabled() {
c := atomic.AddUint32(l.counter, 1)
return c%l.sample == 0
if lvl < l.level || lvl < GlobalLevel() {
return false
}
if l.sampler != nil && !samplingDisabled() {
return l.sampler.Sample(lvl)
}
return true
}
+46 -6
View File
@@ -3,6 +3,7 @@ package log
import (
"context"
"fmt"
"io"
"os"
@@ -22,14 +23,34 @@ func With() zerolog.Context {
return Logger.With()
}
// Level crestes a child logger with the minium accepted level set to level.
// Level creates a child logger with the minimum accepted level set to level.
func Level(level zerolog.Level) zerolog.Logger {
return Logger.Level(level)
}
// Sample returns a logger that only let one message out of every to pass thru.
func Sample(every int) zerolog.Logger {
return Logger.Sample(every)
// Sample returns a logger with the s sampler.
func Sample(s zerolog.Sampler) zerolog.Logger {
return Logger.Sample(s)
}
// Hook returns a logger with the h Hook.
func Hook(h zerolog.Hook) zerolog.Logger {
return Logger.Hook(h)
}
// Err starts a new message with error level with err as a field if not nil or
// with info level if err is nil.
//
// You must call Msg on the returned event in order to send the event.
func Err(err error) *zerolog.Event {
return Logger.Err(err)
}
// Trace starts a new message with trace level.
//
// You must call Msg on the returned event in order to send the event.
func Trace() *zerolog.Event {
return Logger.Trace()
}
// Debug starts a new message with debug level.
@@ -76,16 +97,35 @@ func Panic() *zerolog.Event {
return Logger.Panic()
}
// WithLevel starts a new message with level.
//
// You must call Msg on the returned event in order to send the event.
func WithLevel(level zerolog.Level) *zerolog.Event {
return Logger.WithLevel(level)
}
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
// zerlog.Disabled will still disable events produced by this method.
// zerolog.Disabled will still disable events produced by this method.
//
// You must call Msg on the returned event in order to send the event.
func Log() *zerolog.Event {
return Logger.Log()
}
// Print sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Print.
func Print(v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
}
// Printf sends a log event using debug level and no extra field.
// Arguments are handled in the manner of fmt.Printf.
func Printf(format string, v ...interface{}) {
Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
}
// Ctx returns the Logger associated with the ctx. If no logger
// is associated, a disabled logger is returned.
func Ctx(ctx context.Context) zerolog.Logger {
func Ctx(ctx context.Context) *zerolog.Logger {
return zerolog.Ctx(ctx)
}
+246
View File
@@ -0,0 +1,246 @@
//go:build !binary_log
// +build !binary_log
package log_test
import (
"bytes"
"context"
"errors"
"flag"
"os"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {
// UNIX Time is faster and smaller than most timestamps
// If you set zerolog.TimeFieldFormat to an empty string,
// logs will write with UNIX time
zerolog.TimeFieldFormat = ""
// In order to always output a static time to stdout for these
// examples to pass, we need to override zerolog.TimestampFunc
// and log.Logger globals -- you would not normally need to do this
zerolog.TimestampFunc = func() time.Time {
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
}
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
}
// Simple logging example using the Print function in the log package
// Note that both Print and Printf are at the debug log level by default
func ExamplePrint() {
setup()
log.Print("hello world")
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
}
// Simple logging example using the Printf function in the log package
func ExamplePrintf() {
setup()
log.Printf("hello %s", "world")
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
}
// Example of a log with no particular "level"
func ExampleLog() {
setup()
log.Log().Msg("hello world")
// Output: {"time":1199811905,"message":"hello world"}
}
// Example of a conditional level based on the presence of an error.
func ExampleErr() {
setup()
err := errors.New("some error")
log.Err(err).Msg("hello world")
log.Err(nil).Msg("hello world")
// Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"}
// {"level":"info","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "trace")
func ExampleTrace() {
setup()
log.Trace().Msg("hello world")
// Output: {"level":"trace","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "debug")
func ExampleDebug() {
setup()
log.Debug().Msg("hello world")
// Output: {"level":"debug","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "info")
func ExampleInfo() {
setup()
log.Info().Msg("hello world")
// Output: {"level":"info","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "warn")
func ExampleWarn() {
setup()
log.Warn().Msg("hello world")
// Output: {"level":"warn","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "error")
func ExampleError() {
setup()
log.Error().Msg("hello world")
// Output: {"level":"error","time":1199811905,"message":"hello world"}
}
// Example of a log at a particular "level" (in this case, "fatal")
func ExampleFatal() {
setup()
err := errors.New("A repo man spends his life getting into tense situations")
service := "myservice"
log.Fatal().
Err(err).
Str("service", service).
Msgf("Cannot start %s", service)
// Outputs: {"level":"fatal","time":1199811905,"error":"A repo man spends his life getting into tense situations","service":"myservice","message":"Cannot start myservice"}
}
// Example of a log at a particular "level" (in this case, "panic")
func ExamplePanic() {
setup()
log.Panic().Msg("Cannot start")
// Outputs: {"level":"panic","time":1199811905,"message":"Cannot start"} then panics
}
// This example uses command-line flags to demonstrate various outputs
// depending on the chosen log level.
func Example() {
setup()
debug := flag.Bool("debug", false, "sets log level to debug")
flag.Parse()
// Default level for this example is info, unless debug flag is present
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if *debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Debug().Msg("This message appears only when log level set to Debug")
log.Info().Msg("This message appears when log level set to Debug or Info")
if e := log.Debug(); e.Enabled() {
// Compute log output only if enabled.
value := "bar"
e.Str("foo", value).Msg("some debug message")
}
// Output: {"level":"info","time":1199811905,"message":"This message appears when log level set to Debug or Info"}
}
// Example of using the Output function in the log package to change the output destination
func ExampleOutput() {
setup()
out := &bytes.Buffer{}
tee := log.Output(out)
tee.Info().Msg("hello world")
written := out.Len()
log.Info().Int("bytes", written).Msg("wrote")
// Output: {"level":"info","bytes":59,"time":1199811905,"message":"wrote"}
}
// Example of using the With function to add context fields
func ExampleWith() {
setup()
// you have to assign the result of With() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
augmented := log.With().Str("service", "myservice").Logger()
augmented.Info().Msg("hello world")
// Output: {"level":"info","service":"myservice","time":1199811905,"message":"hello world"}
}
// Example of using the Level function to set the log level
func ExampleLevel() {
setup()
// you have to assign the result of Level() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
leveled := log.Level(zerolog.ErrorLevel)
leveled.Info().Msg("hello world")
leveled.Error().Msg("I said HELLO")
// Output: {"level":"error","time":1199811905,"message":"I said HELLO"}
}
type valueKeyType int
var valueKey valueKeyType = 42
var captainHook = zerolog.HookFunc(func(e *zerolog.Event, l zerolog.Level, msg string) {
e.Interface("key", e.GetCtx().Value(valueKey))
e.Bool("is_error", l > zerolog.ErrorLevel)
e.Int("msg_len", len(msg))
})
// Example of using the Logger Hook function to add hooks
func ExampleLogger_Hook() {
setup()
hooked := log.Hook(captainHook)
hooked.Info().Msg("watch out!")
// Output: {"level":"info","time":1199811905,"key":null,"is_error":false,"msg_len":10,"message":"watch out!"}
}
// Example of using the WithLevel function to set the log level
func ExampleWithLevel() {
setup()
// you have to assign the result of Level() to a new Logger and can't inline the level calls
// because they need a *Logger receiver
event := log.WithLevel(zerolog.ErrorLevel)
event.Msg("taxes are due")
// Output: {"level":"error","time":1199811905,"message":"taxes are due"}
}
// Example of using the Ctx function in the log package to log with context
func ExampleCtx() {
setup()
hooked := log.Hook(captainHook)
ctx := context.WithValue(context.Background(), valueKey, "12345")
logger := hooked.With().Ctx(ctx).Logger()
log.Ctx(logger.WithContext(ctx)).Info().Msg("hello world")
// Output: {"level":"info","time":1199811905,"key":"12345","is_error":false,"msg_len":11,"message":"hello world"}
}
// Example of using the Sample function in the log package to set a sampler
func ExampleSample() {
setup()
sampled := log.Sample(&zerolog.BasicSampler{N: 2})
sampled.Info().Msg("hello world")
sampled.Info().Msg("I said, hello world")
sampled.Info().Msg("Can you here me now world")
// Output: {"level":"info","time":1199811905,"message":"hello world"}
// {"level":"info","time":1199811905,"message":"Can you here me now world"}
}
+371 -38
View File
@@ -1,8 +1,12 @@
// +build !binary_log
package zerolog_test
import (
"errors"
"fmt"
stdlog "log"
"net"
"os"
"time"
@@ -13,7 +17,6 @@ func ExampleNew() {
log := zerolog.New(os.Stdout)
log.Info().Msg("hello world")
// Output: {"level":"info","message":"hello world"}
}
@@ -38,15 +41,77 @@ func ExampleLogger_Level() {
}
func ExampleLogger_Sample() {
log := zerolog.New(os.Stdout).Sample(2)
log := zerolog.New(os.Stdout).Sample(&zerolog.BasicSampler{N: 2})
log.Info().Msg("message 1")
log.Info().Msg("message 2")
log.Info().Msg("message 3")
log.Info().Msg("message 4")
// Output: {"level":"info","sample":2,"message":"message 2"}
// {"level":"info","sample":2,"message":"message 4"}
// Output: {"level":"info","message":"message 1"}
// {"level":"info","message":"message 3"}
}
type LevelNameHook struct{}
func (h LevelNameHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
if l != zerolog.NoLevel {
e.Str("level_name", l.String())
} else {
e.Str("level_name", "NoLevel")
}
}
type MessageHook string
func (h MessageHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
e.Str("the_message", msg)
}
func ExampleLogger_Hook() {
var levelNameHook LevelNameHook
var messageHook MessageHook = "The message"
log := zerolog.New(os.Stdout).Hook(levelNameHook, messageHook)
log.Info().Msg("hello world")
// Output: {"level":"info","level_name":"info","the_message":"hello world","message":"hello world"}
}
func ExampleLogger_Print() {
log := zerolog.New(os.Stdout)
log.Print("hello world")
// Output: {"level":"debug","message":"hello world"}
}
func ExampleLogger_Printf() {
log := zerolog.New(os.Stdout)
log.Printf("hello %s", "world")
// Output: {"level":"debug","message":"hello world"}
}
func ExampleLogger_Println() {
log := zerolog.New(os.Stdout)
log.Println("hello world")
// Output: {"level":"debug","message":"hello world\n"}
}
func ExampleLogger_Trace() {
log := zerolog.New(os.Stdout)
log.Trace().
Str("foo", "bar").
Int("n", 123).
Msg("hello world")
// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
}
func ExampleLogger_Debug() {
@@ -127,12 +192,13 @@ func ExampleLogger_Log() {
func ExampleEvent_Dict() {
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
).
e := log.Log().
Str("foo", "bar")
e.Dict("dict", e.CreateDict().
Str("bar", "baz").
Int("n", 1),
).
Msg("hello world")
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
@@ -150,6 +216,22 @@ func (u User) MarshalZerologObject(e *zerolog.Event) {
Time("created", u.Created)
}
type Price struct {
val uint64
prec int
unit string
}
func (p Price) MarshalZerologObject(e *zerolog.Event) {
denom := uint64(1)
for i := 0; i < p.prec; i++ {
denom *= 10
}
result := []byte(p.unit)
result = append(result, fmt.Sprintf("%d.%d", p.val/denom, p.val%denom)...)
e.Str("price", string(result))
}
type Users []User
func (uu Users) MarshalZerologArray(a *zerolog.Array) {
@@ -161,15 +243,20 @@ func (uu Users) MarshalZerologArray(a *zerolog.Array) {
func ExampleEvent_Array() {
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Array("array", zerolog.Arr().
Str("baz").
Int(1),
).
e := log.Log().
Str("foo", "bar")
e.Array("array", e.CreateArray().
Str("baz").
Int(1).
Dict(e.CreateDict().
Str("bar", "baz").
Int("n", 1),
),
).
Msg("hello world")
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
// Output: {"foo":"bar","array":["baz",1,{"bar":"baz","n":1}],"message":"hello world"}
}
func ExampleEvent_Array_object() {
@@ -203,6 +290,47 @@ func ExampleEvent_Object() {
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleEvent_Objects() {
log := zerolog.New(os.Stdout)
// User implements zerolog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
u2 := User{"Bono", 54, time.Time{}}
users := []User{u, u2}
log.Log().
Objects("users", zerolog.AsLogObjectMarshalers(users)).
Msg("hello world")
// Output: {"users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bono","age":54,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleEvent_ObjectsV() {
log := zerolog.New(os.Stdout)
// User implements zerolog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
u2 := User{"Bono", 54, time.Time{}}
log.Log().
ObjectsV("users", u, u2).
Msg("hello world")
// Output: {"users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bono","age":54,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleEvent_EmbedObject() {
log := zerolog.New(os.Stdout)
price := Price{val: 6449, prec: 2, unit: "$"}
log.Log().
Str("foo", "bar").
EmbedObject(price).
Msg("hello world")
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
func ExampleEvent_Interface() {
log := zerolog.New(os.Stdout)
@@ -221,7 +349,7 @@ func ExampleEvent_Interface() {
}
func ExampleEvent_Dur() {
d := time.Duration(10 * time.Second)
d := 10 * time.Second
log := zerolog.New(os.Stdout)
@@ -235,8 +363,8 @@ func ExampleEvent_Dur() {
func ExampleEvent_Durs() {
d := []time.Duration{
time.Duration(10 * time.Second),
time.Duration(20 * time.Second),
10 * time.Second,
20 * time.Second,
}
log := zerolog.New(os.Stdout)
@@ -249,28 +377,62 @@ func ExampleEvent_Durs() {
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleContext_Dict() {
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Dict("dict", zerolog.Dict().
Str("bar", "baz").
Int("n", 1),
).Logger()
func ExampleEvent_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
log.Log().Msg("hello world")
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleEvent_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
log := zerolog.New(os.Stdout)
log.Log().
Str("foo", "bar").
Fields(fields).
Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Dict() {
ctx := zerolog.New(os.Stdout).With().
Str("foo", "bar")
logger := ctx.Dict("dict", ctx.CreateDict().
Str("bar", "baz").
Int("n", 1),
).Logger()
logger.Log().Msg("hello world")
// Output: {"foo":"bar","dict":{"bar":"baz","n":1},"message":"hello world"}
}
func ExampleContext_Array() {
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Array("array", zerolog.Arr().
Str("baz").
Int(1),
).Logger()
ctx := zerolog.New(os.Stdout).With().
Str("foo", "bar")
log.Log().Msg("hello world")
logger := ctx.Array("array", ctx.CreateArray().
Str("baz").
Int(1),
).Logger()
logger.Log().Msg("hello world")
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
}
@@ -305,6 +467,47 @@ func ExampleContext_Object() {
// Output: {"foo":"bar","user":{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},"message":"hello world"}
}
func ExampleContext_Objects() {
// User implements zerolog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
u2 := User{"Bono", 54, time.Time{}}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Objects("users", []zerolog.LogObjectMarshaler{u, u2}).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bono","age":54,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleContext_ObjectsV() {
// User implements zerolog.LogObjectMarshaler
u := User{"John", 35, time.Time{}}
u2 := User{"Bono", 54, time.Time{}}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
ObjectsV("users", u, u2). // shows variadic version with distinct element arguments
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","users":[{"name":"John","age":35,"created":"0001-01-01T00:00:00Z"},{"name":"Bono","age":54,"created":"0001-01-01T00:00:00Z"}],"message":"hello world"}
}
func ExampleContext_EmbedObject() {
price := Price{val: 6449, prec: 2, unit: "$"}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
EmbedObject(price).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","price":"$64.49","message":"hello world"}
}
func ExampleContext_Interface() {
obj := struct {
@@ -324,7 +527,7 @@ func ExampleContext_Interface() {
}
func ExampleContext_Dur() {
d := time.Duration(10 * time.Second)
d := 10 * time.Second
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
@@ -338,8 +541,8 @@ func ExampleContext_Dur() {
func ExampleContext_Durs() {
d := []time.Duration{
time.Duration(10 * time.Second),
time.Duration(20 * time.Second),
10 * time.Second,
20 * time.Second,
}
log := zerolog.New(os.Stdout).With().
@@ -351,3 +554,133 @@ func ExampleContext_Durs() {
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
}
func ExampleContext_IPAddr() {
hostIP := net.IP{192, 168, 0, 100}
log := zerolog.New(os.Stdout).With().
IPAddr("HostIP", hostIP).
Logger()
log.Log().Msg("hello world")
// Output: {"HostIP":"192.168.0.100","message":"hello world"}
}
func ExampleContext_IPAddrs() {
hostIP := net.IP{192, 168, 0, 100}
log := zerolog.New(os.Stdout).With().
IPAddrs("HostIP", []net.IP{hostIP}).
Logger()
log.Log().Msg("hello world")
// Output: {"HostIP":["192.168.0.100"],"message":"hello world"}
}
func ExampleContext_IPPrefix() {
route := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
log := zerolog.New(os.Stdout).With().
IPPrefix("Route", route).
Logger()
log.Log().Msg("hello world")
// Output: {"Route":"192.168.0.0/24","message":"hello world"}
}
func ExampleContext_IPPrefixes() {
route := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
log := zerolog.New(os.Stdout).With().
IPPrefixes("Route", []net.IPNet{route}).
Logger()
log.Log().Msg("hello world")
// Output: {"Route":["192.168.0.0/24"],"message":"hello world"}
}
func ExampleContext_MACAddr() {
mac := net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}
log := zerolog.New(os.Stdout).With().
MACAddr("hostMAC", mac).
Logger()
log.Log().Msg("hello world")
// Output: {"hostMAC":"00:14:22:01:23:45","message":"hello world"}
}
func ExampleContext_Fields_map() {
fields := map[string]interface{}{
"bar": "baz",
"n": 1,
}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Fields_slice() {
fields := []interface{}{
"bar", "baz",
"n", 1,
}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Fields(fields).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
}
func ExampleContext_Times() {
t1 := time.Time{}
t2 := t1.Add(time.Second * 10)
t := []time.Time{t1, t2}
log := zerolog.New(os.Stdout).With().
Str("foo", "bar").
Times("times", t).
Logger()
log.Log().Msg("hello world")
// Output: {"foo":"bar","times":["0001-01-01T00:00:00Z","0001-01-01T00:00:10Z"],"message":"hello world"}
}
func ExampleEvent_Stringers() {
log := zerolog.New(os.Stdout)
// net.IP values implement fmt.Stringer and can be used with StringersV
a := net.IP{127, 0, 0, 1}
b := net.IP{127, 0, 0, 2}
ips := []net.IP{a, b}
log.Log().
Stringers("ips", zerolog.AsStringers(ips)).
Msg("hello world")
// Output: {"ips":["127.0.0.1","127.0.0.2"],"message":"hello world"}
}
func ExampleContext_StringersV() {
// net.IP values implement fmt.Stringer and can be used with StringersV
a := net.IPv4bcast
b := net.IPv4allrouter
log := zerolog.New(os.Stdout).With().
StringersV("ips", a, b).
Logger()
log.Log().Msg("hello world")
// Output: {"ips":["255.255.255.255","224.0.0.2"],"message":"hello world"}
}
+1093 -43
View File
File diff suppressed because it is too large Load Diff
+5
View File
@@ -0,0 +1,5 @@
// +build !go1.12
package zerolog
const contextCallerSkipFrameCount = 3
+82
View File
@@ -0,0 +1,82 @@
package pkgerrors
import (
"github.com/pkg/errors"
)
var (
StackSourceFileName = "source"
StackSourceLineName = "line"
StackSourceFunctionName = "func"
)
type state struct {
b []byte
}
// Write implement fmt.Formatter interface.
func (s *state) Write(b []byte) (n int, err error) {
s.b = b
return len(b), nil
}
// Width implement fmt.Formatter interface.
func (s *state) Width() (wid int, ok bool) {
return 0, false
}
// Precision implement fmt.Formatter interface.
func (s *state) Precision() (prec int, ok bool) {
return 0, false
}
// Flag implement fmt.Formatter interface.
func (s *state) Flag(c int) bool {
return false
}
func frameField(f errors.Frame, s *state, c rune) string {
f.Format(s, c)
return string(s.b)
}
// MarshalStack implements pkg/errors stack trace marshaling.
//
// zerolog.ErrorStackMarshaler = MarshalStack
func MarshalStack(err error) interface{} {
type stackTracer interface {
StackTrace() errors.StackTrace
}
var sterr stackTracer
var ok bool
for err != nil {
sterr, ok = err.(stackTracer)
if ok {
break
}
u, ok := err.(interface {
Unwrap() error
})
if !ok {
return nil
}
err = u.Unwrap()
}
if sterr == nil {
return nil
}
st := sterr.StackTrace()
s := &state{}
out := make([]map[string]string, 0, len(st))
for _, frame := range st {
out = append(out, map[string]string{
StackSourceFileName: frameField(frame, s, 's'),
StackSourceLineName: frameField(frame, s, 'd'),
StackSourceFunctionName: frameField(frame, s, 'n'),
})
}
return out
}
+90
View File
@@ -0,0 +1,90 @@
// +build !binary_log
package pkgerrors
import (
"bytes"
"fmt"
"regexp"
"testing"
"github.com/pkg/errors"
"github.com/rs/zerolog"
)
func TestLogStack(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out)
err := fmt.Errorf("from error: %w", errors.New("error message"))
log.Log().Stack().Err(err).Msg("")
got := out.String()
want := `\{"stack":\[\{"func":"TestLogStack","line":"21","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
if ok, _ := regexp.MatchString(want, got); !ok {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
func TestLogStackFields(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out)
err := fmt.Errorf("from error: %w", errors.New("error message"))
log.Log().Stack().Fields([]interface{}{"error", err}).Msg("")
got := out.String()
want := `\{"error":"from error: error message","stack":\[\{"func":"TestLogStackFields","line":"37","source":"stacktrace_test.go"\},.*\]\}\n`
if ok, _ := regexp.MatchString(want, got); !ok {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
func TestLogStackFromContext(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out).With().Stack().Logger() // calling Stack() on log context instead of event
err := fmt.Errorf("from error: %w", errors.New("error message"))
log.Log().Err(err).Msg("") // not explicitly calling Stack()
got := out.String()
want := `\{"stack":\[\{"func":"TestLogStackFromContext","line":"53","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
if ok, _ := regexp.MatchString(want, got); !ok {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
func TestLogStackFromContextWith(t *testing.T) {
zerolog.ErrorStackMarshaler = MarshalStack
err := fmt.Errorf("from error: %w", errors.New("error message"))
out := &bytes.Buffer{}
log := zerolog.New(out).With().Stack().Err(err).Logger() // calling Stack() on log context instead of event
log.Error().Msg("")
got := out.String()
want := `\{"level":"error","stack":\[\{"func":"TestLogStackFromContextWith","line":"66","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
if ok, _ := regexp.MatchString(want, got); !ok {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
func BenchmarkLogStack(b *testing.B) {
zerolog.ErrorStackMarshaler = MarshalStack
out := &bytes.Buffer{}
log := zerolog.New(out)
err := errors.Wrap(errors.New("error message"), "from error")
b.ReportAllocs()
for i := 0; i < b.N; i++ {
log.Log().Stack().Err(err).Msg("")
out.Reset()
}
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

+137
View File
@@ -0,0 +1,137 @@
package zerolog
import (
"math/rand"
"sync/atomic"
"time"
)
var (
// Often samples log every ~ 10 events.
Often = RandomSampler(10)
// Sometimes samples log every ~ 100 events.
Sometimes = RandomSampler(100)
// Rarely samples log every ~ 1000 events.
Rarely = RandomSampler(1000)
)
// Sampler defines an interface to a log sampler.
type Sampler interface {
// Sample returns true if the event should be part of the sample, false if
// the event should be dropped.
Sample(lvl Level) bool
}
// RandomSampler use a PRNG to randomly sample an event out of N events,
// regardless of their level.
type RandomSampler uint32
// Sample implements the Sampler interface.
func (s RandomSampler) Sample(lvl Level) bool {
if s <= 0 {
return false
}
if rand.Intn(int(s)) != 0 {
return false
}
return true
}
// BasicSampler is a sampler that will send every Nth events, regardless of
// their level.
type BasicSampler struct {
N uint32
counter uint32
}
// Sample implements the Sampler interface.
func (s *BasicSampler) Sample(lvl Level) bool {
n := s.N
if n == 0 {
return false
}
if n == 1 {
return true
}
c := atomic.AddUint32(&s.counter, 1)
return c%n == 1
}
// BurstSampler lets Burst events pass per Period then pass the decision to
// NextSampler. If Sampler is not set, all subsequent events are rejected.
type BurstSampler struct {
// Burst is the maximum number of event per period allowed before calling
// NextSampler.
Burst uint32
// Period defines the burst period. If 0, NextSampler is always called.
Period time.Duration
// NextSampler is the sampler used after the burst is reached. If nil,
// events are always rejected after the burst.
NextSampler Sampler
counter uint32
resetAt int64
}
// Sample implements the Sampler interface.
func (s *BurstSampler) Sample(lvl Level) bool {
if s.Burst > 0 && s.Period > 0 {
if s.inc() <= s.Burst {
return true
}
}
if s.NextSampler == nil {
return false
}
return s.NextSampler.Sample(lvl)
}
func (s *BurstSampler) inc() uint32 {
now := TimestampFunc().UnixNano()
resetAt := atomic.LoadInt64(&s.resetAt)
var c uint32
if now >= resetAt {
c = 1
atomic.StoreUint32(&s.counter, c)
newResetAt := now + s.Period.Nanoseconds()
reset := atomic.CompareAndSwapInt64(&s.resetAt, resetAt, newResetAt)
if !reset {
// Lost the race with another goroutine trying to reset.
c = atomic.AddUint32(&s.counter, 1)
}
} else {
c = atomic.AddUint32(&s.counter, 1)
}
return c
}
// LevelSampler applies a different sampler for each level.
type LevelSampler struct {
TraceSampler, DebugSampler, InfoSampler, WarnSampler, ErrorSampler Sampler
}
func (s LevelSampler) Sample(lvl Level) bool {
switch lvl {
case TraceLevel:
if s.TraceSampler != nil {
return s.TraceSampler.Sample(lvl)
}
case DebugLevel:
if s.DebugSampler != nil {
return s.DebugSampler.Sample(lvl)
}
case InfoLevel:
if s.InfoSampler != nil {
return s.InfoSampler.Sample(lvl)
}
case WarnLevel:
if s.WarnSampler != nil {
return s.WarnSampler.Sample(lvl)
}
case ErrorLevel:
if s.ErrorSampler != nil {
return s.ErrorSampler.Sample(lvl)
}
}
return true
}
+182
View File
@@ -0,0 +1,182 @@
//go:build !binary_log
// +build !binary_log
package zerolog
import (
"testing"
"time"
)
var samplers = []struct {
name string
sampler func() Sampler
total int
wantMin int
wantMax int
}{
{
"BasicSampler_1",
func() Sampler {
return &BasicSampler{N: 1}
},
100, 100, 100,
},
{
"BasicSampler_5",
func() Sampler {
return &BasicSampler{N: 5}
},
100, 20, 20,
},
{
"BasicSampler_0",
func() Sampler {
return &BasicSampler{N: 0}
},
100, 0, 0,
},
{
"RandomSampler",
func() Sampler {
return RandomSampler(5)
},
100, 10, 30,
},
{
"RandomSampler_0",
func() Sampler {
return RandomSampler(0)
},
100, 0, 0,
},
{
"BurstSampler",
func() Sampler {
return &BurstSampler{Burst: 20, Period: time.Second}
},
100, 20, 20,
},
{
"BurstSampler_0",
func() Sampler {
return &BurstSampler{Burst: 0, Period: time.Second}
},
100, 0, 0,
},
{
"BurstSamplerNext",
func() Sampler {
return &BurstSampler{Burst: 20, Period: time.Second, NextSampler: &BasicSampler{N: 5}}
},
120, 40, 40,
},
}
func TestSamplers(t *testing.T) {
for i := range samplers {
s := samplers[i]
t.Run(s.name, func(t *testing.T) {
sampler := s.sampler()
got := 0
for t := s.total; t > 0; t-- {
if sampler.Sample(0) {
got++
}
}
if got < s.wantMin || got > s.wantMax {
t.Errorf("%s.Sample(0) == true %d on %d, want [%d, %d]", s.name, got, s.total, s.wantMin, s.wantMax)
}
})
}
}
func BenchmarkSamplers(b *testing.B) {
for i := range samplers {
s := samplers[i]
b.Run(s.name, func(b *testing.B) {
sampler := s.sampler()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
sampler.Sample(0)
}
})
})
}
}
func TestBurst(t *testing.T) {
sampler := &BurstSampler{Burst: 1, Period: time.Second}
t0 := time.Now()
now := t0
mockedTime := func() time.Time {
return now
}
TimestampFunc = mockedTime
defer func() { TimestampFunc = time.Now }()
scenario := []struct {
tm time.Time
want bool
}{
{t0, true},
{t0.Add(time.Second - time.Nanosecond), false},
{t0.Add(time.Second), true},
{t0.Add(time.Second + time.Nanosecond), false},
}
for i, step := range scenario {
now = step.tm
got := sampler.Sample(NoLevel)
if got != step.want {
t.Errorf("step %d (t=%s): expect %t got %t", i, step.tm, step.want, got)
}
}
}
func TestLevelSampler(t *testing.T) {
// Create mock samplers that return true for specific levels
traceSampler := &BasicSampler{N: 1} // Always sample
debugSampler := &BasicSampler{N: 0} // Never sample
infoSampler := &BasicSampler{N: 1} // Always sample
warnSampler := &BasicSampler{N: 0} // Never sample
errorSampler := &BasicSampler{N: 1} // Always sample
sampler := LevelSampler{
TraceSampler: traceSampler,
DebugSampler: debugSampler,
InfoSampler: infoSampler,
WarnSampler: warnSampler,
ErrorSampler: errorSampler,
}
// Test each level
if !sampler.Sample(TraceLevel) {
t.Error("TraceLevel should be sampled")
}
if sampler.Sample(DebugLevel) {
t.Error("DebugLevel should not be sampled")
}
if !sampler.Sample(InfoLevel) {
t.Error("InfoLevel should be sampled")
}
if sampler.Sample(WarnLevel) {
t.Error("WarnLevel should not be sampled")
}
if !sampler.Sample(ErrorLevel) {
t.Error("ErrorLevel should be sampled")
}
// Test levels not covered by the LevelSampler sampler (FatalLevel, PanicLevel, NoLevel) - should return true
if !sampler.Sample(FatalLevel) {
t.Error("FatalLevel should return true when no sampler is set")
}
if !sampler.Sample(PanicLevel) {
t.Error("PanicLevel should return true when no sampler is set")
}
if !sampler.Sample(NoLevel) {
t.Error("NoLevel should return true when no sampler is set")
}
}
+247
View File
@@ -0,0 +1,247 @@
package zerolog
import (
"context"
"log/slog"
"time"
)
// SlogHandler implements the slog.Handler interface using a zerolog.Logger
// as the underlying log backend. This allows code that uses the standard
// library's slog package to route log output through zerolog.
type SlogHandler struct {
logger Logger
prefix string // group prefix for nested groups
attrs []slog.Attr
}
// NewSlogHandler creates a new slog.Handler that writes log records to the
// given zerolog.Logger. The handler maps slog levels to zerolog levels and
// converts slog attributes to zerolog fields.
func NewSlogHandler(logger Logger) *SlogHandler {
return &SlogHandler{logger: logger}
}
// Enabled reports whether the handler handles records at the given level.
// It mirrors Logger.should's level and writer checks (without sampling).
func (h *SlogHandler) Enabled(_ context.Context, level slog.Level) bool {
if h.logger.w == nil {
return false
}
zl := slogToZerologLevel(level)
if zl < GlobalLevel() {
return false
}
return zl >= h.logger.level
}
// Handle handles the Record. It converts the slog.Record into a zerolog event
// and writes it using the underlying zerolog.Logger.
func (h *SlogHandler) Handle(ctx context.Context, record slog.Record) error {
zlevel := slogToZerologLevel(record.Level)
event := h.logger.WithLevel(zlevel)
if event == nil {
return nil
}
// Propagate slog context to the zerolog event so that hooks
// relying on Event.GetCtx() (e.g. tracing) can access it.
if ctx != nil {
event = event.Ctx(ctx)
}
// Add pre-attached attrs from WithAttrs
for _, a := range h.attrs {
event = appendSlogAttr(event, a, h.prefix)
}
// Add attrs from the record itself
record.Attrs(func(a slog.Attr) bool {
event = appendSlogAttr(event, a, h.prefix)
return true
})
// Add timestamp from the slog record, but only if the logger doesn't
// already have a timestampHook (added via .With().Timestamp()) to
// avoid duplicate timestamp keys in the output.
if !record.Time.IsZero() && !h.hasTimestampHook() {
event.Time(TimestampFieldName, record.Time)
}
event.Msg(record.Message)
return nil
}
// hasTimestampHook reports whether the logger has a timestampHook installed,
// which would cause duplicate timestamp fields if we also emit record.Time.
func (h *SlogHandler) hasTimestampHook() bool {
for _, hook := range h.logger.hooks {
if _, ok := hook.(timestampHook); ok {
return true
}
}
return false
}
// WithAttrs returns a new Handler with the given attributes pre-attached.
// These attributes will be included in every subsequent log record.
func (h *SlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
if len(attrs) == 0 {
return h
}
h2 := h.clone()
h2.attrs = append(h2.attrs, attrs...)
return h2
}
// WithGroup returns a new Handler with the given group name. All subsequent
// attributes will be nested under this group name in the output.
func (h *SlogHandler) WithGroup(name string) slog.Handler {
if name == "" {
return h
}
h2 := h.clone()
if h2.prefix != "" {
h2.prefix = h2.prefix + "." + name
} else {
h2.prefix = name
}
return h2
}
func (h *SlogHandler) clone() *SlogHandler {
h2 := &SlogHandler{
logger: h.logger,
prefix: h.prefix,
}
if len(h.attrs) > 0 {
h2.attrs = make([]slog.Attr, len(h.attrs))
copy(h2.attrs, h.attrs)
}
return h2
}
// slogToZerologLevel maps slog levels to zerolog levels.
//
// slog levels: Debug=-4, Info=0, Warn=4, Error=8
// zerolog levels: Trace=-1, Debug=0, Info=1, Warn=2, Error=3, Fatal=4, Panic=5
func slogToZerologLevel(level slog.Level) Level {
switch {
case level < slog.LevelDebug:
return TraceLevel
case level < slog.LevelInfo:
return DebugLevel
case level < slog.LevelWarn:
return InfoLevel
case level < slog.LevelError:
return WarnLevel
default:
return ErrorLevel
}
}
// zerologToSlogLevel maps zerolog levels to slog levels.
func zerologToSlogLevel(level Level) slog.Level {
switch level {
case TraceLevel:
return slog.LevelDebug - 4
case DebugLevel:
return slog.LevelDebug
case InfoLevel:
return slog.LevelInfo
case WarnLevel:
return slog.LevelWarn
case ErrorLevel:
return slog.LevelError
case FatalLevel:
return slog.LevelError + 4
case PanicLevel:
return slog.LevelError + 8
default:
return slog.LevelInfo
}
}
// joinPrefix concatenates a prefix and key with a dot separator.
// It avoids allocations when either prefix or key is empty.
func joinPrefix(prefix, key string) string {
if prefix == "" {
return key
}
if key == "" {
return prefix
}
return prefix + "." + key
}
// appendSlogAttr appends a single slog.Attr to the zerolog event, handling
// type-specific encoding to avoid reflection where possible.
func appendSlogAttr(event *Event, attr slog.Attr, prefix string) *Event {
if event == nil {
return event
}
// Resolve the attribute to handle LogValuer types.
// This handles slog.KindLogValuer implicitly by unwrapping
// any values that implement slog.LogValuer to their resolved form.
attr.Value = attr.Value.Resolve()
// For group kinds, handle grouping before key concatenation
if attr.Value.Kind() == slog.KindGroup {
attrs := attr.Value.Group()
if len(attrs) == 0 {
return event
}
groupPrefix := joinPrefix(prefix, attr.Key)
for _, ga := range attrs {
event = appendSlogAttr(event, ga, groupPrefix)
}
return event
}
// Skip empty keys for non-group attributes
if attr.Key == "" {
return event
}
key := joinPrefix(prefix, attr.Key)
val := attr.Value
switch val.Kind() {
case slog.KindString:
event = event.Str(key, val.String())
case slog.KindInt64:
event = event.Int64(key, val.Int64())
case slog.KindUint64:
event = event.Uint64(key, val.Uint64())
case slog.KindFloat64:
event = event.Float64(key, val.Float64())
case slog.KindBool:
event = event.Bool(key, val.Bool())
case slog.KindDuration:
event = event.Dur(key, val.Duration())
case slog.KindTime:
event = event.Time(key, val.Time())
case slog.KindAny:
v := val.Any()
switch cv := v.(type) {
case error:
event = event.AnErr(key, cv)
case time.Duration:
event = event.Dur(key, cv)
case time.Time:
event = event.Time(key, cv)
case []byte:
event = event.Bytes(key, cv)
default:
event = event.Interface(key, v)
}
default:
event = event.Interface(key, val.Any())
}
return event
}
// Verify at compile time that SlogHandler satisfies the slog.Handler interface.
var _ slog.Handler = (*SlogHandler)(nil)
+559
View File
@@ -0,0 +1,559 @@
package zerolog_test
import (
"context"
"bytes"
"encoding/json"
"errors"
"log/slog"
"testing"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/internal/cbor"
)
func newSlogLogger(buf *bytes.Buffer) *slog.Logger {
zl := zerolog.New(buf)
return slog.New(zerolog.NewSlogHandler(zl))
}
// decodeOutput converts the buffer contents to a JSON string,
// handling CBOR-encoded output when built with the binary_log tag.
func decodeOutput(buf *bytes.Buffer) string {
p := buf.Bytes()
if len(p) == 0 || p[0] < 0x7F {
return buf.String()
}
return cbor.DecodeObjectToStr(p) + "\n"
}
func decodeJSON(t *testing.T, buf *bytes.Buffer) map[string]interface{} {
t.Helper()
var m map[string]interface{}
s := decodeOutput(buf)
if err := json.Unmarshal([]byte(s), &m); err != nil {
t.Fatalf("failed to decode JSON %q: %v", s, err)
}
return m
}
func TestSlogHandler_BasicInfo(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("hello world")
m := decodeJSON(t, &buf)
if m["level"] != "info" {
t.Errorf("expected level info, got %v", m["level"])
}
if m["message"] != "hello world" {
t.Errorf("expected message 'hello world', got %v", m["message"])
}
}
func TestSlogHandler_Debug(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf).Level(zerolog.DebugLevel)
logger := slog.New(zerolog.NewSlogHandler(zl))
logger.Debug("debug msg")
m := decodeJSON(t, &buf)
if m["level"] != "debug" {
t.Errorf("expected level debug, got %v", m["level"])
}
if m["message"] != "debug msg" {
t.Errorf("expected message 'debug msg', got %v", m["message"])
}
}
func TestSlogHandler_Warn(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Warn("warn msg")
m := decodeJSON(t, &buf)
if m["level"] != "warn" {
t.Errorf("expected level warn, got %v", m["level"])
}
}
func TestSlogHandler_Error(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Error("error msg")
m := decodeJSON(t, &buf)
if m["level"] != "error" {
t.Errorf("expected level error, got %v", m["level"])
}
}
func TestSlogHandler_WithStringAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", "key", "value")
m := decodeJSON(t, &buf)
if m["key"] != "value" {
t.Errorf("expected key=value, got %v", m["key"])
}
}
func TestSlogHandler_WithIntAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Int("count", 42))
m := decodeJSON(t, &buf)
if m["count"] != float64(42) {
t.Errorf("expected count=42, got %v", m["count"])
}
}
func TestSlogHandler_WithBoolAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Bool("flag", true))
m := decodeJSON(t, &buf)
if m["flag"] != true {
t.Errorf("expected flag=true, got %v", m["flag"])
}
}
func TestSlogHandler_WithFloat64Attr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Float64("pi", 3.14))
m := decodeJSON(t, &buf)
if m["pi"] != 3.14 {
t.Errorf("expected pi=3.14, got %v", m["pi"])
}
}
func TestSlogHandler_WithTimeAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
ts := time.Date(2024, 1, 15, 12, 0, 0, 0, time.UTC)
logger.Info("test", slog.Time("created", ts))
m := decodeJSON(t, &buf)
if m["created"] == nil {
t.Error("expected created field to be present")
}
}
func TestSlogHandler_WithDurationAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Duration("elapsed", 5*time.Second))
m := decodeJSON(t, &buf)
if m["elapsed"] == nil {
t.Error("expected elapsed field to be present")
}
}
func TestSlogHandler_WithErrorAttr(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Any("err", errors.New("something failed")))
m := decodeJSON(t, &buf)
if m["err"] != "something failed" {
t.Errorf("expected err='something failed', got %v", m["err"])
}
}
func TestSlogHandler_WithAttrs(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
child := handler.WithAttrs([]slog.Attr{
slog.String("component", "auth"),
slog.Int("version", 2),
})
logger := slog.New(child)
logger.Info("request handled")
m := decodeJSON(t, &buf)
if m["component"] != "auth" {
t.Errorf("expected component=auth, got %v", m["component"])
}
if m["version"] != float64(2) {
t.Errorf("expected version=2, got %v", m["version"])
}
if m["message"] != "request handled" {
t.Errorf("expected message 'request handled', got %v", m["message"])
}
}
func TestSlogHandler_WithAttrsEmpty(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
// WithAttrs with empty slice should return same handler
child := handler.WithAttrs(nil)
if child != handler {
t.Error("expected WithAttrs(nil) to return same handler")
}
}
func TestSlogHandler_WithGroup(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
child := handler.WithGroup("request")
logger := slog.New(child)
logger.Info("handled", "method", "GET", "status", 200)
m := decodeJSON(t, &buf)
if m["request.method"] != "GET" {
t.Errorf("expected request.method=GET, got %v", m["request.method"])
}
if m["request.status"] != float64(200) {
t.Errorf("expected request.status=200, got %v", m["request.status"])
}
}
func TestSlogHandler_WithGroupEmpty(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
// WithGroup with empty name should return same handler
child := handler.WithGroup("")
if child != handler {
t.Error("expected WithGroup('') to return same handler")
}
}
func TestSlogHandler_WithNestedGroups(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
child := handler.WithGroup("http").WithGroup("request")
logger := slog.New(child)
logger.Info("handled", "method", "POST")
m := decodeJSON(t, &buf)
if m["http.request.method"] != "POST" {
t.Errorf("expected http.request.method=POST, got %v", m["http.request.method"])
}
}
func TestSlogHandler_WithGroupAndAttrs(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
child := handler.WithGroup("server").WithAttrs([]slog.Attr{
slog.String("host", "localhost"),
})
logger := slog.New(child)
logger.Info("started", "port", 8080)
m := decodeJSON(t, &buf)
if m["server.host"] != "localhost" {
t.Errorf("expected server.host=localhost, got %v", m["server.host"])
}
if m["server.port"] != float64(8080) {
t.Errorf("expected server.port=8080, got %v", m["server.port"])
}
}
func TestSlogHandler_GroupAttrInRecord(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", slog.Group("user",
slog.String("name", "alice"),
slog.Int("age", 30),
))
m := decodeJSON(t, &buf)
if m["user.name"] != "alice" {
t.Errorf("expected user.name=alice, got %v", m["user.name"])
}
if m["user.age"] != float64(30) {
t.Errorf("expected user.age=30, got %v", m["user.age"])
}
}
func TestSlogHandler_LevelFiltering(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf).Level(zerolog.WarnLevel)
handler := zerolog.NewSlogHandler(zl)
// Debug should be filtered
if handler.Enabled(nil, slog.LevelDebug) {
t.Error("expected debug to be filtered at warn level")
}
// Info should be filtered
if handler.Enabled(nil, slog.LevelInfo) {
t.Error("expected info to be filtered at warn level")
}
// Warn should pass
if !handler.Enabled(nil, slog.LevelWarn) {
t.Error("expected warn to be enabled at warn level")
}
// Error should pass
if !handler.Enabled(nil, slog.LevelError) {
t.Error("expected error to be enabled at warn level")
}
}
func TestSlogHandler_FilteredMessageNotWritten(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf).Level(zerolog.ErrorLevel)
logger := slog.New(zerolog.NewSlogHandler(zl))
logger.Info("should not appear")
if buf.Len() != 0 {
t.Errorf("expected no output for filtered message, got %q", buf.String())
}
}
func TestSlogHandler_MultipleAttrs(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("multi",
slog.String("a", "1"),
slog.Int("b", 2),
slog.Bool("c", true),
slog.Float64("d", 3.5),
)
m := decodeJSON(t, &buf)
if m["a"] != "1" {
t.Errorf("expected a=1, got %v", m["a"])
}
if m["b"] != float64(2) {
t.Errorf("expected b=2, got %v", m["b"])
}
if m["c"] != true {
t.Errorf("expected c=true, got %v", m["c"])
}
if m["d"] != 3.5 {
t.Errorf("expected d=3.5, got %v", m["d"])
}
}
func TestSlogHandler_LogValuer(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("test", "addr", testLogValuer{host: "example.com", port: 443})
m := decodeJSON(t, &buf)
// LogValuer resolves to a group
if m["addr.host"] != "example.com" {
t.Errorf("expected addr.host=example.com, got %v", m["addr.host"])
}
if m["addr.port"] != float64(443) {
t.Errorf("expected addr.port=443, got %v", m["addr.port"])
}
}
type testLogValuer struct {
host string
port int
}
func (v testLogValuer) LogValue() slog.Value {
return slog.GroupValue(
slog.String("host", v.host),
slog.Int("port", v.port),
)
}
func TestSlogHandler_WithAttrsImmutability(t *testing.T) {
var buf1, buf2 bytes.Buffer
zl1 := zerolog.New(&buf1)
zl2 := zerolog.New(&buf2)
handler := zerolog.NewSlogHandler(zl1)
child1 := handler.WithAttrs([]slog.Attr{slog.String("from", "child1")})
_ = zerolog.NewSlogHandler(zl2).WithAttrs([]slog.Attr{slog.String("from", "child2")})
slog.New(child1).Info("test")
m := decodeJSON(t, &buf1)
if m["from"] != "child1" {
t.Errorf("expected from=child1, got %v", m["from"])
}
}
func TestSlogHandler_LevelMapping(t *testing.T) {
tests := []struct {
slogLevel slog.Level
wantLevel string
}{
{slog.LevelDebug - 4, "trace"},
{slog.LevelDebug, "debug"},
{slog.LevelInfo, "info"},
{slog.LevelWarn, "warn"},
{slog.LevelError, "error"},
}
for _, tt := range tests {
var buf bytes.Buffer
zl := zerolog.New(&buf).Level(zerolog.TraceLevel)
logger := slog.New(zerolog.NewSlogHandler(zl))
logger.Log(nil, tt.slogLevel, "test")
m := decodeJSON(t, &buf)
if m["level"] != tt.wantLevel {
t.Errorf("slog level %d: expected zerolog level %q, got %q",
tt.slogLevel, tt.wantLevel, m["level"])
}
buf.Reset()
}
}
func TestSlogHandler_EmptyMessage(t *testing.T) {
var buf bytes.Buffer
logger := newSlogLogger(&buf)
logger.Info("", "key", "val")
m := decodeJSON(t, &buf)
if m["key"] != "val" {
t.Errorf("expected key=val, got %v", m["key"])
}
}
func TestSlogHandler_WithContext(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf).With().Str("service", "api").Logger()
logger := slog.New(zerolog.NewSlogHandler(zl))
logger.Info("request")
m := decodeJSON(t, &buf)
if m["service"] != "api" {
t.Errorf("expected service=api, got %v", m["service"])
}
if m["message"] != "request" {
t.Errorf("expected message 'request', got %v", m["message"])
}
}
func TestSlogHandler_EnabledRespectsGlobalLevel(t *testing.T) {
var buf bytes.Buffer
zl := zerolog.New(&buf).Level(zerolog.DebugLevel)
handler := zerolog.NewSlogHandler(zl)
// Logger level is debug, so info should be enabled
if !handler.Enabled(nil, slog.LevelInfo) {
t.Fatal("expected info to be enabled before setting global level")
}
// Set global level to error
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
defer zerolog.SetGlobalLevel(zerolog.TraceLevel)
// Now info should be disabled even though logger level allows it
if handler.Enabled(nil, slog.LevelInfo) {
t.Error("expected info to be disabled when GlobalLevel is error")
}
// Error should still be enabled
if !handler.Enabled(nil, slog.LevelError) {
t.Error("expected error to be enabled when GlobalLevel is error")
}
}
func TestSlogHandler_EnabledNilWriter(t *testing.T) {
zl := zerolog.Nop()
handler := zerolog.NewSlogHandler(zl)
if handler.Enabled(nil, slog.LevelError) {
t.Error("expected disabled for nop logger")
}
}
func TestSlogHandler_HandlePropagatesContext(t *testing.T) {
var buf bytes.Buffer
type ctxKey struct{}
ctx := context.WithValue(context.Background(), ctxKey{}, "test-value")
var gotCtx context.Context
hook := zerolog.HookFunc(func(e *zerolog.Event, level zerolog.Level, msg string) {
gotCtx = e.GetCtx()
})
zl := zerolog.New(&buf).Hook(hook)
handler := zerolog.NewSlogHandler(zl)
record := slog.NewRecord(time.Now(), slog.LevelInfo, "test", 0)
_ = handler.Handle(ctx, record)
if gotCtx == nil {
t.Fatal("expected context to be propagated to event")
}
if gotCtx.Value(ctxKey{}) != "test-value" {
t.Error("expected context value to be preserved")
}
}
func TestSlogHandler_NoDuplicateTimestamp(t *testing.T) {
var buf bytes.Buffer
// Create logger with Timestamp() hook - this adds "time" automatically
zl := zerolog.New(&buf).With().Timestamp().Logger()
handler := zerolog.NewSlogHandler(zl)
record := slog.NewRecord(time.Now(), slog.LevelInfo, "test", 0)
_ = handler.Handle(context.Background(), record)
output := decodeOutput(&buf)
// Count occurrences of the timestamp field name - should appear exactly once
count := 0
for i := 0; i < len(output); i++ {
if i+4 <= len(output) && output[i:i+4] == "time" {
count++
}
}
if count > 1 {
t.Errorf("expected at most 1 timestamp field, got %d in output: %s", count, output)
}
}
func TestSlogHandler_TimestampWithoutHook(t *testing.T) {
var buf bytes.Buffer
// Logger without Timestamp() hook - Handle should add the timestamp
zl := zerolog.New(&buf)
handler := zerolog.NewSlogHandler(zl)
ts := time.Date(2024, 6, 15, 12, 0, 0, 0, time.UTC)
record := slog.NewRecord(ts, slog.LevelInfo, "test", 0)
_ = handler.Handle(context.Background(), record)
m := decodeJSON(t, &buf)
if m[zerolog.TimestampFieldName] == nil {
t.Error("expected timestamp field when logger has no timestamp hook")
}
}
+47 -10
View File
@@ -1,8 +1,15 @@
// +build !windows
// +build !binary_log
package zerolog
import "io"
import (
"io"
)
// See http://cee.mitre.org/language/1.0-beta1/clt.html#syslog
// or https://www.rsyslog.com/json-elasticsearch/
const ceePrefix = "@cee:"
// SyslogWriter is an interface matching a syslog.Writer struct.
type SyslogWriter interface {
@@ -16,37 +23,67 @@ type SyslogWriter interface {
}
type syslogWriter struct {
w SyslogWriter
w SyslogWriter
prefix string
}
// SyslogLevelWriter wraps a SyslogWriter and call the right syslog level
// method matching the zerolog level.
func SyslogLevelWriter(w SyslogWriter) LevelWriter {
return syslogWriter{w}
return syslogWriter{w, ""}
}
// SyslogCEEWriter wraps a SyslogWriter with a SyslogLevelWriter that adds a
// MITRE CEE prefix for JSON syslog entries, compatible with rsyslog
// and syslog-ng JSON logging support.
// See https://www.rsyslog.com/json-elasticsearch/
func SyslogCEEWriter(w SyslogWriter) LevelWriter {
return syslogWriter{w, ceePrefix}
}
func (sw syslogWriter) Write(p []byte) (n int, err error) {
return sw.w.Write(p)
var pn int
if sw.prefix != "" {
pn, err = sw.w.Write([]byte(sw.prefix))
if err != nil {
return pn, err
}
}
n, err = sw.w.Write(p)
return pn + n, err
}
// WriteLevel implements LevelWriter interface.
func (sw syslogWriter) WriteLevel(level Level, p []byte) (n int, err error) {
switch level {
case TraceLevel:
case DebugLevel:
err = sw.w.Debug(string(p))
err = sw.w.Debug(sw.prefix + string(p))
case InfoLevel:
err = sw.w.Info(string(p))
err = sw.w.Info(sw.prefix + string(p))
case WarnLevel:
err = sw.w.Warning(string(p))
err = sw.w.Warning(sw.prefix + string(p))
case ErrorLevel:
err = sw.w.Err(string(p))
err = sw.w.Err(sw.prefix + string(p))
case FatalLevel:
err = sw.w.Emerg(string(p))
err = sw.w.Emerg(sw.prefix + string(p))
case PanicLevel:
err = sw.w.Crit(string(p))
err = sw.w.Crit(sw.prefix + string(p))
case NoLevel:
err = sw.w.Info(sw.prefix + string(p))
default:
panic("invalid level")
}
// Any CEE prefix is not part of the message, so we don't include its length
n = len(p)
return
}
// Call the underlying writer's Close method if it is an io.Closer. Otherwise
// does nothing.
func (sw syslogWriter) Close() error {
if c, ok := sw.w.(io.Closer); ok {
return c.Close()
}
return nil
}
+181 -3
View File
@@ -1,7 +1,15 @@
// +build !binary_log
// +build !windows
package zerolog
import "testing"
import "reflect"
import (
"bytes"
"io"
"reflect"
"strings"
"testing"
)
type syslogEvent struct {
level string
@@ -12,7 +20,11 @@ type syslogTestWriter struct {
}
func (w *syslogTestWriter) Write(p []byte) (int, error) {
return 0, nil
return len(p), nil
}
func (w *syslogTestWriter) Trace(m string) error {
w.events = append(w.events, syslogEvent{"Trace", m})
return nil
}
func (w *syslogTestWriter) Debug(m string) error {
w.events = append(w.events, syslogEvent{"Debug", m})
@@ -42,17 +54,183 @@ func (w *syslogTestWriter) Crit(m string) error {
func TestSyslogWriter(t *testing.T) {
sw := &syslogTestWriter{}
log := New(SyslogLevelWriter(sw))
log.Trace().Msg("trace")
log.Debug().Msg("debug")
log.Info().Msg("info")
log.Warn().Msg("warn")
log.Error().Msg("error")
log.Log().Msg("nolevel")
want := []syslogEvent{
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
{"Info", `{"level":"info","message":"info"}` + "\n"},
{"Warning", `{"level":"warn","message":"warn"}` + "\n"},
{"Err", `{"level":"error","message":"error"}` + "\n"},
{"Info", `{"message":"nolevel"}` + "\n"},
}
if got := sw.events; !reflect.DeepEqual(got, want) {
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
}
}
type testCEEwriter struct {
buf *bytes.Buffer
}
// Only implement one method as we're just testing the prefixing
func (c testCEEwriter) Debug(m string) error { return nil }
func (c testCEEwriter) Info(m string) error {
_, err := c.buf.Write([]byte(m))
return err
}
func (c testCEEwriter) Warning(m string) error { return nil }
func (c testCEEwriter) Err(m string) error { return nil }
func (c testCEEwriter) Emerg(m string) error { return nil }
func (c testCEEwriter) Crit(m string) error { return nil }
func (c testCEEwriter) Write(b []byte) (int, error) {
return c.buf.Write(b)
}
func TestSyslogWriter_WithCEE(t *testing.T) {
var buf bytes.Buffer
sw := testCEEwriter{&buf}
log := New(SyslogCEEWriter(sw))
log.Info().Str("key", "value").Msg("message string")
got := buf.String()
want := "@cee:{"
if !strings.HasPrefix(got, want) {
t.Errorf("Bad CEE message start: want %v, got %v", want, got)
}
}
type errorSyslogWriter struct {
*syslogTestWriter
writeError error
}
func (w *errorSyslogWriter) Write(p []byte) (int, error) {
if w.writeError != nil {
return 0, w.writeError
}
return len(p), nil
}
func TestSyslogWriter_Write(t *testing.T) {
// Test Write method without prefix
sw := &syslogTestWriter{}
writer := SyslogLevelWriter(sw)
data := []byte("test message")
n, err := writer.Write(data)
if err != nil {
t.Errorf("Write failed: %v", err)
}
if n != len(data) {
t.Errorf("Write returned wrong length: got %d, want %d", n, len(data))
}
// Test Write method with CEE prefix
sw2 := &syslogTestWriter{}
writer2 := SyslogCEEWriter(sw2)
data2 := []byte("test message")
n2, err2 := writer2.Write(data2)
if err2 != nil {
t.Errorf("Write with CEE failed: %v", err2)
}
expectedLen := len(ceePrefix) + len(data2)
if n2 != expectedLen {
t.Errorf("Write with CEE returned wrong length: got %d, want %d", n2, expectedLen)
}
// Test Write method with CEE prefix and error on prefix write
sw3 := &errorSyslogWriter{syslogTestWriter: &syslogTestWriter{}, writeError: io.EOF}
writer3 := SyslogCEEWriter(sw3)
_, err3 := writer3.Write(data2)
if err3 != io.EOF {
t.Errorf("Write with CEE error failed: got %v, want %v", err3, io.EOF)
}
}
func TestSyslogWriter_WriteLevel_AllLevels(t *testing.T) {
sw := &syslogTestWriter{}
writer := SyslogLevelWriter(sw)
// Test all levels to ensure full coverage
writer.WriteLevel(TraceLevel, []byte(`{"level":"trace","message":"trace"}`+"\n"))
writer.WriteLevel(DebugLevel, []byte(`{"level":"debug","message":"debug"}`+"\n"))
writer.WriteLevel(InfoLevel, []byte(`{"level":"info","message":"info"}`+"\n"))
writer.WriteLevel(WarnLevel, []byte(`{"level":"warn","message":"warn"}`+"\n"))
writer.WriteLevel(ErrorLevel, []byte(`{"level":"error","message":"error"}`+"\n"))
writer.WriteLevel(FatalLevel, []byte(`{"level":"fatal","message":"fatal"}`+"\n"))
writer.WriteLevel(PanicLevel, []byte(`{"level":"panic","message":"panic"}`+"\n"))
writer.WriteLevel(NoLevel, []byte(`{"message":"nolevel"}`+"\n"))
want := []syslogEvent{
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
{"Info", `{"level":"info","message":"info"}` + "\n"},
{"Warning", `{"level":"warn","message":"warn"}` + "\n"},
{"Err", `{"level":"error","message":"error"}` + "\n"},
{"Emerg", `{"level":"fatal","message":"fatal"}` + "\n"},
{"Crit", `{"level":"panic","message":"panic"}` + "\n"},
{"Info", `{"message":"nolevel"}` + "\n"},
}
if got := sw.events; !reflect.DeepEqual(got, want) {
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
}
}
type closableSyslogWriter struct {
*syslogTestWriter
closed bool
}
func (w *closableSyslogWriter) Close() error {
w.closed = true
return nil
}
func TestSyslogWriter_Close(t *testing.T) {
// Test with closable writer
sw := &closableSyslogWriter{syslogTestWriter: &syslogTestWriter{}}
writer := SyslogLevelWriter(sw).(syslogWriter) // Cast to concrete type to access Close
err := writer.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
if !sw.closed {
t.Error("Close was not called on underlying writer")
}
// Test with non-closable writer
sw2 := &syslogTestWriter{}
writer2 := SyslogLevelWriter(sw2).(syslogWriter) // Cast to concrete type to access Close
err = writer2.Close()
if err != nil {
t.Errorf("Close failed for non-closable writer: %v", err)
}
}
func TestSyslogWriter_WriteLevel_InvalidLevel(t *testing.T) {
sw := &syslogTestWriter{}
writer := SyslogLevelWriter(sw)
// Test invalid level - should panic
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic for invalid level")
} else if r != "invalid level" {
t.Errorf("Expected panic 'invalid level', got %v", r)
}
}()
writer.WriteLevel(Level(100), []byte("test"))
}
+280 -25
View File
@@ -1,7 +1,12 @@
package zerolog
import (
"bytes"
"io"
"path"
"runtime"
"strconv"
"strings"
"sync"
)
@@ -12,30 +17,39 @@ type LevelWriter interface {
WriteLevel(level Level, p []byte) (n int, err error)
}
type levelWriterAdapter struct {
// LevelWriterAdapter adapts an io.Writer to support the LevelWriter interface.
type LevelWriterAdapter struct {
io.Writer
}
func (lw levelWriterAdapter) WriteLevel(l Level, p []byte) (n int, err error) {
// WriteLevel simply writes everything to the adapted writer, ignoring the level.
func (lw LevelWriterAdapter) WriteLevel(l Level, p []byte) (n int, err error) {
return lw.Write(p)
}
// Call the underlying writer's Close method if it is an io.Closer. Otherwise
// does nothing.
func (lw LevelWriterAdapter) Close() error {
if closer, ok := lw.Writer.(io.Closer); ok {
return closer.Close()
}
return nil
}
type syncWriter struct {
mu sync.Mutex
lw LevelWriter
}
// SyncWriter wraps w so that each call to Write is synchronized with a mutex.
// This syncer can be the call to writer's Write method is not thread safe.
// Note that os.File Write operation is using write() syscall which is supposed
// to be thread-safe on POSIX systems. So there is no need to use this with
// os.File on such systems as zerolog guaranties to issue a single Write call
// per log event.
// This syncer can be used to wrap the call to writer's Write method if it is
// not thread safe. Note that you do not need this wrapper for os.File Write
// operations on POSIX and Windows systems as they are already thread-safe.
func SyncWriter(w io.Writer) io.Writer {
if lw, ok := w.(LevelWriter); ok {
return &syncWriter{lw: lw}
}
return &syncWriter{lw: levelWriterAdapter{w}}
return &syncWriter{lw: LevelWriterAdapter{w}}
}
// Write implements the io.Writer interface.
@@ -52,36 +66,59 @@ func (s *syncWriter) WriteLevel(l Level, p []byte) (n int, err error) {
return s.lw.WriteLevel(l, p)
}
func (s *syncWriter) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if closer, ok := s.lw.(io.Closer); ok {
return closer.Close()
}
return nil
}
type multiLevelWriter struct {
writers []LevelWriter
}
func (t multiLevelWriter) Write(p []byte) (n int, err error) {
for _, w := range t.writers {
n, err = w.Write(p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
if _n, _err := w.Write(p); err == nil {
n = _n
if _err != nil {
err = _err
} else if _n != len(p) {
err = io.ErrShortWrite
}
}
}
return len(p), nil
return n, err
}
func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) {
for _, w := range t.writers {
n, err = w.WriteLevel(l, p)
if err != nil {
return
}
if n != len(p) {
err = io.ErrShortWrite
return
if _n, _err := w.WriteLevel(l, p); err == nil {
n = _n
if _err != nil {
err = _err
} else if _n != len(p) {
err = io.ErrShortWrite
}
}
}
return len(p), nil
return n, err
}
// Calls close on all the underlying writers that are io.Closers. If any of the
// Close methods return an error, the remainder of the closers are not closed
// and the error is returned.
func (t multiLevelWriter) Close() error {
for _, w := range t.writers {
if closer, ok := w.(io.Closer); ok {
if err := closer.Close(); err != nil {
return err
}
}
}
return nil
}
// MultiLevelWriter creates a writer that duplicates its writes to all the
@@ -93,8 +130,226 @@ func MultiLevelWriter(writers ...io.Writer) LevelWriter {
if lw, ok := w.(LevelWriter); ok {
lwriters = append(lwriters, lw)
} else {
lwriters = append(lwriters, levelWriterAdapter{w})
lwriters = append(lwriters, LevelWriterAdapter{w})
}
}
return multiLevelWriter{lwriters}
}
// TestingLog is the logging interface of testing.TB.
type TestingLog interface {
Log(args ...interface{})
Logf(format string, args ...interface{})
Helper()
}
// TestWriter is a writer that writes to testing.TB.
type TestWriter struct {
T TestingLog
// Frame skips caller frames to capture the original file and line numbers.
Frame int
}
// NewTestWriter creates a writer that logs to the testing.TB.
func NewTestWriter(t TestingLog) TestWriter {
return TestWriter{T: t}
}
// Write to testing.TB.
func (t TestWriter) Write(p []byte) (n int, err error) {
t.T.Helper()
n = len(p)
// Strip trailing newline because t.Log always adds one.
p = bytes.TrimRight(p, "\n")
// Try to correct the log file and line number to the caller.
if t.Frame > 0 {
_, origFile, origLine, _ := runtime.Caller(1)
_, frameFile, frameLine, ok := runtime.Caller(1 + t.Frame)
if ok {
erase := strings.Repeat("\b", len(path.Base(origFile))+len(strconv.Itoa(origLine))+3)
t.T.Logf("%s%s:%d: %s", erase, path.Base(frameFile), frameLine, p)
return n, err
}
}
t.T.Log(string(p))
return n, err
}
// ConsoleTestWriter creates an option that correctly sets the file frame depth for testing.TB log.
func ConsoleTestWriter(t TestingLog) func(w *ConsoleWriter) {
return func(w *ConsoleWriter) {
w.Out = TestWriter{T: t, Frame: 6}
}
}
// FilteredLevelWriter writes only logs at Level or above to Writer.
//
// It should be used only in combination with MultiLevelWriter when you
// want to write to multiple destinations at different levels. Otherwise
// you should just set the level on the logger and filter events early.
// When using MultiLevelWriter then you set the level on the logger to
// the lowest of the levels you use for writers.
type FilteredLevelWriter struct {
Writer LevelWriter
Level Level
}
// Write writes to the underlying Writer.
func (w *FilteredLevelWriter) Write(p []byte) (int, error) {
return w.Writer.Write(p)
}
// WriteLevel calls WriteLevel of the underlying Writer only if the level is equal
// or above the Level.
func (w *FilteredLevelWriter) WriteLevel(level Level, p []byte) (int, error) {
if level >= w.Level {
return w.Writer.WriteLevel(level, p)
}
return len(p), nil
}
// Call the underlying writer's Close method if it is an io.Closer. Otherwise
// does nothing.
func (w *FilteredLevelWriter) Close() error {
if closer, ok := w.Writer.(io.Closer); ok {
return closer.Close()
}
return nil
}
var triggerWriterPool = &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 1024))
},
}
// TriggerLevelWriter buffers log lines at the ConditionalLevel or below
// until a trigger level (or higher) line is emitted. Log lines with level
// higher than ConditionalLevel are always written out to the destination
// writer. If trigger never happens, buffered log lines are never written out.
//
// It can be used to configure "log level per request".
type TriggerLevelWriter struct {
// Destination writer. If LevelWriter is provided (usually), its WriteLevel is used
// instead of Write.
io.Writer
// ConditionalLevel is the level (and below) at which lines are buffered until
// a trigger level (or higher) line is emitted. Usually this is set to DebugLevel.
ConditionalLevel Level
// TriggerLevel is the lowest level that triggers the sending of the conditional
// level lines. Usually this is set to ErrorLevel.
TriggerLevel Level
buf *bytes.Buffer
triggered bool
mu sync.Mutex
}
func (w *TriggerLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) {
w.mu.Lock()
defer w.mu.Unlock()
// At first trigger level or above log line, we flush the buffer and change the
// trigger state to triggered.
if !w.triggered && l >= w.TriggerLevel {
err := w.trigger()
if err != nil {
return 0, err
}
}
// Unless triggered, we buffer everything at and below ConditionalLevel.
if !w.triggered && l <= w.ConditionalLevel {
if w.buf == nil {
w.buf = triggerWriterPool.Get().(*bytes.Buffer)
}
// We prefix each log line with a byte with the level.
// Hopefully we will never have a level value which equals a newline
// (which could interfere with reconstruction of log lines in the trigger method).
w.buf.WriteByte(byte(l))
w.buf.Write(p)
return len(p), nil
}
// Anything above ConditionalLevel is always passed through.
// Once triggered, everything is passed through.
if lw, ok := w.Writer.(LevelWriter); ok {
return lw.WriteLevel(l, p)
}
return w.Write(p)
}
// trigger expects lock to be held.
func (w *TriggerLevelWriter) trigger() error {
if w.triggered {
return nil
}
w.triggered = true
if w.buf == nil {
return nil
}
p := w.buf.Bytes()
for len(p) > 0 {
// We do not use bufio.Scanner here because we already have full buffer
// in the memory and we do not want extra copying from the buffer to
// scanner's token slice, nor we want to hit scanner's token size limit,
// and we also want to preserve newlines.
i := bytes.IndexByte(p, '\n')
line := p[0 : i+1]
p = p[i+1:]
// We prefixed each log line with a byte with the level.
level := Level(line[0])
line = line[1:]
var err error
if lw, ok := w.Writer.(LevelWriter); ok {
_, err = lw.WriteLevel(level, line)
} else {
_, err = w.Write(line)
}
if err != nil {
return err
}
}
return nil
}
// Trigger forces flushing the buffer and change the trigger state to
// triggered, if the writer has not already been triggered before.
func (w *TriggerLevelWriter) Trigger() error {
w.mu.Lock()
defer w.mu.Unlock()
return w.trigger()
}
// Close closes the writer and returns the buffer to the pool.
func (w *TriggerLevelWriter) Close() error {
w.mu.Lock()
defer w.mu.Unlock()
if w.buf == nil {
return nil
}
// We return the buffer only if it has not grown above the limit.
// This prevents accumulation of large buffers in the pool just
// because occasionally a large buffer might be needed.
if w.buf.Cap() <= TriggerLevelWriterBufferReuseLimit {
w.buf.Reset()
triggerWriterPool.Put(w.buf)
}
w.buf = nil
return nil
}
+526
View File
@@ -1,10 +1,47 @@
//go:build !binary_log && !windows
// +build !binary_log,!windows
package zerolog
import (
"bytes"
"errors"
"fmt"
"io"
"reflect"
"testing"
)
type closableBuffer struct {
*bytes.Buffer
closed bool
closeError error
}
func (cb *closableBuffer) Close() error {
cb.closed = true
return cb.closeError
}
type errorWriter struct {
writeError error
shortWrite bool
}
func (ew *errorWriter) Write(p []byte) (int, error) {
if ew.writeError != nil {
return 0, ew.writeError
}
if ew.shortWrite {
return len(p) - 1, nil // Return short write
}
return len(p), nil
}
func (ew *errorWriter) WriteLevel(level Level, p []byte) (int, error) {
return ew.Write(p)
}
func TestMultiSyslogWriter(t *testing.T) {
sw := &syslogTestWriter{}
log := New(MultiLevelWriter(SyslogLevelWriter(sw)))
@@ -12,13 +49,502 @@ func TestMultiSyslogWriter(t *testing.T) {
log.Info().Msg("info")
log.Warn().Msg("warn")
log.Error().Msg("error")
log.Log().Msg("nolevel")
want := []syslogEvent{
{"Debug", `{"level":"debug","message":"debug"}` + "\n"},
{"Info", `{"level":"info","message":"info"}` + "\n"},
{"Warning", `{"level":"warn","message":"warn"}` + "\n"},
{"Err", `{"level":"error","message":"error"}` + "\n"},
{"Info", `{"message":"nolevel"}` + "\n"},
}
if got := sw.events; !reflect.DeepEqual(got, want) {
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
}
}
var writeCalls int
type mockedWriter struct {
wantErr bool
}
func (c mockedWriter) Write(p []byte) (int, error) {
writeCalls++
if c.wantErr {
return -1, errors.New("Expected error")
}
return len(p), nil
}
// Tests that a new writer is only used if it actually works.
func TestResilientMultiWriter(t *testing.T) {
tests := []struct {
name string
writers []io.Writer
}{
{
name: "All valid writers",
writers: []io.Writer{
mockedWriter{
wantErr: false,
},
mockedWriter{
wantErr: false,
},
},
},
{
name: "All invalid writers",
writers: []io.Writer{
mockedWriter{
wantErr: true,
},
mockedWriter{
wantErr: true,
},
},
},
{
name: "First invalid writer",
writers: []io.Writer{
mockedWriter{
wantErr: true,
},
mockedWriter{
wantErr: false,
},
},
},
{
name: "First valid writer",
writers: []io.Writer{
mockedWriter{
wantErr: false,
},
mockedWriter{
wantErr: true,
},
},
},
}
for _, tt := range tests {
writers := tt.writers
multiWriter := MultiLevelWriter(writers...)
logger := New(multiWriter).With().Timestamp().Logger().Level(InfoLevel)
logger.Info().Msg("Test msg")
if len(writers) != writeCalls {
t.Errorf("Expected %d writers to have been called but only %d were.", len(writers), writeCalls)
}
writeCalls = 0
}
}
type testingLog struct {
testing.TB
buf bytes.Buffer
}
func (t *testingLog) Log(args ...interface{}) {
if _, err := t.buf.WriteString(fmt.Sprint(args...)); err != nil {
t.Error(err)
}
}
func (t *testingLog) Logf(format string, args ...interface{}) {
if _, err := t.buf.WriteString(fmt.Sprintf(format, args...)); err != nil {
t.Error(err)
}
}
func TestTestWriter(t *testing.T) {
tests := []struct {
name string
write []byte
want []byte
}{{
name: "newline",
write: []byte("newline\n"),
want: []byte("newline"),
}, {
name: "oneline",
write: []byte("oneline"),
want: []byte("oneline"),
}, {
name: "twoline",
write: []byte("twoline\n\n"),
want: []byte("twoline"),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tb := &testingLog{TB: t} // Capture TB log buffer.
w := TestWriter{T: tb}
n, err := w.Write(tt.write)
if err != nil {
t.Error(err)
}
if n != len(tt.write) {
t.Errorf("Expected %d write length but got %d", len(tt.write), n)
}
p := tb.buf.Bytes()
if !bytes.Equal(tt.want, p) {
t.Errorf("Expected %q, got %q.", tt.want, p)
}
log := New(NewConsoleWriter(ConsoleTestWriter(t)))
log.Info().Str("name", tt.name).Msg("Success!")
tb.buf.Reset()
})
}
}
func TestFilteredLevelWriter(t *testing.T) {
buf := bytes.Buffer{}
writer := FilteredLevelWriter{
Writer: LevelWriterAdapter{&buf},
Level: InfoLevel,
}
_, err := writer.WriteLevel(DebugLevel, []byte("no"))
if err != nil {
t.Error(err)
}
_, err = writer.WriteLevel(InfoLevel, []byte("yes"))
if err != nil {
t.Error(err)
}
p := buf.Bytes()
if want := "yes"; !bytes.Equal([]byte(want), p) {
t.Errorf("Expected %q, got %q.", want, p)
}
}
type testWrite struct {
Level
Line []byte
}
func TestTriggerLevelWriter(t *testing.T) {
tests := []struct {
write []testWrite
want []byte
all []byte
}{{
[]testWrite{
{DebugLevel, []byte("no\n")},
{InfoLevel, []byte("yes\n")},
},
[]byte("yes\n"),
[]byte("yes\nno\n"),
}, {
[]testWrite{
{DebugLevel, []byte("yes1\n")},
{InfoLevel, []byte("yes2\n")},
{ErrorLevel, []byte("yes3\n")},
{DebugLevel, []byte("yes4\n")},
},
[]byte("yes2\nyes1\nyes3\nyes4\n"),
[]byte("yes2\nyes1\nyes3\nyes4\n"),
}}
for k, tt := range tests {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
buf := bytes.Buffer{}
writer := TriggerLevelWriter{Writer: LevelWriterAdapter{&buf}, ConditionalLevel: DebugLevel, TriggerLevel: ErrorLevel}
t.Cleanup(func() { writer.Close() })
for _, w := range tt.write {
_, err := writer.WriteLevel(w.Level, w.Line)
if err != nil {
t.Error(err)
}
}
p := buf.Bytes()
if want := tt.want; !bytes.Equal([]byte(want), p) {
t.Errorf("Expected %q, got %q.", want, p)
}
err := writer.Trigger()
if err != nil {
t.Error(err)
}
p = buf.Bytes()
if want := tt.all; !bytes.Equal([]byte(want), p) {
t.Errorf("Expected %q, got %q.", want, p)
}
})
}
}
func TestLevelWriterAdapter_Close(t *testing.T) {
// Test with closable writer
buf := &bytes.Buffer{}
adapter := LevelWriterAdapter{Writer: buf}
// bytes.Buffer doesn't implement io.Closer, so Close should return nil
err := adapter.Close()
if err != nil {
t.Errorf("Close should not return error for non-closable writer: %v", err)
}
// Test with closable writer
closableBuf := &closableBuffer{Buffer: &bytes.Buffer{}}
adapter2 := LevelWriterAdapter{Writer: closableBuf}
err = adapter2.Close()
if err != nil {
t.Errorf("Close should not return error: %v", err)
}
if !closableBuf.closed {
t.Error("Close should have been called on closable writer")
}
}
func TestSyncWriter(t *testing.T) {
buf := &bytes.Buffer{}
// Test SyncWriter with regular io.Writer
syncWriter := SyncWriter(buf)
// Test Write
data := []byte("test data")
n, err := syncWriter.Write(data)
if err != nil {
t.Errorf("Write failed: %v", err)
}
if n != len(data) {
t.Errorf("Write returned wrong length: got %d, want %d", n, len(data))
}
if got := buf.String(); got != string(data) {
t.Errorf("Write wrote wrong data: got %q, want %q", got, string(data))
}
// Test SyncWriter with LevelWriter - use it with a logger
levelBuf := &bytes.Buffer{}
levelWriter := LevelWriterAdapter{levelBuf}
syncLevelWriter := SyncWriter(levelWriter)
logger := New(syncLevelWriter)
logger.Info().Msg("test message")
expected := `{"level":"info","message":"test message"}` + "\n"
if got := levelBuf.String(); got != expected {
t.Errorf("SyncWriter with LevelWriter failed: got %q, want %q", got, expected)
}
// Test SyncWriter Close with closable writer
closableBuf := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false}
closableSyncWriter := SyncWriter(closableBuf)
if closeable, ok := closableSyncWriter.(io.Closer); !ok {
t.Error("SyncWriter should implement Close method")
} else {
err := closeable.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
}
if !closableBuf.closed {
t.Error("Close should have been called on closable writer")
}
// Test SyncWriter Close with closable writer that returns error
errorBuf := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false, closeError: io.EOF}
errorSyncWriter := SyncWriter(errorBuf)
if closeable, ok := errorSyncWriter.(io.Closer); !ok {
t.Error("SyncWriter should implement Close method")
} else {
err := closeable.Close()
if err != io.EOF {
t.Errorf("Close should have returned EOF error, got: %v", err)
}
}
if !errorBuf.closed {
t.Error("Close should have been called on closable writer")
}
}
func TestMultiLevelWriter_Write(t *testing.T) {
// Test successful writes
buf1 := &bytes.Buffer{}
buf2 := &bytes.Buffer{}
multiWriter := MultiLevelWriter(buf1, buf2)
data := []byte("test data")
n, err := multiWriter.Write(data)
if err != nil {
t.Errorf("Write failed: %v", err)
}
if n != len(data) {
t.Errorf("Write returned wrong length: got %d, want %d", n, len(data))
}
if got1 := buf1.String(); got1 != string(data) {
t.Errorf("First writer got wrong data: got %q, want %q", got1, string(data))
}
if got2 := buf2.String(); got2 != string(data) {
t.Errorf("Second writer got wrong data: got %q, want %q", got2, string(data))
}
// Test with error writer
errorWriter1 := &errorWriter{writeError: io.EOF}
buf3 := &bytes.Buffer{}
errorMultiWriter := MultiLevelWriter(errorWriter1, buf3)
_, err = errorMultiWriter.Write(data)
if err != io.EOF {
t.Errorf("Write should have returned EOF error, got: %v", err)
}
// Test with short write
shortWriter := &errorWriter{shortWrite: true}
buf4 := &bytes.Buffer{}
shortMultiWriter := MultiLevelWriter(shortWriter, buf4)
_, err = shortMultiWriter.Write(data)
if err != io.ErrShortWrite {
t.Errorf("Write should have returned ErrShortWrite, got: %v", err)
}
}
func TestMultiLevelWriter_WriteLevel(t *testing.T) {
// Test successful writes
buf1 := &bytes.Buffer{}
buf2 := &bytes.Buffer{}
multiWriter := MultiLevelWriter(buf1, buf2)
data := []byte("test level data")
n, err := multiWriter.WriteLevel(InfoLevel, data)
if err != nil {
t.Errorf("WriteLevel failed: %v", err)
}
if n != len(data) {
t.Errorf("WriteLevel returned wrong length: got %d, want %d", n, len(data))
}
if got1 := buf1.String(); got1 != string(data) {
t.Errorf("First writer got wrong data: got %q, want %q", got1, string(data))
}
if got2 := buf2.String(); got2 != string(data) {
t.Errorf("Second writer got wrong data: got %q, want %q", got2, string(data))
}
// Test with error writer
errorWriter1 := &errorWriter{writeError: io.EOF}
buf3 := &bytes.Buffer{}
errorMultiWriter := MultiLevelWriter(errorWriter1, buf3)
_, err = errorMultiWriter.WriteLevel(InfoLevel, data)
if err != io.EOF {
t.Errorf("WriteLevel should have returned EOF error, got: %v", err)
}
}
func TestMultiLevelWriter_Close(t *testing.T) {
buf1 := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false}
buf2 := &bytes.Buffer{} // non-closable
multiWriter := MultiLevelWriter(buf1, buf2)
// Cast to concrete type to access Close
mw := multiWriter.(multiLevelWriter)
err := mw.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
if !buf1.closed {
t.Error("First closable writer should have been closed")
}
// Test multiLevelWriter Close with error
errorBuf1 := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false, closeError: io.EOF}
errorBuf2 := &bytes.Buffer{} // non-closable
errorMultiWriter := MultiLevelWriter(errorBuf1, errorBuf2)
emw := errorMultiWriter.(multiLevelWriter)
err = emw.Close()
if err != io.EOF {
t.Errorf("Close should have returned EOF error, got: %v", err)
}
if !errorBuf1.closed {
t.Error("First closable writer should have been closed")
}
}
func TestNewTestWriter(t *testing.T) {
writer := NewTestWriter(t)
if writer.T != t {
t.Error("NewTestWriter should set the testing interface")
}
if writer.Frame != 0 {
t.Errorf("NewTestWriter should set Frame to 0, got %d", writer.Frame)
}
}
func TestFilteredLevelWriter_Write(t *testing.T) {
buf := &bytes.Buffer{}
filteredWriter := FilteredLevelWriter{
Writer: LevelWriterAdapter{buf},
Level: InfoLevel,
}
data := []byte("test data")
n, err := filteredWriter.Write(data)
if err != nil {
t.Errorf("Write failed: %v", err)
}
if n != len(data) {
t.Errorf("Write returned wrong length: got %d, want %d", n, len(data))
}
if got := buf.String(); got != string(data) {
t.Errorf("Write should always write: got %q, want %q", got, string(data))
}
}
func TestFilteredLevelWriter_Close(t *testing.T) {
buf := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false}
filteredWriter := FilteredLevelWriter{
Writer: LevelWriterAdapter{buf},
Level: InfoLevel,
}
err := filteredWriter.Close()
if err != nil {
t.Errorf("Close failed: %v", err)
}
if !buf.closed {
t.Error("Underlying closable writer should have been closed")
}
// Test FilteredLevelWriter Close with error
errorBuf := &closableBuffer{Buffer: &bytes.Buffer{}, closed: false, closeError: io.EOF}
errorFilteredWriter := FilteredLevelWriter{
Writer: LevelWriterAdapter{errorBuf},
Level: InfoLevel,
}
err = errorFilteredWriter.Close()
if err != io.EOF {
t.Errorf("Close should have returned EOF error, got: %v", err)
}
if !errorBuf.closed {
t.Error("Underlying closable writer should have been closed")
}
}