mirror of
https://github.com/rs/zerolog
synced 2026-06-08 17:13:30 +00:00
Compare commits
51 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e30c71369 | |||
| 3f112dae87 | |||
| a4c54e5d8b | |||
| 96f91bb4f5 | |||
| 51c79ca476 | |||
| e7627a4f73 | |||
| baa31cfa85 | |||
| 8e36cbf881 | |||
| 20ad1708e7 | |||
| 338f9bc140 | |||
| e0f8de6c35 | |||
| 972f27185c | |||
| 624b3116d8 | |||
| 785a567b10 | |||
| 84794124e9 | |||
| fc5bbcd9d6 | |||
| 1c8b5945b1 | |||
| 2da253048d | |||
| 8aa660046f | |||
| 470da8d0bb | |||
| 1dde226d45 | |||
| b6f076edc8 | |||
| 85255a5e26 | |||
| 71e1f5e052 | |||
| bae001d86b | |||
| e8a8508f09 | |||
| 372015deb4 | |||
| 9cd6f6eef2 | |||
| 1c6d99b455 | |||
| 1a88fbfdd0 | |||
| dabc72c15b | |||
| c19f1e5eed | |||
| 77db4b4f35 | |||
| 64faaa6980 | |||
| 80d6806aae | |||
| ea197802eb | |||
| c62533f761 | |||
| fb469685aa | |||
| a025d45231 | |||
| b5207c012d | |||
| 533ee32d5d | |||
| ea1184be2b | |||
| a572c9d1f6 | |||
| 79281e4bf6 | |||
| 57da509ee1 | |||
| 89162918d0 | |||
| d2b7a51951 | |||
| 711d95f5f1 | |||
| 1e2ce57d98 | |||
| 70bea47cc0 | |||
| 2ccfab3e07 |
@@ -6,6 +6,7 @@
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
tmp
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
|
||||
@@ -8,9 +8,9 @@ Zerolog's API is designed to provide both a great developer experience and stunn
|
||||
|
||||
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 efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) `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).
|
||||
|
||||

|
||||

|
||||
|
||||
## Who uses zerolog
|
||||
|
||||
@@ -34,32 +34,61 @@ Find out [who uses zerolog](https://github.com/rs/zerolog/wiki/Who-uses-zerolog)
|
||||
```go
|
||||
go get -u github.com/rs/zerolog/log
|
||||
```
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Simple Logging Example
|
||||
|
||||
For simple logging, import the global logger package **github.com/rs/zerolog/log**
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 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 = ""
|
||||
// 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 = ""
|
||||
|
||||
log.Print("hello world")
|
||||
log.Print("hello world")
|
||||
}
|
||||
|
||||
// Output: {"time":1516134303,"level":"debug","message":"hello world"}
|
||||
```
|
||||
> Note: By default log writes to `os.Stderr`
|
||||
> Note: The default log level for `log.Print` is *debug*
|
||||
|
||||
### 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
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
zerolog.TimeFieldFormat = ""
|
||||
|
||||
log.Debug().
|
||||
Str("Scale", "833 cents").
|
||||
Float64("Interval", 833.09).
|
||||
Msg("Fibonacci is everywhere")
|
||||
}
|
||||
|
||||
// Output: {"time":1524104936,"level":"debug","Scale":"833 cents","Interval":833.09,"message":"Fibonacci is everywhere"}
|
||||
```
|
||||
|
||||
> 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)
|
||||
|
||||
### Leveled Logging
|
||||
|
||||
#### Simple Leveled Logging Example
|
||||
@@ -68,26 +97,29 @@ func main() {
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
zerolog.TimeFieldFormat = ""
|
||||
zerolog.TimeFieldFormat = ""
|
||||
|
||||
log.Info().Msg("hello world")
|
||||
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)
|
||||
|
||||
* panic (`zerolog.PanicLevel`, 5)
|
||||
* fatal (`zerolog.FatalLevel`, 4)
|
||||
* error (`zerolog.ErrorLevel`, 3)
|
||||
* warn (`zerolog.WarnLevel`, 2)
|
||||
* info (`zerolog.InfoLevel`, 1)
|
||||
* debug (`zerolog.DebugLevel`, 0)
|
||||
|
||||
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.
|
||||
|
||||
@@ -99,41 +131,44 @@ This example uses command-line flags to demonstrate various outputs depending on
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"flag"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
zerolog.TimeFieldFormat = ""
|
||||
debug := flag.Bool("debug", false, "sets log level to debug")
|
||||
zerolog.TimeFieldFormat = ""
|
||||
debug := flag.Bool("debug", false, "sets log level to debug")
|
||||
|
||||
flag.Parse()
|
||||
flag.Parse()
|
||||
|
||||
// Default level for this example is info, unless debug flag is present
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
if *debug {
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
}
|
||||
// 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")
|
||||
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")
|
||||
}
|
||||
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"}
|
||||
@@ -141,48 +176,59 @@ $ ./logLevelExample -debug
|
||||
{"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 = ""
|
||||
|
||||
log.Log().
|
||||
Str("foo", "bar").
|
||||
Msg("")
|
||||
}
|
||||
|
||||
// Output: {"time":1494567715,"foo":"bar"}
|
||||
```
|
||||
|
||||
#### Logging Fatal Messages
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"errors"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"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"
|
||||
err := errors.New("A repo man spends his life getting into tense situations")
|
||||
service := "myservice"
|
||||
|
||||
zerolog.TimeFieldFormat = ""
|
||||
zerolog.TimeFieldFormat = ""
|
||||
|
||||
log.Fatal().
|
||||
Err(err).
|
||||
Str("service", service).
|
||||
Msgf("Cannot start %s", service)
|
||||
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.
|
||||
|
||||
### Contextual Logging
|
||||
|
||||
#### Fields can be added to log messages
|
||||
|
||||
```go
|
||||
log.Info().
|
||||
Str("foo", "bar").
|
||||
Int("n", 123).
|
||||
Msg("hello world")
|
||||
|
||||
// Output: {"level":"info","time":1494567715,"foo":"bar","n":123,"message":"hello world"}
|
||||
```
|
||||
|
||||
### Create logger instance to manage different outputs
|
||||
|
||||
```go
|
||||
@@ -197,7 +243,7 @@ logger.Info().Str("foo", "bar").Msg("hello world")
|
||||
|
||||
```go
|
||||
sublogger := log.With().
|
||||
Str("component": "foo").
|
||||
Str("component", "foo").
|
||||
Logger()
|
||||
sublogger.Info().Msg("hello world")
|
||||
|
||||
@@ -206,14 +252,38 @@ sublogger.Info().Msg("hello world")
|
||||
|
||||
### 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
|
||||
```
|
||||
|
||||
### Sub dictionary
|
||||
@@ -223,7 +293,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"}
|
||||
@@ -241,29 +311,30 @@ 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
|
||||
|
||||
```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"}
|
||||
```
|
||||
|
||||
|
||||
### 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 follow:
|
||||
|
||||
```go
|
||||
d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
|
||||
fmt.Printf("Dropped %d messages\n", missed)
|
||||
}))
|
||||
w := diode.NewWriter(os.Stdout, d, 10*time.Millisecond)
|
||||
wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
|
||||
fmt.Printf("Logger Dropped %d messages", missed)
|
||||
})
|
||||
log := zerolog.New(w)
|
||||
log.Print("test")
|
||||
```
|
||||
@@ -317,7 +388,7 @@ hooked.Warn().Msg("")
|
||||
### 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")
|
||||
|
||||
@@ -397,15 +468,15 @@ if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||
Some settings can be changed and will by 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.SetGlobalLevel`: Can raise the minimum level of all loggers. Set this to `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 an empty string, times are formated as UNIX timestamp.
|
||||
// DurationFieldUnit defines the unit for time.Duration type fields added
|
||||
// using the Dur method.
|
||||
// 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.
|
||||
|
||||
@@ -430,28 +501,37 @@ Some settings can be changed and will by applied to all loggers:
|
||||
|
||||
## Binary Encoding
|
||||
|
||||
In addition to the default JSON encoding, `zerolog` can produce binary logs using the [cbor](http://cbor.io) encoding. The choice of encoding can be decided at compile time using the build tag `binary_log` as follow:
|
||||
In addition to the default JSON encoding, `zerolog` can produce binary logs using [CBOR](http://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 .
|
||||
```
|
||||
|
||||
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/).
|
||||
|
||||
## Related Projects
|
||||
|
||||
* [grpc-zerolog](https://github.com/cheapRoc/grpc-zerolog): Implementation of `grpclog.LoggerV2` interface using `zerolog`
|
||||
|
||||
## Benchmarks
|
||||
|
||||
See [logbench](http://hackemist.com/logbench/) for more comprehensive and up-to-date benchmarks.
|
||||
|
||||
All operations are allocation free (those numbers *include* JSON 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
|
||||
```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)
|
||||
* [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:
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
remote_theme: rs/gh-readme
|
||||
@@ -1,6 +1,7 @@
|
||||
package zerolog
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -19,6 +20,20 @@ type Array struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func putArray(a *Array) {
|
||||
// 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 {
|
||||
return
|
||||
}
|
||||
arrayPool.Put(a)
|
||||
}
|
||||
|
||||
// Arr creates an array to be added to an Event or Context.
|
||||
func Arr() *Array {
|
||||
a := arrayPool.Get().(*Array)
|
||||
@@ -32,145 +47,178 @@ func (*Array) MarshalZerologArray(*Array) {
|
||||
}
|
||||
|
||||
func (a *Array) write(dst []byte) []byte {
|
||||
dst = appendArrayStart(dst)
|
||||
dst = enc.AppendArrayStart(dst)
|
||||
if len(a.buf) > 0 {
|
||||
dst = append(append(dst, a.buf...))
|
||||
}
|
||||
dst = appendArrayEnd(dst)
|
||||
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 append append it to the array.
|
||||
func (a *Array) Object(obj LogObjectMarshaler) *Array {
|
||||
e := Dict()
|
||||
obj.MarshalZerologObject(e)
|
||||
e.buf = appendEndMarker(e.buf)
|
||||
a.buf = append(appendArrayDelim(a.buf), e.buf...)
|
||||
eventPool.Put(e)
|
||||
e.buf = enc.AppendEndMarker(e.buf)
|
||||
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
||||
putEvent(e)
|
||||
return a
|
||||
}
|
||||
|
||||
// Str append the val as a string to the array.
|
||||
// Str append append the val as a string to the array.
|
||||
func (a *Array) Str(val string) *Array {
|
||||
a.buf = appendString(appendArrayDelim(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 append append the val as a string to the array.
|
||||
func (a *Array) Bytes(val []byte) *Array {
|
||||
a.buf = appendBytes(appendArrayDelim(a.buf), val)
|
||||
a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
|
||||
return a
|
||||
}
|
||||
|
||||
// Hex append the val as a hex string to the array.
|
||||
// Hex append append the val as a hex string to the array.
|
||||
func (a *Array) Hex(val []byte) *Array {
|
||||
a.buf = appendHex(appendArrayDelim(a.buf), val)
|
||||
a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
|
||||
return a
|
||||
}
|
||||
|
||||
// Err append the err as a string to the array.
|
||||
// Err serializes and appends the err to the array.
|
||||
func (a *Array) Err(err error) *Array {
|
||||
a.buf = appendError(appendArrayDelim(a.buf), err)
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case LogObjectMarshaler:
|
||||
e := newEvent(nil, 0)
|
||||
e.buf = e.buf[:0]
|
||||
e.appendObject(m)
|
||||
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
||||
putEvent(e)
|
||||
case error:
|
||||
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.
|
||||
// Bool append append the val as a bool to the array.
|
||||
func (a *Array) Bool(b bool) *Array {
|
||||
a.buf = appendBool(appendArrayDelim(a.buf), b)
|
||||
a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
|
||||
return a
|
||||
}
|
||||
|
||||
// Int append i as a int to the array.
|
||||
// Int append append i as a int to the array.
|
||||
func (a *Array) Int(i int) *Array {
|
||||
a.buf = appendInt(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Int8 append i as a int8 to the array.
|
||||
// Int8 append append i as a int8 to the array.
|
||||
func (a *Array) Int8(i int8) *Array {
|
||||
a.buf = appendInt8(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Int16 append i as a int16 to the array.
|
||||
// Int16 append append i as a int16 to the array.
|
||||
func (a *Array) Int16(i int16) *Array {
|
||||
a.buf = appendInt16(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Int32 append i as a int32 to the array.
|
||||
// Int32 append append i as a int32 to the array.
|
||||
func (a *Array) Int32(i int32) *Array {
|
||||
a.buf = appendInt32(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Int64 append i as a int64 to the array.
|
||||
// Int64 append append i as a int64 to the array.
|
||||
func (a *Array) Int64(i int64) *Array {
|
||||
a.buf = appendInt64(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Uint append i as a uint to the array.
|
||||
// Uint append append i as a uint to the array.
|
||||
func (a *Array) Uint(i uint) *Array {
|
||||
a.buf = appendUint(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Uint8 append i as a uint8 to the array.
|
||||
// Uint8 append append i as a uint8 to the array.
|
||||
func (a *Array) Uint8(i uint8) *Array {
|
||||
a.buf = appendUint8(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Uint16 append i as a uint16 to the array.
|
||||
// Uint16 append append i as a uint16 to the array.
|
||||
func (a *Array) Uint16(i uint16) *Array {
|
||||
a.buf = appendUint16(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Uint32 append i as a uint32 to the array.
|
||||
// Uint32 append append i as a uint32 to the array.
|
||||
func (a *Array) Uint32(i uint32) *Array {
|
||||
a.buf = appendUint32(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Uint64 append i as a uint64 to the array.
|
||||
// Uint64 append append i as a uint64 to the array.
|
||||
func (a *Array) Uint64(i uint64) *Array {
|
||||
a.buf = appendUint64(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// Float32 append f as a float32 to the array.
|
||||
// Float32 append append f as a float32 to the array.
|
||||
func (a *Array) Float32(f float32) *Array {
|
||||
a.buf = appendFloat32(appendArrayDelim(a.buf), f)
|
||||
a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f)
|
||||
return a
|
||||
}
|
||||
|
||||
// Float64 append f as a float64 to the array.
|
||||
// Float64 append append f as a float64 to the array.
|
||||
func (a *Array) Float64(f float64) *Array {
|
||||
a.buf = appendFloat64(appendArrayDelim(a.buf), f)
|
||||
a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f)
|
||||
return a
|
||||
}
|
||||
|
||||
// Time append t formated as string using zerolog.TimeFieldFormat.
|
||||
// Time append append t formated as string using zerolog.TimeFieldFormat.
|
||||
func (a *Array) Time(t time.Time) *Array {
|
||||
a.buf = appendTime(appendArrayDelim(a.buf), t, TimeFieldFormat)
|
||||
a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
|
||||
return a
|
||||
}
|
||||
|
||||
// Dur append d to the array.
|
||||
// Dur append append d to the array.
|
||||
func (a *Array) Dur(d time.Duration) *Array {
|
||||
a.buf = appendDuration(appendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
|
||||
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return a
|
||||
}
|
||||
|
||||
// Interface append i marshaled using reflection.
|
||||
// Interface append append i marshaled using reflection.
|
||||
func (a *Array) Interface(i interface{}) *Array {
|
||||
if obj, ok := i.(LogObjectMarshaler); ok {
|
||||
return a.Object(obj)
|
||||
}
|
||||
a.buf = appendInterface(appendArrayDelim(a.buf), i)
|
||||
a.buf = enc.AppendInterface(enc.AppendArrayDelim(a.buf), i)
|
||||
return a
|
||||
}
|
||||
|
||||
// IPAddr adds 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 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 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
|
||||
}
|
||||
|
||||
+5
-3
@@ -1,6 +1,7 @@
|
||||
package zerolog
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -18,14 +19,15 @@ 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}).
|
||||
Time(time.Time{}).
|
||||
IPAddr(net.IP{192, 168, 0, 10}).
|
||||
Dur(0)
|
||||
want := `[true,1,2,3,4,5,6,7,8,9,10,11,12,"a","b","1f","0001-01-01T00:00:00Z",0]`
|
||||
want := `[true,1,2,3,4,5,6,7,8,9,10,11.98122,12.987654321,"a","b","1f","0001-01-01T00:00:00Z","192.168.0.10",0]`
|
||||
if got := decodeObjectToStr(a.write([]byte{})); got != want {
|
||||
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
|
||||
}
|
||||
|
||||
+48
-2
@@ -6,6 +6,7 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
// "io/ioutil"
|
||||
stdlog "log"
|
||||
"time"
|
||||
@@ -54,8 +55,8 @@ func ExampleLogger_Sample() {
|
||||
log.Info().Msg("message 4")
|
||||
|
||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
||||
// Output: {"level":"info","message":"message 2"}
|
||||
// {"level":"info","message":"message 4"}
|
||||
// Output: {"level":"info","message":"message 1"}
|
||||
// {"level":"info","message":"message 3"}
|
||||
}
|
||||
|
||||
type LevelNameHook1 struct{}
|
||||
@@ -283,6 +284,21 @@ func ExampleEvent_Object() {
|
||||
// Output: {"foo":"bar","user":{"name":"John","age":35,"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)
|
||||
@@ -384,6 +400,36 @@ func ExampleContext_Array_object() {
|
||||
// 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{}}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
# Zerolog Lint
|
||||
|
||||
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 dependecy 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.
|
||||
|
||||
+320
-79
@@ -5,116 +5,271 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
cReset = 0
|
||||
cBold = 1
|
||||
cRed = 31
|
||||
cGreen = 32
|
||||
cYellow = 33
|
||||
cBlue = 34
|
||||
cMagenta = 35
|
||||
cCyan = 36
|
||||
cGray = 37
|
||||
cDarkGray = 90
|
||||
colorBold = iota + 1
|
||||
colorFaint
|
||||
)
|
||||
|
||||
var consoleBufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return bytes.NewBuffer(make([]byte, 0, 100))
|
||||
},
|
||||
}
|
||||
const (
|
||||
colorBlack = iota + 30
|
||||
colorRed
|
||||
colorGreen
|
||||
colorYellow
|
||||
colorBlue
|
||||
colorMagenta
|
||||
colorCyan
|
||||
colorWhite
|
||||
)
|
||||
|
||||
// ConsoleWriter reads a JSON object per write operation and output an
|
||||
// optionally colored human readable version on the Out writer.
|
||||
type ConsoleWriter struct {
|
||||
Out io.Writer
|
||||
NoColor bool
|
||||
}
|
||||
|
||||
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
||||
var event map[string]interface{}
|
||||
p = decodeIfBinaryToBytes(p)
|
||||
err = json.Unmarshal(p, &event)
|
||||
if err != nil {
|
||||
return
|
||||
var (
|
||||
consoleBufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return bytes.NewBuffer(make([]byte, 0, 100))
|
||||
},
|
||||
}
|
||||
buf := consoleBufPool.Get().(*bytes.Buffer)
|
||||
defer consoleBufPool.Put(buf)
|
||||
lvlColor := cReset
|
||||
level := "????"
|
||||
if l, ok := event[LevelFieldName].(string); ok {
|
||||
if !w.NoColor {
|
||||
lvlColor = levelColor(l)
|
||||
|
||||
consoleDefaultTimeFormat = time.Kitchen
|
||||
consoleDefaultFormatter = func(i interface{}) string { return fmt.Sprintf("%s", i) }
|
||||
consoleDefaultPartsOrder = func() []string {
|
||||
return []string{
|
||||
TimestampFieldName,
|
||||
LevelFieldName,
|
||||
CallerFieldName,
|
||||
MessageFieldName,
|
||||
}
|
||||
level = strings.ToUpper(l)[0:4]
|
||||
}
|
||||
fmt.Fprintf(buf, "%s |%s| %s",
|
||||
colorize(event[TimestampFieldName], cDarkGray, !w.NoColor),
|
||||
colorize(level, lvlColor, !w.NoColor),
|
||||
colorize(event[MessageFieldName], cReset, !w.NoColor))
|
||||
fields := make([]string, 0, len(event))
|
||||
for field := range event {
|
||||
|
||||
consoleNoColor = false
|
||||
consoleTimeFormat = consoleDefaultTimeFormat
|
||||
)
|
||||
|
||||
// Formatter transforms the input into a formatted string.
|
||||
type Formatter func(interface{}) 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
|
||||
|
||||
// PartsOrder defines the order of parts in output.
|
||||
PartsOrder []string
|
||||
|
||||
FormatTimestamp Formatter
|
||||
FormatLevel Formatter
|
||||
FormatCaller Formatter
|
||||
FormatMessage Formatter
|
||||
FormatFieldName Formatter
|
||||
FormatFieldValue Formatter
|
||||
FormatErrFieldName Formatter
|
||||
FormatErrFieldValue Formatter
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
// Write transforms the JSON input with formatters and appends to w.Out.
|
||||
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
||||
if w.PartsOrder == nil {
|
||||
w.PartsOrder = consoleDefaultPartsOrder()
|
||||
}
|
||||
if w.TimeFormat == "" && consoleTimeFormat != consoleDefaultTimeFormat {
|
||||
consoleTimeFormat = consoleDefaultTimeFormat
|
||||
}
|
||||
if w.TimeFormat != "" && consoleTimeFormat != w.TimeFormat {
|
||||
consoleTimeFormat = w.TimeFormat
|
||||
}
|
||||
if w.NoColor == false && consoleNoColor != false {
|
||||
consoleNoColor = false
|
||||
}
|
||||
if w.NoColor == true && consoleNoColor != w.NoColor {
|
||||
consoleNoColor = w.NoColor
|
||||
}
|
||||
|
||||
var buf = consoleBufPool.Get().(*bytes.Buffer)
|
||||
defer 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)
|
||||
}
|
||||
|
||||
for _, p := range w.PartsOrder {
|
||||
w.writePart(buf, evt, p)
|
||||
}
|
||||
|
||||
w.writeFields(evt, buf)
|
||||
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteTo(w.Out)
|
||||
return len(p), 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 {
|
||||
switch field {
|
||||
case LevelFieldName, TimestampFieldName, MessageFieldName:
|
||||
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
|
||||
continue
|
||||
}
|
||||
fields = append(fields, field)
|
||||
}
|
||||
sort.Strings(fields)
|
||||
for _, field := range fields {
|
||||
fmt.Fprintf(buf, " %s=", colorize(field, cCyan, !w.NoColor))
|
||||
switch value := event[field].(type) {
|
||||
case string:
|
||||
if needsQuote(value) {
|
||||
buf.WriteString(strconv.Quote(value))
|
||||
} else {
|
||||
buf.WriteString(value)
|
||||
|
||||
if 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
|
||||
}
|
||||
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
|
||||
fmt.Fprint(buf, value)
|
||||
default:
|
||||
b, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
fmt.Fprintf(buf, "[error: %v]", err)
|
||||
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
|
||||
} else {
|
||||
fmt.Fprint(buf, string(b))
|
||||
fn = w.FormatErrFieldName
|
||||
}
|
||||
|
||||
if w.FormatErrFieldValue == nil {
|
||||
fv = consoleDefaultFormatErrFieldValue
|
||||
} else {
|
||||
fv = w.FormatErrFieldValue
|
||||
}
|
||||
} else {
|
||||
if w.FormatFieldName == nil {
|
||||
fn = consoleDefaultFormatFieldName
|
||||
} 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 := json.Marshal(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(' ')
|
||||
}
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
buf.WriteTo(w.Out)
|
||||
n = len(p)
|
||||
return
|
||||
}
|
||||
|
||||
func colorize(s interface{}, color int, enabled bool) string {
|
||||
if !enabled {
|
||||
return fmt.Sprintf("%v", s)
|
||||
}
|
||||
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", color, s)
|
||||
}
|
||||
// writePart appends a formatted part to buf.
|
||||
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
|
||||
var f Formatter
|
||||
|
||||
func levelColor(level string) int {
|
||||
switch level {
|
||||
case "debug":
|
||||
return cMagenta
|
||||
case "info":
|
||||
return cGreen
|
||||
case "warn":
|
||||
return cYellow
|
||||
case "error", "fatal", "panic":
|
||||
return cRed
|
||||
switch p {
|
||||
case LevelFieldName:
|
||||
if w.FormatLevel == nil {
|
||||
f = consoleDefaultFormatLevel
|
||||
} else {
|
||||
f = w.FormatLevel
|
||||
}
|
||||
case TimestampFieldName:
|
||||
if w.FormatTimestamp == nil {
|
||||
f = consoleDefaultFormatTimestamp
|
||||
} else {
|
||||
f = w.FormatTimestamp
|
||||
}
|
||||
case MessageFieldName:
|
||||
if w.FormatMessage == nil {
|
||||
f = consoleDefaultFormatMessage
|
||||
} else {
|
||||
f = w.FormatMessage
|
||||
}
|
||||
case CallerFieldName:
|
||||
if w.FormatCaller == nil {
|
||||
f = consoleDefaultFormatCaller
|
||||
} else {
|
||||
f = w.FormatCaller
|
||||
}
|
||||
default:
|
||||
return cReset
|
||||
if w.FormatFieldValue == nil {
|
||||
f = consoleDefaultFormatFieldValue
|
||||
} else {
|
||||
f = w.FormatFieldValue
|
||||
}
|
||||
}
|
||||
|
||||
var s = f(evt[p])
|
||||
|
||||
if len(s) > 0 {
|
||||
buf.WriteString(s)
|
||||
if p != w.PartsOrder[len(w.PartsOrder)-1] { // Skip space for last part
|
||||
buf.WriteByte(' ')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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] == '"' {
|
||||
@@ -123,3 +278,89 @@ func needsQuote(s string) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// colorize returns the string s wrapped in ANSI code c, unless disabled is true.
|
||||
func colorize(s interface{}, c int, disabled bool) string {
|
||||
if disabled {
|
||||
return fmt.Sprintf("%s", s)
|
||||
}
|
||||
return fmt.Sprintf("\x1b[%dm%v\x1b[0m", c, s)
|
||||
}
|
||||
|
||||
// ----- DEFAULT FORMATTERS ---------------------------------------------------
|
||||
|
||||
var (
|
||||
consoleDefaultFormatTimestamp = func(i interface{}) string {
|
||||
t := "<nil>"
|
||||
if tt, ok := i.(string); ok {
|
||||
ts, err := time.Parse(time.RFC3339, tt)
|
||||
if err != nil {
|
||||
t = tt
|
||||
} else {
|
||||
t = ts.Format(consoleTimeFormat)
|
||||
}
|
||||
}
|
||||
return colorize(t, colorFaint, consoleNoColor)
|
||||
}
|
||||
|
||||
consoleDefaultFormatLevel = func(i interface{}) string {
|
||||
var l string
|
||||
if ll, ok := i.(string); ok {
|
||||
switch ll {
|
||||
case "debug":
|
||||
l = colorize("DBG", colorYellow, consoleNoColor)
|
||||
case "info":
|
||||
l = colorize("INF", colorGreen, consoleNoColor)
|
||||
case "warn":
|
||||
l = colorize("WRN", colorRed, consoleNoColor)
|
||||
case "error":
|
||||
l = colorize(colorize("ERR", colorRed, consoleNoColor), colorBold, consoleNoColor)
|
||||
case "fatal":
|
||||
l = colorize(colorize("FTL", colorRed, consoleNoColor), colorBold, consoleNoColor)
|
||||
case "panic":
|
||||
l = colorize(colorize("PNC", colorRed, consoleNoColor), colorBold, consoleNoColor)
|
||||
default:
|
||||
l = colorize("???", colorBold, consoleNoColor)
|
||||
}
|
||||
} else {
|
||||
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
consoleDefaultFormatCaller = func(i interface{}) string {
|
||||
var c string
|
||||
if cc, ok := i.(string); ok {
|
||||
c = cc
|
||||
}
|
||||
if len(c) > 0 {
|
||||
cwd, err := os.Getwd()
|
||||
if err == nil {
|
||||
c = strings.TrimPrefix(c, cwd)
|
||||
c = strings.TrimPrefix(c, "/")
|
||||
}
|
||||
c = colorize(c, colorBold, consoleNoColor) + colorize(" >", colorFaint, consoleNoColor)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
consoleDefaultFormatMessage = func(i interface{}) string {
|
||||
return fmt.Sprintf("%s", i)
|
||||
}
|
||||
|
||||
consoleDefaultFormatFieldName = func(i interface{}) string {
|
||||
return colorize(fmt.Sprintf("%s=", i), colorFaint, consoleNoColor)
|
||||
}
|
||||
|
||||
consoleDefaultFormatFieldValue = func(i interface{}) string {
|
||||
return fmt.Sprintf("%s", i)
|
||||
}
|
||||
|
||||
consoleDefaultFormatErrFieldName = func(i interface{}) string {
|
||||
return colorize(fmt.Sprintf("%s=", i), colorRed, consoleNoColor)
|
||||
}
|
||||
|
||||
consoleDefaultFormatErrFieldValue = func(i interface{}) string {
|
||||
return colorize(fmt.Sprintf("%s", i), colorRed, consoleNoColor)
|
||||
}
|
||||
)
|
||||
|
||||
+227
-3
@@ -1,14 +1,238 @@
|
||||
package zerolog_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func ExampleConsoleWriter_Write() {
|
||||
func ExampleConsoleWriter() {
|
||||
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true})
|
||||
|
||||
log.Info().Msg("hello world")
|
||||
// Output: <nil> |INFO| hello world
|
||||
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 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[2m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
|
||||
actualOutput := buf.String()
|
||||
if actualOutput != expectedOutput {
|
||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Write fields", func(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
|
||||
|
||||
d := time.Unix(0, 0).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 := "12:00AM DBG Foobar foo=bar\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}
|
||||
|
||||
d := time.Unix(0, 0).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 := "12:00AM 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)
|
||||
}
|
||||
|
||||
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
||||
evt := `{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar", "caller" : "` + cwd + `/foo/bar.go"}`
|
||||
// t.Log(evt)
|
||||
|
||||
_, err = w.Write([]byte(evt))
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error when writing output: %s", err)
|
||||
}
|
||||
|
||||
expectedOutput := "12:00AM DBG foo/bar.go > Foobar foo=bar\n"
|
||||
actualOutput := buf.String()
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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}
|
||||
|
||||
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 := "1970-01-01T00:00:00Z INF Foobar\n"
|
||||
actualOutput := buf.String()
|
||||
if actualOutput != expectedOutput {
|
||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||
}
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
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: ioutil.Discard, NoColor: false}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w.Write(msg)
|
||||
}
|
||||
}
|
||||
|
||||
+106
-57
@@ -2,6 +2,7 @@ package zerolog
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -23,9 +24,9 @@ func (c Context) Fields(fields map[string]interface{}) Context {
|
||||
|
||||
// Dict adds the field key with the dict to the logger context.
|
||||
func (c Context) Dict(key string, dict *Event) Context {
|
||||
dict.buf = appendEndMarker(dict.buf)
|
||||
c.l.context = append(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
|
||||
}
|
||||
|
||||
@@ -33,7 +34,7 @@ func (c Context) Dict(key string, dict *Event) Context {
|
||||
// Use zerolog.Arr() 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 = 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
|
||||
@@ -51,34 +52,43 @@ func (c Context) Array(key string, arr LogArrayMarshaler) Context {
|
||||
|
||||
// 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 := newEvent(levelWriterAdapter{ioutil.Discard}, 0)
|
||||
e.Object(key, obj)
|
||||
c.l.context = appendObjectData(c.l.context, e.buf)
|
||||
eventPool.Put(e)
|
||||
c.l.context = enc.AppendObjectData(c.l.context, e.buf)
|
||||
putEvent(e)
|
||||
return c
|
||||
}
|
||||
|
||||
// EmbedObject marshals and Embeds an object that implement the LogObjectMarshaler interface.
|
||||
func (c Context) EmbedObject(obj LogObjectMarshaler) Context {
|
||||
e := newEvent(levelWriterAdapter{ioutil.Discard}, 0)
|
||||
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 = appendString(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.
|
||||
func (c Context) Strs(key string, vals []string) Context {
|
||||
c.l.context = appendStrings(appendKey(c.l.context, key), vals)
|
||||
c.l.context = enc.AppendStrings(enc.AppendKey(c.l.context, key), vals)
|
||||
return c
|
||||
}
|
||||
|
||||
// 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 = appendBytes(appendKey(c.l.context, key), val)
|
||||
c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val)
|
||||
return c
|
||||
}
|
||||
|
||||
// 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 = appendHex(appendKey(c.l.context, key), val)
|
||||
c.l.context = enc.AppendHex(enc.AppendKey(c.l.context, key), val)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -87,186 +97,206 @@ func (c Context) Hex(key string, val []byte) 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(appendKey(c.l.context, key), b)
|
||||
c.l.context = appendJSON(enc.AppendKey(c.l.context, key), b)
|
||||
return c
|
||||
}
|
||||
|
||||
// AnErr adds the field key with err as a string to the logger context.
|
||||
// AnErr adds the field key with serialized err to the logger context.
|
||||
func (c Context) AnErr(key string, err error) Context {
|
||||
if err != nil {
|
||||
c.l.context = appendError(appendKey(c.l.context, key), err)
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case nil:
|
||||
return c
|
||||
case LogObjectMarshaler:
|
||||
return c.Object(key, m)
|
||||
case error:
|
||||
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 = appendErrors(appendKey(c.l.context, key), errs)
|
||||
return c
|
||||
arr := Arr()
|
||||
for _, err := range errs {
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case LogObjectMarshaler:
|
||||
arr = arr.Object(m)
|
||||
case error:
|
||||
arr = arr.Str(m.Error())
|
||||
case string:
|
||||
arr = arr.Str(m)
|
||||
default:
|
||||
arr = arr.Interface(m)
|
||||
}
|
||||
}
|
||||
|
||||
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 = appendError(appendKey(c.l.context, ErrorFieldName), err)
|
||||
}
|
||||
return c
|
||||
return c.AnErr(ErrorFieldName, err)
|
||||
}
|
||||
|
||||
// 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 = appendBool(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 = appendBools(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 = appendInt(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 = appendInts(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 = appendInt8(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 = appendInts8(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 = appendInt16(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 = appendInts16(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 = appendInt32(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 = appendInts32(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 = appendInt64(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 = appendInts64(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 = appendUint(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 = appendUints(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 = appendUint8(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 = appendUints8(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 = appendUint16(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 = appendUints16(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 = appendUint32(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 = appendUints32(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 = appendUint64(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 = appendUints64(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 = appendFloat32(appendKey(c.l.context, key), f)
|
||||
c.l.context = enc.AppendFloat32(enc.AppendKey(c.l.context, key), f)
|
||||
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 = appendFloats32(appendKey(c.l.context, key), f)
|
||||
c.l.context = enc.AppendFloats32(enc.AppendKey(c.l.context, key), f)
|
||||
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 = appendFloat64(appendKey(c.l.context, key), f)
|
||||
c.l.context = enc.AppendFloat64(enc.AppendKey(c.l.context, key), f)
|
||||
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 = appendFloats64(appendKey(c.l.context, key), f)
|
||||
c.l.context = enc.AppendFloats64(enc.AppendKey(c.l.context, key), f)
|
||||
return c
|
||||
}
|
||||
|
||||
@@ -289,38 +319,39 @@ func (c Context) Timestamp() Context {
|
||||
|
||||
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
||||
func (c Context) Time(key string, t time.Time) Context {
|
||||
c.l.context = appendTime(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.
|
||||
func (c Context) Times(key string, t []time.Time) Context {
|
||||
c.l.context = appendTimes(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.
|
||||
func (c Context) Dur(key string, d time.Duration) Context {
|
||||
c.l.context = appendDuration(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
c.l.context = enc.AppendDuration(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return c
|
||||
}
|
||||
|
||||
// Durs adds the fields key with d divided by unit and stored as a float.
|
||||
func (c Context) Durs(key string, d []time.Duration) Context {
|
||||
c.l.context = appendDurations(appendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
c.l.context = enc.AppendDurations(enc.AppendKey(c.l.context, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return c
|
||||
}
|
||||
|
||||
// Interface adds the field key with obj marshaled using reflection.
|
||||
func (c Context) Interface(key string, i interface{}) Context {
|
||||
c.l.context = appendInterface(appendKey(c.l.context, key), i)
|
||||
c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), i)
|
||||
return c
|
||||
}
|
||||
|
||||
type callerHook struct{}
|
||||
|
||||
func (ch callerHook) Run(e *Event, level Level, msg string) {
|
||||
e.caller(4)
|
||||
// Three extra frames to skip (added by hook infra).
|
||||
e.caller(CallerSkipFrameCount + 3)
|
||||
}
|
||||
|
||||
var ch = callerHook{}
|
||||
@@ -330,3 +361,21 @@ func (c Context) Caller() Context {
|
||||
c.l = c.l.Hook(ch)
|
||||
return c
|
||||
}
|
||||
|
||||
// IPAddr adds 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
|
||||
}
|
||||
|
||||
// IPPrefix adds 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
|
||||
}
|
||||
|
||||
// MACAddr adds 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
|
||||
}
|
||||
|
||||
@@ -2,38 +2,39 @@ package zerolog
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
var disabledLogger *Logger
|
||||
|
||||
func init() {
|
||||
l := New(ioutil.Discard).Level(Disabled)
|
||||
l := Nop()
|
||||
disabledLogger = &l
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
// WithContext returns a copy of ctx with l associated. If an instance of Logger
|
||||
// is already in the context, the pointer to this logger is updated with l.
|
||||
// is already in the context, the context is not updated.
|
||||
//
|
||||
// For instance, to add a field to an existing logger in the context, use this
|
||||
// notation:
|
||||
//
|
||||
// ctx := r.Context()
|
||||
// l := zerolog.Ctx(ctx)
|
||||
// ctx = l.With().Str("foo", "bar").WithContext(ctx)
|
||||
func (l Logger) WithContext(ctx context.Context) context.Context {
|
||||
// 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
|
||||
return ctx
|
||||
}
|
||||
if l.level == Disabled {
|
||||
if lp == l {
|
||||
// Do not store same logger.
|
||||
return ctx
|
||||
}
|
||||
} else if l.level == Disabled {
|
||||
// Do not store disabled logger.
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, ctxKey{}, &l)
|
||||
return context.WithValue(ctx, ctxKey{}, l)
|
||||
}
|
||||
|
||||
// Ctx returns the Logger associated with the ctx. If no logger
|
||||
|
||||
+22
-6
@@ -30,18 +30,34 @@ func TestCtx(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCtxDisabled(t *testing.T) {
|
||||
ctx := disabledLogger.WithContext(context.Background())
|
||||
dl := New(ioutil.Discard).Level(Disabled)
|
||||
ctx := dl.WithContext(context.Background())
|
||||
if ctx != context.Background() {
|
||||
t.Error("WithContext stored a disabled logger")
|
||||
}
|
||||
|
||||
ctx = New(ioutil.Discard).WithContext(ctx)
|
||||
if reflect.DeepEqual(Ctx(ctx), disabledLogger) {
|
||||
l := New(ioutil.Discard).With().Str("foo", "bar").Logger()
|
||||
ctx = l.WithContext(ctx)
|
||||
if Ctx(ctx) != &l {
|
||||
t.Error("WithContext did not store logger")
|
||||
}
|
||||
|
||||
ctx = disabledLogger.WithContext(ctx)
|
||||
if !reflect.DeepEqual(Ctx(ctx), disabledLogger) {
|
||||
t.Error("WithContext did not update logger pointer with disabled logger")
|
||||
l.UpdateContext(func(c Context) Context {
|
||||
return c.Str("bar", "baz")
|
||||
})
|
||||
ctx = l.WithContext(ctx)
|
||||
if Ctx(ctx) != &l {
|
||||
t.Error("WithContext did not store updated logger")
|
||||
}
|
||||
|
||||
l = l.Level(DebugLevel)
|
||||
ctx = l.WithContext(ctx)
|
||||
if Ctx(ctx) != &l {
|
||||
t.Error("WithContext did not store copied logger")
|
||||
}
|
||||
|
||||
ctx = dl.WithContext(ctx)
|
||||
if Ctx(ctx) != &dl {
|
||||
t.Error("WithContext did not overide logger with a disabled logger")
|
||||
}
|
||||
}
|
||||
|
||||
+41
-15
@@ -8,7 +8,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
diodes "code.cloudfoundry.org/go-diodes"
|
||||
"github.com/rs/zerolog/diode/internal/diodes"
|
||||
)
|
||||
|
||||
var bufPool = &sync.Pool{
|
||||
@@ -17,12 +17,18 @@ var bufPool = &sync.Pool{
|
||||
},
|
||||
}
|
||||
|
||||
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 *diodes.ManyToOne
|
||||
p *diodes.Poller
|
||||
d diodeFetcher
|
||||
c context.CancelFunc
|
||||
done chan struct{}
|
||||
}
|
||||
@@ -33,24 +39,34 @@ type Writer struct {
|
||||
//
|
||||
// Use a diode.Writer when
|
||||
//
|
||||
// d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
|
||||
// wr := diode.NewWriter(w, 1000, 0, func(missed int) {
|
||||
// log.Printf("Dropped %d messages", missed)
|
||||
// }))
|
||||
// w := diode.NewWriter(w, d, 10 * time.Millisecond)
|
||||
// log := zerolog.New(w)
|
||||
// })
|
||||
// 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, manyToOneDiode *diodes.ManyToOne, poolInterval time.Duration) Writer {
|
||||
func NewWriter(w io.Writer, size int, poolInterval time.Duration, f Alerter) Writer {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
dw := Writer{
|
||||
w: w,
|
||||
d: manyToOneDiode,
|
||||
p: diodes.NewPoller(manyToOneDiode,
|
||||
diodes.WithPollingInterval(poolInterval),
|
||||
diodes.WithPollingContext(ctx)),
|
||||
w: w,
|
||||
c: cancel,
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
if f == nil {
|
||||
f = func(int) {}
|
||||
}
|
||||
d := diodes.NewManyToOne(size, diodes.AlertFunc(f))
|
||||
if poolInterval > 0 {
|
||||
dw.d = diodes.NewPoller(d,
|
||||
diodes.WithPollingInterval(poolInterval),
|
||||
diodes.WithPollingContext(ctx))
|
||||
} else {
|
||||
dw.d = diodes.NewWaiter(d,
|
||||
diodes.WithWaiterContext(ctx))
|
||||
}
|
||||
go dw.poll()
|
||||
return dw
|
||||
}
|
||||
@@ -77,12 +93,22 @@ func (dw Writer) Close() error {
|
||||
func (dw Writer) poll() {
|
||||
defer close(dw.done)
|
||||
for {
|
||||
d := dw.p.Next()
|
||||
d := dw.d.Next()
|
||||
if d == nil {
|
||||
return
|
||||
}
|
||||
p := *(*[]byte)(d)
|
||||
dw.w.Write(p)
|
||||
bufPool.Put(p[: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(p) <= maxSize {
|
||||
bufPool.Put(p[:0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,15 @@ package diode_test
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
diodes "code.cloudfoundry.org/go-diodes"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/diode"
|
||||
)
|
||||
|
||||
func ExampleNewWriter() {
|
||||
d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
|
||||
w := diode.NewWriter(os.Stdout, 1000, 0, func(missed int) {
|
||||
fmt.Printf("Dropped %d messages\n", missed)
|
||||
}))
|
||||
w := diode.NewWriter(os.Stdout, d, 10*time.Millisecond)
|
||||
})
|
||||
log := zerolog.New(w)
|
||||
log.Print("test")
|
||||
|
||||
|
||||
+20
-16
@@ -9,18 +9,16 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
diodes "code.cloudfoundry.org/go-diodes"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/diode"
|
||||
"github.com/rs/zerolog/internal/cbor"
|
||||
)
|
||||
|
||||
func TestNewWriter(t *testing.T) {
|
||||
d := diodes.NewManyToOne(1000, diodes.AlertFunc(func(missed int) {
|
||||
fmt.Printf("Dropped %d messages\n", missed)
|
||||
}))
|
||||
buf := bytes.Buffer{}
|
||||
w := diode.NewWriter(&buf, d, 10*time.Millisecond)
|
||||
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
|
||||
fmt.Printf("Dropped %d messages\n", missed)
|
||||
})
|
||||
log := zerolog.New(w)
|
||||
log.Print("test")
|
||||
|
||||
@@ -35,16 +33,22 @@ func TestNewWriter(t *testing.T) {
|
||||
func Benchmark(b *testing.B) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
defer log.SetOutput(os.Stderr)
|
||||
d := diodes.NewManyToOne(100000, nil)
|
||||
w := diode.NewWriter(ioutil.Discard, d, 10*time.Millisecond)
|
||||
log := zerolog.New(w)
|
||||
defer w.Close()
|
||||
|
||||
b.SetParallelism(1000)
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
log.Print("test")
|
||||
}
|
||||
})
|
||||
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(ioutil.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")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
Copied from https://github.com/cloudfoundry/go-diodes to avoid test dependencies.
|
||||
@@ -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 {
|
||||
buffer []unsafe.Pointer
|
||||
writeIndex uint64
|
||||
readIndex uint64
|
||||
alerter Alerter
|
||||
}
|
||||
|
||||
// NewManyToOne creates a new diode (ring buffer). The ManyToOne diode
|
||||
// is optimzed 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 not 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 has 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
|
||||
}
|
||||
@@ -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 {
|
||||
buffer []unsafe.Pointer
|
||||
writeIndex uint64
|
||||
readIndex uint64
|
||||
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 has 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
|
||||
}
|
||||
@@ -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 PollerConfigOption(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 PollerConfigOption(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
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
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 WaiterConfigOption(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()
|
||||
w.c.Broadcast()
|
||||
}()
|
||||
|
||||
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
@@ -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, useInt bool) []byte
|
||||
AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte
|
||||
AppendEndMarker(dst []byte) []byte
|
||||
AppendFloat32(dst []byte, val float32) []byte
|
||||
AppendFloat64(dst []byte, val float64) []byte
|
||||
AppendFloats32(dst []byte, vals []float32) []byte
|
||||
AppendFloats64(dst []byte, vals []float64) []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
|
||||
}
|
||||
+4
-187
@@ -5,202 +5,19 @@ package zerolog
|
||||
// This file contains bindings to do binary encoding.
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/internal/cbor"
|
||||
)
|
||||
|
||||
func appendInterface(dst []byte, i interface{}) []byte {
|
||||
return cbor.AppendInterface(dst, i)
|
||||
}
|
||||
var (
|
||||
_ encoder = (*cbor.Encoder)(nil)
|
||||
|
||||
func appendKey(dst []byte, s string) []byte {
|
||||
return cbor.AppendKey(dst, s)
|
||||
}
|
||||
|
||||
func appendFloats64(dst []byte, f []float64) []byte {
|
||||
return cbor.AppendFloats64(dst, f)
|
||||
}
|
||||
|
||||
func appendFloat64(dst []byte, f float64) []byte {
|
||||
return cbor.AppendFloat64(dst, f)
|
||||
}
|
||||
|
||||
func appendFloats32(dst []byte, f []float32) []byte {
|
||||
return cbor.AppendFloats32(dst, f)
|
||||
}
|
||||
|
||||
func appendFloat32(dst []byte, f float32) []byte {
|
||||
return cbor.AppendFloat32(dst, f)
|
||||
}
|
||||
|
||||
func appendUints64(dst []byte, i []uint64) []byte {
|
||||
return cbor.AppendUints64(dst, i)
|
||||
}
|
||||
|
||||
func appendUint64(dst []byte, i uint64) []byte {
|
||||
return cbor.AppendUint64(dst, i)
|
||||
}
|
||||
|
||||
func appendUints32(dst []byte, i []uint32) []byte {
|
||||
return cbor.AppendUints32(dst, i)
|
||||
}
|
||||
|
||||
func appendUint32(dst []byte, i uint32) []byte {
|
||||
return cbor.AppendUint32(dst, i)
|
||||
}
|
||||
|
||||
func appendUints16(dst []byte, i []uint16) []byte {
|
||||
return cbor.AppendUints16(dst, i)
|
||||
}
|
||||
|
||||
func appendUint16(dst []byte, i uint16) []byte {
|
||||
return cbor.AppendUint16(dst, i)
|
||||
}
|
||||
|
||||
func appendUints8(dst []byte, i []uint8) []byte {
|
||||
return cbor.AppendUints8(dst, i)
|
||||
}
|
||||
|
||||
func appendUint8(dst []byte, i uint8) []byte {
|
||||
return cbor.AppendUint8(dst, i)
|
||||
}
|
||||
|
||||
func appendUints(dst []byte, i []uint) []byte {
|
||||
return cbor.AppendUints(dst, i)
|
||||
}
|
||||
|
||||
func appendUint(dst []byte, i uint) []byte {
|
||||
return cbor.AppendUint(dst, i)
|
||||
}
|
||||
|
||||
func appendInts64(dst []byte, i []int64) []byte {
|
||||
return cbor.AppendInts64(dst, i)
|
||||
}
|
||||
|
||||
func appendInt64(dst []byte, i int64) []byte {
|
||||
return cbor.AppendInt64(dst, i)
|
||||
}
|
||||
|
||||
func appendInts32(dst []byte, i []int32) []byte {
|
||||
return cbor.AppendInts32(dst, i)
|
||||
}
|
||||
|
||||
func appendInt32(dst []byte, i int32) []byte {
|
||||
return cbor.AppendInt32(dst, i)
|
||||
}
|
||||
|
||||
func appendInts16(dst []byte, i []int16) []byte {
|
||||
return cbor.AppendInts16(dst, i)
|
||||
}
|
||||
|
||||
func appendInt16(dst []byte, i int16) []byte {
|
||||
return cbor.AppendInt16(dst, i)
|
||||
}
|
||||
|
||||
func appendInts8(dst []byte, i []int8) []byte {
|
||||
return cbor.AppendInts8(dst, i)
|
||||
}
|
||||
|
||||
func appendInt8(dst []byte, i int8) []byte {
|
||||
return cbor.AppendInt8(dst, i)
|
||||
}
|
||||
|
||||
func appendInts(dst []byte, i []int) []byte {
|
||||
return cbor.AppendInts(dst, i)
|
||||
}
|
||||
|
||||
func appendInt(dst []byte, i int) []byte {
|
||||
return cbor.AppendInt(dst, i)
|
||||
}
|
||||
|
||||
func appendBools(dst []byte, b []bool) []byte {
|
||||
return cbor.AppendBools(dst, b)
|
||||
}
|
||||
|
||||
func appendBool(dst []byte, b bool) []byte {
|
||||
return cbor.AppendBool(dst, b)
|
||||
}
|
||||
|
||||
func appendError(dst []byte, e error) []byte {
|
||||
return cbor.AppendError(dst, e)
|
||||
}
|
||||
|
||||
func appendErrors(dst []byte, e []error) []byte {
|
||||
return cbor.AppendErrors(dst, e)
|
||||
}
|
||||
|
||||
func appendString(dst []byte, s string) []byte {
|
||||
return cbor.AppendString(dst, s)
|
||||
}
|
||||
|
||||
func appendStrings(dst []byte, s []string) []byte {
|
||||
return cbor.AppendStrings(dst, s)
|
||||
}
|
||||
|
||||
func appendDuration(dst []byte, t time.Duration, d time.Duration, fmt bool) []byte {
|
||||
return cbor.AppendDuration(dst, t, d, fmt)
|
||||
}
|
||||
|
||||
func appendDurations(dst []byte, t []time.Duration, d time.Duration, fmt bool) []byte {
|
||||
return cbor.AppendDurations(dst, t, d, fmt)
|
||||
}
|
||||
|
||||
func appendTimes(dst []byte, t []time.Time, fmt string) []byte {
|
||||
return cbor.AppendTimes(dst, t, fmt)
|
||||
}
|
||||
|
||||
func appendTime(dst []byte, t time.Time, fmt string) []byte {
|
||||
return cbor.AppendTime(dst, t, fmt)
|
||||
}
|
||||
|
||||
func appendEndMarker(dst []byte) []byte {
|
||||
return cbor.AppendEndMarker(dst)
|
||||
}
|
||||
|
||||
func appendLineBreak(dst []byte) []byte {
|
||||
// No line breaks needed in binary format.
|
||||
return dst
|
||||
}
|
||||
|
||||
func appendBeginMarker(dst []byte) []byte {
|
||||
return cbor.AppendBeginMarker(dst)
|
||||
}
|
||||
|
||||
func appendBytes(dst []byte, b []byte) []byte {
|
||||
return cbor.AppendBytes(dst, b)
|
||||
}
|
||||
|
||||
func appendArrayStart(dst []byte) []byte {
|
||||
return cbor.AppendArrayStart(dst)
|
||||
}
|
||||
|
||||
func appendArrayEnd(dst []byte) []byte {
|
||||
return cbor.AppendArrayEnd(dst)
|
||||
}
|
||||
|
||||
func appendArrayDelim(dst []byte) []byte {
|
||||
return cbor.AppendArrayDelim(dst)
|
||||
}
|
||||
|
||||
func appendObjectData(dst []byte, src []byte) []byte {
|
||||
// Map begin character is present in the src, which
|
||||
// should not be copied when appending to existing data.
|
||||
return cbor.AppendObjectData(dst, src[1:])
|
||||
}
|
||||
enc = cbor.Encoder{}
|
||||
)
|
||||
|
||||
func appendJSON(dst []byte, j []byte) []byte {
|
||||
return cbor.AppendEmbeddedJSON(dst, j)
|
||||
}
|
||||
|
||||
func appendNil(dst []byte) []byte {
|
||||
return cbor.AppendNull(dst)
|
||||
}
|
||||
|
||||
func appendHex(dst []byte, val []byte) []byte {
|
||||
return cbor.AppendHex(dst, val)
|
||||
}
|
||||
|
||||
// decodeIfBinaryToString - converts a binary formatted log msg to a
|
||||
// JSON formatted String Log message.
|
||||
func decodeIfBinaryToString(in []byte) string {
|
||||
|
||||
+4
-188
@@ -6,199 +6,19 @@ package zerolog
|
||||
// JSON encoded byte stream.
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/internal/json"
|
||||
)
|
||||
|
||||
func appendInterface(dst []byte, i interface{}) []byte {
|
||||
return json.AppendInterface(dst, i)
|
||||
}
|
||||
var (
|
||||
_ encoder = (*json.Encoder)(nil)
|
||||
|
||||
func appendKey(dst []byte, s string) []byte {
|
||||
return json.AppendKey(dst, s)
|
||||
}
|
||||
|
||||
func appendFloats64(dst []byte, f []float64) []byte {
|
||||
return json.AppendFloats64(dst, f)
|
||||
}
|
||||
|
||||
func appendFloat64(dst []byte, f float64) []byte {
|
||||
return json.AppendFloat64(dst, f)
|
||||
}
|
||||
|
||||
func appendFloats32(dst []byte, f []float32) []byte {
|
||||
return json.AppendFloats32(dst, f)
|
||||
}
|
||||
|
||||
func appendFloat32(dst []byte, f float32) []byte {
|
||||
return json.AppendFloat32(dst, f)
|
||||
}
|
||||
|
||||
func appendUints64(dst []byte, i []uint64) []byte {
|
||||
return json.AppendUints64(dst, i)
|
||||
}
|
||||
|
||||
func appendUint64(dst []byte, i uint64) []byte {
|
||||
return strconv.AppendUint(dst, uint64(i), 10)
|
||||
}
|
||||
|
||||
func appendUints32(dst []byte, i []uint32) []byte {
|
||||
return json.AppendUints32(dst, i)
|
||||
}
|
||||
|
||||
func appendUint32(dst []byte, i uint32) []byte {
|
||||
return strconv.AppendUint(dst, uint64(i), 10)
|
||||
}
|
||||
|
||||
func appendUints16(dst []byte, i []uint16) []byte {
|
||||
return json.AppendUints16(dst, i)
|
||||
}
|
||||
|
||||
func appendUint16(dst []byte, i uint16) []byte {
|
||||
return strconv.AppendUint(dst, uint64(i), 10)
|
||||
}
|
||||
|
||||
func appendUints8(dst []byte, i []uint8) []byte {
|
||||
return json.AppendUints8(dst, i)
|
||||
}
|
||||
|
||||
func appendUint8(dst []byte, i uint8) []byte {
|
||||
return strconv.AppendUint(dst, uint64(i), 10)
|
||||
}
|
||||
|
||||
func appendUints(dst []byte, i []uint) []byte {
|
||||
return json.AppendUints(dst, i)
|
||||
}
|
||||
|
||||
func appendUint(dst []byte, i uint) []byte {
|
||||
return strconv.AppendUint(dst, uint64(i), 10)
|
||||
}
|
||||
|
||||
func appendInts64(dst []byte, i []int64) []byte {
|
||||
return json.AppendInts64(dst, i)
|
||||
}
|
||||
|
||||
func appendInt64(dst []byte, i int64) []byte {
|
||||
return strconv.AppendInt(dst, int64(i), 10)
|
||||
}
|
||||
|
||||
func appendInts32(dst []byte, i []int32) []byte {
|
||||
return json.AppendInts32(dst, i)
|
||||
}
|
||||
|
||||
func appendInt32(dst []byte, i int32) []byte {
|
||||
return strconv.AppendInt(dst, int64(i), 10)
|
||||
}
|
||||
|
||||
func appendInts16(dst []byte, i []int16) []byte {
|
||||
return json.AppendInts16(dst, i)
|
||||
}
|
||||
|
||||
func appendInt16(dst []byte, i int16) []byte {
|
||||
return strconv.AppendInt(dst, int64(i), 10)
|
||||
}
|
||||
|
||||
func appendInts8(dst []byte, i []int8) []byte {
|
||||
return json.AppendInts8(dst, i)
|
||||
}
|
||||
|
||||
func appendInt8(dst []byte, i int8) []byte {
|
||||
return strconv.AppendInt(dst, int64(i), 10)
|
||||
}
|
||||
|
||||
func appendInts(dst []byte, i []int) []byte {
|
||||
return json.AppendInts(dst, i)
|
||||
}
|
||||
|
||||
func appendInt(dst []byte, i int) []byte {
|
||||
return strconv.AppendInt(dst, int64(i), 10)
|
||||
}
|
||||
|
||||
func appendBools(dst []byte, b []bool) []byte {
|
||||
return json.AppendBools(dst, b)
|
||||
}
|
||||
|
||||
func appendBool(dst []byte, b bool) []byte {
|
||||
return strconv.AppendBool(dst, b)
|
||||
}
|
||||
|
||||
func appendError(dst []byte, e error) []byte {
|
||||
return json.AppendError(dst, e)
|
||||
}
|
||||
|
||||
func appendErrors(dst []byte, e []error) []byte {
|
||||
return json.AppendErrors(dst, e)
|
||||
}
|
||||
|
||||
func appendString(dst []byte, s string) []byte {
|
||||
return json.AppendString(dst, s)
|
||||
}
|
||||
|
||||
func appendStrings(dst []byte, s []string) []byte {
|
||||
return json.AppendStrings(dst, s)
|
||||
}
|
||||
|
||||
func appendDuration(dst []byte, t time.Duration, d time.Duration, fmt bool) []byte {
|
||||
return json.AppendDuration(dst, t, d, fmt)
|
||||
}
|
||||
|
||||
func appendDurations(dst []byte, t []time.Duration, d time.Duration, fmt bool) []byte {
|
||||
return json.AppendDurations(dst, t, d, fmt)
|
||||
}
|
||||
|
||||
func appendTimes(dst []byte, t []time.Time, fmt string) []byte {
|
||||
return json.AppendTimes(dst, t, fmt)
|
||||
}
|
||||
|
||||
func appendTime(dst []byte, t time.Time, fmt string) []byte {
|
||||
return json.AppendTime(dst, t, fmt)
|
||||
}
|
||||
|
||||
func appendEndMarker(dst []byte) []byte {
|
||||
return append(dst, '}')
|
||||
}
|
||||
|
||||
func appendLineBreak(dst []byte) []byte {
|
||||
return append(dst, '\n')
|
||||
}
|
||||
|
||||
func appendBeginMarker(dst []byte) []byte {
|
||||
return append(dst, '{')
|
||||
}
|
||||
|
||||
func appendBytes(dst []byte, b []byte) []byte {
|
||||
return json.AppendBytes(dst, b)
|
||||
}
|
||||
|
||||
func appendArrayStart(dst []byte) []byte {
|
||||
return append(dst, '[')
|
||||
}
|
||||
|
||||
func appendArrayEnd(dst []byte) []byte {
|
||||
return append(dst, ']')
|
||||
}
|
||||
|
||||
func appendArrayDelim(dst []byte) []byte {
|
||||
if len(dst) > 0 {
|
||||
return append(dst, ',')
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func appendObjectData(dst []byte, src []byte) []byte {
|
||||
return json.AppendObjectData(dst, src)
|
||||
}
|
||||
enc = json.Encoder{}
|
||||
)
|
||||
|
||||
func appendJSON(dst []byte, j []byte) []byte {
|
||||
return append(dst, j...)
|
||||
}
|
||||
|
||||
func appendNil(dst []byte) []byte {
|
||||
return append(dst, "null"...)
|
||||
}
|
||||
|
||||
func decodeIfBinaryToString(in []byte) string {
|
||||
return string(in)
|
||||
}
|
||||
@@ -210,7 +30,3 @@ func decodeObjectToStr(in []byte) string {
|
||||
func decodeIfBinaryToBytes(in []byte) []byte {
|
||||
return in
|
||||
}
|
||||
|
||||
func appendHex(in []byte, val []byte) []byte {
|
||||
return json.AppendHex(in, val)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package zerolog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"runtime"
|
||||
"strconv"
|
||||
@@ -17,6 +18,11 @@ var eventPool = &sync.Pool{
|
||||
},
|
||||
}
|
||||
|
||||
// ErrorMarshalFunc allows customization of global error marshaling
|
||||
var ErrorMarshalFunc = func(err error) interface{} {
|
||||
return err
|
||||
}
|
||||
|
||||
// Event represents a log event. It is instanced by one of the level method of
|
||||
// Logger and finalized by the Msg or Msgf method.
|
||||
type Event struct {
|
||||
@@ -25,7 +31,20 @@ type Event struct {
|
||||
level Level
|
||||
done func(msg string)
|
||||
ch []Hook // hooks from context
|
||||
h []Hook
|
||||
}
|
||||
|
||||
func putEvent(e *Event) {
|
||||
// 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(e.buf) > maxSize {
|
||||
return
|
||||
}
|
||||
eventPool.Put(e)
|
||||
}
|
||||
|
||||
// LogObjectMarshaler provides a strongly-typed and encoding-agnostic interface
|
||||
@@ -40,14 +59,11 @@ type LogArrayMarshaler interface {
|
||||
MarshalZerologArray(a *Array)
|
||||
}
|
||||
|
||||
func newEvent(w LevelWriter, level Level, enabled bool) *Event {
|
||||
if !enabled {
|
||||
return &Event{}
|
||||
}
|
||||
func newEvent(w LevelWriter, level Level) *Event {
|
||||
e := eventPool.Get().(*Event)
|
||||
e.buf = e.buf[:0]
|
||||
e.h = e.h[:0]
|
||||
e.buf = appendBeginMarker(e.buf)
|
||||
e.ch = nil
|
||||
e.buf = enc.AppendBeginMarker(e.buf)
|
||||
e.w = w
|
||||
e.level = level
|
||||
return e
|
||||
@@ -57,19 +73,30 @@ func (e *Event) write() (err error) {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
e.buf = appendEndMarker(e.buf)
|
||||
e.buf = appendLineBreak(e.buf)
|
||||
if e.w != nil {
|
||||
_, err = e.w.WriteLevel(e.level, e.buf)
|
||||
if e.level != Disabled {
|
||||
e.buf = enc.AppendEndMarker(e.buf)
|
||||
e.buf = enc.AppendLineBreak(e.buf)
|
||||
if e.w != nil {
|
||||
_, err = e.w.WriteLevel(e.level, e.buf)
|
||||
}
|
||||
}
|
||||
eventPool.Put(e)
|
||||
putEvent(e)
|
||||
return
|
||||
}
|
||||
|
||||
// Enabled return false if the *Event is going to be filtered out by
|
||||
// log level or sampling.
|
||||
func (e *Event) Enabled() bool {
|
||||
return e != nil
|
||||
return e != nil && e.level != Disabled
|
||||
}
|
||||
|
||||
// Discard disables the event so Msg(f) won't print it.
|
||||
func (e *Event) Discard() *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.level = Disabled
|
||||
return nil
|
||||
}
|
||||
|
||||
// Msg sends the *Event with msg added as the message field if not empty.
|
||||
@@ -80,31 +107,7 @@ func (e *Event) Msg(msg string) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
if len(e.ch) > 0 {
|
||||
e.ch[0].Run(e, e.level, msg)
|
||||
if len(e.ch) > 1 {
|
||||
for _, hook := range e.ch[1:] {
|
||||
hook.Run(e, e.level, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(e.h) > 0 {
|
||||
e.h[0].Run(e, e.level, msg)
|
||||
if len(e.h) > 1 {
|
||||
for _, hook := range e.h[1:] {
|
||||
hook.Run(e, e.level, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
if msg != "" {
|
||||
e.buf = appendString(appendKey(e.buf, MessageFieldName), msg)
|
||||
}
|
||||
if e.done != nil {
|
||||
defer e.done(msg)
|
||||
}
|
||||
if err := e.write(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v", err)
|
||||
}
|
||||
e.msg(msg)
|
||||
}
|
||||
|
||||
// Msgf sends the event with formated msg added as the message field if not empty.
|
||||
@@ -115,7 +118,27 @@ func (e *Event) Msgf(format string, v ...interface{}) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
e.Msg(fmt.Sprintf(format, v...))
|
||||
e.msg(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (e *Event) msg(msg string) {
|
||||
if len(e.ch) > 0 {
|
||||
e.ch[0].Run(e, e.level, msg)
|
||||
if len(e.ch) > 1 {
|
||||
for _, hook := range e.ch[1:] {
|
||||
hook.Run(e, e.level, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
if msg != "" {
|
||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg)
|
||||
}
|
||||
if e.done != nil {
|
||||
defer e.done(msg)
|
||||
}
|
||||
if err := e.write(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Fields is a helper function to use a map to set fields using type assertion.
|
||||
@@ -133,9 +156,9 @@ func (e *Event) Dict(key string, dict *Event) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
dict.buf = appendEndMarker(dict.buf)
|
||||
e.buf = append(appendKey(e.buf, key), dict.buf...)
|
||||
eventPool.Put(dict)
|
||||
dict.buf = enc.AppendEndMarker(dict.buf)
|
||||
e.buf = append(enc.AppendKey(e.buf, key), dict.buf...)
|
||||
putEvent(dict)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -143,7 +166,7 @@ func (e *Event) Dict(key string, dict *Event) *Event {
|
||||
// Call usual field methods like Str, Int etc to add fields to this
|
||||
// event and give it as argument the *Event.Dict method.
|
||||
func Dict() *Event {
|
||||
return newEvent(nil, 0, true)
|
||||
return newEvent(nil, 0)
|
||||
}
|
||||
|
||||
// Array adds the field key with an array to the event context.
|
||||
@@ -153,7 +176,7 @@ func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendKey(e.buf, key)
|
||||
e.buf = enc.AppendKey(e.buf, key)
|
||||
var a *Array
|
||||
if aa, ok := arr.(*Array); ok {
|
||||
a = aa
|
||||
@@ -166,9 +189,9 @@ func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
|
||||
}
|
||||
|
||||
func (e *Event) appendObject(obj LogObjectMarshaler) {
|
||||
e.buf = appendBeginMarker(e.buf)
|
||||
e.buf = enc.AppendBeginMarker(e.buf)
|
||||
obj.MarshalZerologObject(e)
|
||||
e.buf = appendEndMarker(e.buf)
|
||||
e.buf = enc.AppendEndMarker(e.buf)
|
||||
}
|
||||
|
||||
// Object marshals an object that implement the LogObjectMarshaler interface.
|
||||
@@ -176,17 +199,26 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendKey(e.buf, key)
|
||||
e.buf = enc.AppendKey(e.buf, key)
|
||||
e.appendObject(obj)
|
||||
return e
|
||||
}
|
||||
|
||||
// Object marshals an object that implement the LogObjectMarshaler interface.
|
||||
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
obj.MarshalZerologObject(e)
|
||||
return e
|
||||
}
|
||||
|
||||
// Str adds the field key with val as a string to the *Event context.
|
||||
func (e *Event) Str(key, val string) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendString(appendKey(e.buf, key), val)
|
||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, key), val)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -195,7 +227,7 @@ func (e *Event) Strs(key string, vals []string) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendStrings(appendKey(e.buf, key), vals)
|
||||
e.buf = enc.AppendStrings(enc.AppendKey(e.buf, key), vals)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -207,7 +239,7 @@ func (e *Event) Bytes(key string, val []byte) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendBytes(appendKey(e.buf, key), val)
|
||||
e.buf = enc.AppendBytes(enc.AppendKey(e.buf, key), val)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -216,7 +248,7 @@ func (e *Event) Hex(key string, val []byte) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendHex(appendKey(e.buf, key), val)
|
||||
e.buf = enc.AppendHex(enc.AppendKey(e.buf, key), val)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -228,43 +260,58 @@ func (e *Event) RawJSON(key string, b []byte) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendJSON(appendKey(e.buf, key), b)
|
||||
e.buf = appendJSON(enc.AppendKey(e.buf, key), b)
|
||||
return e
|
||||
}
|
||||
|
||||
// AnErr adds the field key with err as a string to the *Event context.
|
||||
// AnErr adds the field key with serialized err to the *Event context.
|
||||
// If err is nil, no field is added.
|
||||
func (e *Event) AnErr(key string, err error) *Event {
|
||||
if e == nil {
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case nil:
|
||||
return e
|
||||
case LogObjectMarshaler:
|
||||
return e.Object(key, m)
|
||||
case error:
|
||||
return e.Str(key, m.Error())
|
||||
case string:
|
||||
return e.Str(key, m)
|
||||
default:
|
||||
return e.Interface(key, m)
|
||||
}
|
||||
if err != nil {
|
||||
e.buf = appendError(appendKey(e.buf, key), err)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Errs adds the field key with errs as an array of strings to the *Event context.
|
||||
// If err is nil, no field is added.
|
||||
// Errs adds the field key with errs as an array of serialized errors to the
|
||||
// *Event context.
|
||||
func (e *Event) Errs(key string, errs []error) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendErrors(appendKey(e.buf, key), errs)
|
||||
return e
|
||||
|
||||
arr := Arr()
|
||||
for _, err := range errs {
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case LogObjectMarshaler:
|
||||
arr = arr.Object(m)
|
||||
case error:
|
||||
arr = arr.Err(m)
|
||||
case string:
|
||||
arr = arr.Str(m)
|
||||
default:
|
||||
arr = arr.Interface(m)
|
||||
}
|
||||
}
|
||||
|
||||
return e.Array(key, arr)
|
||||
}
|
||||
|
||||
// Err adds the field "error" with err as a string to the *Event context.
|
||||
// Err adds the field "error" with serialized err to the *Event context.
|
||||
// If err is nil, no field is added.
|
||||
// To customize the key name, change zerolog.ErrorFieldName.
|
||||
func (e *Event) Err(err error) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
if err != nil {
|
||||
e.buf = appendError(appendKey(e.buf, ErrorFieldName), err)
|
||||
}
|
||||
return e
|
||||
return e.AnErr(ErrorFieldName, err)
|
||||
}
|
||||
|
||||
// Bool adds the field key with val as a bool to the *Event context.
|
||||
@@ -272,7 +319,7 @@ func (e *Event) Bool(key string, b bool) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendBool(appendKey(e.buf, key), b)
|
||||
e.buf = enc.AppendBool(enc.AppendKey(e.buf, key), b)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -281,7 +328,7 @@ func (e *Event) Bools(key string, b []bool) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendBools(appendKey(e.buf, key), b)
|
||||
e.buf = enc.AppendBools(enc.AppendKey(e.buf, key), b)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -290,7 +337,7 @@ func (e *Event) Int(key string, i int) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInt(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInt(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -299,7 +346,7 @@ func (e *Event) Ints(key string, i []int) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInts(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInts(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -308,7 +355,7 @@ func (e *Event) Int8(key string, i int8) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInt8(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInt8(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -317,7 +364,7 @@ func (e *Event) Ints8(key string, i []int8) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInts8(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInts8(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -326,7 +373,7 @@ func (e *Event) Int16(key string, i int16) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInt16(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInt16(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -335,7 +382,7 @@ func (e *Event) Ints16(key string, i []int16) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInts16(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInts16(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -344,7 +391,7 @@ func (e *Event) Int32(key string, i int32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInt32(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInt32(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -353,7 +400,7 @@ func (e *Event) Ints32(key string, i []int32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInts32(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInts32(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -362,7 +409,7 @@ func (e *Event) Int64(key string, i int64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInt64(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInt64(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -371,7 +418,7 @@ func (e *Event) Ints64(key string, i []int64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendInts64(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInts64(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -380,7 +427,7 @@ func (e *Event) Uint(key string, i uint) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUint(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUint(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -389,7 +436,7 @@ func (e *Event) Uints(key string, i []uint) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUints(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUints(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -398,7 +445,7 @@ func (e *Event) Uint8(key string, i uint8) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUint8(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUint8(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -407,7 +454,7 @@ func (e *Event) Uints8(key string, i []uint8) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUints8(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUints8(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -416,7 +463,7 @@ func (e *Event) Uint16(key string, i uint16) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUint16(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUint16(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -425,7 +472,7 @@ func (e *Event) Uints16(key string, i []uint16) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUints16(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUints16(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -434,7 +481,7 @@ func (e *Event) Uint32(key string, i uint32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUint32(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUint32(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -443,7 +490,7 @@ func (e *Event) Uints32(key string, i []uint32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUints32(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUints32(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -452,7 +499,7 @@ func (e *Event) Uint64(key string, i uint64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUint64(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUint64(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -461,7 +508,7 @@ func (e *Event) Uints64(key string, i []uint64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendUints64(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendUints64(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -470,7 +517,7 @@ func (e *Event) Float32(key string, f float32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendFloat32(appendKey(e.buf, key), f)
|
||||
e.buf = enc.AppendFloat32(enc.AppendKey(e.buf, key), f)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -479,7 +526,7 @@ func (e *Event) Floats32(key string, f []float32) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendFloats32(appendKey(e.buf, key), f)
|
||||
e.buf = enc.AppendFloats32(enc.AppendKey(e.buf, key), f)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -488,7 +535,7 @@ func (e *Event) Float64(key string, f float64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendFloat64(appendKey(e.buf, key), f)
|
||||
e.buf = enc.AppendFloat64(enc.AppendKey(e.buf, key), f)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -497,7 +544,7 @@ func (e *Event) Floats64(key string, f []float64) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendFloats64(appendKey(e.buf, key), f)
|
||||
e.buf = enc.AppendFloats64(enc.AppendKey(e.buf, key), f)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -510,7 +557,7 @@ func (e *Event) Timestamp() *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendTime(appendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
|
||||
e.buf = enc.AppendTime(enc.AppendKey(e.buf, TimestampFieldName), TimestampFunc(), TimeFieldFormat)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -519,7 +566,7 @@ func (e *Event) Time(key string, t time.Time) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendTime(appendKey(e.buf, key), t, TimeFieldFormat)
|
||||
e.buf = enc.AppendTime(enc.AppendKey(e.buf, key), t, TimeFieldFormat)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -528,7 +575,7 @@ func (e *Event) Times(key string, t []time.Time) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendTimes(appendKey(e.buf, key), t, TimeFieldFormat)
|
||||
e.buf = enc.AppendTimes(enc.AppendKey(e.buf, key), t, TimeFieldFormat)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -539,7 +586,7 @@ func (e *Event) Dur(key string, d time.Duration) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -550,7 +597,7 @@ func (e *Event) Durs(key string, d []time.Duration) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = appendDurations(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
e.buf = enc.AppendDurations(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -565,7 +612,7 @@ func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
|
||||
if t.After(start) {
|
||||
d = t.Sub(start)
|
||||
}
|
||||
e.buf = appendDuration(appendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
e.buf = enc.AppendDuration(enc.AppendKey(e.buf, key), d, DurationFieldUnit, DurationFieldInteger)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -577,13 +624,13 @@ func (e *Event) Interface(key string, i interface{}) *Event {
|
||||
if obj, ok := i.(LogObjectMarshaler); ok {
|
||||
return e.Object(key, obj)
|
||||
}
|
||||
e.buf = appendInterface(appendKey(e.buf, key), i)
|
||||
e.buf = enc.AppendInterface(enc.AppendKey(e.buf, key), i)
|
||||
return e
|
||||
}
|
||||
|
||||
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
||||
func (e *Event) Caller() *Event {
|
||||
return e.caller(2)
|
||||
return e.caller(CallerSkipFrameCount)
|
||||
}
|
||||
|
||||
func (e *Event) caller(skip int) *Event {
|
||||
@@ -594,6 +641,33 @@ func (e *Event) caller(skip int) *Event {
|
||||
if !ok {
|
||||
return e
|
||||
}
|
||||
e.buf = appendString(appendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
|
||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
|
||||
return e
|
||||
}
|
||||
|
||||
// IPAddr adds IPv4 or IPv6 Address to the event
|
||||
func (e *Event) IPAddr(key string, ip net.IP) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = enc.AppendIPAddr(enc.AppendKey(e.buf, key), ip)
|
||||
return e
|
||||
}
|
||||
|
||||
// IPPrefix adds IPv4 or IPv6 Prefix (address and mask) to the event
|
||||
func (e *Event) IPPrefix(key string, pfx net.IPNet) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = enc.AppendIPPrefix(enc.AppendKey(e.buf, key), pfx)
|
||||
return e
|
||||
}
|
||||
|
||||
// MACAddr adds MAC address to the event
|
||||
func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event {
|
||||
if e == nil {
|
||||
return e
|
||||
}
|
||||
e.buf = enc.AppendMACAddr(enc.AppendKey(e.buf, key), ha)
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package zerolog
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
@@ -12,114 +13,229 @@ func appendFields(dst []byte, fields map[string]interface{}) []byte {
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
dst = appendKey(dst, key)
|
||||
switch val := fields[key].(type) {
|
||||
dst = enc.AppendKey(dst, key)
|
||||
val := fields[key]
|
||||
if val, ok := val.(LogObjectMarshaler); ok {
|
||||
e := newEvent(nil, 0)
|
||||
e.buf = e.buf[:0]
|
||||
e.appendObject(val)
|
||||
dst = append(dst, e.buf...)
|
||||
putEvent(e)
|
||||
continue
|
||||
}
|
||||
switch val := val.(type) {
|
||||
case string:
|
||||
dst = appendString(dst, val)
|
||||
dst = enc.AppendString(dst, val)
|
||||
case []byte:
|
||||
dst = appendBytes(dst, val)
|
||||
dst = enc.AppendBytes(dst, val)
|
||||
case error:
|
||||
dst = appendError(dst, val)
|
||||
marshaled := ErrorMarshalFunc(val)
|
||||
switch m := marshaled.(type) {
|
||||
case LogObjectMarshaler:
|
||||
e := newEvent(nil, 0)
|
||||
e.buf = e.buf[:0]
|
||||
e.appendObject(m)
|
||||
dst = append(dst, e.buf...)
|
||||
putEvent(e)
|
||||
case error:
|
||||
dst = enc.AppendString(dst, m.Error())
|
||||
case string:
|
||||
dst = enc.AppendString(dst, m)
|
||||
default:
|
||||
dst = enc.AppendInterface(dst, m)
|
||||
}
|
||||
case []error:
|
||||
dst = appendErrors(dst, val)
|
||||
dst = enc.AppendArrayStart(dst)
|
||||
for i, err := range val {
|
||||
marshaled := ErrorMarshalFunc(err)
|
||||
switch m := marshaled.(type) {
|
||||
case LogObjectMarshaler:
|
||||
e := newEvent(nil, 0)
|
||||
e.buf = e.buf[:0]
|
||||
e.appendObject(m)
|
||||
dst = append(dst, e.buf...)
|
||||
putEvent(e)
|
||||
case error:
|
||||
dst = enc.AppendString(dst, m.Error())
|
||||
case string:
|
||||
dst = enc.AppendString(dst, m)
|
||||
default:
|
||||
dst = enc.AppendInterface(dst, m)
|
||||
}
|
||||
|
||||
if i < (len(val) - 1) {
|
||||
enc.AppendArrayDelim(dst)
|
||||
}
|
||||
}
|
||||
dst = enc.AppendArrayEnd(dst)
|
||||
case bool:
|
||||
dst = appendBool(dst, val)
|
||||
dst = enc.AppendBool(dst, val)
|
||||
case int:
|
||||
dst = appendInt(dst, val)
|
||||
dst = enc.AppendInt(dst, val)
|
||||
case int8:
|
||||
dst = appendInt8(dst, val)
|
||||
dst = enc.AppendInt8(dst, val)
|
||||
case int16:
|
||||
dst = appendInt16(dst, val)
|
||||
dst = enc.AppendInt16(dst, val)
|
||||
case int32:
|
||||
dst = appendInt32(dst, val)
|
||||
dst = enc.AppendInt32(dst, val)
|
||||
case int64:
|
||||
dst = appendInt64(dst, val)
|
||||
dst = enc.AppendInt64(dst, val)
|
||||
case uint:
|
||||
dst = appendUint(dst, val)
|
||||
dst = enc.AppendUint(dst, val)
|
||||
case uint8:
|
||||
dst = appendUint8(dst, val)
|
||||
dst = enc.AppendUint8(dst, val)
|
||||
case uint16:
|
||||
dst = appendUint16(dst, val)
|
||||
dst = enc.AppendUint16(dst, val)
|
||||
case uint32:
|
||||
dst = appendUint32(dst, val)
|
||||
dst = enc.AppendUint32(dst, val)
|
||||
case uint64:
|
||||
dst = appendUint64(dst, val)
|
||||
dst = enc.AppendUint64(dst, val)
|
||||
case float32:
|
||||
dst = appendFloat32(dst, val)
|
||||
dst = enc.AppendFloat32(dst, val)
|
||||
case float64:
|
||||
dst = appendFloat64(dst, val)
|
||||
dst = enc.AppendFloat64(dst, val)
|
||||
case time.Time:
|
||||
dst = appendTime(dst, val, TimeFieldFormat)
|
||||
dst = enc.AppendTime(dst, val, TimeFieldFormat)
|
||||
case time.Duration:
|
||||
dst = appendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
|
||||
dst = enc.AppendDuration(dst, val, DurationFieldUnit, DurationFieldInteger)
|
||||
case *string:
|
||||
dst = appendString(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendString(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *bool:
|
||||
dst = appendBool(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendBool(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *int:
|
||||
dst = appendInt(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendInt(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *int8:
|
||||
dst = appendInt8(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendInt8(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *int16:
|
||||
dst = appendInt16(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendInt16(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *int32:
|
||||
dst = appendInt32(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendInt32(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *int64:
|
||||
dst = appendInt64(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendInt64(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *uint:
|
||||
dst = appendUint(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendUint(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *uint8:
|
||||
dst = appendUint8(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendUint8(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *uint16:
|
||||
dst = appendUint16(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendUint16(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *uint32:
|
||||
dst = appendUint32(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendUint32(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *uint64:
|
||||
dst = appendUint64(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendUint64(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *float32:
|
||||
dst = appendFloat32(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendFloat32(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *float64:
|
||||
dst = appendFloat64(dst, *val)
|
||||
if val != nil {
|
||||
dst = enc.AppendFloat64(dst, *val)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *time.Time:
|
||||
dst = appendTime(dst, *val, TimeFieldFormat)
|
||||
if val != nil {
|
||||
dst = enc.AppendTime(dst, *val, TimeFieldFormat)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case *time.Duration:
|
||||
dst = appendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
|
||||
if val != nil {
|
||||
dst = enc.AppendDuration(dst, *val, DurationFieldUnit, DurationFieldInteger)
|
||||
} else {
|
||||
dst = enc.AppendNil(dst)
|
||||
}
|
||||
case []string:
|
||||
dst = appendStrings(dst, val)
|
||||
dst = enc.AppendStrings(dst, val)
|
||||
case []bool:
|
||||
dst = appendBools(dst, val)
|
||||
dst = enc.AppendBools(dst, val)
|
||||
case []int:
|
||||
dst = appendInts(dst, val)
|
||||
dst = enc.AppendInts(dst, val)
|
||||
case []int8:
|
||||
dst = appendInts8(dst, val)
|
||||
dst = enc.AppendInts8(dst, val)
|
||||
case []int16:
|
||||
dst = appendInts16(dst, val)
|
||||
dst = enc.AppendInts16(dst, val)
|
||||
case []int32:
|
||||
dst = appendInts32(dst, val)
|
||||
dst = enc.AppendInts32(dst, val)
|
||||
case []int64:
|
||||
dst = appendInts64(dst, val)
|
||||
dst = enc.AppendInts64(dst, val)
|
||||
case []uint:
|
||||
dst = appendUints(dst, val)
|
||||
dst = enc.AppendUints(dst, val)
|
||||
// case []uint8:
|
||||
// dst = appendUints8(dst, val)
|
||||
// dst = enc.AppendUints8(dst, val)
|
||||
case []uint16:
|
||||
dst = appendUints16(dst, val)
|
||||
dst = enc.AppendUints16(dst, val)
|
||||
case []uint32:
|
||||
dst = appendUints32(dst, val)
|
||||
dst = enc.AppendUints32(dst, val)
|
||||
case []uint64:
|
||||
dst = appendUints64(dst, val)
|
||||
dst = enc.AppendUints64(dst, val)
|
||||
case []float32:
|
||||
dst = appendFloats32(dst, val)
|
||||
dst = enc.AppendFloats32(dst, val)
|
||||
case []float64:
|
||||
dst = appendFloats64(dst, val)
|
||||
dst = enc.AppendFloats64(dst, val)
|
||||
case []time.Time:
|
||||
dst = appendTimes(dst, val, TimeFieldFormat)
|
||||
dst = enc.AppendTimes(dst, val, TimeFieldFormat)
|
||||
case []time.Duration:
|
||||
dst = appendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
|
||||
dst = enc.AppendDurations(dst, val, DurationFieldUnit, DurationFieldInteger)
|
||||
case nil:
|
||||
dst = appendNil(dst)
|
||||
dst = enc.AppendNil(dst)
|
||||
case net.IP:
|
||||
dst = enc.AppendIPAddr(dst, val)
|
||||
case net.IPNet:
|
||||
dst = enc.AppendIPPrefix(dst, val)
|
||||
case net.HardwareAddr:
|
||||
dst = enc.AppendMACAddr(dst, val)
|
||||
default:
|
||||
dst = appendInterface(dst, val)
|
||||
dst = enc.AppendInterface(dst, val)
|
||||
}
|
||||
}
|
||||
return dst
|
||||
|
||||
+5
-1
@@ -19,6 +19,9 @@ var (
|
||||
// CallerFieldName is the field name used for caller field.
|
||||
CallerFieldName = "caller"
|
||||
|
||||
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
|
||||
CallerSkipFrameCount = 2
|
||||
|
||||
// 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.
|
||||
@@ -49,7 +52,8 @@ func SetGlobalLevel(l Level) {
|
||||
atomic.StoreUint32(gLevel, uint32(l))
|
||||
}
|
||||
|
||||
func globalLevel() Level {
|
||||
// GlobalLevel returns the current global log level
|
||||
func GlobalLevel() Level {
|
||||
return Level(atomic.LoadUint32(gLevel))
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -129,7 +129,12 @@ 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
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,15 @@ type Hook interface {
|
||||
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, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
|
||||
|
||||
+125
-194
@@ -6,204 +6,135 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
type LevelNameHook struct{}
|
||||
|
||||
func (h LevelNameHook) Run(e *Event, level Level, msg string) {
|
||||
levelName := level.String()
|
||||
if level == NoLevel {
|
||||
levelName = "nolevel"
|
||||
}
|
||||
e.Str("level_name", levelName)
|
||||
}
|
||||
|
||||
type SimpleHook struct{}
|
||||
|
||||
func (h SimpleHook) Run(e *Event, level Level, msg string) {
|
||||
e.Bool("has_level", level != NoLevel)
|
||||
e.Str("test", "logged")
|
||||
}
|
||||
|
||||
type CopyHook struct{}
|
||||
|
||||
func (h CopyHook) Run(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)
|
||||
}
|
||||
|
||||
type NopHook struct{}
|
||||
|
||||
func (h NopHook) Run(e *Event, level Level, msg string) {
|
||||
}
|
||||
|
||||
var (
|
||||
levelNameHook LevelNameHook
|
||||
simpleHook SimpleHook
|
||||
copyHook CopyHook
|
||||
nopHook NopHook
|
||||
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()
|
||||
})
|
||||
)
|
||||
|
||||
func TestHook(t *testing.T) {
|
||||
t.Run("Message", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook)
|
||||
log.Log().Msg("test message")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level_name":"nolevel","message":"test message"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("NoLevel", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook)
|
||||
log.Log().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level_name":"nolevel"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Print", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook)
|
||||
log.Print("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"debug","level_name":"debug"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Error", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Copy/1", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(copyHook)
|
||||
log.Log().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"copy_has_level":false,"copy_msg":""}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Copy/2", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(copyHook)
|
||||
log.Info().Msg("a message")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"info","copy_has_level":true,"copy_level":"info","copy_msg":"a message","message":"a message"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Multi", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook).Hook(simpleHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Multi/Message", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook).Hook(simpleHook)
|
||||
log.Error().Msg("a message")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error","has_level":true,"test":"logged","message":"a message"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Output/single/pre", func(t *testing.T) {
|
||||
ignored := &bytes.Buffer{}
|
||||
out := &bytes.Buffer{}
|
||||
log := New(ignored).Hook(levelNameHook).Output(out)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Output/single/post", func(t *testing.T) {
|
||||
ignored := &bytes.Buffer{}
|
||||
out := &bytes.Buffer{}
|
||||
log := New(ignored).Output(out).Hook(levelNameHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Output/multi/pre", func(t *testing.T) {
|
||||
ignored := &bytes.Buffer{}
|
||||
out := &bytes.Buffer{}
|
||||
log := New(ignored).Hook(levelNameHook).Hook(simpleHook).Output(out)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Output/multi/post", func(t *testing.T) {
|
||||
ignored := &bytes.Buffer{}
|
||||
out := &bytes.Buffer{}
|
||||
log := New(ignored).Output(out).Hook(levelNameHook).Hook(simpleHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("Output/mixed", func(t *testing.T) {
|
||||
ignored := &bytes.Buffer{}
|
||||
out := &bytes.Buffer{}
|
||||
log := New(ignored).Hook(levelNameHook).Output(out).Hook(simpleHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("With/single/pre", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook).With().Str("with", "pre").Logger()
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","with":"pre","level_name":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("With/single/post", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).With().Str("with", "post").Logger().Hook(levelNameHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","with":"post","level_name":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("With/multi/pre", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook).Hook(simpleHook).With().Str("with", "pre").Logger()
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","with":"pre","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("With/multi/post", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).With().Str("with", "post").Logger().Hook(levelNameHook).Hook(simpleHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","with":"post","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("With/mixed", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out).Hook(levelNameHook).With().Str("with", "mixed").Logger().Hook(simpleHook)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error","with":"mixed","level_name":"error","has_level":true,"test":"logged"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
t.Run("None", func(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out)
|
||||
log.Error().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"error"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
})
|
||||
tests := []struct {
|
||||
name string
|
||||
want string
|
||||
test func(log Logger)
|
||||
}{
|
||||
{"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")
|
||||
}},
|
||||
{"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 BenchmarkHooks(b *testing.B) {
|
||||
|
||||
+52
-70
@@ -1,74 +1,56 @@
|
||||
Reference:
|
||||
CBOR Encoding is described in RFC7049 https://tools.ietf.org/html/rfc7049
|
||||
## 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.
|
||||
|
||||
|
||||
Tests and benchmark:
|
||||
|
||||
CPU Usage savings are below:
|
||||
```
|
||||
sprint @ cbor>go test -v -benchmem -bench=.
|
||||
=== RUN TestDecodeInteger
|
||||
--- PASS: TestDecodeInteger (0.00s)
|
||||
=== RUN TestDecodeString
|
||||
--- PASS: TestDecodeString (0.00s)
|
||||
=== RUN TestDecodeArray
|
||||
--- PASS: TestDecodeArray (0.00s)
|
||||
=== RUN TestDecodeMap
|
||||
--- PASS: TestDecodeMap (0.00s)
|
||||
=== RUN TestDecodeBool
|
||||
--- PASS: TestDecodeBool (0.00s)
|
||||
=== RUN TestDecodeFloat
|
||||
--- PASS: TestDecodeFloat (0.00s)
|
||||
=== RUN TestDecodeTimestamp
|
||||
--- PASS: TestDecodeTimestamp (0.00s)
|
||||
=== RUN TestDecodeCbor2Json
|
||||
--- PASS: TestDecodeCbor2Json (0.00s)
|
||||
=== RUN TestAppendString
|
||||
--- PASS: TestAppendString (0.00s)
|
||||
=== RUN TestAppendBytes
|
||||
--- PASS: TestAppendBytes (0.00s)
|
||||
=== RUN TestAppendTimeNow
|
||||
--- PASS: TestAppendTimeNow (0.00s)
|
||||
=== RUN TestAppendTimePastPresentInteger
|
||||
--- PASS: TestAppendTimePastPresentInteger (0.00s)
|
||||
=== RUN TestAppendTimePastPresentFloat
|
||||
--- PASS: TestAppendTimePastPresentFloat (0.00s)
|
||||
=== RUN TestAppendNull
|
||||
--- PASS: TestAppendNull (0.00s)
|
||||
=== RUN TestAppendBool
|
||||
--- PASS: TestAppendBool (0.00s)
|
||||
=== RUN TestAppendBoolArray
|
||||
--- PASS: TestAppendBoolArray (0.00s)
|
||||
=== RUN TestAppendInt
|
||||
--- PASS: TestAppendInt (0.00s)
|
||||
=== RUN TestAppendIntArray
|
||||
--- PASS: TestAppendIntArray (0.00s)
|
||||
=== RUN TestAppendFloat32
|
||||
--- PASS: TestAppendFloat32 (0.00s)
|
||||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: github.com/toravir/zerolog/internal/cbor
|
||||
BenchmarkAppendString/MultiBytesLast-4 30000000 43.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/NoEncoding-4 30000000 48.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/EncodingFirst-4 30000000 48.2 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/EncodingMiddle-4 30000000 41.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/EncodingLast-4 30000000 51.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/MultiBytesFirst-4 50000000 38.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendString/MultiBytesMiddle-4 50000000 38.0 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendTime/Integer-4 50000000 39.6 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendTime/Float-4 30000000 56.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/uint8-4 50000000 29.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/uint16-4 50000000 30.3 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/uint32-4 50000000 37.1 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int8-4 100000000 21.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int16-4 50000000 25.8 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int32-4 50000000 26.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int-Positive-4 100000000 21.5 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int-Negative-4 100000000 20.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/uint64-4 50000000 36.7 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendInt/int64-4 30000000 39.6 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendFloat/Float32-4 50000000 23.9 ns/op 0 B/op 0 allocs/op
|
||||
BenchmarkAppendFloat/Float64-4 50000000 32.8 ns/op 0 B/op 0 allocs/op
|
||||
PASS
|
||||
ok github.com/toravir/zerolog/internal/cbor 34.969s
|
||||
sprint @ cbor>
|
||||
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).
|
||||
|
||||
+6
-38
@@ -1,43 +1,11 @@
|
||||
package cbor
|
||||
|
||||
type Encoder struct{}
|
||||
|
||||
// AppendKey adds a key (string) to the binary encoded log message
|
||||
func AppendKey(dst []byte, key string) []byte {
|
||||
func (e Encoder) AppendKey(dst []byte, key string) []byte {
|
||||
if len(dst) < 1 {
|
||||
dst = AppendBeginMarker(dst)
|
||||
dst = e.AppendBeginMarker(dst)
|
||||
}
|
||||
return AppendString(dst, key)
|
||||
}
|
||||
|
||||
// AppendError adds the Error to the log message if error is NOT nil
|
||||
func AppendError(dst []byte, err error) []byte {
|
||||
if err == nil {
|
||||
return append(dst, `null`...)
|
||||
}
|
||||
return AppendString(dst, err.Error())
|
||||
}
|
||||
|
||||
// AppendErrors when given an array of errors,
|
||||
// adds them to the log message if a specific error is nil, then
|
||||
// Nil is added, or else the error string is added.
|
||||
func AppendErrors(dst []byte, errs []error) []byte {
|
||||
if len(errs) == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
}
|
||||
dst = AppendArrayStart(dst)
|
||||
if errs[0] != nil {
|
||||
dst = AppendString(dst, errs[0].Error())
|
||||
} else {
|
||||
dst = AppendNull(dst)
|
||||
}
|
||||
if len(errs) > 1 {
|
||||
for _, err := range errs[1:] {
|
||||
if err == nil {
|
||||
dst = AppendNull(dst)
|
||||
continue
|
||||
}
|
||||
dst = AppendString(dst, err.Error())
|
||||
}
|
||||
}
|
||||
dst = AppendArrayEnd(dst)
|
||||
return dst
|
||||
}
|
||||
return e.AppendString(dst, key)
|
||||
}
|
||||
+17
-8
@@ -7,25 +7,34 @@ import "time"
|
||||
const (
|
||||
majorOffset = 5
|
||||
additionalMax = 23
|
||||
//Non Values
|
||||
|
||||
// Non Values.
|
||||
additionalTypeBoolFalse byte = 20
|
||||
additionalTypeBoolTrue byte = 21
|
||||
additionalTypeNull byte = 22
|
||||
//Integer (+ve and -ve) Sub-types
|
||||
|
||||
// Integer (+ve and -ve) Sub-types.
|
||||
additionalTypeIntUint8 byte = 24
|
||||
additionalTypeIntUint16 byte = 25
|
||||
additionalTypeIntUint32 byte = 26
|
||||
additionalTypeIntUint64 byte = 27
|
||||
//Float Sub-types
|
||||
|
||||
// Float Sub-types.
|
||||
additionalTypeFloat16 byte = 25
|
||||
additionalTypeFloat32 byte = 26
|
||||
additionalTypeFloat64 byte = 27
|
||||
additionalTypeBreak byte = 31
|
||||
//Tag Sub-types
|
||||
additionalTypeTimestamp byte = 01
|
||||
additionalTypeEmbeddedJSON byte = 31
|
||||
additionalTypeTagHexString uint16 = 262
|
||||
//Unspecified number of elements
|
||||
|
||||
// Tag Sub-types.
|
||||
additionalTypeTimestamp byte = 01
|
||||
|
||||
// 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 (
|
||||
|
||||
@@ -0,0 +1,614 @@
|
||||
package cbor
|
||||
|
||||
// This file contains code to decode a stream of CBOR Data into JSON.
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"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 decodeIntAdditonalType(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 := decodeIntAdditonalType(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 suppported 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 := decodeIntAdditonalType(src, minor)
|
||||
len := int(length)
|
||||
pbs := readNBytes(src, len)
|
||||
result = append(result, pbs...)
|
||||
if noQuotes {
|
||||
return result
|
||||
}
|
||||
return append(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 := decodeIntAdditonalType(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 an 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 := decodeIntAdditonalType(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] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||
readByte(src)
|
||||
break
|
||||
}
|
||||
}
|
||||
cbor2JsonOneObject(src, dst)
|
||||
if unSpecifiedCount {
|
||||
pb, e := src.Peek(1)
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if pb[0] == byte(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 := decodeIntAdditonalType(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] == byte(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] == byte(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)
|
||||
|
||||
// Tag value is larger than 256 (so uint16).
|
||||
case additionalTypeIntUint16:
|
||||
val := decodeIntAdditonalType(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 != byte(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 neigther 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
|
||||
}
|
||||
@@ -1,548 +0,0 @@
|
||||
package cbor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var decodeTimeZone *time.Location
|
||||
|
||||
const hexTable = "0123456789abcdef"
|
||||
|
||||
func decodeIntAdditonalType(src []byte, minor byte) (int64, uint, error) {
|
||||
val := int64(0)
|
||||
bytesRead := 0
|
||||
if minor <= 23 {
|
||||
val = int64(minor)
|
||||
bytesRead = 0
|
||||
} else {
|
||||
switch minor {
|
||||
case additionalTypeIntUint8:
|
||||
bytesRead = 1
|
||||
case additionalTypeIntUint16:
|
||||
bytesRead = 2
|
||||
case additionalTypeIntUint32:
|
||||
bytesRead = 4
|
||||
case additionalTypeIntUint64:
|
||||
bytesRead = 8
|
||||
default:
|
||||
return 0, 0, fmt.Errorf("Invalid Additional Type: %d in decodeInteger (expected <28)", minor)
|
||||
}
|
||||
for i := 0; i < bytesRead; i++ {
|
||||
val = val * 256
|
||||
val += int64(src[i])
|
||||
}
|
||||
}
|
||||
return val, uint(bytesRead), nil
|
||||
}
|
||||
|
||||
func decodeInteger(src []byte) (int64, uint, error) {
|
||||
major := src[0] & maskOutAdditionalType
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeUnsignedInt && major != majorTypeNegativeInt {
|
||||
return 0, 0, fmt.Errorf("Major type is: %d in decodeInteger!! (expected 0 or 1)", major)
|
||||
}
|
||||
val, bytesRead, err := decodeIntAdditonalType(src[1:], minor)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
if major == 0 {
|
||||
return val, 1 + bytesRead, nil
|
||||
}
|
||||
return (-1 - val), 1 + bytesRead, nil
|
||||
}
|
||||
|
||||
func decodeFloat(src []byte) (float64, uint, error) {
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeSimpleAndFloat {
|
||||
return 0, 0, fmt.Errorf("Incorrect Major type is: %d in decodeFloat", major)
|
||||
}
|
||||
|
||||
switch minor {
|
||||
case additionalTypeFloat16:
|
||||
return 0, 0, fmt.Errorf("float16 is not suppported in decodeFloat")
|
||||
case additionalTypeFloat32:
|
||||
switch string(src[1:5]) {
|
||||
case float32Nan:
|
||||
return math.NaN(), 5, nil
|
||||
case float32PosInfinity:
|
||||
return math.Inf(0), 5, nil
|
||||
case float32NegInfinity:
|
||||
return math.Inf(-1), 5, nil
|
||||
}
|
||||
n := uint32(0)
|
||||
for i := 0; i < 4; i++ {
|
||||
n = n * 256
|
||||
n += uint32(src[i+1])
|
||||
}
|
||||
val := math.Float32frombits(n)
|
||||
return float64(val), 5, nil
|
||||
case additionalTypeFloat64:
|
||||
switch string(src[1:9]) {
|
||||
case float64Nan:
|
||||
return math.NaN(), 9, nil
|
||||
case float64PosInfinity:
|
||||
return math.Inf(0), 9, nil
|
||||
case float64NegInfinity:
|
||||
return math.Inf(-1), 9, nil
|
||||
}
|
||||
n := uint64(0)
|
||||
for i := 0; i < 8; i++ {
|
||||
n = n * 256
|
||||
n += uint64(src[i+1])
|
||||
}
|
||||
val := math.Float64frombits(n)
|
||||
return val, 9, nil
|
||||
}
|
||||
return 0, 0, fmt.Errorf("Invalid Additional Type: %d in decodeFloat", minor)
|
||||
}
|
||||
|
||||
func decodeStringComplex(dst []byte, s string, pos uint) []byte {
|
||||
i := int(pos)
|
||||
const hex = "0123456789abcdef"
|
||||
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', hex[b>>4], hex[b&0xF])
|
||||
}
|
||||
i++
|
||||
start = i
|
||||
}
|
||||
if start < len(s) {
|
||||
dst = append(dst, s[start:]...)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
func decodeString(src []byte, noQuotes bool) ([]byte, uint, error) {
|
||||
major := src[0] & maskOutAdditionalType
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeByteString {
|
||||
return []byte{}, 0, fmt.Errorf("Major type is: %d in decodeString", major)
|
||||
}
|
||||
result := []byte{'"'}
|
||||
if noQuotes {
|
||||
result = []byte{}
|
||||
}
|
||||
length, bytesRead, err := decodeIntAdditonalType(src[1:], minor)
|
||||
if err != nil {
|
||||
return []byte{}, 0, err
|
||||
}
|
||||
bytesRead++
|
||||
st := bytesRead
|
||||
len := uint(length)
|
||||
bytesRead += len
|
||||
|
||||
result = append(result, src[st:st+len]...)
|
||||
if noQuotes {
|
||||
return result, bytesRead, nil
|
||||
}
|
||||
return append(result, '"'), bytesRead, nil
|
||||
}
|
||||
|
||||
func decodeUTF8String(src []byte) ([]byte, uint, error) {
|
||||
major := src[0] & maskOutAdditionalType
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeUtf8String {
|
||||
return []byte{}, 0, fmt.Errorf("Major type is: %d in decodeUTF8String", major)
|
||||
}
|
||||
result := []byte{'"'}
|
||||
length, bytesRead, err := decodeIntAdditonalType(src[1:], minor)
|
||||
if err != nil {
|
||||
return []byte{}, 0, err
|
||||
}
|
||||
bytesRead++
|
||||
st := bytesRead
|
||||
len := uint(length)
|
||||
bytesRead += len
|
||||
|
||||
for i := st; i < bytesRead; 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 src[i] < 0x20 || src[i] > 0x7e || src[i] == '\\' || src[i] == '"' {
|
||||
// We encountered a character that needs to be encoded. Switch
|
||||
// to complex version of the algorithm.
|
||||
dst := []byte{'"'}
|
||||
dst = decodeStringComplex(dst, string(src[st:st+len]), i-st)
|
||||
return append(dst, '"'), bytesRead, nil
|
||||
}
|
||||
}
|
||||
// The string has no need for encoding an therefore is directly
|
||||
// appended to the byte slice.
|
||||
result = append(result, src[st:st+len]...)
|
||||
return append(result, '"'), bytesRead, nil
|
||||
}
|
||||
|
||||
func array2Json(src []byte, dst io.Writer) (uint, error) {
|
||||
dst.Write([]byte{'['})
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeArray {
|
||||
return 0, fmt.Errorf("Major type is: %d in array2Json", major)
|
||||
}
|
||||
len := 0
|
||||
bytesRead := uint(0)
|
||||
unSpecifiedCount := false
|
||||
if minor == additionalTypeInfiniteCount {
|
||||
unSpecifiedCount = true
|
||||
bytesRead = 1
|
||||
} else {
|
||||
var length int64
|
||||
var err error
|
||||
length, bytesRead, err = decodeIntAdditonalType(src[1:], minor)
|
||||
if err != nil {
|
||||
fmt.Println("Error!!!")
|
||||
return 0, err
|
||||
}
|
||||
len = int(length)
|
||||
bytesRead++
|
||||
}
|
||||
curPos := bytesRead
|
||||
for i := 0; unSpecifiedCount || i < len; i++ {
|
||||
bc, err := Cbor2JsonOneObject(src[curPos:], dst)
|
||||
if err != nil {
|
||||
if src[curPos] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||
bytesRead++
|
||||
break
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
curPos += bc
|
||||
bytesRead += bc
|
||||
if unSpecifiedCount {
|
||||
if src[curPos] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||
bytesRead++
|
||||
break
|
||||
}
|
||||
dst.Write([]byte{','})
|
||||
} else if i+1 < len {
|
||||
dst.Write([]byte{','})
|
||||
}
|
||||
}
|
||||
dst.Write([]byte{']'})
|
||||
return bytesRead, nil
|
||||
}
|
||||
|
||||
func map2Json(src []byte, dst io.Writer) (uint, error) {
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeMap {
|
||||
return 0, fmt.Errorf("Major type is: %d in map2Json", major)
|
||||
}
|
||||
len := 0
|
||||
bytesRead := uint(0)
|
||||
unSpecifiedCount := false
|
||||
if minor == additionalTypeInfiniteCount {
|
||||
unSpecifiedCount = true
|
||||
bytesRead = 1
|
||||
} else {
|
||||
var length int64
|
||||
var err error
|
||||
length, bytesRead, err = decodeIntAdditonalType(src[1:], minor)
|
||||
if err != nil {
|
||||
fmt.Println("Error!!!")
|
||||
return 0, err
|
||||
}
|
||||
len = int(length)
|
||||
bytesRead++
|
||||
}
|
||||
if len%2 == 1 {
|
||||
return 0, fmt.Errorf("Invalid Length of map %d - has to be even", len)
|
||||
}
|
||||
dst.Write([]byte{'{'})
|
||||
curPos := bytesRead
|
||||
for i := 0; unSpecifiedCount || i < len; i++ {
|
||||
bc, err := Cbor2JsonOneObject(src[curPos:], dst)
|
||||
if err != nil {
|
||||
//We hit the BREAK
|
||||
if src[curPos] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||
bytesRead++
|
||||
break
|
||||
}
|
||||
return 0, err
|
||||
}
|
||||
curPos += bc
|
||||
bytesRead += bc
|
||||
if i%2 == 0 {
|
||||
//Even position values are keys
|
||||
dst.Write([]byte{':'})
|
||||
} else {
|
||||
if unSpecifiedCount {
|
||||
if src[curPos] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||
bytesRead++
|
||||
break
|
||||
}
|
||||
dst.Write([]byte{','})
|
||||
} else if i+1 < len {
|
||||
dst.Write([]byte{','})
|
||||
}
|
||||
}
|
||||
}
|
||||
dst.Write([]byte{'}'})
|
||||
return bytesRead, nil
|
||||
}
|
||||
|
||||
func decodeTagData(src []byte) ([]byte, uint, error) {
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeTags {
|
||||
return nil, 0, fmt.Errorf("Major type is: %d in decodeTagData", major)
|
||||
}
|
||||
if minor == additionalTypeTimestamp {
|
||||
tsMajor := src[1] & maskOutAdditionalType
|
||||
if tsMajor == majorTypeUnsignedInt || tsMajor == majorTypeNegativeInt {
|
||||
n, bc, err := decodeInteger(src[1:])
|
||||
if err != nil {
|
||||
return []byte{}, 0, err
|
||||
}
|
||||
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, 1 + bc, nil
|
||||
} else if tsMajor == majorTypeSimpleAndFloat {
|
||||
n, bc, err := decodeFloat(src[1:])
|
||||
if err != nil {
|
||||
return []byte{}, 0, err
|
||||
}
|
||||
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, 1 + bc, nil
|
||||
} else {
|
||||
return nil, 0, fmt.Errorf("TS format is neigther int nor float: %d", tsMajor)
|
||||
}
|
||||
} else if minor == additionalTypeEmbeddedJSON {
|
||||
dataMajor := src[1] & maskOutAdditionalType
|
||||
if dataMajor == majorTypeByteString {
|
||||
emb, bc, err := decodeString(src[1:], true)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return emb, 1 + bc, nil
|
||||
}
|
||||
return nil, 0, fmt.Errorf("Unsupported embedded Type: %d in decodeEmbeddedJSON", dataMajor)
|
||||
} else if minor == additionalTypeIntUint16 {
|
||||
val,_,_ := decodeIntAdditonalType(src[1:], minor)
|
||||
if uint16(val) == additionalTypeTagHexString {
|
||||
emb, bc, _ := decodeString(src[3:], true)
|
||||
dst := []byte{'"'}
|
||||
for _, v := range emb {
|
||||
dst = append(dst, hexTable[v>>4], hexTable[v&0x0f])
|
||||
}
|
||||
return append(dst, '"'), 3+bc, nil
|
||||
}
|
||||
}
|
||||
return nil, 0, fmt.Errorf("Unsupported Additional Type: %d in decodeTagData", minor)
|
||||
}
|
||||
|
||||
func decodeSimpleFloat(src []byte) ([]byte, uint, error) {
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
minor := src[0] & maskOutMajorType
|
||||
if major != majorTypeSimpleAndFloat {
|
||||
return nil, 0, fmt.Errorf("Major type is: %d in decodeSimpleFloat", major)
|
||||
}
|
||||
switch minor {
|
||||
case additionalTypeBoolTrue:
|
||||
return []byte("true"), 1, nil
|
||||
case additionalTypeBoolFalse:
|
||||
return []byte("false"), 1, nil
|
||||
case additionalTypeNull:
|
||||
return []byte("null"), 1, nil
|
||||
|
||||
case additionalTypeFloat16:
|
||||
fallthrough
|
||||
case additionalTypeFloat32:
|
||||
fallthrough
|
||||
case additionalTypeFloat64:
|
||||
v, bc, err := decodeFloat(src)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
ba := []byte{}
|
||||
switch {
|
||||
case math.IsNaN(v):
|
||||
return []byte("\"NaN\""), bc, nil
|
||||
case math.IsInf(v, 1):
|
||||
return []byte("\"+Inf\""), bc, nil
|
||||
case math.IsInf(v, -1):
|
||||
return []byte("\"-Inf\""), bc, nil
|
||||
}
|
||||
if bc == 5 {
|
||||
ba = strconv.AppendFloat(ba, v, 'f', -1, 32)
|
||||
} else {
|
||||
ba = strconv.AppendFloat(ba, v, 'f', -1, 64)
|
||||
}
|
||||
return ba, bc, nil
|
||||
default:
|
||||
return nil, 0, fmt.Errorf("Invalid Additional Type: %d in decodeSimpleFloat", minor)
|
||||
}
|
||||
}
|
||||
|
||||
// Cbor2JsonOneObject takes in byte array and decodes ONE CBOR Object
|
||||
// usually a MAP. Use this when only ONE CBOR object needs decoding.
|
||||
// Decoded string is written to the dst.
|
||||
// Returns the bytes decoded and if any error was encountered.
|
||||
func Cbor2JsonOneObject(src []byte, dst io.Writer) (uint, error) {
|
||||
var err error
|
||||
major := (src[0] & maskOutAdditionalType)
|
||||
bc := uint(0)
|
||||
var s []byte
|
||||
switch major {
|
||||
case majorTypeUnsignedInt:
|
||||
fallthrough
|
||||
case majorTypeNegativeInt:
|
||||
var n int64
|
||||
n, bc, err = decodeInteger(src)
|
||||
dst.Write([]byte(strconv.Itoa(int(n))))
|
||||
|
||||
case majorTypeByteString:
|
||||
s, bc, err = decodeString(src, false)
|
||||
dst.Write(s)
|
||||
|
||||
case majorTypeUtf8String:
|
||||
s, bc, err = decodeUTF8String(src)
|
||||
dst.Write(s)
|
||||
|
||||
case majorTypeArray:
|
||||
bc, err = array2Json(src, dst)
|
||||
|
||||
case majorTypeMap:
|
||||
bc, err = map2Json(src, dst)
|
||||
|
||||
case majorTypeTags:
|
||||
s, bc, err = decodeTagData(src)
|
||||
dst.Write(s)
|
||||
|
||||
case majorTypeSimpleAndFloat:
|
||||
s, bc, err = decodeSimpleFloat(src)
|
||||
dst.Write(s)
|
||||
}
|
||||
return bc, err
|
||||
}
|
||||
|
||||
// Cbor2JsonManyObjects decodes all the CBOR Objects present in the
|
||||
// source byte array. It keeps on decoding until it runs out of bytes.
|
||||
// Decoded string is written to the dst. At the end of every CBOR Object
|
||||
// newline is written to the output stream.
|
||||
// Returns the number of bytes decoded and if any error was encountered.
|
||||
func Cbor2JsonManyObjects(src []byte, dst io.Writer) (uint, error) {
|
||||
curPos := uint(0)
|
||||
totalBytes := uint(len(src))
|
||||
for curPos < totalBytes {
|
||||
bc, err := Cbor2JsonOneObject(src[curPos:], dst)
|
||||
if err != nil {
|
||||
return curPos, err
|
||||
}
|
||||
dst.Write([]byte("\n"))
|
||||
curPos += bc
|
||||
}
|
||||
return curPos, 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
|
||||
}
|
||||
|
||||
// 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(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(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(in, &b)
|
||||
return b.Bytes()
|
||||
}
|
||||
return in
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
func TestDecodeInteger(t *testing.T) {
|
||||
for _, tc := range integerTestCases {
|
||||
gotv, gotc, err := decodeInteger([]byte(tc.binary))
|
||||
if gotv != int64(tc.val) || int(gotc) != len(tc.binary) || err != nil {
|
||||
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)
|
||||
}
|
||||
@@ -19,10 +19,7 @@ func TestDecodeInteger(t *testing.T) {
|
||||
|
||||
func TestDecodeString(t *testing.T) {
|
||||
for _, tt := range encodeStringTests {
|
||||
got, _, err := decodeUTF8String([]byte(tt.binary))
|
||||
if err != nil {
|
||||
t.Errorf("Got Error for the case: %s", hex.EncodeToString([]byte(tt.binary)))
|
||||
}
|
||||
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)))
|
||||
@@ -33,10 +30,7 @@ func TestDecodeString(t *testing.T) {
|
||||
func TestDecodeArray(t *testing.T) {
|
||||
for _, tc := range integerArrayTestCases {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
_, err := array2Json([]byte(tc.binary), buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -54,20 +48,14 @@ func TestDecodeArray(t *testing.T) {
|
||||
}
|
||||
for _, tc := range infiniteArrayTestCases {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
_, err := array2Json([]byte(tc.in), buf)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
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 booleanArrayTestCases {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
_, err := array2Json([]byte(tc.binary), buf)
|
||||
if err != nil {
|
||||
t.Errorf("array2Json(0x%s) errored out: %s", hex.EncodeToString([]byte(tc.binary)), err.Error())
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -94,20 +82,14 @@ var mapDecodeTestCases = []struct {
|
||||
func TestDecodeMap(t *testing.T) {
|
||||
for _, tc := range mapDecodeTestCases {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
_, err := map2Json(tc.bin, buf)
|
||||
if err != nil {
|
||||
t.Errorf("map2Json(0x%s) returned error", err)
|
||||
}
|
||||
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{})
|
||||
_, err := map2Json(tc.bin, buf)
|
||||
if err != nil {
|
||||
t.Errorf("map2Json(0x%s) returned error", err)
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -116,10 +98,7 @@ func TestDecodeMap(t *testing.T) {
|
||||
|
||||
func TestDecodeBool(t *testing.T) {
|
||||
for _, tc := range booleanTestCases {
|
||||
got, _, err := decodeSimpleFloat([]byte(tc.binary))
|
||||
if err != nil {
|
||||
t.Errorf("decodeSimpleFloat(0x%s) errored %s", hex.EncodeToString([]byte(tc.binary)), err.Error())
|
||||
}
|
||||
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)
|
||||
}
|
||||
@@ -128,10 +107,7 @@ func TestDecodeBool(t *testing.T) {
|
||||
|
||||
func TestDecodeFloat(t *testing.T) {
|
||||
for _, tc := range float32TestCases {
|
||||
got, _, err := decodeFloat([]byte(tc.binary))
|
||||
if err != nil {
|
||||
t.Errorf("decodeFloat(0x%s) returned error: %s", hex.EncodeToString([]byte(tc.binary)), err.Error())
|
||||
}
|
||||
got, _ := decodeFloat(getReader(tc.binary))
|
||||
if got != float64(tc.val) {
|
||||
t.Errorf("decodeFloat(0x%s)=%f, want:%f", hex.EncodeToString([]byte(tc.binary)), got, tc.val)
|
||||
}
|
||||
@@ -141,19 +117,13 @@ func TestDecodeFloat(t *testing.T) {
|
||||
func TestDecodeTimestamp(t *testing.T) {
|
||||
decodeTimeZone, _ = time.LoadLocation("UTC")
|
||||
for _, tc := range timeIntegerTestcases {
|
||||
tm, _, err := decodeTagData([]byte(tc.binary))
|
||||
if err != nil {
|
||||
t.Errorf("decodeTagData(0x%s) returned error: %s", hex.EncodeToString([]byte(tc.binary)), err.Error())
|
||||
}
|
||||
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 timeFloatTestcases {
|
||||
tm, _, err := decodeTagData([]byte(tc.out))
|
||||
if err != nil {
|
||||
t.Errorf("decodeTagData(0x%s) returned error: %s", hex.EncodeToString([]byte(tc.out)), err.Error())
|
||||
}
|
||||
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)
|
||||
@@ -166,6 +136,33 @@ func TestDecodeTimestamp(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeNetworkAddr(t *testing.T) {
|
||||
for _, tc := range 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 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 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
|
||||
@@ -177,12 +174,32 @@ var compositeCborTestCases = []struct {
|
||||
func TestDecodeCbor2Json(t *testing.T) {
|
||||
for _, tc := range compositeCborTestCases {
|
||||
buf := bytes.NewBuffer([]byte{})
|
||||
_, err := Cbor2JsonManyObjects(tc.binary, buf)
|
||||
if err != nil {
|
||||
t.Errorf("cbor2JsonManyObjects(0x%s) returned error", err)
|
||||
}
|
||||
if buf.String() != tc.json {
|
||||
t.Errorf("cbor2JsonManyObjects(0x%s)=%s, want: %s", hex.EncodeToString(tc.binary), buf.String(), tc.json)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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
|
||||
+10
-5
@@ -1,7 +1,7 @@
|
||||
package cbor
|
||||
|
||||
// AppendStrings encodes and adds an array of strings to the dst byte array.
|
||||
func AppendStrings(dst []byte, vals []string) []byte {
|
||||
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l <= additionalMax {
|
||||
@@ -11,13 +11,13 @@ func AppendStrings(dst []byte, vals []string) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendString(dst, v)
|
||||
dst = e.AppendString(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendString encodes and adds a string to the dst byte array.
|
||||
func AppendString(dst []byte, s string) []byte {
|
||||
func (Encoder) AppendString(dst []byte, s string) []byte {
|
||||
major := majorTypeUtf8String
|
||||
|
||||
l := len(s)
|
||||
@@ -31,7 +31,7 @@ func AppendString(dst []byte, s string) []byte {
|
||||
}
|
||||
|
||||
// AppendBytes encodes and adds an array of bytes to the dst byte array.
|
||||
func AppendBytes(dst, s []byte) []byte {
|
||||
func (Encoder) AppendBytes(dst, s []byte) []byte {
|
||||
major := majorTypeByteString
|
||||
|
||||
l := len(s)
|
||||
@@ -48,8 +48,13 @@ func AppendBytes(dst, s []byte) []byte {
|
||||
func AppendEmbeddedJSON(dst, s []byte) []byte {
|
||||
major := majorTypeTags
|
||||
minor := additionalTypeEmbeddedJSON
|
||||
dst = append(dst, byte(major|minor))
|
||||
|
||||
// Append the TAG to indicate this is Embedded JSON.
|
||||
dst = append(dst, byte(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)
|
||||
|
||||
@@ -59,7 +59,7 @@ var encodeByteTests = []struct {
|
||||
|
||||
func TestAppendString(t *testing.T) {
|
||||
for _, tt := range encodeStringTests {
|
||||
b := AppendString([]byte{}, tt.plain)
|
||||
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)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func TestAppendString(t *testing.T) {
|
||||
}
|
||||
inp := buffer.String()
|
||||
want := "\x7a\x00\x01\x11\x70" + inp
|
||||
b := AppendString([]byte{}, inp)
|
||||
b := enc.AppendString([]byte{}, inp)
|
||||
if got := string(b); got != want {
|
||||
t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func TestAppendString(t *testing.T) {
|
||||
|
||||
func TestAppendBytes(t *testing.T) {
|
||||
for _, tt := range encodeByteTests {
|
||||
b := AppendBytes([]byte{}, tt.plain)
|
||||
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)
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func TestAppendBytes(t *testing.T) {
|
||||
inp = append(inp, byte('a'))
|
||||
}
|
||||
want := "\x5a\x00\x01\x11\x70" + string(inp)
|
||||
b := AppendBytes([]byte{}, inp)
|
||||
b := enc.AppendBytes([]byte{}, inp)
|
||||
if got := string(b); got != want {
|
||||
t.Errorf("appendString(%q) = %#q, want %#q", inp, got, want)
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func BenchmarkAppendString(b *testing.B) {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
buf := make([]byte, 0, 120)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = AppendString(buf, str)
|
||||
_ = enc.AppendString(buf, str)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+13
-13
@@ -21,7 +21,7 @@ func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
|
||||
return dst
|
||||
}
|
||||
|
||||
func appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
||||
func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
||||
major := majorTypeTags
|
||||
minor := additionalTypeTimestamp
|
||||
dst = append(dst, byte(major|minor))
|
||||
@@ -29,24 +29,24 @@ func appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
||||
nanos := t.Nanosecond()
|
||||
var val float64
|
||||
val = float64(secs)*1.0 + float64(nanos)*1E-9
|
||||
return AppendFloat64(dst, val)
|
||||
return e.AppendFloat64(dst, val)
|
||||
}
|
||||
|
||||
// AppendTime encodes and adds a timestamp to the dst byte array.
|
||||
func AppendTime(dst []byte, t time.Time, unused string) []byte {
|
||||
func (e Encoder) AppendTime(dst []byte, t time.Time, unused string) []byte {
|
||||
utc := t.UTC()
|
||||
if utc.Nanosecond() == 0 {
|
||||
return appendIntegerTimestamp(dst, utc)
|
||||
}
|
||||
return appendFloatTimestamp(dst, utc)
|
||||
return e.appendFloatTimestamp(dst, utc)
|
||||
}
|
||||
|
||||
// AppendTimes encodes and adds an array of timestamps to the dst byte array.
|
||||
func AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
|
||||
func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -56,7 +56,7 @@ func AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
|
||||
}
|
||||
|
||||
for _, t := range vals {
|
||||
dst = AppendTime(dst, t, unused)
|
||||
dst = e.AppendTime(dst, t, unused)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -64,21 +64,21 @@ func AppendTimes(dst []byte, vals []time.Time, unused string) []byte {
|
||||
// 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 AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
if useInt {
|
||||
return AppendInt64(dst, int64(d/unit))
|
||||
return e.AppendInt64(dst, int64(d/unit))
|
||||
}
|
||||
return AppendFloat64(dst, float64(d)/float64(unit))
|
||||
return e.AppendFloat64(dst, float64(d)/float64(unit))
|
||||
}
|
||||
|
||||
// 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 AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -87,7 +87,7 @@ func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useIn
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, d := range vals {
|
||||
dst = AppendDuration(dst, d, unit, useInt)
|
||||
dst = e.AppendDuration(dst, d, unit, useInt)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
func TestAppendTimeNow(t *testing.T) {
|
||||
tm := time.Now()
|
||||
s := AppendTime([]byte{}, tm, "unused")
|
||||
s := enc.AppendTime([]byte{}, tm, "unused")
|
||||
got := string(s)
|
||||
|
||||
tm1 := float64(tm.Unix()) + float64(tm.Nanosecond())*1E-9
|
||||
@@ -43,7 +43,7 @@ func TestAppendTimePastPresentInteger(t *testing.T) {
|
||||
fmt.Println("Cannot parse input", tt.txt, ".. Skipping!", err)
|
||||
continue
|
||||
}
|
||||
b := AppendTime([]byte{}, tin, "unused")
|
||||
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),
|
||||
@@ -68,7 +68,7 @@ func TestAppendTimePastPresentFloat(t *testing.T) {
|
||||
fmt.Println("Cannot parse input", tt.rfcStr, ".. Skipping!")
|
||||
continue
|
||||
}
|
||||
b := AppendTime([]byte{}, tin, "unused")
|
||||
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),
|
||||
@@ -92,7 +92,7 @@ func BenchmarkAppendTime(b *testing.B) {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
buf := make([]byte, 0, 100)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = AppendTime(buf, t, "unused")
|
||||
_ = enc.AppendTime(buf, t, "unused")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
+125
-85
@@ -4,25 +4,55 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
)
|
||||
|
||||
// AppendNull inserts a 'Nil' object into the dst byte array.
|
||||
func AppendNull(dst []byte) []byte {
|
||||
// AppendNil inserts a 'Nil' object into the dst byte array.
|
||||
func (Encoder) AppendNil(dst []byte) []byte {
|
||||
return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeNull))
|
||||
}
|
||||
|
||||
// AppendBeginMarker inserts a map start into the dst byte array.
|
||||
func AppendBeginMarker(dst []byte) []byte {
|
||||
func (Encoder) AppendBeginMarker(dst []byte) []byte {
|
||||
return append(dst, byte(majorTypeMap|additionalTypeInfiniteCount))
|
||||
}
|
||||
|
||||
// AppendEndMarker inserts a map end into the dst byte array.
|
||||
func AppendEndMarker(dst []byte) []byte {
|
||||
func (Encoder) AppendEndMarker(dst []byte) []byte {
|
||||
return append(dst, byte(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, byte(majorTypeArray|additionalTypeInfiniteCount))
|
||||
}
|
||||
|
||||
// AppendArrayEnd adds markers to indicate the end of an array.
|
||||
func (Encoder) AppendArrayEnd(dst []byte) []byte {
|
||||
return append(dst, byte(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 AppendBool(dst []byte, val bool) []byte {
|
||||
func (Encoder) AppendBool(dst []byte, val bool) []byte {
|
||||
b := additionalTypeBoolFalse
|
||||
if val {
|
||||
b = additionalTypeBoolTrue
|
||||
@@ -31,11 +61,11 @@ func AppendBool(dst []byte, val bool) []byte {
|
||||
}
|
||||
|
||||
// AppendBools encodes and inserts an array of boolean values into the dst byte array.
|
||||
func AppendBools(dst []byte, vals []bool) []byte {
|
||||
func (e Encoder) AppendBools(dst []byte, vals []bool) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -44,13 +74,13 @@ func AppendBools(dst []byte, vals []bool) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendBool(dst, v)
|
||||
dst = e.AppendBool(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInt encodes and inserts an integer value into the dst byte array.
|
||||
func AppendInt(dst []byte, val int) []byte {
|
||||
func (Encoder) AppendInt(dst []byte, val int) []byte {
|
||||
major := majorTypeUnsignedInt
|
||||
contentVal := val
|
||||
if val < 0 {
|
||||
@@ -67,11 +97,11 @@ func AppendInt(dst []byte, val int) []byte {
|
||||
}
|
||||
|
||||
// AppendInts encodes and inserts an array of integer values into the dst byte array.
|
||||
func AppendInts(dst []byte, vals []int) []byte {
|
||||
func (e Encoder) AppendInts(dst []byte, vals []int) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -80,22 +110,22 @@ func AppendInts(dst []byte, vals []int) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendInt(dst, v)
|
||||
dst = e.AppendInt(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInt8 encodes and inserts an int8 value into the dst byte array.
|
||||
func AppendInt8(dst []byte, val int8) []byte {
|
||||
return AppendInt(dst, int(val))
|
||||
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 AppendInts8(dst []byte, vals []int8) []byte {
|
||||
func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -104,22 +134,22 @@ func AppendInts8(dst []byte, vals []int8) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendInt(dst, int(v))
|
||||
dst = e.AppendInt(dst, int(v))
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInt16 encodes and inserts a int16 value into the dst byte array.
|
||||
func AppendInt16(dst []byte, val int16) []byte {
|
||||
return AppendInt(dst, int(val))
|
||||
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 AppendInts16(dst []byte, vals []int16) []byte {
|
||||
func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -128,22 +158,22 @@ func AppendInts16(dst []byte, vals []int16) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendInt(dst, int(v))
|
||||
dst = e.AppendInt(dst, int(v))
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInt32 encodes and inserts a int32 value into the dst byte array.
|
||||
func AppendInt32(dst []byte, val int32) []byte {
|
||||
return AppendInt(dst, int(val))
|
||||
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 AppendInts32(dst []byte, vals []int32) []byte {
|
||||
func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -152,13 +182,13 @@ func AppendInts32(dst []byte, vals []int32) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendInt(dst, int(v))
|
||||
dst = e.AppendInt(dst, int(v))
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInt64 encodes and inserts a int64 value into the dst byte array.
|
||||
func AppendInt64(dst []byte, val int64) []byte {
|
||||
func (Encoder) AppendInt64(dst []byte, val int64) []byte {
|
||||
major := majorTypeUnsignedInt
|
||||
contentVal := val
|
||||
if val < 0 {
|
||||
@@ -175,11 +205,11 @@ func AppendInt64(dst []byte, val int64) []byte {
|
||||
}
|
||||
|
||||
// AppendInts64 encodes and inserts an array of int64 values into the dst byte array.
|
||||
func AppendInts64(dst []byte, vals []int64) []byte {
|
||||
func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -188,22 +218,22 @@ func AppendInts64(dst []byte, vals []int64) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendInt64(dst, v)
|
||||
dst = e.AppendInt64(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendUint encodes and inserts an unsigned integer value into the dst byte array.
|
||||
func AppendUint(dst []byte, val uint) []byte {
|
||||
return AppendInt64(dst, int64(val))
|
||||
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 AppendUints(dst []byte, vals []uint) []byte {
|
||||
func (e Encoder) AppendUints(dst []byte, vals []uint) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -212,22 +242,22 @@ func AppendUints(dst []byte, vals []uint) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendUint(dst, v)
|
||||
dst = e.AppendUint(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendUint8 encodes and inserts a unsigned int8 value into the dst byte array.
|
||||
func AppendUint8(dst []byte, val uint8) []byte {
|
||||
return AppendUint(dst, uint(val))
|
||||
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 AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -236,22 +266,22 @@ func AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendUint8(dst, v)
|
||||
dst = e.AppendUint8(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendUint16 encodes and inserts a uint16 value into the dst byte array.
|
||||
func AppendUint16(dst []byte, val uint16) []byte {
|
||||
return AppendUint(dst, uint(val))
|
||||
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 AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -260,22 +290,22 @@ func AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendUint16(dst, v)
|
||||
dst = e.AppendUint16(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendUint32 encodes and inserts a uint32 value into the dst byte array.
|
||||
func AppendUint32(dst []byte, val uint32) []byte {
|
||||
return AppendUint(dst, uint(val))
|
||||
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 AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -284,13 +314,13 @@ func AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendUint32(dst, v)
|
||||
dst = e.AppendUint32(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendUint64 encodes and inserts a uint64 value into the dst byte array.
|
||||
func AppendUint64(dst []byte, val uint64) []byte {
|
||||
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
||||
major := majorTypeUnsignedInt
|
||||
contentVal := val
|
||||
if contentVal <= additionalMax {
|
||||
@@ -303,11 +333,11 @@ func AppendUint64(dst []byte, val uint64) []byte {
|
||||
}
|
||||
|
||||
// AppendUints64 encodes and inserts an array of uint64 values into the dst byte array.
|
||||
func AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -316,13 +346,13 @@ func AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendUint64(dst, v)
|
||||
dst = e.AppendUint64(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendFloat32 encodes and inserts a single precision float value into the dst byte array.
|
||||
func AppendFloat32(dst []byte, val float32) []byte {
|
||||
func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
|
||||
switch {
|
||||
case math.IsNaN(float64(val)):
|
||||
return append(dst, "\xfa\x7f\xc0\x00\x00"...)
|
||||
@@ -342,11 +372,11 @@ func AppendFloat32(dst []byte, val float32) []byte {
|
||||
}
|
||||
|
||||
// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.
|
||||
func AppendFloats32(dst []byte, vals []float32) []byte {
|
||||
func (e Encoder) AppendFloats32(dst []byte, vals []float32) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -355,13 +385,13 @@ func AppendFloats32(dst []byte, vals []float32) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendFloat32(dst, v)
|
||||
dst = e.AppendFloat32(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendFloat64 encodes and inserts a double precision float value into the dst byte array.
|
||||
func AppendFloat64(dst []byte, val float64) []byte {
|
||||
func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
|
||||
switch {
|
||||
case math.IsNaN(val):
|
||||
return append(dst, "\xfb\x7f\xf8\x00\x00\x00\x00\x00\x00"...)
|
||||
@@ -382,11 +412,11 @@ func AppendFloat64(dst []byte, val float64) []byte {
|
||||
}
|
||||
|
||||
// AppendFloats64 encodes and inserts an array of double precision float values into the dst byte array.
|
||||
func AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
major := majorTypeArray
|
||||
l := len(vals)
|
||||
if l == 0 {
|
||||
return AppendArrayEnd(AppendArrayStart(dst))
|
||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
||||
}
|
||||
if l <= additionalMax {
|
||||
lb := byte(l)
|
||||
@@ -395,44 +425,54 @@ func AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||
}
|
||||
for _, v := range vals {
|
||||
dst = AppendFloat64(dst, v)
|
||||
dst = e.AppendFloat64(dst, v)
|
||||
}
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.
|
||||
func AppendInterface(dst []byte, i interface{}) []byte {
|
||||
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
||||
marshaled, err := json.Marshal(i)
|
||||
if err != nil {
|
||||
return AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
||||
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
||||
}
|
||||
return AppendEmbeddedJSON(dst, marshaled)
|
||||
}
|
||||
|
||||
// AppendObjectData takes an object in form of a byte array and appends to dst.
|
||||
func AppendObjectData(dst []byte, o []byte) []byte {
|
||||
return append(dst, o...)
|
||||
// AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6).
|
||||
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
||||
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
||||
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
||||
return e.AppendBytes(dst, ip)
|
||||
}
|
||||
|
||||
// AppendArrayStart adds markers to indicate the start of an array.
|
||||
func AppendArrayStart(dst []byte) []byte {
|
||||
return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount))
|
||||
// AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length).
|
||||
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
|
||||
dst = append(dst, byte(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, byte(majorTypeMap|0x1))
|
||||
dst = e.AppendBytes(dst, pfx.IP)
|
||||
maskLen, _ := pfx.Mask.Size()
|
||||
return e.AppendUint8(dst, uint8(maskLen))
|
||||
}
|
||||
|
||||
// AppendArrayEnd adds markers to indicate the end of an array.
|
||||
func AppendArrayEnd(dst []byte) []byte {
|
||||
return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak))
|
||||
// AppendMACAddr encodes and inserts an Hardware (MAC) address.
|
||||
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
||||
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
||||
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
||||
return e.AppendBytes(dst, ha)
|
||||
}
|
||||
|
||||
// AppendArrayDelim adds markers to indicate end of a particular array element.
|
||||
func AppendArrayDelim(dst []byte) []byte {
|
||||
//No delimiters needed in cbor
|
||||
return dst
|
||||
}
|
||||
|
||||
func AppendHex (dst []byte, val []byte) []byte {
|
||||
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||
dst = append(dst, byte(additionalTypeTagHexString>>8))
|
||||
dst = append(dst, byte(additionalTypeTagHexString&0xff))
|
||||
return AppendBytes(dst, val)
|
||||
// AppendHex adds a TAG and inserts a hex bytes as a string.
|
||||
func (e Encoder) AppendHex(dst []byte, val []byte) []byte {
|
||||
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||
dst = append(dst, byte(additionalTypeTagHexString>>8))
|
||||
dst = append(dst, byte(additionalTypeTagHexString&0xff))
|
||||
return e.AppendBytes(dst, val)
|
||||
}
|
||||
|
||||
+87
-18
@@ -2,11 +2,14 @@ package cbor
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppendNull(t *testing.T) {
|
||||
s := AppendNull([]byte{})
|
||||
var enc = Encoder{}
|
||||
|
||||
func TestAppendNil(t *testing.T) {
|
||||
s := enc.AppendNil([]byte{})
|
||||
got := string(s)
|
||||
want := "\xf6"
|
||||
if got != want {
|
||||
@@ -26,7 +29,7 @@ var booleanTestCases = []struct {
|
||||
|
||||
func TestAppendBool(t *testing.T) {
|
||||
for _, tc := range booleanTestCases {
|
||||
s := AppendBool([]byte{}, tc.val)
|
||||
s := enc.AppendBool([]byte{}, tc.val)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendBool(%s)=0x%s, want: 0x%s",
|
||||
@@ -47,7 +50,7 @@ var booleanArrayTestCases = []struct {
|
||||
|
||||
func TestAppendBoolArray(t *testing.T) {
|
||||
for _, tc := range booleanArrayTestCases {
|
||||
s := AppendBools([]byte{}, tc.val)
|
||||
s := enc.AppendBools([]byte{}, tc.val)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendBools(%s)=0x%s, want: 0x%s",
|
||||
@@ -122,7 +125,7 @@ var integerTestCases = []struct {
|
||||
|
||||
func TestAppendInt(t *testing.T) {
|
||||
for _, tc := range integerTestCases {
|
||||
s := AppendInt([]byte{}, tc.val)
|
||||
s := enc.AppendInt([]byte{}, tc.val)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendInt(0x%x)=0x%s, want: 0x%s",
|
||||
@@ -147,7 +150,7 @@ var integerArrayTestCases = []struct {
|
||||
|
||||
func TestAppendIntArray(t *testing.T) {
|
||||
for _, tc := range integerArrayTestCases {
|
||||
s := AppendInts([]byte{}, tc.val)
|
||||
s := enc.AppendInts([]byte{}, tc.val)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendInts(%s)=0x%s, want: 0x%s",
|
||||
@@ -172,7 +175,7 @@ var float32TestCases = []struct {
|
||||
|
||||
func TestAppendFloat32(t *testing.T) {
|
||||
for _, tc := range float32TestCases {
|
||||
s := AppendFloat32([]byte{}, tc.val)
|
||||
s := enc.AppendFloat32([]byte{}, tc.val)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendFloat32(%f)=0x%s, want: 0x%s",
|
||||
@@ -182,6 +185,72 @@ func TestAppendFloat32(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
var ipAddrTestCases = []struct {
|
||||
ipaddr net.IP
|
||||
text string // ASCII representation of ipaddr
|
||||
binary string // CBOR representation of ipaddr
|
||||
}{
|
||||
{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"},
|
||||
}
|
||||
|
||||
func TestAppendNetworkAddr(t *testing.T) {
|
||||
for _, tc := range ipAddrTestCases {
|
||||
s := enc.AppendIPAddr([]byte{}, tc.ipaddr)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendIPAddr(%s)=0x%s, want: 0x%s",
|
||||
tc.ipaddr, hex.EncodeToString(s),
|
||||
hex.EncodeToString([]byte(tc.binary)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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"},
|
||||
}
|
||||
|
||||
func TestAppendMacAddr(t *testing.T) {
|
||||
for _, tc := range macAddrTestCases {
|
||||
s := enc.AppendMACAddr([]byte{}, tc.macaddr)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendMACAddr(%s)=0x%s, want: 0x%s",
|
||||
tc.macaddr.String(), hex.EncodeToString(s),
|
||||
hex.EncodeToString([]byte(tc.binary)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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"},
|
||||
}
|
||||
|
||||
func TestAppendIPPrefix(t *testing.T) {
|
||||
for _, tc := range IPPrefixTestCases {
|
||||
s := enc.AppendIPPrefix([]byte{}, tc.pfx)
|
||||
got := string(s)
|
||||
if got != tc.binary {
|
||||
t.Errorf("AppendIPPrefix(%s)=0x%s, want: 0x%s",
|
||||
tc.pfx.String(), hex.EncodeToString(s),
|
||||
hex.EncodeToString([]byte(tc.binary)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAppendInt(b *testing.B) {
|
||||
type st struct {
|
||||
sz byte
|
||||
@@ -205,23 +274,23 @@ func BenchmarkAppendInt(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
switch str.sz {
|
||||
case 0:
|
||||
_ = AppendInt(buf, int(str.val))
|
||||
_ = enc.AppendInt(buf, int(str.val))
|
||||
case 1:
|
||||
_ = AppendUint8(buf, uint8(str.val))
|
||||
_ = enc.AppendUint8(buf, uint8(str.val))
|
||||
case 2:
|
||||
_ = AppendUint16(buf, uint16(str.val))
|
||||
_ = enc.AppendUint16(buf, uint16(str.val))
|
||||
case 4:
|
||||
_ = AppendUint32(buf, uint32(str.val))
|
||||
_ = enc.AppendUint32(buf, uint32(str.val))
|
||||
case 8:
|
||||
_ = AppendUint64(buf, uint64(str.val))
|
||||
_ = enc.AppendUint64(buf, uint64(str.val))
|
||||
case 21:
|
||||
_ = AppendInt8(buf, int8(str.val))
|
||||
_ = enc.AppendInt8(buf, int8(str.val))
|
||||
case 22:
|
||||
_ = AppendInt16(buf, int16(str.val))
|
||||
_ = enc.AppendInt16(buf, int16(str.val))
|
||||
case 23:
|
||||
_ = AppendInt32(buf, int32(str.val))
|
||||
_ = enc.AppendInt32(buf, int32(str.val))
|
||||
case 24:
|
||||
_ = AppendInt64(buf, int64(str.val))
|
||||
_ = enc.AppendInt64(buf, int64(str.val))
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -243,9 +312,9 @@ func BenchmarkAppendFloat(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
switch str.sz {
|
||||
case 4:
|
||||
_ = AppendFloat32(buf, float32(str.val))
|
||||
_ = enc.AppendFloat32(buf, float32(str.val))
|
||||
case 8:
|
||||
_ = AppendFloat64(buf, str.val)
|
||||
_ = enc.AppendFloat64(buf, str.val)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
+5
-37
@@ -1,44 +1,12 @@
|
||||
package json
|
||||
|
||||
type Encoder struct{}
|
||||
|
||||
// AppendKey appends a new key to the output JSON.
|
||||
func AppendKey(dst []byte, key string) []byte {
|
||||
func (e Encoder) AppendKey(dst []byte, key string) []byte {
|
||||
if len(dst) > 1 && dst[len(dst)-1] != '{' {
|
||||
dst = append(dst, ',')
|
||||
}
|
||||
dst = AppendString(dst, key)
|
||||
dst = e.AppendString(dst, key)
|
||||
return append(dst, ':')
|
||||
}
|
||||
|
||||
// AppendError encodes the error string to json and appends
|
||||
// the encoded string to the input byte slice.
|
||||
func AppendError(dst []byte, err error) []byte {
|
||||
if err == nil {
|
||||
return append(dst, `null`...)
|
||||
}
|
||||
return AppendString(dst, err.Error())
|
||||
}
|
||||
|
||||
// AppendErrors encodes the error strings to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package json
|
||||
import "unicode/utf8"
|
||||
|
||||
// AppendBytes is a mirror of appendString with []byte arg
|
||||
func AppendBytes(dst, s []byte) []byte {
|
||||
func (Encoder) AppendBytes(dst, s []byte) []byte {
|
||||
dst = append(dst, '"')
|
||||
for i := 0; i < len(s); i++ {
|
||||
if !noEscapeTable[s[i]] {
|
||||
@@ -20,7 +20,7 @@ func AppendBytes(dst, s []byte) []byte {
|
||||
//
|
||||
// The operation loops though each byte and encodes it as hex using
|
||||
// the hex lookup table.
|
||||
func AppendHex(dst, s []byte) []byte {
|
||||
func (Encoder) AppendHex(dst, s []byte) []byte {
|
||||
dst = append(dst, '"')
|
||||
for _, v := range s {
|
||||
dst = append(dst, hex[v>>4], hex[v&0x0f])
|
||||
|
||||
+15
-13
@@ -5,9 +5,11 @@ import (
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var enc = Encoder{}
|
||||
|
||||
func TestAppendBytes(t *testing.T) {
|
||||
for _, tt := range encodeStringTests {
|
||||
b := AppendBytes([]byte{}, []byte(tt.in))
|
||||
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)
|
||||
}
|
||||
@@ -16,7 +18,7 @@ func TestAppendBytes(t *testing.T) {
|
||||
|
||||
func TestAppendHex(t *testing.T) {
|
||||
for _, tt := range encodeHexTests {
|
||||
b := AppendHex([]byte{}, []byte{tt.in})
|
||||
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)
|
||||
}
|
||||
@@ -32,31 +34,31 @@ func TestStringBytes(t *testing.T) {
|
||||
}
|
||||
s := string(r) + "\xff\xff\xffhello" // some invalid UTF-8 too
|
||||
|
||||
enc := string(AppendString([]byte{}, s))
|
||||
encBytes := string(AppendBytes([]byte{}, []byte(s)))
|
||||
encStr := string(enc.AppendString([]byte{}, s))
|
||||
encBytes := string(enc.AppendBytes([]byte{}, []byte(s)))
|
||||
|
||||
if enc != encBytes {
|
||||
if encStr != encBytes {
|
||||
i := 0
|
||||
for i < len(enc) && i < len(encBytes) && enc[i] == encBytes[i] {
|
||||
for i < len(encStr) && i < len(encBytes) && encStr[i] == encBytes[i] {
|
||||
i++
|
||||
}
|
||||
enc = enc[i:]
|
||||
encStr = encStr[i:]
|
||||
encBytes = encBytes[i:]
|
||||
i = 0
|
||||
for i < len(enc) && i < len(encBytes) && enc[len(enc)-i-1] == encBytes[len(encBytes)-i-1] {
|
||||
for i < len(encStr) && i < len(encBytes) && encStr[len(encStr)-i-1] == encBytes[len(encBytes)-i-1] {
|
||||
i++
|
||||
}
|
||||
enc = enc[:len(enc)-i]
|
||||
encStr = encStr[:len(encStr)-i]
|
||||
encBytes = encBytes[:len(encBytes)-i]
|
||||
|
||||
if len(enc) > 20 {
|
||||
enc = enc[:20] + "..."
|
||||
if len(encStr) > 20 {
|
||||
encStr = encStr[:20] + "..."
|
||||
}
|
||||
if len(encBytes) > 20 {
|
||||
encBytes = encBytes[:20] + "..."
|
||||
}
|
||||
|
||||
t.Errorf("encodings differ at %#q vs %#q", enc, encBytes)
|
||||
t.Errorf("encodings differ at %#q vs %#q", encStr, encBytes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +77,7 @@ func BenchmarkAppendBytes(b *testing.B) {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
buf := make([]byte, 0, 100)
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = AppendBytes(buf, byt)
|
||||
_ = enc.AppendBytes(buf, byt)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -14,15 +14,15 @@ func init() {
|
||||
|
||||
// AppendStrings encodes the input strings to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendStrings(dst []byte, vals []string) []byte {
|
||||
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, ']')
|
||||
@@ -38,7 +38,7 @@ func AppendStrings(dst []byte, vals []string) []byte {
|
||||
// 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.
|
||||
|
||||
@@ -65,7 +65,7 @@ var encodeHexTests = []struct {
|
||||
|
||||
func TestAppendString(t *testing.T) {
|
||||
for _, tt := range encodeStringTests {
|
||||
b := AppendString([]byte{}, tt.in)
|
||||
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)
|
||||
}
|
||||
@@ -86,7 +86,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)
|
||||
_ = enc.AppendString(buf, str)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
|
||||
// AppendTime formats the input time with the given format
|
||||
// and appends the encoded string to the input byte slice.
|
||||
func AppendTime(dst []byte, t time.Time, format string) []byte {
|
||||
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
||||
if format == "" {
|
||||
return AppendInt64(dst, t.Unix())
|
||||
return e.AppendInt64(dst, t.Unix())
|
||||
}
|
||||
return append(t.AppendFormat(append(dst, '"'), format), '"')
|
||||
}
|
||||
|
||||
// AppendTimes converts the input times with the given format
|
||||
// and appends the encoded string list to the input byte slice.
|
||||
func AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
||||
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
||||
if format == "" {
|
||||
return appendUnixTimes(dst, vals)
|
||||
}
|
||||
@@ -42,7 +42,7 @@ 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, ']')
|
||||
@@ -51,24 +51,24 @@ func appendUnixTimes(dst []byte, vals []time.Time) []byte {
|
||||
|
||||
// AppendDuration formats the input duration with the given unit & format
|
||||
// and appends the encoded string to the input byte slice.
|
||||
func AppendDuration(dst []byte, d time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
func (e Encoder) 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))
|
||||
return e.AppendFloat64(dst, float64(d)/float64(unit))
|
||||
}
|
||||
|
||||
// AppendDurations formats the input durations with the given unit & format
|
||||
// and appends the encoded string list to the input byte slice.
|
||||
func AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, useInt bool) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
dst = append(dst, '[')
|
||||
dst = AppendDuration(dst, vals[0], unit, useInt)
|
||||
dst = e.AppendDuration(dst, vals[0], unit, useInt)
|
||||
if len(vals) > 1 {
|
||||
for _, d := range vals[1:] {
|
||||
dst = AppendDuration(append(dst, ','), d, unit, useInt)
|
||||
dst = e.AppendDuration(append(dst, ','), d, unit, useInt)
|
||||
}
|
||||
}
|
||||
dst = append(dst, ']')
|
||||
|
||||
+93
-38
@@ -4,18 +4,57 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 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 AppendBool(dst []byte, val bool) []byte {
|
||||
func (Encoder) AppendBool(dst []byte, val bool) []byte {
|
||||
return strconv.AppendBool(dst, val)
|
||||
}
|
||||
|
||||
// AppendBools encodes the input bools to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendBools(dst []byte, vals []bool) []byte {
|
||||
func (Encoder) AppendBools(dst []byte, vals []bool) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -32,13 +71,13 @@ func AppendBools(dst []byte, vals []bool) []byte {
|
||||
|
||||
// AppendInt converts the input int to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInt(dst []byte, val int) []byte {
|
||||
func (Encoder) AppendInt(dst []byte, val int) []byte {
|
||||
return strconv.AppendInt(dst, int64(val), 10)
|
||||
}
|
||||
|
||||
// AppendInts encodes the input ints to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendInts(dst []byte, vals []int) []byte {
|
||||
func (Encoder) AppendInts(dst []byte, vals []int) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -55,13 +94,13 @@ func AppendInts(dst []byte, vals []int) []byte {
|
||||
|
||||
// AppendInt8 converts the input []int8 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInt8(dst []byte, val int8) []byte {
|
||||
func (Encoder) AppendInt8(dst []byte, val int8) []byte {
|
||||
return strconv.AppendInt(dst, int64(val), 10)
|
||||
}
|
||||
|
||||
// AppendInts8 encodes the input int8s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendInts8(dst []byte, vals []int8) []byte {
|
||||
func (Encoder) AppendInts8(dst []byte, vals []int8) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -78,13 +117,13 @@ func AppendInts8(dst []byte, vals []int8) []byte {
|
||||
|
||||
// AppendInt16 converts the input int16 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInt16(dst []byte, val int16) []byte {
|
||||
func (Encoder) AppendInt16(dst []byte, val int16) []byte {
|
||||
return strconv.AppendInt(dst, int64(val), 10)
|
||||
}
|
||||
|
||||
// AppendInts16 encodes the input int16s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendInts16(dst []byte, vals []int16) []byte {
|
||||
func (Encoder) AppendInts16(dst []byte, vals []int16) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -101,13 +140,13 @@ func AppendInts16(dst []byte, vals []int16) []byte {
|
||||
|
||||
// AppendInt32 converts the input int32 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInt32(dst []byte, val int32) []byte {
|
||||
func (Encoder) AppendInt32(dst []byte, val int32) []byte {
|
||||
return strconv.AppendInt(dst, int64(val), 10)
|
||||
}
|
||||
|
||||
// AppendInts32 encodes the input int32s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendInts32(dst []byte, vals []int32) []byte {
|
||||
func (Encoder) AppendInts32(dst []byte, vals []int32) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -124,13 +163,13 @@ func AppendInts32(dst []byte, vals []int32) []byte {
|
||||
|
||||
// AppendInt64 converts the input int64 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInt64(dst []byte, val int64) []byte {
|
||||
func (Encoder) AppendInt64(dst []byte, val int64) []byte {
|
||||
return strconv.AppendInt(dst, val, 10)
|
||||
}
|
||||
|
||||
// AppendInts64 encodes the input int64s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendInts64(dst []byte, vals []int64) []byte {
|
||||
func (Encoder) AppendInts64(dst []byte, vals []int64) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -147,13 +186,13 @@ func AppendInts64(dst []byte, vals []int64) []byte {
|
||||
|
||||
// AppendUint converts the input uint to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendUint(dst []byte, val uint) []byte {
|
||||
func (Encoder) AppendUint(dst []byte, val uint) []byte {
|
||||
return strconv.AppendUint(dst, uint64(val), 10)
|
||||
}
|
||||
|
||||
// AppendUints encodes the input uints to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendUints(dst []byte, vals []uint) []byte {
|
||||
func (Encoder) AppendUints(dst []byte, vals []uint) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -170,13 +209,13 @@ func AppendUints(dst []byte, vals []uint) []byte {
|
||||
|
||||
// AppendUint8 converts the input uint8 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendUint8(dst []byte, val uint8) []byte {
|
||||
func (Encoder) AppendUint8(dst []byte, val uint8) []byte {
|
||||
return strconv.AppendUint(dst, uint64(val), 10)
|
||||
}
|
||||
|
||||
// AppendUints8 encodes the input uint8s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
func (Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -193,13 +232,13 @@ func AppendUints8(dst []byte, vals []uint8) []byte {
|
||||
|
||||
// AppendUint16 converts the input uint16 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendUint16(dst []byte, val uint16) []byte {
|
||||
func (Encoder) AppendUint16(dst []byte, val uint16) []byte {
|
||||
return strconv.AppendUint(dst, uint64(val), 10)
|
||||
}
|
||||
|
||||
// AppendUints16 encodes the input uint16s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
func (Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -216,13 +255,13 @@ func AppendUints16(dst []byte, vals []uint16) []byte {
|
||||
|
||||
// AppendUint32 converts the input uint32 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendUint32(dst []byte, val uint32) []byte {
|
||||
func (Encoder) AppendUint32(dst []byte, val uint32) []byte {
|
||||
return strconv.AppendUint(dst, uint64(val), 10)
|
||||
}
|
||||
|
||||
// AppendUints32 encodes the input uint32s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -239,13 +278,13 @@ func AppendUints32(dst []byte, vals []uint32) []byte {
|
||||
|
||||
// AppendUint64 converts the input uint64 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendUint64(dst []byte, val uint64) []byte {
|
||||
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
||||
return strconv.AppendUint(dst, uint64(val), 10)
|
||||
}
|
||||
|
||||
// AppendUints64 encodes the input uint64s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
@@ -260,9 +299,7 @@ func AppendUints64(dst []byte, vals []uint64) []byte {
|
||||
return dst
|
||||
}
|
||||
|
||||
// AppendFloat converts the input float to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendFloat(dst []byte, val float64, bitSize int) []byte {
|
||||
func appendFloat(dst []byte, val float64, bitSize 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
|
||||
// make a tradeoff and store those types as string.
|
||||
@@ -279,21 +316,21 @@ func AppendFloat(dst []byte, val float64, bitSize int) []byte {
|
||||
|
||||
// AppendFloat32 converts the input float32 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendFloat32(dst []byte, val float32) []byte {
|
||||
return AppendFloat(dst, float64(val), 32)
|
||||
func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
|
||||
return appendFloat(dst, float64(val), 32)
|
||||
}
|
||||
|
||||
// AppendFloats32 encodes the input float32s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendFloats32(dst []byte, vals []float32) []byte {
|
||||
func (Encoder) AppendFloats32(dst []byte, vals []float32) []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)
|
||||
if len(vals) > 1 {
|
||||
for _, val := range vals[1:] {
|
||||
dst = AppendFloat(append(dst, ','), float64(val), 32)
|
||||
dst = appendFloat(append(dst, ','), float64(val), 32)
|
||||
}
|
||||
}
|
||||
dst = append(dst, ']')
|
||||
@@ -302,21 +339,21 @@ func AppendFloats32(dst []byte, vals []float32) []byte {
|
||||
|
||||
// AppendFloat64 converts the input float64 to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendFloat64(dst []byte, val float64) []byte {
|
||||
return AppendFloat(dst, val, 64)
|
||||
func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
|
||||
return appendFloat(dst, val, 64)
|
||||
}
|
||||
|
||||
// AppendFloats64 encodes the input float64s to json and
|
||||
// appends the encoded string list to the input byte slice.
|
||||
func AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
if len(vals) == 0 {
|
||||
return append(dst, '[', ']')
|
||||
}
|
||||
dst = append(dst, '[')
|
||||
dst = AppendFloat(dst, vals[0], 32)
|
||||
dst = appendFloat(dst, vals[0], 32)
|
||||
if len(vals) > 1 {
|
||||
for _, val := range vals[1:] {
|
||||
dst = AppendFloat(append(dst, ','), val, 64)
|
||||
dst = appendFloat(append(dst, ','), val, 64)
|
||||
}
|
||||
}
|
||||
dst = append(dst, ']')
|
||||
@@ -325,15 +362,17 @@ func AppendFloats64(dst []byte, vals []float64) []byte {
|
||||
|
||||
// AppendInterface marshals the input interface to a string and
|
||||
// appends the encoded string to the input byte slice.
|
||||
func AppendInterface(dst []byte, i interface{}) []byte {
|
||||
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
||||
marshaled, err := json.Marshal(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...)
|
||||
}
|
||||
|
||||
func AppendObjectData(dst []byte, o []byte) []byte {
|
||||
// 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 {
|
||||
// Two conditions we want to put a ',' between existing content and
|
||||
// new content:
|
||||
// 1. new content starts with '{' - which shd be dropped OR
|
||||
@@ -345,3 +384,19 @@ func AppendObjectData(dst []byte, o []byte) []byte {
|
||||
}
|
||||
return append(dst, o...)
|
||||
}
|
||||
|
||||
// AppendIPAddr adds IPv4 or IPv6 address to dst.
|
||||
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
||||
return e.AppendString(dst, ip.String())
|
||||
}
|
||||
|
||||
// AppendIPPrefix adds IPv4 or IPv6 Prefix (address & mask) to dst.
|
||||
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
|
||||
return e.AppendString(dst, pfx.String())
|
||||
|
||||
}
|
||||
|
||||
// AppendMACAddr adds MAC address to dst.
|
||||
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
||||
return e.AppendString(dst, ha.String())
|
||||
}
|
||||
|
||||
+116
-12
@@ -2,24 +2,25 @@ package json
|
||||
|
||||
import (
|
||||
"math"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppendType(t *testing.T) {
|
||||
w := map[string]func(interface{}) []byte{
|
||||
"AppendInt": func(v interface{}) []byte { return AppendInt([]byte{}, v.(int)) },
|
||||
"AppendInt8": func(v interface{}) []byte { return AppendInt8([]byte{}, v.(int8)) },
|
||||
"AppendInt16": func(v interface{}) []byte { return AppendInt16([]byte{}, v.(int16)) },
|
||||
"AppendInt32": func(v interface{}) []byte { return AppendInt32([]byte{}, v.(int32)) },
|
||||
"AppendInt64": func(v interface{}) []byte { return AppendInt64([]byte{}, v.(int64)) },
|
||||
"AppendUint": func(v interface{}) []byte { return AppendUint([]byte{}, v.(uint)) },
|
||||
"AppendUint8": func(v interface{}) []byte { return AppendUint8([]byte{}, v.(uint8)) },
|
||||
"AppendUint16": func(v interface{}) []byte { return AppendUint16([]byte{}, v.(uint16)) },
|
||||
"AppendUint32": func(v interface{}) []byte { return AppendUint32([]byte{}, v.(uint32)) },
|
||||
"AppendUint64": func(v interface{}) []byte { return AppendUint64([]byte{}, v.(uint64)) },
|
||||
"AppendFloat32": func(v interface{}) []byte { return AppendFloat32([]byte{}, v.(float32)) },
|
||||
"AppendFloat64": func(v interface{}) []byte { return AppendFloat64([]byte{}, v.(float64)) },
|
||||
"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)) },
|
||||
"AppendFloat64": func(v interface{}) []byte { return enc.AppendFloat64([]byte{}, v.(float64)) },
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -61,3 +62,106 @@ func TestAppendType(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_appendMAC(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 Test_appendIP(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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_appendIPPrefix(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 Test_appendMac(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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
// +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 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"
|
||||
"github.com/coreos/go-systemd/journal"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/internal/cbor"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const defaultJournalDPrio = journal.PriNotice
|
||||
|
||||
// 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.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
|
||||
}
|
||||
|
||||
func (w journalWriter) Write(p []byte) (n int, err error) {
|
||||
if !journal.Enabled() {
|
||||
err = fmt.Errorf("Cannot connect to journalD!!")
|
||||
return
|
||||
}
|
||||
var event map[string]interface{}
|
||||
p = cbor.DecodeIfBinaryToBytes(p)
|
||||
d := json.NewDecoder(bytes.NewReader(p))
|
||||
d.UseNumber()
|
||||
err = d.Decode(&event)
|
||||
jPrio := defaultJournalDPrio
|
||||
args := make(map[string]string, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if l, ok := event[zerolog.LevelFieldName].(string); ok {
|
||||
jPrio = levelToJPrio(l)
|
||||
}
|
||||
|
||||
msg := ""
|
||||
for key, value := range event {
|
||||
jKey := strings.ToUpper(key)
|
||||
switch key {
|
||||
case zerolog.LevelFieldName, zerolog.TimestampFieldName:
|
||||
continue
|
||||
case zerolog.MessageFieldName:
|
||||
msg, _ = value.(string)
|
||||
continue
|
||||
}
|
||||
|
||||
switch value.(type) {
|
||||
case string:
|
||||
args[jKey], _ = value.(string)
|
||||
case json.Number:
|
||||
args[jKey] = fmt.Sprint(value)
|
||||
default:
|
||||
b, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
args[jKey] = fmt.Sprintf("[error: %v]", err)
|
||||
} else {
|
||||
args[jKey] = string(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
args["JSON"] = string(p)
|
||||
err = journal.Send(msg, jPrio, args)
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
// +build !windows
|
||||
|
||||
package journald_test
|
||||
|
||||
import "github.com/rs/zerolog"
|
||||
import "github.com/rs/zerolog/journald"
|
||||
|
||||
func ExampleNewJournalDWriter() {
|
||||
log := zerolog.New(journald.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
|
||||
*/
|
||||
@@ -148,6 +148,28 @@ func (l Level) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 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 levelStr {
|
||||
case DebugLevel.String():
|
||||
return DebugLevel, nil
|
||||
case InfoLevel.String():
|
||||
return InfoLevel, nil
|
||||
case WarnLevel.String():
|
||||
return WarnLevel, nil
|
||||
case ErrorLevel.String():
|
||||
return ErrorLevel, nil
|
||||
case FatalLevel.String():
|
||||
return FatalLevel, nil
|
||||
case PanicLevel.String():
|
||||
return PanicLevel, nil
|
||||
case NoLevel.String():
|
||||
return NoLevel, nil
|
||||
}
|
||||
return NoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
|
||||
}
|
||||
|
||||
// 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
|
||||
@@ -270,22 +292,24 @@ func (l *Logger) Error() *Event {
|
||||
}
|
||||
|
||||
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
||||
// is called by the Msg method.
|
||||
// is called by the Msg method, which terminates the program immediately.
|
||||
//
|
||||
// You must call Msg on the returned event in order to send the event.
|
||||
func (l *Logger) Fatal() *Event {
|
||||
return l.newEvent(FatalLevel, func(msg string) { os.Exit(1) })
|
||||
}
|
||||
|
||||
// Panic starts a new message with panic level. The message is also sent
|
||||
// to the panic function.
|
||||
// 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) Panic() *Event {
|
||||
return l.newEvent(PanicLevel, func(msg string) { panic(msg) })
|
||||
}
|
||||
|
||||
// WithLevel starts a new message with level.
|
||||
// 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 gourotine 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 {
|
||||
@@ -299,9 +323,9 @@ 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:
|
||||
@@ -352,21 +376,21 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
|
||||
if !enabled {
|
||||
return nil
|
||||
}
|
||||
e := newEvent(l.w, level, true)
|
||||
e := newEvent(l.w, level)
|
||||
e.done = done
|
||||
e.ch = l.hooks
|
||||
if level != NoLevel {
|
||||
e.Str(LevelFieldName, level.String())
|
||||
}
|
||||
if l.context != nil && len(l.context) > 0 {
|
||||
e.buf = appendObjectData(e.buf, l.context)
|
||||
e.buf = enc.AppendObjectData(e.buf, l.context)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// should returns true if the log event should be logged.
|
||||
func (l *Logger) should(lvl Level) bool {
|
||||
if lvl < l.level || lvl < globalLevel() {
|
||||
if lvl < l.level || lvl < GlobalLevel() {
|
||||
return false
|
||||
}
|
||||
if l.sampler != nil && !samplingDisabled() {
|
||||
|
||||
+80
-2
@@ -4,7 +4,9 @@ package zerolog_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
stdlog "log"
|
||||
"net"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -46,8 +48,8 @@ func ExampleLogger_Sample() {
|
||||
log.Info().Msg("message 3")
|
||||
log.Info().Msg("message 4")
|
||||
|
||||
// Output: {"level":"info","message":"message 2"}
|
||||
// {"level":"info","message":"message 4"}
|
||||
// Output: {"level":"info","message":"message 1"}
|
||||
// {"level":"info","message":"message 3"}
|
||||
}
|
||||
|
||||
type LevelNameHook struct{}
|
||||
@@ -194,6 +196,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) {
|
||||
@@ -247,6 +265,19 @@ func ExampleEvent_Object() {
|
||||
// Output: {"foo":"bar","user":{"name":"John","age":35,"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)
|
||||
|
||||
@@ -350,6 +381,20 @@ func ExampleContext_Object() {
|
||||
// Output: {"foo":"bar","user":{"name":"John","age":35,"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 {
|
||||
Name string `json:"name"`
|
||||
@@ -395,3 +440,36 @@ 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_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_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"}
|
||||
}
|
||||
|
||||
+134
-8
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"testing"
|
||||
@@ -94,14 +95,14 @@ func TestWith(t *testing.T) {
|
||||
Uint16("uint16", 8).
|
||||
Uint32("uint32", 9).
|
||||
Uint64("uint64", 10).
|
||||
Float32("float32", 11).
|
||||
Float64("float64", 12).
|
||||
Float32("float32", 11.101).
|
||||
Float64("float64", 12.30303).
|
||||
Time("time", time.Time{})
|
||||
_, file, line, _ := runtime.Caller(0)
|
||||
caller := fmt.Sprintf("%s:%d", file, line+3)
|
||||
log := ctx.Caller().Logger()
|
||||
log.Log().Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11.101,"float64":12.30303,"time":"0001-01-01T00:00:00Z","caller":"`+caller+`"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
@@ -127,10 +128,12 @@ func TestFieldsMap(t *testing.T) {
|
||||
"uint64": uint64(10),
|
||||
"float32": float32(11),
|
||||
"float64": float64(12),
|
||||
"ipv6": net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34},
|
||||
"dur": 1 * time.Second,
|
||||
"time": time.Time{},
|
||||
"obj": obj{"a", "b", 1},
|
||||
}).Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"nil":null,"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\n"; got != want {
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":true,"bytes":"bar","dur":1000,"error":"some error","float32":11,"float64":12,"int":1,"int16":3,"int32":4,"int64":5,"int8":2,"ipv6":"2001:db8:85a3::8a2e:370:7334","nil":null,"obj":{"Pub":"a","Tag":"b","priv":1},"string":"foo","time":"0001-01-01T00:00:00Z","uint":6,"uint16":8,"uint32":9,"uint64":10,"uint8":7}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
@@ -161,6 +164,52 @@ func TestFieldsMapPnt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldsMapNilPnt(t *testing.T) {
|
||||
var (
|
||||
stringPnt *string
|
||||
boolPnt *bool
|
||||
intPnt *int
|
||||
int8Pnt *int8
|
||||
int16Pnt *int16
|
||||
int32Pnt *int32
|
||||
int64Pnt *int64
|
||||
uintPnt *uint
|
||||
uint8Pnt *uint8
|
||||
uint16Pnt *uint16
|
||||
uint32Pnt *uint32
|
||||
uint64Pnt *uint64
|
||||
float32Pnt *float32
|
||||
float64Pnt *float64
|
||||
durPnt *time.Duration
|
||||
timePnt *time.Time
|
||||
)
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out)
|
||||
fields := map[string]interface{}{
|
||||
"string": stringPnt,
|
||||
"bool": boolPnt,
|
||||
"int": intPnt,
|
||||
"int8": int8Pnt,
|
||||
"int16": int16Pnt,
|
||||
"int32": int32Pnt,
|
||||
"int64": int64Pnt,
|
||||
"uint": uintPnt,
|
||||
"uint8": uint8Pnt,
|
||||
"uint16": uint16Pnt,
|
||||
"uint32": uint32Pnt,
|
||||
"uint64": uint64Pnt,
|
||||
"float32": float32Pnt,
|
||||
"float64": float64Pnt,
|
||||
"dur": durPnt,
|
||||
"time": timePnt,
|
||||
}
|
||||
|
||||
log.Log().Fields(fields).Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"bool":null,"dur":null,"float32":null,"float64":null,"int":null,"int16":null,"int32":null,"int64":null,"int8":null,"string":null,"time":null,"uint":null,"uint16":null,"uint32":null,"uint64":null,"uint8":null}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFields(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out)
|
||||
@@ -186,13 +235,17 @@ func TestFields(t *testing.T) {
|
||||
Uint16("uint16", 8).
|
||||
Uint32("uint32", 9).
|
||||
Uint64("uint64", 10).
|
||||
Float32("float32", 11).
|
||||
Float64("float64", 12).
|
||||
IPAddr("IPv4", net.IP{192, 168, 0, 100}).
|
||||
IPAddr("IPv6", net.IP{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34}).
|
||||
MACAddr("Mac", net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}).
|
||||
IPPrefix("Prefix", net.IPNet{IP: net.IP{192, 168, 0, 100}, Mask: net.CIDRMask(24, 32)}).
|
||||
Float32("float32", 11.1234).
|
||||
Float64("float64", 12.321321321).
|
||||
Dur("dur", 1*time.Second).
|
||||
Time("time", time.Time{}).
|
||||
TimeDiff("diff", now, now.Add(-10*time.Second)).
|
||||
Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"float32":11,"float64":12,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"IPv4":"192.168.0.100","IPv6":"2001:db8:85a3::8a2e:370:7334","Mac":"00:14:22:01:23:45","Prefix":"192.168.0.100/24","float32":11.1234,"float64":12.321321321,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
@@ -391,7 +444,22 @@ func TestSampling(t *testing.T) {
|
||||
log.Log().Int("i", 2).Msg("")
|
||||
log.Log().Int("i", 3).Msg("")
|
||||
log.Log().Int("i", 4).Msg("")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), "{\"i\":2}\n{\"i\":4}\n"; got != want {
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), "{\"i\":1}\n{\"i\":3}\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscard(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out)
|
||||
log.Log().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
|
||||
// Double call
|
||||
log.Log().Discard().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six"))
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
@@ -514,3 +582,61 @@ func TestOutputWithTimestamp(t *testing.T) {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
type loggableError struct {
|
||||
error
|
||||
}
|
||||
|
||||
func (l loggableError) MarshalZerologObject(e *Event) {
|
||||
e.Str("message", l.error.Error()+": loggableError")
|
||||
}
|
||||
|
||||
func TestErrorMarshalFunc(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
log := New(out)
|
||||
|
||||
// test default behaviour
|
||||
log.Log().Err(errors.New("err")).Msg("msg")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err","message":"msg"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
out.Reset()
|
||||
|
||||
log.Log().Err(loggableError{errors.New("err")}).Msg("msg")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":{"message":"err: loggableError"},"message":"msg"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
out.Reset()
|
||||
|
||||
// test overriding the ErrorMarshalFunc
|
||||
originalErrorMarshalFunc := ErrorMarshalFunc
|
||||
defer func() {
|
||||
ErrorMarshalFunc = originalErrorMarshalFunc
|
||||
}()
|
||||
|
||||
ErrorMarshalFunc = func(err error) interface{} {
|
||||
return err.Error() + ": marshaled string"
|
||||
}
|
||||
log.Log().Err(errors.New("err")).Msg("msg")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err: marshaled string","message":"msg"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
|
||||
out.Reset()
|
||||
ErrorMarshalFunc = func(err error) interface{} {
|
||||
return errors.New(err.Error() + ": new error")
|
||||
}
|
||||
log.Log().Err(errors.New("err")).Msg("msg")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":"err: new error","message":"msg"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
|
||||
out.Reset()
|
||||
ErrorMarshalFunc = func(err error) interface{} {
|
||||
return loggableError{err}
|
||||
}
|
||||
log.Log().Err(errors.New("err")).Msg("msg")
|
||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"error":{"message":"err: loggableError"},"message":"msg"}`+"\n"; got != want {
|
||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 377 KiB After Width: | Height: | Size: 141 KiB |
+1
-1
@@ -47,7 +47,7 @@ type BasicSampler struct {
|
||||
// Sample implements the Sampler interface.
|
||||
func (s *BasicSampler) Sample(lvl Level) bool {
|
||||
c := atomic.AddUint32(&s.counter, 1)
|
||||
return c%s.N == 0
|
||||
return c%s.N == s.N-1
|
||||
}
|
||||
|
||||
// BurstSampler lets Burst events pass per Period then pass the decision to
|
||||
|
||||
+8
-1
@@ -15,7 +15,14 @@ var samplers = []struct {
|
||||
wantMax int
|
||||
}{
|
||||
{
|
||||
"BasicSampler",
|
||||
"BasicSampler_1",
|
||||
func() Sampler {
|
||||
return &BasicSampler{N: 1}
|
||||
},
|
||||
100, 100, 100,
|
||||
},
|
||||
{
|
||||
"BasicSampler_5",
|
||||
func() Sampler {
|
||||
return &BasicSampler{N: 5}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user