Compare commits
5 Commits
master
...
stacktrace
Author | SHA1 | Date |
---|---|---|
Olivier Poitrey | 9f1f25d2fa | |
Olivier Poitrey | 6d2153805b | |
Olivier Poitrey | 2988c1e444 | |
Olivier Poitrey | adb19ff852 | |
Olivier Poitrey | 3786fbfa73 |
|
@ -1,10 +0,0 @@
|
||||||
version: 2
|
|
||||||
updates:
|
|
||||||
- package-ecosystem: github-actions
|
|
||||||
directory: /
|
|
||||||
schedule:
|
|
||||||
interval: weekly
|
|
||||||
- package-ecosystem: gomod
|
|
||||||
directory: /
|
|
||||||
schedule:
|
|
||||||
interval: weekly
|
|
|
@ -1,27 +0,0 @@
|
||||||
on: [push, pull_request]
|
|
||||||
name: Test
|
|
||||||
jobs:
|
|
||||||
test:
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
go-version: [1.18.x, 1.19.x]
|
|
||||||
os: [ubuntu-latest, macos-latest]
|
|
||||||
runs-on: ${{ matrix.os }}
|
|
||||||
steps:
|
|
||||||
- name: Install Go
|
|
||||||
uses: actions/setup-go@v4
|
|
||||||
with:
|
|
||||||
go-version: ${{ matrix.go-version }}
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v3
|
|
||||||
- uses: actions/cache@v3
|
|
||||||
with:
|
|
||||||
path: ~/go/pkg/mod
|
|
||||||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
|
||||||
restore-keys: |
|
|
||||||
${{ runner.os }}-go-
|
|
||||||
- name: Test
|
|
||||||
run: go test -race -bench . -benchmem ./...
|
|
||||||
- name: Test CBOR
|
|
||||||
run: go test -tags binary_log ./...
|
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
language: go
|
||||||
|
go:
|
||||||
|
- "1.7"
|
||||||
|
- "1.8"
|
||||||
|
- "1.9"
|
||||||
|
- "1.10"
|
||||||
|
- "master"
|
||||||
|
matrix:
|
||||||
|
allow_failures:
|
||||||
|
- go: "master"
|
||||||
|
script:
|
||||||
|
- go test -v -race -cpu=1,2,4 -bench . -benchmem ./...
|
||||||
|
- go test -v -tags binary_log -race -cpu=1,2,4 -bench . -benchmem ./...
|
361
README.md
361
README.md
|
@ -1,64 +1,59 @@
|
||||||
###zlog
|
|
||||||
|
|
||||||
opinionated defaults on zerolog
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Zero Allocation JSON Logger
|
# Zero Allocation JSON Logger
|
||||||
|
|
||||||
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/tuxpa.in/a/zlog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zlog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/zlog.svg?branch=master)](https://travis-ci.org/rs/zlog) [![Coverage](http://gocover.io/_badge/tuxpa.in/a/zlog)](http://gocover.io/tuxpa.in/a/zlog)
|
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/zerolog.svg?branch=master)](https://travis-ci.org/rs/zerolog) [![Coverage](http://gocover.io/_badge/github.com/rs/zerolog)](http://gocover.io/github.com/rs/zerolog)
|
||||||
|
|
||||||
The zlog package provides a fast and simple logger dedicated to JSON output.
|
The zerolog package provides a fast and simple logger dedicated to JSON output.
|
||||||
|
|
||||||
Zerolog's API is designed to provide both a great developer experience and stunning [performance](#benchmarks). Its unique chaining API allows zlog to write JSON (or CBOR) log events by avoiding allocations and reflection.
|
Zerolog's API is designed to provide both a great developer experience and stunning [performance](#benchmarks). Its unique chaining API allows zerolog to write JSON (or CBOR) log events by avoiding allocations and reflection.
|
||||||
|
|
||||||
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.
|
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, zlog focuses on efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) [`zlog.ConsoleWriter`](#pretty-logging).
|
To keep the code base and the API simple, zerolog focuses on efficient structured logging only. Pretty logging on the console is made possible using the provided (but inefficient) [`zerolog.ConsoleWriter`](#pretty-logging).
|
||||||
|
|
||||||
![Pretty Logging Image](pretty.png)
|
![Pretty Logging Image](pretty.png)
|
||||||
|
|
||||||
## Who uses zlog
|
## Who uses zerolog
|
||||||
|
|
||||||
Find out [who uses zlog](https://tuxpa.in/a/zlog/wiki/Who-uses-zlog) and add your company / project to the list.
|
Find out [who uses zerolog](https://github.com/rs/zerolog/wiki/Who-uses-zerolog) and add your company / project to the list.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* [Blazing fast](#benchmarks)
|
* Blazing fast
|
||||||
* [Low to zero allocation](#benchmarks)
|
* Low to zero allocation
|
||||||
* [Leveled logging](#leveled-logging)
|
* Level logging
|
||||||
* [Sampling](#log-sampling)
|
* Sampling
|
||||||
* [Hooks](#hooks)
|
* Hooks
|
||||||
* [Contextual fields](#contextual-logging)
|
* Contextual fields
|
||||||
* [`context.Context` integration](#contextcontext-integration)
|
* `context.Context` integration
|
||||||
* [Integration with `net/http`](#integration-with-nethttp)
|
* `net/http` helpers
|
||||||
* [JSON and CBOR encoding formats](#binary-encoding)
|
* JSON and CBOR encoding formats
|
||||||
* [Pretty logging for development](#pretty-logging)
|
* Pretty logging for development
|
||||||
* [Error Logging (with optional Stacktrace)](#error-logging)
|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```go
|
||||||
go get -u tuxpa.in/a/zlog/log
|
go get -u github.com/rs/zerolog/log
|
||||||
```
|
```
|
||||||
|
|
||||||
## Getting Started
|
## Getting Started
|
||||||
|
|
||||||
### Simple Logging Example
|
### Simple Logging Example
|
||||||
|
|
||||||
For simple logging, import the global logger package **tuxpa.in/a/zlog/log**
|
For simple logging, import the global logger package **github.com/rs/zerolog/log**
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// UNIX Time is faster and smaller than most timestamps
|
// UNIX Time is faster and smaller than most timestamps
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
// 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")
|
||||||
}
|
}
|
||||||
|
@ -70,31 +65,26 @@ func main() {
|
||||||
|
|
||||||
### Contextual Logging
|
### Contextual Logging
|
||||||
|
|
||||||
**zlog** 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:
|
**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
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
zerolog.TimeFieldFormat = ""
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("Scale", "833 cents").
|
Str("Scale", "833 cents").
|
||||||
Float64("Interval", 833.09).
|
Float64("Interval", 833.09).
|
||||||
Msg("Fibonacci is everywhere")
|
Msg("Fibonacci is everywhere")
|
||||||
|
|
||||||
log.Debug().
|
|
||||||
Str("Name", "Tom").
|
|
||||||
Send()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output: {"level":"debug","Scale":"833 cents","Interval":833.09,"time":1562212768,"message":"Fibonacci is everywhere"}
|
// Output: {"time":1524104936,"level":"debug","Scale":"833 cents","Interval":833.09,"message":"Fibonacci is everywhere"}
|
||||||
// Output: {"level":"debug","Name":"Tom","time":1562212768}
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> 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)
|
> 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)
|
||||||
|
@ -107,12 +97,12 @@ func main() {
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
zerolog.TimeFieldFormat = ""
|
||||||
|
|
||||||
log.Info().Msg("hello world")
|
log.Info().Msg("hello world")
|
||||||
}
|
}
|
||||||
|
@ -120,19 +110,18 @@ func main() {
|
||||||
// Output: {"time":1516134303,"level":"info","message":"hello world"}
|
// Output: {"time":1516134303,"level":"info","message":"hello world"}
|
||||||
```
|
```
|
||||||
|
|
||||||
> It is very important to note that when using the **zlog** 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.
|
> 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.
|
||||||
|
|
||||||
**zlog** allows for logging at the following levels (from highest to lowest):
|
**zerolog** allows for logging at the following levels (from highest to lowest):
|
||||||
|
|
||||||
* panic (`zlog.PanicLevel`, 5)
|
* panic (`zerolog.PanicLevel`, 5)
|
||||||
* fatal (`zlog.FatalLevel`, 4)
|
* fatal (`zerolog.FatalLevel`, 4)
|
||||||
* error (`zlog.ErrorLevel`, 3)
|
* error (`zerolog.ErrorLevel`, 3)
|
||||||
* warn (`zlog.WarnLevel`, 2)
|
* warn (`zerolog.WarnLevel`, 2)
|
||||||
* info (`zlog.InfoLevel`, 1)
|
* info (`zerolog.InfoLevel`, 1)
|
||||||
* debug (`zlog.DebugLevel`, 0)
|
* debug (`zerolog.DebugLevel`, 0)
|
||||||
* trace (`zlog.TraceLevel`, -1)
|
|
||||||
|
|
||||||
You can set the Global logging level to any of these options using the `SetGlobalLevel` function in the zlog package, passing in one of the given constants above, e.g. `zlog.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 `zlog.Disabled` constant.
|
You can set the Global logging level to any of these options using the `SetGlobalLevel` function in the zerolog package, passing in one of the given constants above, e.g. `zerolog.InfoLevel` would be the "info" level. Whichever level is chosen, all logs with a level greater than or equal to that level will be written. To turn off logging entirely, pass the `zerolog.Disabled` constant.
|
||||||
|
|
||||||
#### Setting Global Log Level
|
#### Setting Global Log Level
|
||||||
|
|
||||||
|
@ -144,20 +133,20 @@ package main
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
zerolog.TimeFieldFormat = ""
|
||||||
debug := flag.Bool("debug", false, "sets log level to debug")
|
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
|
// Default level for this example is info, unless debug flag is present
|
||||||
zlog.SetGlobalLevel(zlog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
if *debug {
|
if *debug {
|
||||||
zlog.SetGlobalLevel(zlog.DebugLevel)
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("This message appears only when log level set to Debug")
|
log.Debug().Msg("This message appears only when log level set to Debug")
|
||||||
|
@ -195,12 +184,12 @@ You may choose to log without a specific level by using the `Log` method. You ma
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
zerolog.TimeFieldFormat = ""
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -210,80 +199,6 @@ func main() {
|
||||||
// Output: {"time":1494567715,"foo":"bar"}
|
// Output: {"time":1494567715,"foo":"bar"}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Error Logging
|
|
||||||
|
|
||||||
You can log errors using the `Err` method
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
"tuxpa.in/a/zlog/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
|
||||||
|
|
||||||
err := errors.New("seems we have an error here")
|
|
||||||
log.Error().Err(err).Msg("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output: {"level":"error","error":"seems we have an error here","time":1609085256}
|
|
||||||
```
|
|
||||||
|
|
||||||
> The default field name for errors is `error`, you can change this by setting `zlog.ErrorFieldName` to meet your needs.
|
|
||||||
|
|
||||||
#### Error Logging with Stacktrace
|
|
||||||
|
|
||||||
Using `github.com/pkg/errors`, you can add a formatted stacktrace to your errors.
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"tuxpa.in/a/zlog/pkgerrors"
|
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
"tuxpa.in/a/zlog/log"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
|
||||||
zlog.ErrorStackMarshaler = pkgerrors.MarshalStack
|
|
||||||
|
|
||||||
err := outer()
|
|
||||||
log.Error().Stack().Err(err).Msg("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func inner() error {
|
|
||||||
return errors.New("seems we have an error here")
|
|
||||||
}
|
|
||||||
|
|
||||||
func middle() error {
|
|
||||||
err := inner()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func outer() error {
|
|
||||||
err := middle()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output: {"level":"error","stack":[{"func":"inner","line":"20","source":"errors.go"},{"func":"middle","line":"24","source":"errors.go"},{"func":"outer","line":"32","source":"errors.go"},{"func":"main","line":"15","source":"errors.go"},{"func":"main","line":"204","source":"proc.go"},{"func":"goexit","line":"1374","source":"asm_amd64.s"}],"error":"seems we have an error here","time":1609086683}
|
|
||||||
```
|
|
||||||
|
|
||||||
> zlog.ErrorStackMarshaler must be set in order for the stack to output anything.
|
|
||||||
|
|
||||||
#### Logging Fatal Messages
|
#### Logging Fatal Messages
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -292,15 +207,15 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
err := errors.New("A repo man spends his life getting into tense situations")
|
err := errors.New("A repo man spends his life getting into tense situations")
|
||||||
service := "myservice"
|
service := "myservice"
|
||||||
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
zerolog.TimeFieldFormat = ""
|
||||||
|
|
||||||
log.Fatal().
|
log.Fatal().
|
||||||
Err(err).
|
Err(err).
|
||||||
|
@ -314,11 +229,10 @@ func main() {
|
||||||
|
|
||||||
> NOTE: Using `Msgf` generates one allocation even when the logger is disabled.
|
> NOTE: Using `Msgf` generates one allocation even when the logger is disabled.
|
||||||
|
|
||||||
|
|
||||||
### Create logger instance to manage different outputs
|
### Create logger instance to manage different outputs
|
||||||
|
|
||||||
```go
|
```go
|
||||||
logger := zlog.New(os.Stderr).With().Timestamp().Logger()
|
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
|
||||||
|
|
||||||
logger.Info().Str("foo", "bar").Msg("hello world")
|
logger.Info().Str("foo", "bar").Msg("hello world")
|
||||||
|
|
||||||
|
@ -338,10 +252,10 @@ sublogger.Info().Msg("hello world")
|
||||||
|
|
||||||
### Pretty logging
|
### Pretty logging
|
||||||
|
|
||||||
To log a human-friendly, colorized output, use `zlog.ConsoleWriter`:
|
To log a human-friendly, colorized output, use `zerolog.ConsoleWriter`:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
log.Logger = log.Output(zlog.ConsoleWriter{Out: os.Stderr})
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||||
|
|
||||||
log.Info().Str("foo", "bar").Msg("Hello world")
|
log.Info().Str("foo", "bar").Msg("Hello world")
|
||||||
|
|
||||||
|
@ -351,7 +265,7 @@ log.Info().Str("foo", "bar").Msg("Hello world")
|
||||||
To customize the configuration and formatting:
|
To customize the configuration and formatting:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
output := zlog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
output := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
|
||||||
output.FormatLevel = func(i interface{}) string {
|
output.FormatLevel = func(i interface{}) string {
|
||||||
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
|
return strings.ToUpper(fmt.Sprintf("| %-6s|", i))
|
||||||
}
|
}
|
||||||
|
@ -365,7 +279,7 @@ output.FormatFieldValue = func(i interface{}) string {
|
||||||
return strings.ToUpper(fmt.Sprintf("%s", i))
|
return strings.ToUpper(fmt.Sprintf("%s", i))
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zlog.New(output).With().Timestamp().Logger()
|
log := zerolog.New(output).With().Timestamp().Logger()
|
||||||
|
|
||||||
log.Info().Str("foo", "bar").Msg("Hello World")
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
||||||
|
|
||||||
|
@ -377,7 +291,7 @@ log.Info().Str("foo", "bar").Msg("Hello World")
|
||||||
```go
|
```go
|
||||||
log.Info().
|
log.Info().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Dict("dict", zlog.Dict().
|
Dict("dict", zerolog.Dict().
|
||||||
Str("bar", "baz").
|
Str("bar", "baz").
|
||||||
Int("n", 1),
|
Int("n", 1),
|
||||||
).Msg("hello world")
|
).Msg("hello world")
|
||||||
|
@ -388,9 +302,9 @@ log.Info().
|
||||||
### Customize automatic field names
|
### Customize automatic field names
|
||||||
|
|
||||||
```go
|
```go
|
||||||
zlog.TimestampFieldName = "t"
|
zerolog.TimestampFieldName = "t"
|
||||||
zlog.LevelFieldName = "l"
|
zerolog.LevelFieldName = "l"
|
||||||
zlog.MessageFieldName = "m"
|
zerolog.MessageFieldName = "m"
|
||||||
|
|
||||||
log.Info().Msg("hello world")
|
log.Info().Msg("hello world")
|
||||||
|
|
||||||
|
@ -405,8 +319,6 @@ log.Logger = log.With().Str("foo", "bar").Logger()
|
||||||
|
|
||||||
### Add file and line number to log
|
### Add file and line number to log
|
||||||
|
|
||||||
Equivalent of `Llongfile`:
|
|
||||||
|
|
||||||
```go
|
```go
|
||||||
log.Logger = log.With().Caller().Logger()
|
log.Logger = log.With().Caller().Logger()
|
||||||
log.Info().Msg("hello world")
|
log.Info().Msg("hello world")
|
||||||
|
@ -414,35 +326,16 @@ log.Info().Msg("hello world")
|
||||||
// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"}
|
// Output: {"level": "info", "message": "hello world", "caller": "/go/src/your_project/some_file:21"}
|
||||||
```
|
```
|
||||||
|
|
||||||
Equivalent of `Lshortfile`:
|
|
||||||
|
|
||||||
```go
|
|
||||||
zlog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
|
||||||
short := file
|
|
||||||
for i := len(file) - 1; i > 0; i-- {
|
|
||||||
if file[i] == '/' {
|
|
||||||
short = file[i+1:]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
file = short
|
|
||||||
return file + ":" + strconv.Itoa(line)
|
|
||||||
}
|
|
||||||
log.Logger = log.With().Caller().Logger()
|
|
||||||
log.Info().Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"level": "info", "message": "hello world", "caller": "some_file:21"}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Thread-safe, lock-free, non-blocking writer
|
### Thread-safe, lock-free, non-blocking writer
|
||||||
|
|
||||||
If your writer might be slow or not thread-safe and you need your log producers to never get slowed down by a slow writer, you can use a `diode.Writer` as follows:
|
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
|
```go
|
||||||
wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
|
wr := diode.NewWriter(os.Stdout, 1000, 10*time.Millisecond, func(missed int) {
|
||||||
fmt.Printf("Logger Dropped %d messages", missed)
|
fmt.Printf("Logger Dropped %d messages", missed)
|
||||||
})
|
})
|
||||||
log := zlog.New(wr)
|
log := zerolog.New(w)
|
||||||
log.Print("test")
|
log.Print("test")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -451,7 +344,7 @@ You will need to install `code.cloudfoundry.org/go-diodes` to use this feature.
|
||||||
### Log Sampling
|
### Log Sampling
|
||||||
|
|
||||||
```go
|
```go
|
||||||
sampled := log.Sample(&zlog.BasicSampler{N: 10})
|
sampled := log.Sample(&zerolog.BasicSampler{N: 10})
|
||||||
sampled.Info().Msg("will be logged every 10 messages")
|
sampled.Info().Msg("will be logged every 10 messages")
|
||||||
|
|
||||||
// Output: {"time":1494567715,"level":"info","message":"will be logged every 10 messages"}
|
// Output: {"time":1494567715,"level":"info","message":"will be logged every 10 messages"}
|
||||||
|
@ -463,11 +356,11 @@ More advanced sampling:
|
||||||
// Will let 5 debug messages per period of 1 second.
|
// Will let 5 debug messages per period of 1 second.
|
||||||
// Over 5 debug message, 1 every 100 debug messages are logged.
|
// Over 5 debug message, 1 every 100 debug messages are logged.
|
||||||
// Other levels are not sampled.
|
// Other levels are not sampled.
|
||||||
sampled := log.Sample(zlog.LevelSampler{
|
sampled := log.Sample(zerolog.LevelSampler{
|
||||||
DebugSampler: &zlog.BurstSampler{
|
DebugSampler: &zerolog.BurstSampler{
|
||||||
Burst: 5,
|
Burst: 5,
|
||||||
Period: 1*time.Second,
|
Period: 1*time.Second,
|
||||||
NextSampler: &zlog.BasicSampler{N: 100},
|
NextSampler: &zerolog.BasicSampler{N: 100},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
sampled.Debug().Msg("hello world")
|
sampled.Debug().Msg("hello world")
|
||||||
|
@ -480,8 +373,8 @@ sampled.Debug().Msg("hello world")
|
||||||
```go
|
```go
|
||||||
type SeverityHook struct{}
|
type SeverityHook struct{}
|
||||||
|
|
||||||
func (h SeverityHook) Run(e *zlog.Event, level zlog.Level, msg string) {
|
func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||||
if level != zlog.NoLevel {
|
if level != zerolog.NoLevel {
|
||||||
e.Str("severity", level.String())
|
e.Str("severity", level.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -505,7 +398,7 @@ log.Ctx(ctx).Info().Msg("hello world")
|
||||||
### Set as standard logger output
|
### Set as standard logger output
|
||||||
|
|
||||||
```go
|
```go
|
||||||
stdlog := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
|
@ -517,38 +410,14 @@ stdlog.Print("hello world")
|
||||||
// Output: {"foo":"bar","message":"hello world"}
|
// Output: {"foo":"bar","message":"hello world"}
|
||||||
```
|
```
|
||||||
|
|
||||||
### context.Context integration
|
|
||||||
|
|
||||||
The `Logger` instance could be attached to `context.Context` values with `logger.WithContext(ctx)`
|
|
||||||
and extracted from it using `zerolog.Ctx(ctx)`.
|
|
||||||
|
|
||||||
Example to add logger to context:
|
|
||||||
```go
|
|
||||||
// this code attach logger instance to context fields
|
|
||||||
ctx := context.Background()
|
|
||||||
logger := zerolog.New(os.Stdout)
|
|
||||||
ctx = logger.WithContext(ctx)
|
|
||||||
someFunc(ctx)
|
|
||||||
```
|
|
||||||
|
|
||||||
Extracting logger from context:
|
|
||||||
```go
|
|
||||||
func someFunc(ctx context.Context) {
|
|
||||||
// get logger from context. if it's nill, then `zerolog.DefaultContextLogger` is returned,
|
|
||||||
// if `DefaultContextLogger` is nil, then disabled logger returned.
|
|
||||||
logger := zerolog.Ctx(ctx)
|
|
||||||
logger.Info().Msg("Hello")
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Integration with `net/http`
|
### Integration with `net/http`
|
||||||
|
|
||||||
The `tuxpa.in/a/zlog/hlog` package provides some helpers to integrate zlog with `http.Handler`.
|
The `github.com/rs/zerolog/hlog` package provides some helpers to integrate zerolog with `http.Handler`.
|
||||||
|
|
||||||
In this example we use [alice](https://github.com/justinas/alice) to install logger for better readability.
|
In this example we use [alice](https://github.com/justinas/alice) to install logger for better readability.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Timestamp().
|
Timestamp().
|
||||||
Str("role", "my-service").
|
Str("role", "my-service").
|
||||||
Str("host", host).
|
Str("host", host).
|
||||||
|
@ -560,11 +429,11 @@ c := alice.New()
|
||||||
c = c.Append(hlog.NewHandler(log))
|
c = c.Append(hlog.NewHandler(log))
|
||||||
|
|
||||||
// Install some provided extra handler to set some request's context fields.
|
// Install some provided extra handler to set some request's context fields.
|
||||||
// Thanks to that handler, all our logs will come with some prepopulated fields.
|
// Thanks to those handler, all our logs will come with some pre-populated fields.
|
||||||
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
c = c.Append(hlog.AccessHandler(func(r *http.Request, status, size int, duration time.Duration) {
|
||||||
hlog.FromRequest(r).Info().
|
hlog.FromRequest(r).Info().
|
||||||
Str("method", r.Method).
|
Str("method", r.Method).
|
||||||
Stringer("url", r.URL).
|
Str("url", r.URL.String()).
|
||||||
Int("status", status).
|
Int("status", status).
|
||||||
Int("size", size).
|
Int("size", size).
|
||||||
Dur("duration", duration).
|
Dur("duration", duration).
|
||||||
|
@ -594,40 +463,23 @@ if err := http.ListenAndServe(":8080", nil); err != nil {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Multiple Log Output
|
|
||||||
`zlog.MultiLevelWriter` may be used to send the log message to multiple outputs.
|
|
||||||
In this example, we send the log message to both `os.Stdout` and the in-built ConsoleWriter.
|
|
||||||
```go
|
|
||||||
func main() {
|
|
||||||
consoleWriter := zlog.ConsoleWriter{Out: os.Stdout}
|
|
||||||
|
|
||||||
multi := zlog.MultiLevelWriter(consoleWriter, os.Stdout)
|
|
||||||
|
|
||||||
logger := zlog.New(multi).With().Timestamp().Logger()
|
|
||||||
|
|
||||||
logger.Info().Msg("Hello World!")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output (Line 1: Console; Line 2: Stdout)
|
|
||||||
// 12:36PM INF Hello World!
|
|
||||||
// {"level":"info","time":"2019-11-07T12:36:38+03:00","message":"Hello World!"}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Global Settings
|
## Global Settings
|
||||||
|
|
||||||
Some settings can be changed and will be applied to all loggers:
|
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).
|
* `log.Logger`: You can set this value to customize the global logger (the one used by package level methods).
|
||||||
* `zlog.SetGlobalLevel`: Can raise the minimum level of all loggers. Call this with `zlog.Disabled` 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).
|
||||||
* `zlog.DisableSampling`: If argument is `true`, all sampled loggers will stop sampling and issue 100% of their log events.
|
* `zerolog.DisableSampling`: If argument is `true`, all sampled loggers will stop sampling and issue 100% of their log events.
|
||||||
* `zlog.TimestampFieldName`: Can be set to customize `Timestamp` field name.
|
* `zerolog.TimestampFieldName`: Can be set to customize `Timestamp` field name.
|
||||||
* `zlog.LevelFieldName`: Can be set to customize level field name.
|
* `zerolog.LevelFieldName`: Can be set to customize level field name.
|
||||||
* `zlog.MessageFieldName`: Can be set to customize message field name.
|
* `zerolog.MessageFieldName`: Can be set to customize message field name.
|
||||||
* `zlog.ErrorFieldName`: Can be set to customize `Err` field name.
|
* `zerolog.ErrorFieldName`: Can be set to customize `Err` field name.
|
||||||
* `zlog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with `zlog.TimeFormatUnix`, `zlog.TimeFormatUnixMs` or `zlog.TimeFormatUnixMicro`, times are formated as UNIX timestamp.
|
* `zerolog.TimeFieldFormat`: Can be set to customize `Time` field value formatting. If set with an empty string, times are formated as UNIX timestamp.
|
||||||
* `zlog.DurationFieldUnit`: Can be set to customize the unit for time.Duration type fields added by `Dur` (default: `time.Millisecond`).
|
// DurationFieldUnit defines the unit for time.Duration type fields added
|
||||||
* `zlog.DurationFieldInteger`: If set to `true`, `Dur` fields are formatted as integers instead of floats (default: `false`).
|
// using the Dur method.
|
||||||
* `zlog.ErrorHandler`: Called whenever zlog fails to write an event on its output. If not set, an error is printed on the stderr. This handler must be thread safe and non-blocking.
|
* `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.
|
||||||
|
* `ErrorHandler`: Called whenever zerolog fails to write an event on its output. If not set, an error is printed on the stderr. This handler must be thread safe and non-blocking.
|
||||||
|
|
||||||
## Field Types
|
## Field Types
|
||||||
|
|
||||||
|
@ -641,38 +493,27 @@ Some settings can be changed and will be applied to all loggers:
|
||||||
|
|
||||||
### Advanced Fields
|
### Advanced Fields
|
||||||
|
|
||||||
* `Err`: Takes an `error` and renders it as a string using the `zlog.ErrorFieldName` field name.
|
* `Err`: Takes an `error` and render it as a string using the `zerolog.ErrorFieldName` field name.
|
||||||
* `Func`: Run a `func` only if the level is enabled.
|
* `Timestamp`: Insert a timestamp field with `zerolog.TimestampFieldName` field name and formatted using `zerolog.TimeFieldFormat`.
|
||||||
* `Timestamp`: Inserts a timestamp field with `zlog.TimestampFieldName` field name, formatted using `zlog.TimeFieldFormat`.
|
* `Time`: Adds a field with the time formated with the `zerolog.TimeFieldFormat`.
|
||||||
* `Time`: Adds a field with time formatted with `zlog.TimeFieldFormat`.
|
* `Dur`: Adds a field with a `time.Duration`.
|
||||||
* `Dur`: Adds a field with `time.Duration`.
|
|
||||||
* `Dict`: Adds a sub-key/value as a field of the event.
|
* `Dict`: Adds a sub-key/value as a field of the event.
|
||||||
* `RawJSON`: Adds a field with an already encoded JSON (`[]byte`)
|
|
||||||
* `Hex`: Adds a field with value formatted as a hexadecimal string (`[]byte`)
|
|
||||||
* `Interface`: Uses reflection to marshal the type.
|
* `Interface`: Uses reflection to marshal the type.
|
||||||
|
|
||||||
Most fields are also available in the slice format (`Strs` for `[]string`, `Errs` for `[]error` etc.)
|
|
||||||
|
|
||||||
## Binary Encoding
|
## Binary Encoding
|
||||||
|
|
||||||
<<<<<<< HEAD
|
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:
|
||||||
In addition to the default JSON encoding, `zlog` 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:
|
|
||||||
=======
|
|
||||||
In addition to the default JSON encoding, `zerolog` can produce binary logs using [CBOR](https://cbor.io) encoding. The choice of encoding can be decided at compile time using the build tag `binary_log` as follows:
|
|
||||||
>>>>>>> github
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
go build -tags binary_log .
|
go build -tags binary_log .
|
||||||
```
|
```
|
||||||
|
|
||||||
To Decode binary encoded log files you can use any CBOR decoder. One has been tested to work
|
To Decode binary encoded log files you can use any CBOR decoder. One has been tested to work
|
||||||
with zlog library is [CSD](https://github.com/toravir/csd/).
|
with zerolog library is [CSD](https://github.com/toravir/csd/).
|
||||||
|
|
||||||
## Related Projects
|
## Related Projects
|
||||||
|
|
||||||
* [grpc-zlog](https://github.com/cheapRoc/grpc-zlog): Implementation of `grpclog.LoggerV2` interface using `zlog`
|
* [grpc-zerolog](https://github.com/cheapRoc/grpc-zerolog): Implementation of `grpclog.LoggerV2` interface using `zerolog`
|
||||||
* [overlog](https://github.com/Trendyol/overlog): Implementation of `Mapped Diagnostic Context` interface using `zlog`
|
|
||||||
* [zlogr](https://github.com/go-logr/zlogr): Implementation of `logr.LogSink` interface using `zlog`
|
|
||||||
|
|
||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
|
@ -688,7 +529,7 @@ 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
|
BenchmarkLogFields-8 10000000 184 ns/op 0 B/op 0 allocs/op
|
||||||
```
|
```
|
||||||
|
|
||||||
There are a few Go logging benchmarks and comparisons that include zlog.
|
There are a few Go logging benchmarks and comparisons that include zerolog.
|
||||||
|
|
||||||
* [imkira/go-loggers-bench](https://github.com/imkira/go-loggers-bench)
|
* [imkira/go-loggers-bench](https://github.com/imkira/go-loggers-bench)
|
||||||
* [uber-common/zap](https://github.com/uber-go/zap#performance)
|
* [uber-common/zap](https://github.com/uber-go/zap#performance)
|
||||||
|
@ -699,7 +540,7 @@ Log a message and 10 fields:
|
||||||
|
|
||||||
| Library | Time | Bytes Allocated | Objects Allocated |
|
| Library | Time | Bytes Allocated | Objects Allocated |
|
||||||
| :--- | :---: | :---: | :---: |
|
| :--- | :---: | :---: | :---: |
|
||||||
| zlog | 767 ns/op | 552 B/op | 6 allocs/op |
|
| zerolog | 767 ns/op | 552 B/op | 6 allocs/op |
|
||||||
| :zap: zap | 848 ns/op | 704 B/op | 2 allocs/op |
|
| :zap: zap | 848 ns/op | 704 B/op | 2 allocs/op |
|
||||||
| :zap: zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op |
|
| :zap: zap (sugared) | 1363 ns/op | 1610 B/op | 20 allocs/op |
|
||||||
| go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op |
|
| go-kit | 3614 ns/op | 2895 B/op | 66 allocs/op |
|
||||||
|
@ -712,7 +553,7 @@ Log a message with a logger that already has 10 fields of context:
|
||||||
|
|
||||||
| Library | Time | Bytes Allocated | Objects Allocated |
|
| Library | Time | Bytes Allocated | Objects Allocated |
|
||||||
| :--- | :---: | :---: | :---: |
|
| :--- | :---: | :---: | :---: |
|
||||||
| zlog | 52 ns/op | 0 B/op | 0 allocs/op |
|
| zerolog | 52 ns/op | 0 B/op | 0 allocs/op |
|
||||||
| :zap: zap | 283 ns/op | 0 B/op | 0 allocs/op |
|
| :zap: zap | 283 ns/op | 0 B/op | 0 allocs/op |
|
||||||
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
|
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
|
||||||
| lion | 2702 ns/op | 4074 B/op | 38 allocs/op |
|
| lion | 2702 ns/op | 4074 B/op | 38 allocs/op |
|
||||||
|
@ -725,7 +566,7 @@ Log a static string, without any context or `printf`-style templating:
|
||||||
|
|
||||||
| Library | Time | Bytes Allocated | Objects Allocated |
|
| Library | Time | Bytes Allocated | Objects Allocated |
|
||||||
| :--- | :---: | :---: | :---: |
|
| :--- | :---: | :---: | :---: |
|
||||||
| zlog | 50 ns/op | 0 B/op | 0 allocs/op |
|
| zerolog | 50 ns/op | 0 B/op | 0 allocs/op |
|
||||||
| :zap: zap | 236 ns/op | 0 B/op | 0 allocs/op |
|
| :zap: zap | 236 ns/op | 0 B/op | 0 allocs/op |
|
||||||
| standard library | 453 ns/op | 80 B/op | 2 allocs/op |
|
| standard library | 453 ns/op | 80 B/op | 2 allocs/op |
|
||||||
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
|
| :zap: zap (sugared) | 337 ns/op | 80 B/op | 2 allocs/op |
|
||||||
|
@ -737,14 +578,14 @@ Log a static string, without any context or `printf`-style templating:
|
||||||
|
|
||||||
## Caveats
|
## Caveats
|
||||||
|
|
||||||
Note that zlog does no de-duplication of fields. Using the same key multiple times creates multiple keys in final JSON:
|
Note that zerolog does de-duplication fields. Using the same key multiple times creates multiple keys in final JSON:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
logger := zlog.New(os.Stderr).With().Timestamp().Logger()
|
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
|
||||||
logger.Info().
|
logger.Info().
|
||||||
Timestamp().
|
Timestamp().
|
||||||
Msg("dup")
|
Msg("dup")
|
||||||
// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
|
// Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
|
||||||
```
|
```
|
||||||
|
|
||||||
In this case, many consumers will take the last value, but this is not guaranteed; check yours if in doubt.
|
However, it’s not a big deal as JSON accepts dup keys; the last one prevails.
|
||||||
|
|
64
array.go
64
array.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
@ -49,7 +49,7 @@ func (*Array) MarshalZerologArray(*Array) {
|
||||||
func (a *Array) write(dst []byte) []byte {
|
func (a *Array) write(dst []byte) []byte {
|
||||||
dst = enc.AppendArrayStart(dst)
|
dst = enc.AppendArrayStart(dst)
|
||||||
if len(a.buf) > 0 {
|
if len(a.buf) > 0 {
|
||||||
dst = append(dst, a.buf...)
|
dst = append(append(dst, a.buf...))
|
||||||
}
|
}
|
||||||
dst = enc.AppendArrayEnd(dst)
|
dst = enc.AppendArrayEnd(dst)
|
||||||
putArray(a)
|
putArray(a)
|
||||||
|
@ -57,7 +57,7 @@ func (a *Array) write(dst []byte) []byte {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Object marshals an object that implement the LogObjectMarshaler
|
// Object marshals an object that implement the LogObjectMarshaler
|
||||||
// interface and appends it to the array.
|
// interface and append append it to the array.
|
||||||
func (a *Array) Object(obj LogObjectMarshaler) *Array {
|
func (a *Array) Object(obj LogObjectMarshaler) *Array {
|
||||||
e := Dict()
|
e := Dict()
|
||||||
obj.MarshalZerologObject(e)
|
obj.MarshalZerologObject(e)
|
||||||
|
@ -67,33 +67,28 @@ func (a *Array) Object(obj LogObjectMarshaler) *Array {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Str appends 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 {
|
func (a *Array) Str(val string) *Array {
|
||||||
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val)
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), val)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytes appends 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 {
|
func (a *Array) Bytes(val []byte) *Array {
|
||||||
a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
|
a.buf = enc.AppendBytes(enc.AppendArrayDelim(a.buf), val)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hex appends 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 {
|
func (a *Array) Hex(val []byte) *Array {
|
||||||
a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
|
a.buf = enc.AppendHex(enc.AppendArrayDelim(a.buf), val)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// RawJSON adds already encoded JSON to the array.
|
|
||||||
func (a *Array) RawJSON(val []byte) *Array {
|
|
||||||
a.buf = appendJSON(enc.AppendArrayDelim(a.buf), val)
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
||||||
// Err serializes and appends the err to the array.
|
// Err serializes and appends the err to the array.
|
||||||
func (a *Array) Err(err error) *Array {
|
func (a *Array) Err(err error) *Array {
|
||||||
switch m := ErrorMarshalFunc(err).(type) {
|
marshaled := ErrorMarshalFunc(err)
|
||||||
|
switch m := marshaled.(type) {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
e := newEvent(nil, 0)
|
e := newEvent(nil, 0)
|
||||||
e.buf = e.buf[:0]
|
e.buf = e.buf[:0]
|
||||||
|
@ -101,11 +96,7 @@ func (a *Array) Err(err error) *Array {
|
||||||
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
a.buf = append(enc.AppendArrayDelim(a.buf), e.buf...)
|
||||||
putEvent(e)
|
putEvent(e)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
a.buf = enc.AppendNil(enc.AppendArrayDelim(a.buf))
|
|
||||||
} else {
|
|
||||||
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error())
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m)
|
a.buf = enc.AppendString(enc.AppendArrayDelim(a.buf), m)
|
||||||
default:
|
default:
|
||||||
|
@ -115,97 +106,97 @@ func (a *Array) Err(err error) *Array {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bool appends 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 {
|
func (a *Array) Bool(b bool) *Array {
|
||||||
a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
|
a.buf = enc.AppendBool(enc.AppendArrayDelim(a.buf), b)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int appends i as a int to the array.
|
// Int append append i as a int to the array.
|
||||||
func (a *Array) Int(i int) *Array {
|
func (a *Array) Int(i int) *Array {
|
||||||
a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendInt(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int8 appends i as a int8 to the array.
|
// Int8 append append i as a int8 to the array.
|
||||||
func (a *Array) Int8(i int8) *Array {
|
func (a *Array) Int8(i int8) *Array {
|
||||||
a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendInt8(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int16 appends i as a int16 to the array.
|
// Int16 append append i as a int16 to the array.
|
||||||
func (a *Array) Int16(i int16) *Array {
|
func (a *Array) Int16(i int16) *Array {
|
||||||
a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendInt16(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int32 appends i as a int32 to the array.
|
// Int32 append append i as a int32 to the array.
|
||||||
func (a *Array) Int32(i int32) *Array {
|
func (a *Array) Int32(i int32) *Array {
|
||||||
a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendInt32(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64 appends i as a int64 to the array.
|
// Int64 append append i as a int64 to the array.
|
||||||
func (a *Array) Int64(i int64) *Array {
|
func (a *Array) Int64(i int64) *Array {
|
||||||
a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendInt64(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint appends i as a uint to the array.
|
// Uint append append i as a uint to the array.
|
||||||
func (a *Array) Uint(i uint) *Array {
|
func (a *Array) Uint(i uint) *Array {
|
||||||
a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendUint(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint8 appends i as a uint8 to the array.
|
// Uint8 append append i as a uint8 to the array.
|
||||||
func (a *Array) Uint8(i uint8) *Array {
|
func (a *Array) Uint8(i uint8) *Array {
|
||||||
a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendUint8(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint16 appends i as a uint16 to the array.
|
// Uint16 append append i as a uint16 to the array.
|
||||||
func (a *Array) Uint16(i uint16) *Array {
|
func (a *Array) Uint16(i uint16) *Array {
|
||||||
a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendUint16(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint32 appends i as a uint32 to the array.
|
// Uint32 append append i as a uint32 to the array.
|
||||||
func (a *Array) Uint32(i uint32) *Array {
|
func (a *Array) Uint32(i uint32) *Array {
|
||||||
a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendUint32(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint64 appends i as a uint64 to the array.
|
// Uint64 append append i as a uint64 to the array.
|
||||||
func (a *Array) Uint64(i uint64) *Array {
|
func (a *Array) Uint64(i uint64) *Array {
|
||||||
a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
|
a.buf = enc.AppendUint64(enc.AppendArrayDelim(a.buf), i)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float32 appends f as a float32 to the array.
|
// Float32 append append f as a float32 to the array.
|
||||||
func (a *Array) Float32(f float32) *Array {
|
func (a *Array) Float32(f float32) *Array {
|
||||||
a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f)
|
a.buf = enc.AppendFloat32(enc.AppendArrayDelim(a.buf), f)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float64 appends f as a float64 to the array.
|
// Float64 append append f as a float64 to the array.
|
||||||
func (a *Array) Float64(f float64) *Array {
|
func (a *Array) Float64(f float64) *Array {
|
||||||
a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f)
|
a.buf = enc.AppendFloat64(enc.AppendArrayDelim(a.buf), f)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time appends t formatted as string using zlog.TimeFieldFormat.
|
// Time append append t formated as string using zerolog.TimeFieldFormat.
|
||||||
func (a *Array) Time(t time.Time) *Array {
|
func (a *Array) Time(t time.Time) *Array {
|
||||||
a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
|
a.buf = enc.AppendTime(enc.AppendArrayDelim(a.buf), t, TimeFieldFormat)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dur appends d to the array.
|
// Dur append append d to the array.
|
||||||
func (a *Array) Dur(d time.Duration) *Array {
|
func (a *Array) Dur(d time.Duration) *Array {
|
||||||
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
|
a.buf = enc.AppendDuration(enc.AppendArrayDelim(a.buf), d, DurationFieldUnit, DurationFieldInteger)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Interface appends i marshaled using reflection.
|
// Interface append append i marshaled using reflection.
|
||||||
func (a *Array) Interface(i interface{}) *Array {
|
func (a *Array) Interface(i interface{}) *Array {
|
||||||
if obj, ok := i.(LogObjectMarshaler); ok {
|
if obj, ok := i.(LogObjectMarshaler); ok {
|
||||||
return a.Object(obj)
|
return a.Object(obj)
|
||||||
|
@ -231,10 +222,3 @@ func (a *Array) MACAddr(ha net.HardwareAddr) *Array {
|
||||||
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
|
a.buf = enc.AppendMACAddr(enc.AppendArrayDelim(a.buf), ha)
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dict adds the dict Event to the array
|
|
||||||
func (a *Array) Dict(dict *Event) *Array {
|
|
||||||
dict.buf = enc.AppendEndMarker(dict.buf)
|
|
||||||
a.buf = append(enc.AppendArrayDelim(a.buf), dict.buf...)
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
@ -24,15 +24,10 @@ func TestArray(t *testing.T) {
|
||||||
Str("a").
|
Str("a").
|
||||||
Bytes([]byte("b")).
|
Bytes([]byte("b")).
|
||||||
Hex([]byte{0x1f}).
|
Hex([]byte{0x1f}).
|
||||||
RawJSON([]byte(`{"some":"json"}`)).
|
|
||||||
Time(time.Time{}).
|
Time(time.Time{}).
|
||||||
IPAddr(net.IP{192, 168, 0, 10}).
|
IPAddr(net.IP{192, 168, 0, 10}).
|
||||||
Dur(0).
|
Dur(0)
|
||||||
Dict(Dict().
|
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]`
|
||||||
Str("bar", "baz").
|
|
||||||
Int("n", 1),
|
|
||||||
)
|
|
||||||
want := `[true,1,2,3,4,5,6,7,8,9,10,11.98122,12.987654321,"a","b","1f",{"some":"json"},"0001-01-01T00:00:00Z","192.168.0.10",0,{"bar":"baz","n":1}]`
|
|
||||||
if got := decodeObjectToStr(a.write([]byte{})); got != want {
|
if got := decodeObjectToStr(a.write([]byte{})); got != want {
|
||||||
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
|
t.Errorf("Array.write()\ngot: %s\nwant: %s", got, want)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -148,16 +147,16 @@ func BenchmarkLogFieldType(b *testing.B) {
|
||||||
{"a", "a", 0},
|
{"a", "a", 0},
|
||||||
}
|
}
|
||||||
objects := []obj{
|
objects := []obj{
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
}
|
}
|
||||||
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
|
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
|
||||||
types := map[string]func(e *Event) *Event{
|
types := map[string]func(e *Event) *Event{
|
||||||
|
@ -235,13 +234,12 @@ func BenchmarkLogFieldType(b *testing.B) {
|
||||||
|
|
||||||
func BenchmarkContextFieldType(b *testing.B) {
|
func BenchmarkContextFieldType(b *testing.B) {
|
||||||
oldFormat := TimeFieldFormat
|
oldFormat := TimeFieldFormat
|
||||||
TimeFieldFormat = TimeFormatUnix
|
TimeFieldFormat = ""
|
||||||
defer func() { TimeFieldFormat = oldFormat }()
|
defer func() { TimeFieldFormat = oldFormat }()
|
||||||
bools := []bool{true, false, true, false, true, false, true, false, true, false}
|
bools := []bool{true, false, true, false, true, false, true, false, true, false}
|
||||||
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
ints := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||||
floats := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
floats := []float64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||||
strings := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
|
strings := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}
|
||||||
stringer := net.IP{127, 0, 0, 1}
|
|
||||||
durations := []time.Duration{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
durations := []time.Duration{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
|
||||||
times := []time.Time{
|
times := []time.Time{
|
||||||
time.Unix(0, 0),
|
time.Unix(0, 0),
|
||||||
|
@ -272,16 +270,16 @@ func BenchmarkContextFieldType(b *testing.B) {
|
||||||
{"a", "a", 0},
|
{"a", "a", 0},
|
||||||
}
|
}
|
||||||
objects := []obj{
|
objects := []obj{
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
{"a", "a", 0},
|
obj{"a", "a", 0},
|
||||||
}
|
}
|
||||||
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
|
errs := []error{errors.New("a"), errors.New("b"), errors.New("c"), errors.New("d"), errors.New("e")}
|
||||||
types := map[string]func(c Context) Context{
|
types := map[string]func(c Context) Context{
|
||||||
|
@ -309,9 +307,6 @@ func BenchmarkContextFieldType(b *testing.B) {
|
||||||
"Strs": func(c Context) Context {
|
"Strs": func(c Context) Context {
|
||||||
return c.Strs("k", strings)
|
return c.Strs("k", strings)
|
||||||
},
|
},
|
||||||
"Stringer": func(c Context) Context {
|
|
||||||
return c.Stringer("k", stringer)
|
|
||||||
},
|
|
||||||
"Err": func(c Context) Context {
|
"Err": func(c Context) Context {
|
||||||
return c.Err(errs[0])
|
return c.Err(errs[0])
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// +build binary_log
|
// +build binary_log
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -108,19 +108,6 @@ func ExampleLogger_Printf() {
|
||||||
// Output: {"level":"debug","message":"hello world"}
|
// Output: {"level":"debug","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Trace() {
|
|
||||||
dst := bytes.Buffer{}
|
|
||||||
log := New(&dst)
|
|
||||||
|
|
||||||
log.Trace().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Int("n", 123).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
|
||||||
// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleLogger_Debug() {
|
func ExampleLogger_Debug() {
|
||||||
dst := bytes.Buffer{}
|
dst := bytes.Buffer{}
|
||||||
log := New(&dst)
|
log := New(&dst)
|
||||||
|
@ -364,42 +351,6 @@ func ExampleEvent_Durs() {
|
||||||
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Fields_map() {
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"bar": "baz",
|
|
||||||
"n": 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
dst := bytes.Buffer{}
|
|
||||||
log := New(&dst)
|
|
||||||
|
|
||||||
log.Log().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleEvent_Fields_slice() {
|
|
||||||
fields := []interface{}{
|
|
||||||
"bar", "baz",
|
|
||||||
"n", 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
dst := bytes.Buffer{}
|
|
||||||
log := New(&dst)
|
|
||||||
|
|
||||||
log.Log().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleContext_Dict() {
|
func ExampleContext_Dict() {
|
||||||
dst := bytes.Buffer{}
|
dst := bytes.Buffer{}
|
||||||
log := New(&dst).With().
|
log := New(&dst).With().
|
||||||
|
@ -546,39 +497,3 @@ func ExampleContext_Durs() {
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
||||||
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Fields_map() {
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"bar": "baz",
|
|
||||||
"n": 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
dst := bytes.Buffer{}
|
|
||||||
log := New(&dst).With().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
log.Log().Msg("hello world")
|
|
||||||
|
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleContext_Fields_slice() {
|
|
||||||
fields := []interface{}{
|
|
||||||
"bar", "baz",
|
|
||||||
"n", 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
dst := bytes.Buffer{}
|
|
||||||
log := New(&dst).With().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
log.Log().Msg("hello world")
|
|
||||||
|
|
||||||
fmt.Println(decodeIfBinaryToString(dst.Bytes()))
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
module tuxpa.in/a/zlog/cmd/lint
|
|
||||||
|
|
||||||
go 1.15
|
|
||||||
|
|
||||||
require golang.org/x/tools v0.1.8
|
|
|
@ -1,28 +0,0 @@
|
||||||
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
|
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
|
||||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
|
||||||
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
|
|
||||||
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
|
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
|
||||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
|
||||||
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
|
||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
|
||||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0=
|
|
||||||
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
|
||||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
|
||||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
|
||||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
|
||||||
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
|
|
||||||
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
|
|
||||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
|
|
@ -29,8 +29,8 @@ func init() {
|
||||||
flag.Var(&allowedFinishers, "finisher", "allowed finisher for the event chain")
|
flag.Var(&allowedFinishers, "finisher", "allowed finisher for the event chain")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// add zlog to recursively ignored packages
|
// add zerolog to recursively ignored packages
|
||||||
recursivelyIgnoredPkgs = append(recursivelyIgnoredPkgs, "tuxpa.in/a/zlog")
|
recursivelyIgnoredPkgs = append(recursivelyIgnoredPkgs, "github.com/rs/zerolog")
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
fmt.Fprintln(os.Stderr, "you must provide exactly one package path")
|
fmt.Fprintln(os.Stderr, "you must provide exactly one package path")
|
||||||
|
@ -49,14 +49,14 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the tuxpa.in/a/zlog.Event type
|
// get the github.com/rs/zerolog.Event type
|
||||||
event := getEvent(p)
|
event := getEvent(p)
|
||||||
if event == nil {
|
if event == nil {
|
||||||
fmt.Fprintln(os.Stderr, "Error: tuxpa.in/a/zlog.Event declaration not found, maybe zlog is not imported in the scanned package?")
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get all selections (function calls) with the tuxpa.in/a/zlog.Event (or pointer) receiver
|
// get all selections (function calls) with the github.com/rs/zerolog.Event (or pointer) receiver
|
||||||
selections := getSelectionsWithReceiverType(p, event)
|
selections := getSelectionsWithReceiverType(p, event)
|
||||||
|
|
||||||
// print the violations (if any)
|
// print the violations (if any)
|
||||||
|
@ -80,7 +80,7 @@ func main() {
|
||||||
|
|
||||||
func getEvent(p *loader.Program) types.Type {
|
func getEvent(p *loader.Program) types.Type {
|
||||||
for _, pkg := range p.AllPackages {
|
for _, pkg := range p.AllPackages {
|
||||||
if strings.HasSuffix(pkg.Pkg.Path(), "tuxpa.in/a/zlog") {
|
if strings.HasSuffix(pkg.Pkg.Path(), "github.com/rs/zerolog") {
|
||||||
for _, d := range pkg.Defs {
|
for _, d := range pkg.Defs {
|
||||||
if d != nil && d.Name() == "Event" {
|
if d != nil && d.Name() == "Event" {
|
||||||
return d.Type()
|
return d.Type()
|
||||||
|
|
|
@ -4,13 +4,13 @@ This is a basic linter that checks for missing log event finishers. Finds errors
|
||||||
|
|
||||||
## Problem
|
## Problem
|
||||||
|
|
||||||
When using zlog 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:
|
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 **zlog** 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.
|
> 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
|
## Solution
|
||||||
|
|
||||||
A basic linter like this one here that looks for method invocations on `zlog.Event` can examine the last call in a method call chain and check if it is a finisher, thus pointing out these errors.
|
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
|
## Usage
|
||||||
|
|
||||||
|
@ -27,11 +27,11 @@ The command accepts only one argument - the package to be inspected - and 4 opti
|
||||||
- ignoreFile
|
- ignoreFile
|
||||||
- which files to ignore, either by full path or by go path (package/file.go)
|
- which files to ignore, either by full path or by go path (package/file.go)
|
||||||
- ignorePkg
|
- ignorePkg
|
||||||
- do not inspect the specified package if found in the dependency tree
|
- do not inspect the specified package if found in the dependecy tree
|
||||||
- ignorePkgRecursively
|
- ignorePkgRecursively
|
||||||
- do not inspect the specified package or its subpackages if found in the dependency tree
|
- do not inspect the specified package or its subpackages if found in the dependency tree
|
||||||
|
|
||||||
## Drawbacks
|
## 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 `zlog.Event` the linter will flag it because you are obviously not finishing the event. This will be solved in later release.
|
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.
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
# Zerolog PrettyLog
|
|
||||||
|
|
||||||
This is a basic CLI utility that will colorize and pretty print your structured JSON logs.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
You can compile it or run it directly. The only issue is that by default Zerolog does not output to `stdout`
|
|
||||||
but rather to `stderr` so we must pipe `stderr` stream to this CLI tool.
|
|
||||||
|
|
||||||
### Linux
|
|
||||||
|
|
||||||
These commands will redirect `stderr` to our `prettylog` tool and `stdout` will remain unaffected.
|
|
||||||
|
|
||||||
1. Compiled version
|
|
||||||
|
|
||||||
```shell
|
|
||||||
some_program_with_zerolog 2> >(prettylog)
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Run it directly with `go run`
|
|
||||||
|
|
||||||
```shell
|
|
||||||
some_program_with_zerolog 2> >(go run cmd/prettylog/prettylog.go)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Windows
|
|
||||||
|
|
||||||
These commands will redirect `stderr` to `stdout` and then pipe it to our `prettylog` tool.
|
|
||||||
|
|
||||||
1. Compiled version
|
|
||||||
|
|
||||||
```shell
|
|
||||||
some_program_with_zerolog 2>&1 | prettylog
|
|
||||||
```
|
|
||||||
|
|
||||||
2. Run it directly with `go run`
|
|
||||||
|
|
||||||
```shell
|
|
||||||
some_program_with_zerolog 2>&1 | go run cmd/prettylog/prettylog.go
|
|
||||||
```
|
|
|
@ -1,26 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
)
|
|
||||||
|
|
||||||
func isInputFromPipe() bool {
|
|
||||||
fileInfo, _ := os.Stdin.Stat()
|
|
||||||
return fileInfo.Mode()&os.ModeCharDevice == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
if !isInputFromPipe() {
|
|
||||||
fmt.Println("The command is intended to work with pipes.")
|
|
||||||
fmt.Println("Usage: app_with_zerolog | 2> >(prettylog)")
|
|
||||||
os.Exit(1)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writer := zlog.NewConsoleWriter()
|
|
||||||
_, _ = io.Copy(writer, os.Stdin)
|
|
||||||
}
|
|
131
console.go
131
console.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -6,14 +6,16 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
"github.com/mattn/go-colorable"
|
const (
|
||||||
|
colorBold = iota + 1
|
||||||
|
colorFaint
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -25,9 +27,6 @@ const (
|
||||||
colorMagenta
|
colorMagenta
|
||||||
colorCyan
|
colorCyan
|
||||||
colorWhite
|
colorWhite
|
||||||
|
|
||||||
colorBold = 1
|
|
||||||
colorDarkGray = 90
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -60,12 +59,6 @@ type ConsoleWriter struct {
|
||||||
// PartsOrder defines the order of parts in output.
|
// PartsOrder defines the order of parts in output.
|
||||||
PartsOrder []string
|
PartsOrder []string
|
||||||
|
|
||||||
// PartsExclude defines parts to not display in output.
|
|
||||||
PartsExclude []string
|
|
||||||
|
|
||||||
// FieldsExclude defines contextual fields to not display in output.
|
|
||||||
FieldsExclude []string
|
|
||||||
|
|
||||||
FormatTimestamp Formatter
|
FormatTimestamp Formatter
|
||||||
FormatLevel Formatter
|
FormatLevel Formatter
|
||||||
FormatCaller Formatter
|
FormatCaller Formatter
|
||||||
|
@ -74,8 +67,6 @@ type ConsoleWriter struct {
|
||||||
FormatFieldValue Formatter
|
FormatFieldValue Formatter
|
||||||
FormatErrFieldName Formatter
|
FormatErrFieldName Formatter
|
||||||
FormatErrFieldValue Formatter
|
FormatErrFieldValue Formatter
|
||||||
|
|
||||||
FormatExtra func(map[string]interface{}, *bytes.Buffer) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConsoleWriter creates and initializes a new ConsoleWriter.
|
// NewConsoleWriter creates and initializes a new ConsoleWriter.
|
||||||
|
@ -90,21 +81,11 @@ func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter {
|
||||||
opt(&w)
|
opt(&w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fix color on Windows
|
|
||||||
if w.Out == os.Stdout || w.Out == os.Stderr {
|
|
||||||
w.Out = colorable.NewColorable(w.Out.(*os.File))
|
|
||||||
}
|
|
||||||
|
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write transforms the JSON input with formatters and appends to w.Out.
|
// Write transforms the JSON input with formatters and appends to w.Out.
|
||||||
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
||||||
// Fix color on Windows
|
|
||||||
if w.Out == os.Stdout || w.Out == os.Stderr {
|
|
||||||
w.Out = colorable.NewColorable(w.Out.(*os.File))
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.PartsOrder == nil {
|
if w.PartsOrder == nil {
|
||||||
w.PartsOrder = consoleDefaultPartsOrder()
|
w.PartsOrder = consoleDefaultPartsOrder()
|
||||||
}
|
}
|
||||||
|
@ -130,18 +111,10 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
||||||
|
|
||||||
w.writeFields(evt, buf)
|
w.writeFields(evt, buf)
|
||||||
|
|
||||||
if w.FormatExtra != nil {
|
|
||||||
err = w.FormatExtra(evt, buf)
|
|
||||||
if err != nil {
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = buf.WriteByte('\n')
|
err = buf.WriteByte('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = buf.WriteTo(w.Out)
|
_, err = buf.WriteTo(w.Out)
|
||||||
return len(p), err
|
return len(p), err
|
||||||
}
|
}
|
||||||
|
@ -150,17 +123,6 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
|
||||||
func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {
|
func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer) {
|
||||||
var fields = make([]string, 0, len(evt))
|
var fields = make([]string, 0, len(evt))
|
||||||
for field := range evt {
|
for field := range evt {
|
||||||
var isExcluded bool
|
|
||||||
for _, excluded := range w.FieldsExclude {
|
|
||||||
if field == excluded {
|
|
||||||
isExcluded = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if isExcluded {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
switch field {
|
switch field {
|
||||||
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
|
case LevelFieldName, TimestampFieldName, MessageFieldName, CallerFieldName:
|
||||||
continue
|
continue
|
||||||
|
@ -169,8 +131,7 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
|
||||||
}
|
}
|
||||||
sort.Strings(fields)
|
sort.Strings(fields)
|
||||||
|
|
||||||
// Write space only if something has already been written to the buffer, and if there are fields.
|
if len(fields) > 0 {
|
||||||
if buf.Len() > 0 && len(fields) > 0 {
|
|
||||||
buf.WriteByte(' ')
|
buf.WriteByte(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -231,7 +192,7 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
|
||||||
case json.Number:
|
case json.Number:
|
||||||
buf.WriteString(fv(fValue))
|
buf.WriteString(fv(fValue))
|
||||||
default:
|
default:
|
||||||
b, err := InterfaceMarshalFunc(fValue)
|
b, err := json.Marshal(fValue)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(buf, colorize("[error: %v]", colorRed, w.NoColor), err)
|
fmt.Fprintf(buf, colorize("[error: %v]", colorRed, w.NoColor), err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -249,14 +210,6 @@ func (w ConsoleWriter) writeFields(evt map[string]interface{}, buf *bytes.Buffer
|
||||||
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
|
func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{}, p string) {
|
||||||
var f Formatter
|
var f Formatter
|
||||||
|
|
||||||
if w.PartsExclude != nil && len(w.PartsExclude) > 0 {
|
|
||||||
for _, exclude := range w.PartsExclude {
|
|
||||||
if exclude == p {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch p {
|
switch p {
|
||||||
case LevelFieldName:
|
case LevelFieldName:
|
||||||
if w.FormatLevel == nil {
|
if w.FormatLevel == nil {
|
||||||
|
@ -293,10 +246,10 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
|
||||||
var s = f(evt[p])
|
var s = f(evt[p])
|
||||||
|
|
||||||
if len(s) > 0 {
|
if len(s) > 0 {
|
||||||
if buf.Len() > 0 {
|
|
||||||
buf.WriteByte(' ') // Write space only if not the first part
|
|
||||||
}
|
|
||||||
buf.WriteString(s)
|
buf.WriteString(s)
|
||||||
|
if p != w.PartsOrder[len(w.PartsOrder)-1] { // Skip space for last part
|
||||||
|
buf.WriteByte(' ')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,35 +290,16 @@ func consoleDefaultFormatTimestamp(timeFormat string, noColor bool) Formatter {
|
||||||
t := "<nil>"
|
t := "<nil>"
|
||||||
switch tt := i.(type) {
|
switch tt := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
ts, err := time.ParseInLocation(TimeFieldFormat, tt, time.Local)
|
ts, err := time.Parse(TimeFieldFormat, tt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t = tt
|
t = tt
|
||||||
} else {
|
} else {
|
||||||
t = ts.Local().Format(timeFormat)
|
|
||||||
}
|
|
||||||
case json.Number:
|
|
||||||
i, err := tt.Int64()
|
|
||||||
if err != nil {
|
|
||||||
t = tt.String()
|
|
||||||
} else {
|
|
||||||
var sec, nsec int64
|
|
||||||
|
|
||||||
switch TimeFieldFormat {
|
|
||||||
case TimeFormatUnixNano:
|
|
||||||
sec, nsec = 0, i
|
|
||||||
case TimeFormatUnixMicro:
|
|
||||||
sec, nsec = 0, int64(time.Duration(i)*time.Microsecond)
|
|
||||||
case TimeFormatUnixMs:
|
|
||||||
sec, nsec = 0, int64(time.Duration(i)*time.Millisecond)
|
|
||||||
default:
|
|
||||||
sec, nsec = i, 0
|
|
||||||
}
|
|
||||||
|
|
||||||
ts := time.Unix(sec, nsec)
|
|
||||||
t = ts.Format(timeFormat)
|
t = ts.Format(timeFormat)
|
||||||
}
|
}
|
||||||
|
case json.Number:
|
||||||
|
t = tt.String()
|
||||||
}
|
}
|
||||||
return colorize(t, colorDarkGray, noColor)
|
return colorize(t, colorFaint, noColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -374,30 +308,24 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
|
||||||
var l string
|
var l string
|
||||||
if ll, ok := i.(string); ok {
|
if ll, ok := i.(string); ok {
|
||||||
switch ll {
|
switch ll {
|
||||||
case LevelTraceValue:
|
case "debug":
|
||||||
l = colorize("TRC", colorMagenta, noColor)
|
|
||||||
case LevelDebugValue:
|
|
||||||
l = colorize("DBG", colorYellow, noColor)
|
l = colorize("DBG", colorYellow, noColor)
|
||||||
case LevelInfoValue:
|
case "info":
|
||||||
l = colorize("INF", colorGreen, noColor)
|
l = colorize("INF", colorGreen, noColor)
|
||||||
case LevelWarnValue:
|
case "warn":
|
||||||
l = colorize("WRN", colorRed, noColor)
|
l = colorize("WRN", colorRed, noColor)
|
||||||
case LevelErrorValue:
|
case "error":
|
||||||
l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
|
l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
|
||||||
case LevelFatalValue:
|
case "fatal":
|
||||||
l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
|
l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
|
||||||
case LevelPanicValue:
|
case "panic":
|
||||||
l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
|
l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
|
||||||
default:
|
default:
|
||||||
l = colorize(ll, colorBold, noColor)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if i == nil {
|
|
||||||
l = colorize("???", colorBold, noColor)
|
l = colorize("???", colorBold, noColor)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
|
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -409,27 +337,24 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
|
||||||
c = cc
|
c = cc
|
||||||
}
|
}
|
||||||
if len(c) > 0 {
|
if len(c) > 0 {
|
||||||
if cwd, err := os.Getwd(); err == nil {
|
cwd, err := os.Getwd()
|
||||||
if rel, err := filepath.Rel(cwd, c); err == nil {
|
if err == nil {
|
||||||
c = rel
|
c = strings.TrimPrefix(c, cwd)
|
||||||
|
c = strings.TrimPrefix(c, "/")
|
||||||
}
|
}
|
||||||
}
|
c = colorize(c, colorBold, noColor) + colorize(" >", colorFaint, noColor)
|
||||||
c = colorize(c, colorBold, noColor) + colorize(" >", colorCyan, noColor)
|
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func consoleDefaultFormatMessage(i interface{}) string {
|
func consoleDefaultFormatMessage(i interface{}) string {
|
||||||
if i == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("%s", i)
|
return fmt.Sprintf("%s", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func consoleDefaultFormatFieldName(noColor bool) Formatter {
|
func consoleDefaultFormatFieldName(noColor bool) Formatter {
|
||||||
return func(i interface{}) string {
|
return func(i interface{}) string {
|
||||||
return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
|
return colorize(fmt.Sprintf("%s=", i), colorFaint, noColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,7 +364,7 @@ func consoleDefaultFormatFieldValue(i interface{}) string {
|
||||||
|
|
||||||
func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
|
func consoleDefaultFormatErrFieldName(noColor bool) Formatter {
|
||||||
return func(i interface{}) string {
|
return func(i interface{}) string {
|
||||||
return colorize(fmt.Sprintf("%s=", i), colorCyan, noColor)
|
return colorize(fmt.Sprintf("%s=", i), colorRed, noColor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
260
console_test.go
260
console_test.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog_test
|
package zerolog_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -9,39 +9,39 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleConsoleWriter() {
|
func ExampleConsoleWriter() {
|
||||||
log := zlog.New(zlog.ConsoleWriter{Out: os.Stdout, NoColor: true})
|
log := zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true})
|
||||||
|
|
||||||
log.Info().Str("foo", "bar").Msg("Hello World")
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
||||||
// Output: <nil> INF Hello World foo=bar
|
// Output: <nil> INF Hello World foo=bar
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleConsoleWriter_customFormatters() {
|
func ExampleConsoleWriter_customFormatters() {
|
||||||
out := zlog.ConsoleWriter{Out: os.Stdout, NoColor: true}
|
out := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: true}
|
||||||
out.FormatLevel = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%-6s|", i)) }
|
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.FormatFieldName = func(i interface{}) string { return fmt.Sprintf("%s:", i) }
|
||||||
out.FormatFieldValue = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }
|
out.FormatFieldValue = func(i interface{}) string { return strings.ToUpper(fmt.Sprintf("%s", i)) }
|
||||||
log := zlog.New(out)
|
log := zerolog.New(out)
|
||||||
|
|
||||||
log.Info().Str("foo", "bar").Msg("Hello World")
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
||||||
// Output: <nil> INFO | Hello World foo:BAR
|
// Output: <nil> INFO | Hello World foo:BAR
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNewConsoleWriter() {
|
func ExampleNewConsoleWriter() {
|
||||||
out := zlog.NewConsoleWriter()
|
out := zerolog.NewConsoleWriter()
|
||||||
out.NoColor = true // For testing purposes only
|
out.NoColor = true // For testing purposes only
|
||||||
log := zlog.New(out)
|
log := zerolog.New(out)
|
||||||
|
|
||||||
log.Debug().Str("foo", "bar").Msg("Hello World")
|
log.Debug().Str("foo", "bar").Msg("Hello World")
|
||||||
// Output: <nil> DBG Hello World foo=bar
|
// Output: <nil> DBG Hello World foo=bar
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleNewConsoleWriter_customFormatters() {
|
func ExampleNewConsoleWriter_customFormatters() {
|
||||||
out := zlog.NewConsoleWriter(
|
out := zerolog.NewConsoleWriter(
|
||||||
func(w *zlog.ConsoleWriter) {
|
func(w *zerolog.ConsoleWriter) {
|
||||||
// Customize time format
|
// Customize time format
|
||||||
w.TimeFormat = time.RFC822
|
w.TimeFormat = time.RFC822
|
||||||
// Customize level formatting
|
// Customize level formatting
|
||||||
|
@ -50,7 +50,7 @@ func ExampleNewConsoleWriter_customFormatters() {
|
||||||
)
|
)
|
||||||
out.NoColor = true // For testing purposes only
|
out.NoColor = true // For testing purposes only
|
||||||
|
|
||||||
log := zlog.New(out)
|
log := zerolog.New(out)
|
||||||
|
|
||||||
log.Info().Str("foo", "bar").Msg("Hello World")
|
log.Info().Str("foo", "bar").Msg("Hello World")
|
||||||
// Output: <nil> [INFO ] Hello World foo=bar
|
// Output: <nil> [INFO ] Hello World foo=bar
|
||||||
|
@ -59,7 +59,7 @@ func ExampleNewConsoleWriter_customFormatters() {
|
||||||
func TestConsoleLogger(t *testing.T) {
|
func TestConsoleLogger(t *testing.T) {
|
||||||
t.Run("Numbers", func(t *testing.T) {
|
t.Run("Numbers", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
log := zlog.New(zlog.ConsoleWriter{Out: buf, NoColor: true})
|
log := zerolog.New(zerolog.ConsoleWriter{Out: buf, NoColor: true})
|
||||||
log.Info().
|
log.Info().
|
||||||
Float64("float", 1.23).
|
Float64("float", 1.23).
|
||||||
Uint64("small", 123).
|
Uint64("small", 123).
|
||||||
|
@ -74,7 +74,7 @@ func TestConsoleLogger(t *testing.T) {
|
||||||
func TestConsoleWriter(t *testing.T) {
|
func TestConsoleWriter(t *testing.T) {
|
||||||
t.Run("Default field formatter", func(t *testing.T) {
|
t.Run("Default field formatter", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"foo"}}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"foo"}}
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"foo" : "DEFAULT"}`))
|
_, err := w.Write([]byte(`{"foo" : "DEFAULT"}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -90,14 +90,14 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Write colorized", func(t *testing.T) {
|
t.Run("Write colorized", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: false}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"level" : "warn", "message" : "Foobar"}`))
|
_, err := w.Write([]byte(`{"level" : "warn", "message" : "Foobar"}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
|
expectedOutput := "\x1b[2m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
@ -106,130 +106,15 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Write fields", func(t *testing.T) {
|
t.Run("Write fields", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
|
||||||
|
|
||||||
ts := time.Unix(0, 0)
|
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
||||||
d := ts.UTC().Format(time.RFC3339)
|
|
||||||
_, err := w.Write([]byte(`{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar"}`))
|
_, err := w.Write([]byte(`{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar"}`))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := ts.Format(time.Kitchen) + " DBG Foobar foo=bar\n"
|
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("Unix timestamp input format", func(t *testing.T) {
|
|
||||||
of := zlog.TimeFieldFormat
|
|
||||||
defer func() {
|
|
||||||
zlog.TimeFieldFormat = of
|
|
||||||
}()
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnix
|
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"time": 1234, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := time.Unix(1234, 0).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Unix timestamp ms input format", func(t *testing.T) {
|
|
||||||
of := zlog.TimeFieldFormat
|
|
||||||
defer func() {
|
|
||||||
zlog.TimeFieldFormat = of
|
|
||||||
}()
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnixMs
|
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMilli, NoColor: true}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"time": 1234567, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := time.Unix(1234, 567000000).Format(time.StampMilli) + " DBG Foobar foo=bar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Unix timestamp us input format", func(t *testing.T) {
|
|
||||||
of := zlog.TimeFieldFormat
|
|
||||||
defer func() {
|
|
||||||
zlog.TimeFieldFormat = of
|
|
||||||
}()
|
|
||||||
zlog.TimeFieldFormat = zlog.TimeFormatUnixMicro
|
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, TimeFormat: time.StampMicro, NoColor: true}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"time": 1234567891, "level": "debug", "message": "Foobar", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := time.Unix(1234, 567891000).Format(time.StampMicro) + " DBG Foobar foo=bar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("No message field", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"level": "debug", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "<nil> DBG foo=bar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("No level field", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"message": "Foobar", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "<nil> ??? Foobar foo=bar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Write colorized fields", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: false}
|
|
||||||
|
|
||||||
_, err := w.Write([]byte(`{"level": "warn", "message": "Foobar", "foo": "bar"}`))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "\x1b[90m<nil>\x1b[0m \x1b[31mWRN\x1b[0m Foobar \x1b[36mfoo=\x1b[0mbar\n"
|
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
@ -238,10 +123,9 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Write error field", func(t *testing.T) {
|
t.Run("Write error field", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
|
||||||
|
|
||||||
ts := time.Unix(0, 0)
|
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
||||||
d := ts.UTC().Format(time.RFC3339)
|
|
||||||
evt := `{"time" : "` + d + `", "level" : "error", "message" : "Foobar", "aaa" : "bbb", "error" : "Error"}`
|
evt := `{"time" : "` + d + `", "level" : "error", "message" : "Foobar", "aaa" : "bbb", "error" : "Error"}`
|
||||||
// t.Log(evt)
|
// t.Log(evt)
|
||||||
|
|
||||||
|
@ -250,7 +134,7 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := ts.Format(time.Kitchen) + " ERR Foobar error=Error aaa=bbb\n"
|
expectedOutput := "12:00AM ERR Foobar error=Error aaa=bbb\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
@ -259,15 +143,14 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Write caller field", func(t *testing.T) {
|
t.Run("Write caller field", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Cannot get working directory: %s", err)
|
t.Fatalf("Cannot get working directory: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ts := time.Unix(0, 0)
|
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
||||||
d := ts.UTC().Format(time.RFC3339)
|
|
||||||
evt := `{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar", "caller" : "` + cwd + `/foo/bar.go"}`
|
evt := `{"time" : "` + d + `", "level" : "debug", "message" : "Foobar", "foo" : "bar", "caller" : "` + cwd + `/foo/bar.go"}`
|
||||||
// t.Log(evt)
|
// t.Log(evt)
|
||||||
|
|
||||||
|
@ -276,7 +159,7 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := ts.Format(time.Kitchen) + " DBG foo/bar.go > Foobar foo=bar\n"
|
expectedOutput := "12:00AM DBG foo/bar.go > Foobar foo=bar\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
@ -285,7 +168,7 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Write JSON field", func(t *testing.T) {
|
t.Run("Write JSON field", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true}
|
||||||
|
|
||||||
evt := `{"level" : "debug", "message" : "Foobar", "foo" : [1, 2, 3], "bar" : true}`
|
evt := `{"level" : "debug", "message" : "Foobar", "foo" : [1, 2, 3], "bar" : true}`
|
||||||
// t.Log(evt)
|
// t.Log(evt)
|
||||||
|
@ -306,10 +189,9 @@ func TestConsoleWriter(t *testing.T) {
|
||||||
func TestConsoleWriterConfiguration(t *testing.T) {
|
func TestConsoleWriterConfiguration(t *testing.T) {
|
||||||
t.Run("Sets TimeFormat", func(t *testing.T) {
|
t.Run("Sets TimeFormat", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: time.RFC3339}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: time.RFC3339}
|
||||||
|
|
||||||
ts := time.Unix(0, 0)
|
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
||||||
d := ts.UTC().Format(time.RFC3339)
|
|
||||||
evt := `{"time" : "` + d + `", "level" : "info", "message" : "Foobar"}`
|
evt := `{"time" : "` + d + `", "level" : "info", "message" : "Foobar"}`
|
||||||
|
|
||||||
_, err := w.Write([]byte(evt))
|
_, err := w.Write([]byte(evt))
|
||||||
|
@ -317,7 +199,7 @@ func TestConsoleWriterConfiguration(t *testing.T) {
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
t.Errorf("Unexpected error when writing output: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expectedOutput := ts.Format(time.RFC3339) + " INF Foobar\n"
|
expectedOutput := "1970-01-01T00:00:00Z INF Foobar\n"
|
||||||
actualOutput := buf.String()
|
actualOutput := buf.String()
|
||||||
if actualOutput != expectedOutput {
|
if actualOutput != expectedOutput {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
|
@ -326,7 +208,7 @@ func TestConsoleWriterConfiguration(t *testing.T) {
|
||||||
|
|
||||||
t.Run("Sets PartsOrder", func(t *testing.T) {
|
t.Run("Sets PartsOrder", func(t *testing.T) {
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}
|
w := zerolog.ConsoleWriter{Out: buf, NoColor: true, PartsOrder: []string{"message", "level"}}
|
||||||
|
|
||||||
evt := `{"level" : "info", "message" : "Foobar"}`
|
evt := `{"level" : "info", "message" : "Foobar"}`
|
||||||
_, err := w.Write([]byte(evt))
|
_, err := w.Write([]byte(evt))
|
||||||
|
@ -340,90 +222,6 @@ func TestConsoleWriterConfiguration(t *testing.T) {
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Sets PartsExclude", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, PartsExclude: []string{"time"}}
|
|
||||||
|
|
||||||
d := time.Unix(0, 0).UTC().Format(time.RFC3339)
|
|
||||||
evt := `{"time": "` + d + `", "level": "info", "message": "Foobar"}`
|
|
||||||
_, err := w.Write([]byte(evt))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "INF Foobar\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Sets FieldsExclude", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, FieldsExclude: []string{"foo"}}
|
|
||||||
|
|
||||||
evt := `{"level": "info", "message": "Foobar", "foo":"bar", "baz":"quux"}`
|
|
||||||
_, err := w.Write([]byte(evt))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "<nil> INF Foobar baz=quux\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Sets FormatExtra", func(t *testing.T) {
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{
|
|
||||||
Out: buf, NoColor: true, PartsOrder: []string{"level", "message"},
|
|
||||||
FormatExtra: func(evt map[string]interface{}, buf *bytes.Buffer) error {
|
|
||||||
buf.WriteString("\nAdditional stacktrace")
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
evt := `{"level": "info", "message": "Foobar"}`
|
|
||||||
_, err := w.Write([]byte(evt))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expectedOutput := "INF Foobar\nAdditional stacktrace\n"
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("Uses local time for console writer without time zone", func(t *testing.T) {
|
|
||||||
// Regression test for issue #483 (check there for more details)
|
|
||||||
|
|
||||||
timeFormat := "2006-01-02 15:04:05"
|
|
||||||
expectedOutput := "2022-10-20 20:24:50 INF Foobar\n"
|
|
||||||
evt := `{"time": "2022-10-20 20:24:50", "level": "info", "message": "Foobar"}`
|
|
||||||
|
|
||||||
of := zlog.TimeFieldFormat
|
|
||||||
defer func() {
|
|
||||||
zlog.TimeFieldFormat = of
|
|
||||||
}()
|
|
||||||
zlog.TimeFieldFormat = timeFormat
|
|
||||||
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
w := zlog.ConsoleWriter{Out: buf, NoColor: true, TimeFormat: timeFormat}
|
|
||||||
_, err := w.Write([]byte(evt))
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error when writing output: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
actualOutput := buf.String()
|
|
||||||
if actualOutput != expectedOutput {
|
|
||||||
t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkConsoleWriter(b *testing.B) {
|
func BenchmarkConsoleWriter(b *testing.B) {
|
||||||
|
@ -432,7 +230,7 @@ func BenchmarkConsoleWriter(b *testing.B) {
|
||||||
|
|
||||||
var msg = []byte(`{"level" : "info", "foo" : "bar", "message" : "HELLO", "time" : "1990-01-01"}`)
|
var msg = []byte(`{"level" : "info", "foo" : "bar", "message" : "HELLO", "time" : "1990-01-01"}`)
|
||||||
|
|
||||||
w := zlog.ConsoleWriter{Out: ioutil.Discard, NoColor: false}
|
w := zerolog.ConsoleWriter{Out: ioutil.Discard, NoColor: false}
|
||||||
|
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
w.Write(msg)
|
w.Write(msg)
|
||||||
|
|
87
context.go
87
context.go
|
@ -1,9 +1,7 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -18,10 +16,8 @@ func (c Context) Logger() Logger {
|
||||||
return c.l
|
return c.l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields is a helper function to use a map or slice to set fields using type assertion.
|
// Fields is a helper function to use a map to set fields using type assertion.
|
||||||
// Only map[string]interface{} and []interface{} are accepted. []interface{} must
|
func (c Context) Fields(fields map[string]interface{}) Context {
|
||||||
// alternate string keys and arbitrary values, and extraneous ones are ignored.
|
|
||||||
func (c Context) Fields(fields interface{}) Context {
|
|
||||||
c.l.context = appendFields(c.l.context, fields)
|
c.l.context = appendFields(c.l.context, fields)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
@ -35,7 +31,7 @@ func (c Context) Dict(key string, dict *Event) Context {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array adds the field key with an array to the event context.
|
// Array adds the field key with an array to the event context.
|
||||||
// Use zlog.Arr() to create the array or pass a type that
|
// Use zerolog.Arr() to create the array or pass a type that
|
||||||
// implement the LogArrayMarshaler interface.
|
// implement the LogArrayMarshaler interface.
|
||||||
func (c Context) Array(key string, arr LogArrayMarshaler) Context {
|
func (c Context) Array(key string, arr LogArrayMarshaler) Context {
|
||||||
c.l.context = enc.AppendKey(c.l.context, key)
|
c.l.context = enc.AppendKey(c.l.context, key)
|
||||||
|
@ -84,17 +80,6 @@ func (c Context) Strs(key string, vals []string) Context {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stringer adds the field key with val.String() (or null if val is nil) to the logger context.
|
|
||||||
func (c Context) Stringer(key string, val fmt.Stringer) Context {
|
|
||||||
if val != nil {
|
|
||||||
c.l.context = enc.AppendString(enc.AppendKey(c.l.context, key), val.String())
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
c.l.context = enc.AppendInterface(enc.AppendKey(c.l.context, key), nil)
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bytes adds the field key with val as a []byte to the logger context.
|
// Bytes adds the field key with val as a []byte to the logger context.
|
||||||
func (c Context) Bytes(key string, val []byte) Context {
|
func (c Context) Bytes(key string, val []byte) Context {
|
||||||
c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val)
|
c.l.context = enc.AppendBytes(enc.AppendKey(c.l.context, key), val)
|
||||||
|
@ -118,17 +103,14 @@ func (c Context) RawJSON(key string, b []byte) Context {
|
||||||
|
|
||||||
// AnErr adds the field key with serialized err 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 {
|
func (c Context) AnErr(key string, err error) Context {
|
||||||
switch m := ErrorMarshalFunc(err).(type) {
|
marshaled := ErrorMarshalFunc(err)
|
||||||
|
switch m := marshaled.(type) {
|
||||||
case nil:
|
case nil:
|
||||||
return c
|
return c
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
return c.Object(key, m)
|
return c.Object(key, m)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
return c
|
|
||||||
} else {
|
|
||||||
return c.Str(key, m.Error())
|
return c.Str(key, m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
return c.Str(key, m)
|
return c.Str(key, m)
|
||||||
default:
|
default:
|
||||||
|
@ -141,15 +123,12 @@ func (c Context) AnErr(key string, err error) Context {
|
||||||
func (c Context) Errs(key string, errs []error) Context {
|
func (c Context) Errs(key string, errs []error) Context {
|
||||||
arr := Arr()
|
arr := Arr()
|
||||||
for _, err := range errs {
|
for _, err := range errs {
|
||||||
switch m := ErrorMarshalFunc(err).(type) {
|
marshaled := ErrorMarshalFunc(err)
|
||||||
|
switch m := marshaled.(type) {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
arr = arr.Object(m)
|
arr = arr.Object(m)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
arr = arr.Interface(nil)
|
|
||||||
} else {
|
|
||||||
arr = arr.Str(m.Error())
|
arr = arr.Str(m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
arr = arr.Str(m)
|
arr = arr.Str(m)
|
||||||
default:
|
default:
|
||||||
|
@ -329,9 +308,8 @@ func (ts timestampHook) Run(e *Event, level Level, msg string) {
|
||||||
|
|
||||||
var th = timestampHook{}
|
var th = timestampHook{}
|
||||||
|
|
||||||
// Timestamp adds the current local time to the logger context with the "time" key, formatted using zlog.TimeFieldFormat.
|
// Timestamp adds the current local time as UNIX timestamp to the logger context with the "time" key.
|
||||||
// To customize the key name, change zlog.TimestampFieldName.
|
// To customize the key name, change zerolog.TimestampFieldName.
|
||||||
// To customize the time format, change zlog.TimeFieldFormat.
|
|
||||||
//
|
//
|
||||||
// NOTE: It won't dedupe the "time" key if the *Context has one already.
|
// NOTE: It won't dedupe the "time" key if the *Context has one already.
|
||||||
func (c Context) Timestamp() Context {
|
func (c Context) Timestamp() Context {
|
||||||
|
@ -339,13 +317,13 @@ func (c Context) Timestamp() Context {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time adds the field key with t formated as string using zlog.TimeFieldFormat.
|
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
||||||
func (c Context) Time(key string, t time.Time) Context {
|
func (c Context) Time(key string, t time.Time) Context {
|
||||||
c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
c.l.context = enc.AppendTime(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Times adds the field key with t formated as string using zlog.TimeFieldFormat.
|
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
||||||
func (c Context) Times(key string, t []time.Time) Context {
|
func (c Context) Times(key string, t []time.Time) Context {
|
||||||
c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
c.l.context = enc.AppendTimes(enc.AppendKey(c.l.context, key), t, TimeFieldFormat)
|
||||||
return c
|
return c
|
||||||
|
@ -369,49 +347,32 @@ func (c Context) Interface(key string, i interface{}) Context {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
type callerHook struct {
|
type callerHook struct{}
|
||||||
callerSkipFrameCount int
|
|
||||||
}
|
|
||||||
|
|
||||||
func newCallerHook(skipFrameCount int) callerHook {
|
|
||||||
return callerHook{callerSkipFrameCount: skipFrameCount}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ch callerHook) Run(e *Event, level Level, msg string) {
|
func (ch callerHook) Run(e *Event, level Level, msg string) {
|
||||||
switch ch.callerSkipFrameCount {
|
// Three extra frames to skip (added by hook infra).
|
||||||
case useGlobalSkipFrameCount:
|
e.caller(CallerSkipFrameCount + 3)
|
||||||
// Extra frames to skip (added by hook infra).
|
|
||||||
e.caller(CallerSkipFrameCount + contextCallerSkipFrameCount)
|
|
||||||
default:
|
|
||||||
// Extra frames to skip (added by hook infra).
|
|
||||||
e.caller(ch.callerSkipFrameCount + contextCallerSkipFrameCount)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// useGlobalSkipFrameCount acts as a flag to informat callerHook.Run
|
var ch = callerHook{}
|
||||||
// to use the global CallerSkipFrameCount.
|
|
||||||
const useGlobalSkipFrameCount = math.MinInt32
|
|
||||||
|
|
||||||
// ch is the default caller hook using the global CallerSkipFrameCount.
|
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
||||||
var ch = newCallerHook(useGlobalSkipFrameCount)
|
|
||||||
|
|
||||||
// Caller adds the file:line of the caller with the zlog.CallerFieldName key.
|
|
||||||
func (c Context) Caller() Context {
|
func (c Context) Caller() Context {
|
||||||
c.l = c.l.Hook(ch)
|
c.l = c.l.Hook(ch)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// CallerWithSkipFrameCount adds the file:line of the caller with the zlog.CallerFieldName key.
|
type stackTraceHook struct{}
|
||||||
// The specified skipFrameCount int will override the global CallerSkipFrameCount for this context's respective logger.
|
|
||||||
// If set to -1 the global CallerSkipFrameCount will be used.
|
func (sh stackTraceHook) Run(e *Event, level Level, msg string) {
|
||||||
func (c Context) CallerWithSkipFrameCount(skipFrameCount int) Context {
|
e.Stack()
|
||||||
c.l = c.l.Hook(newCallerHook(skipFrameCount))
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sh = stackTraceHook{}
|
||||||
|
|
||||||
// Stack enables stack trace printing for the error passed to Err().
|
// Stack enables stack trace printing for the error passed to Err().
|
||||||
func (c Context) Stack() Context {
|
func (c Context) Stack() Context {
|
||||||
c.l.stack = true
|
c.l = c.l.Hook(sh)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
33
ctx.go
33
ctx.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -7,46 +7,41 @@ import (
|
||||||
var disabledLogger *Logger
|
var disabledLogger *Logger
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
SetGlobalLevel(TraceLevel)
|
|
||||||
l := Nop()
|
l := Nop()
|
||||||
disabledLogger = &l
|
disabledLogger = &l
|
||||||
}
|
}
|
||||||
|
|
||||||
type ctxKey struct{}
|
type ctxKey struct{}
|
||||||
|
|
||||||
// WithContext returns a copy of ctx with the receiver attached. The Logger
|
// WithContext returns a copy of ctx with l associated. If an instance of Logger
|
||||||
// attached to the provided Context (if any) will not be effected. If the
|
// is already in the context, the context is not updated.
|
||||||
// receiver's log level is Disabled it will only be attached to the returned
|
|
||||||
// Context if the provided Context has a previously attached Logger. If the
|
|
||||||
// provided Context has no attached Logger, a Disabled Logger will not be
|
|
||||||
// attached.
|
|
||||||
//
|
//
|
||||||
// Note: to modify the existing Logger attached to a Context (instead of
|
// For instance, to add a field to an existing logger in the context, use this
|
||||||
// replacing it in a new Context), use UpdateContext with the following
|
|
||||||
// notation:
|
// notation:
|
||||||
//
|
//
|
||||||
// ctx := r.Context()
|
// ctx := r.Context()
|
||||||
// l := zlog.Ctx(ctx)
|
// l := zerolog.Ctx(ctx)
|
||||||
// l.UpdateContext(func(c Context) Context {
|
// l.UpdateContext(func(c Context) Context {
|
||||||
// return c.Str("bar", "baz")
|
// return c.Str("bar", "baz")
|
||||||
// })
|
// })
|
||||||
//
|
func (l *Logger) WithContext(ctx context.Context) context.Context {
|
||||||
func (l Logger) WithContext(ctx context.Context) context.Context {
|
if lp, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
||||||
if _, ok := ctx.Value(ctxKey{}).(*Logger); !ok && l.level == Disabled {
|
if lp == l {
|
||||||
|
// Do not store same logger.
|
||||||
|
return ctx
|
||||||
|
}
|
||||||
|
} else if l.level == Disabled {
|
||||||
// Do not store disabled logger.
|
// Do not store disabled logger.
|
||||||
return ctx
|
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
|
// Ctx returns the Logger associated with the ctx. If no logger
|
||||||
// is associated, DefaultContextLogger is returned, unless DefaultContextLogger
|
// is associated, a disabled logger is returned.
|
||||||
// is nil, in which case a disabled logger is returned.
|
|
||||||
func Ctx(ctx context.Context) *Logger {
|
func Ctx(ctx context.Context) *Logger {
|
||||||
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
if l, ok := ctx.Value(ctxKey{}).(*Logger); ok {
|
||||||
return l
|
return l
|
||||||
} else if l = DefaultContextLogger; l != nil {
|
|
||||||
return l
|
|
||||||
}
|
}
|
||||||
return disabledLogger
|
return disabledLogger
|
||||||
}
|
}
|
||||||
|
|
19
ctx_test.go
19
ctx_test.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
@ -27,13 +27,6 @@ func TestCtx(t *testing.T) {
|
||||||
if log2 != disabledLogger {
|
if log2 != disabledLogger {
|
||||||
t.Error("Ctx did not return the expected logger")
|
t.Error("Ctx did not return the expected logger")
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultContextLogger = &log
|
|
||||||
t.Cleanup(func() { DefaultContextLogger = nil })
|
|
||||||
log2 = Ctx(context.Background())
|
|
||||||
if log2 != &log {
|
|
||||||
t.Error("Ctx did not return the expected logger")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCtxDisabled(t *testing.T) {
|
func TestCtxDisabled(t *testing.T) {
|
||||||
|
@ -45,7 +38,7 @@ func TestCtxDisabled(t *testing.T) {
|
||||||
|
|
||||||
l := New(ioutil.Discard).With().Str("foo", "bar").Logger()
|
l := New(ioutil.Discard).With().Str("foo", "bar").Logger()
|
||||||
ctx = l.WithContext(ctx)
|
ctx = l.WithContext(ctx)
|
||||||
if !reflect.DeepEqual(Ctx(ctx), &l) {
|
if Ctx(ctx) != &l {
|
||||||
t.Error("WithContext did not store logger")
|
t.Error("WithContext did not store logger")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,18 +46,18 @@ func TestCtxDisabled(t *testing.T) {
|
||||||
return c.Str("bar", "baz")
|
return c.Str("bar", "baz")
|
||||||
})
|
})
|
||||||
ctx = l.WithContext(ctx)
|
ctx = l.WithContext(ctx)
|
||||||
if !reflect.DeepEqual(Ctx(ctx), &l) {
|
if Ctx(ctx) != &l {
|
||||||
t.Error("WithContext did not store updated logger")
|
t.Error("WithContext did not store updated logger")
|
||||||
}
|
}
|
||||||
|
|
||||||
l = l.Level(DebugLevel)
|
l = l.Level(DebugLevel)
|
||||||
ctx = l.WithContext(ctx)
|
ctx = l.WithContext(ctx)
|
||||||
if !reflect.DeepEqual(Ctx(ctx), &l) {
|
if Ctx(ctx) != &l {
|
||||||
t.Error("WithContext did not store copied logger")
|
t.Error("WithContext did not store copied logger")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx = dl.WithContext(ctx)
|
ctx = dl.WithContext(ctx)
|
||||||
if !reflect.DeepEqual(Ctx(ctx), &dl) {
|
if Ctx(ctx) != &dl {
|
||||||
t.Error("WithContext did not override logger with a disabled logger")
|
t.Error("WithContext did not overide logger with a disabled logger")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog/diode/internal/diodes"
|
"github.com/rs/zerolog/diode/internal/diodes"
|
||||||
)
|
)
|
||||||
|
|
||||||
var bufPool = &sync.Pool{
|
var bufPool = &sync.Pool{
|
||||||
|
@ -42,13 +42,13 @@ type Writer struct {
|
||||||
// wr := diode.NewWriter(w, 1000, 0, func(missed int) {
|
// wr := diode.NewWriter(w, 1000, 0, func(missed int) {
|
||||||
// log.Printf("Dropped %d messages", missed)
|
// log.Printf("Dropped %d messages", missed)
|
||||||
// })
|
// })
|
||||||
// log := zlog.New(wr)
|
// log := zerolog.New(wr)
|
||||||
//
|
//
|
||||||
// If pollInterval is greater than 0, a poller is used otherwise a waiter is
|
// If pollInterval is greater than 0, a poller is used otherwise a waiter is
|
||||||
// used.
|
// used.
|
||||||
//
|
//
|
||||||
// See code.cloudfoundry.org/go-diodes for more info on diode.
|
// See code.cloudfoundry.org/go-diodes for more info on diode.
|
||||||
func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Writer {
|
func NewWriter(w io.Writer, size int, poolInterval time.Duration, f Alerter) Writer {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
dw := Writer{
|
dw := Writer{
|
||||||
w: w,
|
w: w,
|
||||||
|
@ -59,9 +59,9 @@ func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Wri
|
||||||
f = func(int) {}
|
f = func(int) {}
|
||||||
}
|
}
|
||||||
d := diodes.NewManyToOne(size, diodes.AlertFunc(f))
|
d := diodes.NewManyToOne(size, diodes.AlertFunc(f))
|
||||||
if pollInterval > 0 {
|
if poolInterval > 0 {
|
||||||
dw.d = diodes.NewPoller(d,
|
dw.d = diodes.NewPoller(d,
|
||||||
diodes.WithPollingInterval(pollInterval),
|
diodes.WithPollingInterval(poolInterval),
|
||||||
diodes.WithPollingContext(ctx))
|
diodes.WithPollingContext(ctx))
|
||||||
} else {
|
} else {
|
||||||
dw.d = diodes.NewWaiter(d,
|
dw.d = diodes.NewWaiter(d,
|
||||||
|
@ -72,7 +72,7 @@ func NewWriter(w io.Writer, size int, pollInterval time.Duration, f Alerter) Wri
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dw Writer) Write(p []byte) (n int, err error) {
|
func (dw Writer) Write(p []byte) (n int, err error) {
|
||||||
// p is pooled in zlog so we can't hold it passed this call, hence the
|
// p is pooled in zerolog so we can't hold it passed this call, hence the
|
||||||
// copy.
|
// copy.
|
||||||
p = append(bufPool.Get().([]byte), p...)
|
p = append(bufPool.Get().([]byte), p...)
|
||||||
dw.d.Set(diodes.GenericDataType(&p))
|
dw.d.Set(diodes.GenericDataType(&p))
|
||||||
|
|
|
@ -6,15 +6,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/diode"
|
"github.com/rs/zerolog/diode"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleNewWriter() {
|
func ExampleNewWriter() {
|
||||||
w := diode.NewWriter(os.Stdout, 1000, 0, func(missed int) {
|
w := diode.NewWriter(os.Stdout, 1000, 0, func(missed int) {
|
||||||
fmt.Printf("Dropped %d messages\n", missed)
|
fmt.Printf("Dropped %d messages\n", missed)
|
||||||
})
|
})
|
||||||
log := zlog.New(w)
|
log := zerolog.New(w)
|
||||||
log.Print("test")
|
log.Print("test")
|
||||||
|
|
||||||
w.Close()
|
w.Close()
|
||||||
|
|
|
@ -9,9 +9,9 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/diode"
|
"github.com/rs/zerolog/diode"
|
||||||
"tuxpa.in/a/zlog/internal/cbor"
|
"github.com/rs/zerolog/internal/cbor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewWriter(t *testing.T) {
|
func TestNewWriter(t *testing.T) {
|
||||||
|
@ -19,7 +19,7 @@ func TestNewWriter(t *testing.T) {
|
||||||
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
|
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {
|
||||||
fmt.Printf("Dropped %d messages\n", missed)
|
fmt.Printf("Dropped %d messages\n", missed)
|
||||||
})
|
})
|
||||||
log := zlog.New(w)
|
log := zerolog.New(w)
|
||||||
log.Print("test")
|
log.Print("test")
|
||||||
|
|
||||||
w.Close()
|
w.Close()
|
||||||
|
@ -30,14 +30,6 @@ func TestNewWriter(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestClose(t *testing.T) {
|
|
||||||
buf := bytes.Buffer{}
|
|
||||||
w := diode.NewWriter(&buf, 1000, 0, func(missed int) {})
|
|
||||||
log := zlog.New(w)
|
|
||||||
log.Print("test")
|
|
||||||
w.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Benchmark(b *testing.B) {
|
func Benchmark(b *testing.B) {
|
||||||
log.SetOutput(ioutil.Discard)
|
log.SetOutput(ioutil.Discard)
|
||||||
defer log.SetOutput(os.Stderr)
|
defer log.SetOutput(os.Stderr)
|
||||||
|
@ -48,7 +40,7 @@ func Benchmark(b *testing.B) {
|
||||||
for name, interval := range benchs {
|
for name, interval := range benchs {
|
||||||
b.Run(name, func(b *testing.B) {
|
b.Run(name, func(b *testing.B) {
|
||||||
w := diode.NewWriter(ioutil.Discard, 100000, interval, nil)
|
w := diode.NewWriter(ioutil.Discard, 100000, interval, nil)
|
||||||
log := zlog.New(w)
|
log := zerolog.New(w)
|
||||||
defer w.Close()
|
defer w.Close()
|
||||||
|
|
||||||
b.SetParallelism(1000)
|
b.SetParallelism(1000)
|
||||||
|
|
|
@ -9,14 +9,14 @@ import (
|
||||||
// ManyToOne diode is optimal for many writers (go-routines B-n) and a single
|
// 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.
|
// reader (go-routine A). It is not thread safe for multiple readers.
|
||||||
type ManyToOne struct {
|
type ManyToOne struct {
|
||||||
|
buffer []unsafe.Pointer
|
||||||
writeIndex uint64
|
writeIndex uint64
|
||||||
readIndex uint64
|
readIndex uint64
|
||||||
buffer []unsafe.Pointer
|
|
||||||
alerter Alerter
|
alerter Alerter
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewManyToOne creates a new diode (ring buffer). The ManyToOne diode
|
// NewManyToOne creates a new diode (ring buffer). The ManyToOne diode
|
||||||
// is optimized for many writers (on go-routines B-n) and a single reader
|
// 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
|
// (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
|
// called when it notices that the writer go-routine has passed it and wrote
|
||||||
// over data. A nil can be used to ignore alerts.
|
// over data. A nil can be used to ignore alerts.
|
||||||
|
@ -66,7 +66,7 @@ func (d *ManyToOne) Set(data GenericDataType) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryNext will attempt to read from the next slot of the ring buffer.
|
// TryNext will attempt to read from the next slot of the ring buffer.
|
||||||
// If there is no data available, it will return (nil, false).
|
// If there is not data available, it will return (nil, false).
|
||||||
func (d *ManyToOne) TryNext() (data GenericDataType, ok bool) {
|
func (d *ManyToOne) TryNext() (data GenericDataType, ok bool) {
|
||||||
// Read a value from the ring buffer based on the readIndex.
|
// Read a value from the ring buffer based on the readIndex.
|
||||||
idx := d.readIndex % uint64(len(d.buffer))
|
idx := d.readIndex % uint64(len(d.buffer))
|
||||||
|
@ -80,7 +80,7 @@ func (d *ManyToOne) TryNext() (data GenericDataType, ok bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the seq value is less than the current read index that means a
|
// When the seq value is less than the current read index that means a
|
||||||
// value was read from idx that was previously written but since has
|
// 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
|
// been dropped. This value must be ignored and the read head must not
|
||||||
// increment.
|
// increment.
|
||||||
//
|
//
|
||||||
|
|
|
@ -31,9 +31,9 @@ type bucket struct {
|
||||||
// OneToOne diode is meant to be used by a single reader and a single writer.
|
// OneToOne diode is meant to be used by a single reader and a single writer.
|
||||||
// It is not thread safe if used otherwise.
|
// It is not thread safe if used otherwise.
|
||||||
type OneToOne struct {
|
type OneToOne struct {
|
||||||
|
buffer []unsafe.Pointer
|
||||||
writeIndex uint64
|
writeIndex uint64
|
||||||
readIndex uint64
|
readIndex uint64
|
||||||
buffer []unsafe.Pointer
|
|
||||||
alerter Alerter
|
alerter Alerter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func (d *OneToOne) TryNext() (data GenericDataType, ok bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the seq value is less than the current read index that means a
|
// When the seq value is less than the current read index that means a
|
||||||
// value was read from idx that was previously written but since has
|
// 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
|
// been dropped. This value must be ignored and the read head must not
|
||||||
// increment.
|
// increment.
|
||||||
//
|
//
|
||||||
|
|
|
@ -24,18 +24,18 @@ type PollerConfigOption func(*Poller)
|
||||||
// WithPollingInterval sets the interval at which the diode is queried
|
// WithPollingInterval sets the interval at which the diode is queried
|
||||||
// for new data. The default is 10ms.
|
// for new data. The default is 10ms.
|
||||||
func WithPollingInterval(interval time.Duration) PollerConfigOption {
|
func WithPollingInterval(interval time.Duration) PollerConfigOption {
|
||||||
return func(c *Poller) {
|
return PollerConfigOption(func(c *Poller) {
|
||||||
c.interval = interval
|
c.interval = interval
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithPollingContext sets the context to cancel any retrieval (Next()). It
|
// WithPollingContext sets the context to cancel any retrieval (Next()). It
|
||||||
// will not change any results for adding data (Set()). Default is
|
// will not change any results for adding data (Set()). Default is
|
||||||
// context.Background().
|
// context.Background().
|
||||||
func WithPollingContext(ctx context.Context) PollerConfigOption {
|
func WithPollingContext(ctx context.Context) PollerConfigOption {
|
||||||
return func(c *Poller) {
|
return PollerConfigOption(func(c *Poller) {
|
||||||
c.ctx = ctx
|
c.ctx = ctx
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewPoller returns a new Poller that wraps the given diode.
|
// NewPoller returns a new Poller that wraps the given diode.
|
||||||
|
|
|
@ -21,9 +21,9 @@ type WaiterConfigOption func(*Waiter)
|
||||||
// will not change any results for adding data (Set()). Default is
|
// will not change any results for adding data (Set()). Default is
|
||||||
// context.Background().
|
// context.Background().
|
||||||
func WithWaiterContext(ctx context.Context) WaiterConfigOption {
|
func WithWaiterContext(ctx context.Context) WaiterConfigOption {
|
||||||
return func(c *Waiter) {
|
return WaiterConfigOption(func(c *Waiter) {
|
||||||
c.ctx = ctx
|
c.ctx = ctx
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWaiter returns a new Waiter that wraps the given diode.
|
// NewWaiter returns a new Waiter that wraps the given diode.
|
||||||
|
@ -39,12 +39,7 @@ func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-w.ctx.Done()
|
<-w.ctx.Done()
|
||||||
|
|
||||||
// Mutex is strictly necessary here to avoid a race in Next() (between
|
|
||||||
// w.isDone() and w.c.Wait()) and w.c.Broadcast() here.
|
|
||||||
w.mu.Lock()
|
|
||||||
w.c.Broadcast()
|
w.c.Broadcast()
|
||||||
w.mu.Unlock()
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return w
|
return w
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net"
|
"net"
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
// +build binary_log
|
// +build binary_log
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
// This file contains bindings to do binary encoding.
|
// This file contains bindings to do binary encoding.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"tuxpa.in/a/zlog/internal/cbor"
|
"github.com/rs/zerolog/internal/cbor"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -14,19 +14,9 @@ var (
|
||||||
enc = cbor.Encoder{}
|
enc = cbor.Encoder{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
// using closure to reflect the changes at runtime.
|
|
||||||
cbor.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
|
|
||||||
return InterfaceMarshalFunc(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendJSON(dst []byte, j []byte) []byte {
|
func appendJSON(dst []byte, j []byte) []byte {
|
||||||
return cbor.AppendEmbeddedJSON(dst, j)
|
return cbor.AppendEmbeddedJSON(dst, j)
|
||||||
}
|
}
|
||||||
func appendCBOR(dst []byte, c []byte) []byte {
|
|
||||||
return cbor.AppendEmbeddedCBOR(dst, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// decodeIfBinaryToString - converts a binary formatted log msg to a
|
// decodeIfBinaryToString - converts a binary formatted log msg to a
|
||||||
// JSON formatted String Log message.
|
// JSON formatted String Log message.
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
//go:build !binary_log
|
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
// encoder_json.go file contains bindings to generate
|
// encoder_json.go file contains bindings to generate
|
||||||
// JSON encoded byte stream.
|
// JSON encoded byte stream.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"github.com/rs/zerolog/internal/json"
|
||||||
"tuxpa.in/a/zlog/internal/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -17,27 +15,9 @@ var (
|
||||||
enc = json.Encoder{}
|
enc = json.Encoder{}
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
// using closure to reflect the changes at runtime.
|
|
||||||
json.JSONMarshalFunc = func(v interface{}) ([]byte, error) {
|
|
||||||
return InterfaceMarshalFunc(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendJSON(dst []byte, j []byte) []byte {
|
func appendJSON(dst []byte, j []byte) []byte {
|
||||||
return append(dst, j...)
|
return append(dst, j...)
|
||||||
}
|
}
|
||||||
func appendCBOR(dst []byte, cbor []byte) []byte {
|
|
||||||
dst = append(dst, []byte("\"data:application/cbor;base64,")...)
|
|
||||||
l := len(dst)
|
|
||||||
enc := base64.StdEncoding
|
|
||||||
n := enc.EncodedLen(len(cbor))
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
dst = append(dst, '.')
|
|
||||||
}
|
|
||||||
enc.Encode(dst[l:], cbor)
|
|
||||||
return append(dst, '"')
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeIfBinaryToString(in []byte) string {
|
func decodeIfBinaryToString(in []byte) string {
|
||||||
return string(in)
|
return string(in)
|
||||||
|
|
167
event.go
167
event.go
|
@ -1,10 +1,11 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -26,7 +27,6 @@ type Event struct {
|
||||||
done func(msg string)
|
done func(msg string)
|
||||||
stack bool // enable error stack trace
|
stack bool // enable error stack trace
|
||||||
ch []Hook // hooks from context
|
ch []Hook // hooks from context
|
||||||
skipFrame int // The number of additional frames to skip when printing the caller.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func putEvent(e *Event) {
|
func putEvent(e *Event) {
|
||||||
|
@ -62,8 +62,6 @@ func newEvent(w LevelWriter, level Level) *Event {
|
||||||
e.buf = enc.AppendBeginMarker(e.buf)
|
e.buf = enc.AppendBeginMarker(e.buf)
|
||||||
e.w = w
|
e.w = w
|
||||||
e.level = level
|
e.level = level
|
||||||
e.stack = false
|
|
||||||
e.skipFrame = 0
|
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,20 +106,10 @@ func (e *Event) Msg(msg string) {
|
||||||
e.msg(msg)
|
e.msg(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send is equivalent to calling Msg("").
|
// Msgf sends the event with formated msg added as the message field if not empty.
|
||||||
//
|
//
|
||||||
// NOTICE: once this method is called, the *Event should be disposed.
|
// NOTICE: once this methid is called, the *Event should be disposed.
|
||||||
func (e *Event) Send() {
|
// Calling Msg twice can have unexpected result.
|
||||||
if e == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
e.msg("")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Msgf sends the event with formatted msg added as the message field if not empty.
|
|
||||||
//
|
|
||||||
// NOTICE: once this method is called, the *Event should be disposed.
|
|
||||||
// Calling Msgf twice can have unexpected result.
|
|
||||||
func (e *Event) Msgf(format string, v ...interface{}) {
|
func (e *Event) Msgf(format string, v ...interface{}) {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return
|
return
|
||||||
|
@ -129,17 +117,15 @@ func (e *Event) Msgf(format string, v ...interface{}) {
|
||||||
e.msg(fmt.Sprintf(format, v...))
|
e.msg(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) MsgFunc(createMsg func() string) {
|
|
||||||
if e == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
e.msg(createMsg())
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Event) msg(msg string) {
|
func (e *Event) msg(msg string) {
|
||||||
for _, hook := range e.ch {
|
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)
|
hook.Run(e, e.level, msg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if msg != "" {
|
if msg != "" {
|
||||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg)
|
e.buf = enc.AppendString(enc.AppendKey(e.buf, MessageFieldName), msg)
|
||||||
}
|
}
|
||||||
|
@ -150,15 +136,13 @@ func (e *Event) msg(msg string) {
|
||||||
if ErrorHandler != nil {
|
if ErrorHandler != nil {
|
||||||
ErrorHandler(err)
|
ErrorHandler(err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(os.Stderr, "zlog: could not write event: %v\n", err)
|
fmt.Fprintf(os.Stderr, "zerolog: could not write event: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fields is a helper function to use a map or slice to set fields using type assertion.
|
// Fields is a helper function to use a map to set fields using type assertion.
|
||||||
// Only map[string]interface{} and []interface{} are accepted. []interface{} must
|
func (e *Event) Fields(fields map[string]interface{}) *Event {
|
||||||
// alternate string keys and arbitrary values, and extraneous ones are ignored.
|
|
||||||
func (e *Event) Fields(fields interface{}) *Event {
|
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
@ -167,7 +151,7 @@ func (e *Event) Fields(fields interface{}) *Event {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dict adds the field key with a dict to the event context.
|
// Dict adds the field key with a dict to the event context.
|
||||||
// Use zlog.Dict() to create the dictionary.
|
// Use zerolog.Dict() to create the dictionary.
|
||||||
func (e *Event) Dict(key string, dict *Event) *Event {
|
func (e *Event) Dict(key string, dict *Event) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
|
@ -186,7 +170,7 @@ func Dict() *Event {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Array adds the field key with an array to the event context.
|
// Array adds the field key with an array to the event context.
|
||||||
// Use zlog.Arr() to create the array or pass a type that
|
// Use zerolog.Arr() to create the array or pass a type that
|
||||||
// implement the LogArrayMarshaler interface.
|
// implement the LogArrayMarshaler interface.
|
||||||
func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
|
func (e *Event) Array(key string, arr LogArrayMarshaler) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
|
@ -216,32 +200,15 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
e.buf = enc.AppendKey(e.buf, key)
|
e.buf = enc.AppendKey(e.buf, key)
|
||||||
if obj == nil {
|
|
||||||
e.buf = enc.AppendNil(e.buf)
|
|
||||||
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
e.appendObject(obj)
|
e.appendObject(obj)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Func allows an anonymous func to run only if the event is enabled.
|
// Object marshals an object that implement the LogObjectMarshaler interface.
|
||||||
func (e *Event) Func(f func(e *Event)) *Event {
|
|
||||||
if e != nil && e.Enabled() {
|
|
||||||
f(e)
|
|
||||||
}
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// EmbedObject marshals an object that implement the LogObjectMarshaler interface.
|
|
||||||
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
|
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
if obj == nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
obj.MarshalZerologObject(e)
|
obj.MarshalZerologObject(e)
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
@ -264,27 +231,6 @@ func (e *Event) Strs(key string, vals []string) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stringer adds the field key with val.String() (or null if val is nil)
|
|
||||||
// to the *Event context.
|
|
||||||
func (e *Event) Stringer(key string, val fmt.Stringer) *Event {
|
|
||||||
if e == nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
e.buf = enc.AppendStringer(enc.AppendKey(e.buf, key), val)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stringers adds the field key with vals where each individual val
|
|
||||||
// is used as val.String() (or null if val is empty) to the *Event
|
|
||||||
// context.
|
|
||||||
func (e *Event) Stringers(key string, vals []fmt.Stringer) *Event {
|
|
||||||
if e == nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
e.buf = enc.AppendStringers(enc.AppendKey(e.buf, key), vals)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Bytes adds the field key with val as a string to the *Event context.
|
// Bytes adds the field key with val as a string to the *Event context.
|
||||||
//
|
//
|
||||||
// Runes outside of normal ASCII ranges will be hex-encoded in the resulting
|
// Runes outside of normal ASCII ranges will be hex-encoded in the resulting
|
||||||
|
@ -318,18 +264,6 @@ func (e *Event) RawJSON(key string, b []byte) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// RawCBOR adds already encoded CBOR to the log line under key.
|
|
||||||
//
|
|
||||||
// No sanity check is performed on b
|
|
||||||
// Note: The full featureset of CBOR is supported as data will not be mapped to json but stored as data-url
|
|
||||||
func (e *Event) RawCBOR(key string, b []byte) *Event {
|
|
||||||
if e == nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
e.buf = appendCBOR(enc.AppendKey(e.buf, key), b)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// AnErr adds the field key with serialized err to the *Event context.
|
// AnErr adds the field key with serialized err to the *Event context.
|
||||||
// If err is nil, no field is added.
|
// If err is nil, no field is added.
|
||||||
func (e *Event) AnErr(key string, err error) *Event {
|
func (e *Event) AnErr(key string, err error) *Event {
|
||||||
|
@ -342,11 +276,7 @@ func (e *Event) AnErr(key string, err error) *Event {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
return e.Object(key, m)
|
return e.Object(key, m)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
return e
|
|
||||||
} else {
|
|
||||||
return e.Str(key, m.Error())
|
return e.Str(key, m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
return e.Str(key, m)
|
return e.Str(key, m)
|
||||||
default:
|
default:
|
||||||
|
@ -379,12 +309,13 @@ func (e *Event) Errs(key string, errs []error) *Event {
|
||||||
|
|
||||||
// Err adds the field "error" with serialized err to the *Event context.
|
// Err adds the field "error" with serialized err to the *Event context.
|
||||||
// If err is nil, no field is added.
|
// If err is nil, no field is added.
|
||||||
|
// To customize the key name, change zerolog.ErrorFieldName.
|
||||||
//
|
//
|
||||||
// To customize the key name, change zlog.ErrorFieldName.
|
// To customize the key name, change zerolog.ErrorFieldName.
|
||||||
//
|
//
|
||||||
// If Stack() has been called before and zlog.ErrorStackMarshaler is defined,
|
// If Stack() has been called before and zerolog.ErrorStackMarshaler is defined,
|
||||||
// the err is passed to ErrorStackMarshaler and the result is appended to the
|
// the err is passed to ErrorStackMarshaler and the result is appended to the
|
||||||
// zlog.ErrorStackFieldName.
|
// zerolog.ErrorStackFieldName.
|
||||||
func (e *Event) Err(err error) *Event {
|
func (e *Event) Err(err error) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
|
@ -395,9 +326,7 @@ func (e *Event) Err(err error) *Event {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
e.Object(ErrorStackFieldName, m)
|
e.Object(ErrorStackFieldName, m)
|
||||||
case error:
|
case error:
|
||||||
if m != nil && !isNilValue(m) {
|
|
||||||
e.Str(ErrorStackFieldName, m.Error())
|
e.Str(ErrorStackFieldName, m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
e.Str(ErrorStackFieldName, m)
|
e.Str(ErrorStackFieldName, m)
|
||||||
default:
|
default:
|
||||||
|
@ -652,7 +581,7 @@ func (e *Event) Floats64(key string, f []float64) *Event {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key.
|
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key.
|
||||||
// To customize the key name, change zlog.TimestampFieldName.
|
// To customize the key name, change zerolog.TimestampFieldName.
|
||||||
//
|
//
|
||||||
// NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one
|
// NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one
|
||||||
// already.
|
// already.
|
||||||
|
@ -664,7 +593,7 @@ func (e *Event) Timestamp() *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Time adds the field key with t formatted as string using zlog.TimeFieldFormat.
|
// Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
||||||
func (e *Event) Time(key string, t time.Time) *Event {
|
func (e *Event) Time(key string, t time.Time) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
|
@ -673,7 +602,7 @@ func (e *Event) Time(key string, t time.Time) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Times adds the field key with t formatted as string using zlog.TimeFieldFormat.
|
// Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
|
||||||
func (e *Event) Times(key string, t []time.Time) *Event {
|
func (e *Event) Times(key string, t []time.Time) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
|
@ -682,8 +611,8 @@ func (e *Event) Times(key string, t []time.Time) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dur adds the field key with duration d stored as zlog.DurationFieldUnit.
|
// Dur adds the field key with duration d stored as zerolog.DurationFieldUnit.
|
||||||
// If zlog.DurationFieldInteger is true, durations are rendered as integer
|
// If zerolog.DurationFieldInteger is true, durations are rendered as integer
|
||||||
// instead of float.
|
// instead of float.
|
||||||
func (e *Event) Dur(key string, d time.Duration) *Event {
|
func (e *Event) Dur(key string, d time.Duration) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
|
@ -693,8 +622,8 @@ func (e *Event) Dur(key string, d time.Duration) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Durs adds the field key with duration d stored as zlog.DurationFieldUnit.
|
// Durs adds the field key with duration d stored as zerolog.DurationFieldUnit.
|
||||||
// If zlog.DurationFieldInteger is true, durations are rendered as integer
|
// If zerolog.DurationFieldInteger is true, durations are rendered as integer
|
||||||
// instead of float.
|
// instead of float.
|
||||||
func (e *Event) Durs(key string, d []time.Duration) *Event {
|
func (e *Event) Durs(key string, d []time.Duration) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
|
@ -719,11 +648,6 @@ func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Any is a wrapper around Event.Interface.
|
|
||||||
func (e *Event) Any(key string, i interface{}) *Event {
|
|
||||||
return e.Interface(key, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Interface adds the field key with i marshaled using reflection.
|
// Interface adds the field key with i marshaled using reflection.
|
||||||
func (e *Event) Interface(key string, i interface{}) *Event {
|
func (e *Event) Interface(key string, i interface{}) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
|
@ -736,45 +660,20 @@ func (e *Event) Interface(key string, i interface{}) *Event {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
// Type adds the field key with val's type using reflection.
|
// Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
|
||||||
func (e *Event) Type(key string, val interface{}) *Event {
|
func (e *Event) Caller() *Event {
|
||||||
if e == nil {
|
return e.caller(CallerSkipFrameCount)
|
||||||
return e
|
|
||||||
}
|
|
||||||
e.buf = enc.AppendType(enc.AppendKey(e.buf, key), val)
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// CallerSkipFrame instructs any future Caller calls to skip the specified number of frames.
|
|
||||||
// This includes those added via hooks from the context.
|
|
||||||
func (e *Event) CallerSkipFrame(skip int) *Event {
|
|
||||||
if e == nil {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
e.skipFrame += skip
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
|
|
||||||
// Caller adds the file:line of the caller with the zlog.CallerFieldName key.
|
|
||||||
// The argument skip is the number of stack frames to ascend
|
|
||||||
// Skip If not passed, use the global variable CallerSkipFrameCount
|
|
||||||
func (e *Event) Caller(skip ...int) *Event {
|
|
||||||
sk := CallerSkipFrameCount
|
|
||||||
if len(skip) > 0 {
|
|
||||||
sk = skip[0] + CallerSkipFrameCount
|
|
||||||
}
|
|
||||||
return e.caller(sk)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Event) caller(skip int) *Event {
|
func (e *Event) caller(skip int) *Event {
|
||||||
if e == nil {
|
if e == nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
pc, file, line, ok := runtime.Caller(skip + e.skipFrame)
|
_, file, line, ok := runtime.Caller(skip)
|
||||||
if !ok {
|
if !ok {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), CallerMarshalFunc(pc, file, line))
|
e.buf = enc.AppendString(enc.AppendKey(e.buf, CallerFieldName), file+":"+strconv.Itoa(line))
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
// +build !binary_log
|
|
||||||
|
|
||||||
package zlog
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type nilError struct{}
|
|
||||||
|
|
||||||
func (nilError) Error() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEvent_AnErr(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
err error
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"nil", nil, `{}`},
|
|
||||||
{"error", errors.New("test"), `{"err":"test"}`},
|
|
||||||
{"nil interface", func() *nilError { return nil }(), `{}`},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
||||||
e.AnErr("err", tt.err)
|
|
||||||
_ = e.write()
|
|
||||||
if got, want := strings.TrimSpace(buf.String()), tt.want; got != want {
|
|
||||||
t.Errorf("Event.AnErr() = %v, want %v", got, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEvent_ObjectWithNil(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
||||||
_ = e.Object("obj", nil)
|
|
||||||
_ = e.write()
|
|
||||||
|
|
||||||
want := `{"obj":null}`
|
|
||||||
got := strings.TrimSpace(buf.String())
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("Event.Object() = %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEvent_EmbedObjectWithNil(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
e := newEvent(levelWriterAdapter{&buf}, DebugLevel)
|
|
||||||
_ = e.EmbedObject(nil)
|
|
||||||
_ = e.write()
|
|
||||||
|
|
||||||
want := "{}"
|
|
||||||
got := strings.TrimSpace(buf.String())
|
|
||||||
if got != want {
|
|
||||||
t.Errorf("Event.EmbedObject() = %q, want %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
49
fields.go
49
fields.go
|
@ -1,47 +1,20 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net"
|
"net"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isNilValue(i interface{}) bool {
|
func appendFields(dst []byte, fields map[string]interface{}) []byte {
|
||||||
return (*[2]uintptr)(unsafe.Pointer(&i))[1] == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendFields(dst []byte, fields interface{}) []byte {
|
|
||||||
switch fields := fields.(type) {
|
|
||||||
case []interface{}:
|
|
||||||
if n := len(fields); n&0x1 == 1 { // odd number
|
|
||||||
fields = fields[:n-1]
|
|
||||||
}
|
|
||||||
dst = appendFieldList(dst, fields)
|
|
||||||
case map[string]interface{}:
|
|
||||||
keys := make([]string, 0, len(fields))
|
keys := make([]string, 0, len(fields))
|
||||||
for key := range fields {
|
for key := range fields {
|
||||||
keys = append(keys, key)
|
keys = append(keys, key)
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
kv := make([]interface{}, 2)
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
kv[0], kv[1] = key, fields[key]
|
|
||||||
dst = appendFieldList(dst, kv)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
|
|
||||||
func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
|
||||||
for i, n := 0, len(kvList); i < n; i += 2 {
|
|
||||||
key, val := kvList[i], kvList[i+1]
|
|
||||||
if key, ok := key.(string); ok {
|
|
||||||
dst = enc.AppendKey(dst, key)
|
dst = enc.AppendKey(dst, key)
|
||||||
} else {
|
val := fields[key]
|
||||||
continue
|
|
||||||
}
|
|
||||||
if val, ok := val.(LogObjectMarshaler); ok {
|
if val, ok := val.(LogObjectMarshaler); ok {
|
||||||
e := newEvent(nil, 0)
|
e := newEvent(nil, 0)
|
||||||
e.buf = e.buf[:0]
|
e.buf = e.buf[:0]
|
||||||
|
@ -56,7 +29,8 @@ func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
||||||
case []byte:
|
case []byte:
|
||||||
dst = enc.AppendBytes(dst, val)
|
dst = enc.AppendBytes(dst, val)
|
||||||
case error:
|
case error:
|
||||||
switch m := ErrorMarshalFunc(val).(type) {
|
marshaled := ErrorMarshalFunc(val)
|
||||||
|
switch m := marshaled.(type) {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
e := newEvent(nil, 0)
|
e := newEvent(nil, 0)
|
||||||
e.buf = e.buf[:0]
|
e.buf = e.buf[:0]
|
||||||
|
@ -64,11 +38,7 @@ func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
||||||
dst = append(dst, e.buf...)
|
dst = append(dst, e.buf...)
|
||||||
putEvent(e)
|
putEvent(e)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
dst = enc.AppendNil(dst)
|
|
||||||
} else {
|
|
||||||
dst = enc.AppendString(dst, m.Error())
|
dst = enc.AppendString(dst, m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
dst = enc.AppendString(dst, m)
|
dst = enc.AppendString(dst, m)
|
||||||
default:
|
default:
|
||||||
|
@ -77,7 +47,8 @@ func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
||||||
case []error:
|
case []error:
|
||||||
dst = enc.AppendArrayStart(dst)
|
dst = enc.AppendArrayStart(dst)
|
||||||
for i, err := range val {
|
for i, err := range val {
|
||||||
switch m := ErrorMarshalFunc(err).(type) {
|
marshaled := ErrorMarshalFunc(err)
|
||||||
|
switch m := marshaled.(type) {
|
||||||
case LogObjectMarshaler:
|
case LogObjectMarshaler:
|
||||||
e := newEvent(nil, 0)
|
e := newEvent(nil, 0)
|
||||||
e.buf = e.buf[:0]
|
e.buf = e.buf[:0]
|
||||||
|
@ -85,11 +56,7 @@ func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
||||||
dst = append(dst, e.buf...)
|
dst = append(dst, e.buf...)
|
||||||
putEvent(e)
|
putEvent(e)
|
||||||
case error:
|
case error:
|
||||||
if m == nil || isNilValue(m) {
|
|
||||||
dst = enc.AppendNil(dst)
|
|
||||||
} else {
|
|
||||||
dst = enc.AppendString(dst, m.Error())
|
dst = enc.AppendString(dst, m.Error())
|
||||||
}
|
|
||||||
case string:
|
case string:
|
||||||
dst = enc.AppendString(dst, m)
|
dst = enc.AppendString(dst, m)
|
||||||
default:
|
default:
|
||||||
|
@ -267,8 +234,6 @@ func appendFieldList(dst []byte, kvList []interface{}) []byte {
|
||||||
dst = enc.AppendIPPrefix(dst, val)
|
dst = enc.AppendIPPrefix(dst, val)
|
||||||
case net.HardwareAddr:
|
case net.HardwareAddr:
|
||||||
dst = enc.AppendMACAddr(dst, val)
|
dst = enc.AppendMACAddr(dst, val)
|
||||||
case json.RawMessage:
|
|
||||||
dst = appendJSON(dst, val)
|
|
||||||
default:
|
default:
|
||||||
dst = enc.AppendInterface(dst, val)
|
dst = enc.AppendInterface(dst, val)
|
||||||
}
|
}
|
||||||
|
|
83
globals.go
83
globals.go
|
@ -1,29 +1,7 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import "time"
|
||||||
"encoding/json"
|
import "sync/atomic"
|
||||||
"strconv"
|
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
// TimeFormatUnix defines a time format that makes time fields to be
|
|
||||||
// serialized as Unix timestamp integers.
|
|
||||||
TimeFormatUnix = ""
|
|
||||||
|
|
||||||
// TimeFormatUnixMs defines a time format that makes time fields to be
|
|
||||||
// serialized as Unix timestamp integers in milliseconds.
|
|
||||||
TimeFormatUnixMs = "UNIXMS"
|
|
||||||
|
|
||||||
// TimeFormatUnixMicro defines a time format that makes time fields to be
|
|
||||||
// serialized as Unix timestamp integers in microseconds.
|
|
||||||
TimeFormatUnixMicro = "UNIXMICRO"
|
|
||||||
|
|
||||||
// TimeFormatUnixNano defines a time format that makes time fields to be
|
|
||||||
// serialized as Unix timestamp integers in nanoseconds.
|
|
||||||
TimeFormatUnixNano = "UNIXNANO"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// TimestampFieldName is the field name used for the timestamp field.
|
// TimestampFieldName is the field name used for the timestamp field.
|
||||||
|
@ -32,26 +10,6 @@ var (
|
||||||
// LevelFieldName is the field name used for the level field.
|
// LevelFieldName is the field name used for the level field.
|
||||||
LevelFieldName = "level"
|
LevelFieldName = "level"
|
||||||
|
|
||||||
// LevelTraceValue is the value used for the trace level field.
|
|
||||||
LevelTraceValue = "trace"
|
|
||||||
// LevelDebugValue is the value used for the debug level field.
|
|
||||||
LevelDebugValue = "debug"
|
|
||||||
// LevelInfoValue is the value used for the info level field.
|
|
||||||
LevelInfoValue = "info"
|
|
||||||
// LevelWarnValue is the value used for the warn level field.
|
|
||||||
LevelWarnValue = "warn"
|
|
||||||
// LevelErrorValue is the value used for the error level field.
|
|
||||||
LevelErrorValue = "error"
|
|
||||||
// LevelFatalValue is the value used for the fatal level field.
|
|
||||||
LevelFatalValue = "fatal"
|
|
||||||
// LevelPanicValue is the value used for the panic level field.
|
|
||||||
LevelPanicValue = "panic"
|
|
||||||
|
|
||||||
// LevelFieldMarshalFunc allows customization of global level field marshaling.
|
|
||||||
LevelFieldMarshalFunc = func(l Level) string {
|
|
||||||
return l.String()
|
|
||||||
}
|
|
||||||
|
|
||||||
// MessageFieldName is the field name used for the message field.
|
// MessageFieldName is the field name used for the message field.
|
||||||
MessageFieldName = "message"
|
MessageFieldName = "message"
|
||||||
|
|
||||||
|
@ -64,11 +22,6 @@ var (
|
||||||
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
|
// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
|
||||||
CallerSkipFrameCount = 2
|
CallerSkipFrameCount = 2
|
||||||
|
|
||||||
// CallerMarshalFunc allows customization of global caller marshaling
|
|
||||||
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
|
||||||
return file + ":" + strconv.Itoa(line)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ErrorStackFieldName is the field name used for error stacks.
|
// ErrorStackFieldName is the field name used for error stacks.
|
||||||
ErrorStackFieldName = "stack"
|
ErrorStackFieldName = "stack"
|
||||||
|
|
||||||
|
@ -80,13 +33,9 @@ var (
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// InterfaceMarshalFunc allows customization of interface marshaling.
|
// TimeFieldFormat defines the time format of the Time field type.
|
||||||
// Default: "encoding/json.Marshal"
|
// If set to an empty string, the time is formatted as an UNIX timestamp
|
||||||
InterfaceMarshalFunc = json.Marshal
|
// as integer.
|
||||||
|
|
||||||
// TimeFieldFormat defines the time format of the Time field type. If set to
|
|
||||||
// TimeFormatUnix, TimeFormatUnixMs, TimeFormatUnixMicro or TimeFormatUnixNano, the time is formatted as a UNIX
|
|
||||||
// timestamp as integer.
|
|
||||||
TimeFieldFormat = time.RFC3339
|
TimeFieldFormat = time.RFC3339
|
||||||
|
|
||||||
// TimestampFunc defines the function called to generate a timestamp.
|
// TimestampFunc defines the function called to generate a timestamp.
|
||||||
|
@ -100,19 +49,15 @@ var (
|
||||||
// set to true.
|
// set to true.
|
||||||
DurationFieldInteger = false
|
DurationFieldInteger = false
|
||||||
|
|
||||||
// ErrorHandler is called whenever zlog fails to write an event on its
|
// ErrorHandler is called whenever zerolog fails to write an event on its
|
||||||
// output. If not set, an error is printed on the stderr. This handler must
|
// output. If not set, an error is printed on the stderr. This handler must
|
||||||
// be thread safe and non-blocking.
|
// be thread safe and non-blocking.
|
||||||
ErrorHandler func(err error)
|
ErrorHandler func(err error)
|
||||||
|
|
||||||
// DefaultContextLogger is returned from Ctx() if there is no logger associated
|
|
||||||
// with the context.
|
|
||||||
DefaultContextLogger *Logger
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gLevel = new(int32)
|
gLevel = new(uint32)
|
||||||
disableSampling = new(int32)
|
disableSampling = new(uint32)
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetGlobalLevel sets the global override for log level. If this
|
// SetGlobalLevel sets the global override for log level. If this
|
||||||
|
@ -120,23 +65,23 @@ var (
|
||||||
//
|
//
|
||||||
// To globally disable logs, set GlobalLevel to Disabled.
|
// To globally disable logs, set GlobalLevel to Disabled.
|
||||||
func SetGlobalLevel(l Level) {
|
func SetGlobalLevel(l Level) {
|
||||||
atomic.StoreInt32(gLevel, int32(l))
|
atomic.StoreUint32(gLevel, uint32(l))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalLevel returns the current global log level
|
// GlobalLevel returns the current global log level
|
||||||
func GlobalLevel() Level {
|
func GlobalLevel() Level {
|
||||||
return Level(atomic.LoadInt32(gLevel))
|
return Level(atomic.LoadUint32(gLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisableSampling will disable sampling in all Loggers if true.
|
// DisableSampling will disable sampling in all Loggers if true.
|
||||||
func DisableSampling(v bool) {
|
func DisableSampling(v bool) {
|
||||||
var i int32
|
var i uint32
|
||||||
if v {
|
if v {
|
||||||
i = 1
|
i = 1
|
||||||
}
|
}
|
||||||
atomic.StoreInt32(disableSampling, i)
|
atomic.StoreUint32(disableSampling, i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func samplingDisabled() bool {
|
func samplingDisabled() bool {
|
||||||
return atomic.LoadInt32(disableSampling) == 1
|
return atomic.LoadUint32(disableSampling) == 1
|
||||||
}
|
}
|
||||||
|
|
13
go.mod
13
go.mod
|
@ -1,12 +1 @@
|
||||||
module tuxpa.in/a/zlog
|
module github.com/rs/zerolog
|
||||||
|
|
||||||
go 1.15
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0
|
|
||||||
github.com/mattn/go-colorable v0.1.12
|
|
||||||
github.com/pkg/errors v0.9.1
|
|
||||||
github.com/rs/xid v1.5.0
|
|
||||||
github.com/rs/zerolog v1.28.0
|
|
||||||
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 // indirect
|
|
||||||
)
|
|
||||||
|
|
19
go.sum
19
go.sum
|
@ -1,19 +0,0 @@
|
||||||
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8iXXhfZs=
|
|
||||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
|
||||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
|
||||||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
|
|
||||||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
|
||||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
|
||||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
|
||||||
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
|
||||||
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
|
|
||||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
|
||||||
github.com/rs/zerolog v1.28.0 h1:MirSo27VyNi7RJYP3078AA1+Cyzd2GB66qy3aUHvsWY=
|
|
||||||
github.com/rs/zerolog v1.28.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
|
|
||||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
||||||
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41 h1:ohgcoMbSofXygzo6AD2I1kz3BFmW1QArPYTtwEM3UXc=
|
|
||||||
golang.org/x/sys v0.0.0-20220915200043-7b5979e65e41/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
|
7
go112.go
7
go112.go
|
@ -1,7 +0,0 @@
|
||||||
// +build go1.12
|
|
||||||
|
|
||||||
package zlog
|
|
||||||
|
|
||||||
// Since go 1.12, some auto generated init functions are hidden from
|
|
||||||
// runtime.Caller.
|
|
||||||
const contextCallerSkipFrameCount = 2
|
|
82
hlog/hlog.go
82
hlog/hlog.go
|
@ -1,25 +1,26 @@
|
||||||
// Package hlog provides a set of http.Handler helpers for zlog.
|
// Package hlog provides a set of http.Handler helpers for zerolog.
|
||||||
package hlog
|
package hlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/hlog/internal/mutil"
|
"github.com/rs/zerolog/log"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/zenazn/goji/web/mutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FromRequest gets the logger in the request's context.
|
// FromRequest gets the logger in the request's context.
|
||||||
// This is a shortcut for log.Ctx(r.Context())
|
// This is a shortcut for log.Ctx(r.Context())
|
||||||
func FromRequest(r *http.Request) *zlog.Logger {
|
func FromRequest(r *http.Request) *zerolog.Logger {
|
||||||
return log.Ctx(r.Context())
|
return log.Ctx(r.Context())
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler injects log into requests context.
|
// NewHandler injects log into requests context.
|
||||||
func NewHandler(log zlog.Logger) func(http.Handler) http.Handler {
|
func NewHandler(log zerolog.Logger) func(http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Create a copy of the logger (including internal context slice)
|
// Create a copy of the logger (including internal context slice)
|
||||||
|
@ -36,8 +37,8 @@ func NewHandler(log zlog.Logger) func(http.Handler) http.Handler {
|
||||||
func URLHandler(fieldKey string) func(next http.Handler) http.Handler {
|
func URLHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, r.URL.String())
|
return c.Str(fieldKey, r.URL.String())
|
||||||
})
|
})
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -50,8 +51,8 @@ func URLHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
func MethodHandler(fieldKey string) func(next http.Handler) http.Handler {
|
func MethodHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, r.Method)
|
return c.Str(fieldKey, r.Method)
|
||||||
})
|
})
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -64,8 +65,8 @@ func MethodHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
|
func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, r.Method+" "+r.URL.String())
|
return c.Str(fieldKey, r.Method+" "+r.URL.String())
|
||||||
})
|
})
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -78,10 +79,10 @@ func RequestHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler {
|
func RemoteAddrHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.RemoteAddr != "" {
|
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, r.RemoteAddr)
|
return c.Str(fieldKey, host)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
@ -95,8 +96,8 @@ func UserAgentHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if ua := r.Header.Get("User-Agent"); ua != "" {
|
if ua := r.Header.Get("User-Agent"); ua != "" {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, ua)
|
return c.Str(fieldKey, ua)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -111,8 +112,8 @@ func RefererHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if ref := r.Header.Get("Referer"); ref != "" {
|
if ref := r.Header.Get("Referer"); ref != "" {
|
||||||
log := zlog.Ctx(r.Context())
|
log := zerolog.Ctx(r.Context())
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, ref)
|
return c.Str(fieldKey, ref)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -121,20 +122,6 @@ func RefererHandler(fieldKey string) func(next http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProtoHandler adds the requests protocol version as a field to the context logger
|
|
||||||
// using fieldKey as field Key.
|
|
||||||
func ProtoHandler(fieldKey string) func(next http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log := zlog.Ctx(r.Context())
|
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
|
||||||
return c.Str(fieldKey, r.Proto)
|
|
||||||
})
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type idKey struct{}
|
type idKey struct{}
|
||||||
|
|
||||||
// IDFromRequest returns the unique id associated to the request if any.
|
// IDFromRequest returns the unique id associated to the request if any.
|
||||||
|
@ -151,11 +138,6 @@ func IDFromCtx(ctx context.Context) (id xid.ID, ok bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// CtxWithID adds the given xid.ID to the context
|
|
||||||
func CtxWithID(ctx context.Context, id xid.ID) context.Context {
|
|
||||||
return context.WithValue(ctx, idKey{}, id)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequestIDHandler returns a handler setting a unique id to the request which can
|
// RequestIDHandler returns a handler setting a unique id to the request which can
|
||||||
// be gathered using IDFromRequest(req). This generated id is added as a field to the
|
// be gathered using IDFromRequest(req). This generated id is added as a field to the
|
||||||
// logger using the passed fieldKey as field name. The id is also added as a response
|
// logger using the passed fieldKey as field name. The id is also added as a response
|
||||||
|
@ -172,12 +154,12 @@ func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.
|
||||||
id, ok := IDFromRequest(r)
|
id, ok := IDFromRequest(r)
|
||||||
if !ok {
|
if !ok {
|
||||||
id = xid.New()
|
id = xid.New()
|
||||||
ctx = CtxWithID(ctx, id)
|
ctx = context.WithValue(ctx, idKey{}, id)
|
||||||
r = r.WithContext(ctx)
|
r = r.WithContext(ctx)
|
||||||
}
|
}
|
||||||
if fieldKey != "" {
|
if fieldKey != "" {
|
||||||
log := zlog.Ctx(ctx)
|
log := zerolog.Ctx(ctx)
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
log.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str(fieldKey, id.String())
|
return c.Str(fieldKey, id.String())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -189,22 +171,6 @@ func RequestIDHandler(fieldKey, headerName string) func(next http.Handler) http.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomHeaderHandler adds given header from request's header as a field to
|
|
||||||
// the context's logger using fieldKey as field key.
|
|
||||||
func CustomHeaderHandler(fieldKey, header string) func(next http.Handler) http.Handler {
|
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if val := r.Header.Get(header); val != "" {
|
|
||||||
log := zlog.Ctx(r.Context())
|
|
||||||
log.UpdateContext(func(c zlog.Context) zlog.Context {
|
|
||||||
return c.Str(fieldKey, val)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
next.ServeHTTP(w, r)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AccessHandler returns a handler that call f after each request.
|
// AccessHandler returns a handler that call f after each request.
|
||||||
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler {
|
func AccessHandler(f func(r *http.Request, status, size int, duration time.Duration)) func(next http.Handler) http.Handler {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(next http.Handler) http.Handler {
|
||||||
|
|
|
@ -9,8 +9,8 @@ import (
|
||||||
|
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/hlog"
|
"github.com/rs/zerolog/hlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// fake alice to avoid dep
|
// fake alice to avoid dep
|
||||||
|
@ -31,13 +31,13 @@ func (a alice) Then(h http.Handler) http.Handler {
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
zlog.TimestampFunc = func() time.Time {
|
zerolog.TimestampFunc = func() time.Time {
|
||||||
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
return time.Date(2001, time.February, 3, 4, 5, 6, 7, time.UTC)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Example_handler() {
|
func Example_handler() {
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Timestamp().
|
Timestamp().
|
||||||
Str("role", "my-service").
|
Str("role", "my-service").
|
||||||
Str("host", "local-hostname").
|
Str("host", "local-hostname").
|
||||||
|
@ -48,8 +48,8 @@ func Example_handler() {
|
||||||
// Install the logger handler with default output on the console
|
// Install the logger handler with default output on the console
|
||||||
c = c.Append(hlog.NewHandler(log))
|
c = c.Append(hlog.NewHandler(log))
|
||||||
|
|
||||||
// Install some provided extra handlers to set some request's context fields.
|
// Install some provided extra handler to set some request's context fields.
|
||||||
// Thanks to those handlers, all our logs will come with some pre-populated fields.
|
// Thanks to those handler, all our logs will come with some pre-populated fields.
|
||||||
c = c.Append(hlog.RemoteAddrHandler("ip"))
|
c = c.Append(hlog.RemoteAddrHandler("ip"))
|
||||||
c = c.Append(hlog.UserAgentHandler("user_agent"))
|
c = c.Append(hlog.UserAgentHandler("user_agent"))
|
||||||
c = c.Append(hlog.RefererHandler("referer"))
|
c = c.Append(hlog.RefererHandler("referer"))
|
||||||
|
@ -63,11 +63,11 @@ func Example_handler() {
|
||||||
hlog.FromRequest(r).Info().
|
hlog.FromRequest(r).Info().
|
||||||
Str("user", "current user").
|
Str("user", "current user").
|
||||||
Str("status", "ok").
|
Str("status", "ok").
|
||||||
Msg("Something happened")
|
Msg("Something happend")
|
||||||
}))
|
}))
|
||||||
http.Handle("/", h)
|
http.Handle("/", h)
|
||||||
|
|
||||||
h.ServeHTTP(httptest.NewRecorder(), &http.Request{})
|
h.ServeHTTP(httptest.NewRecorder(), &http.Request{})
|
||||||
|
|
||||||
// Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happened"}
|
// Output: {"level":"info","role":"my-service","host":"local-hostname","user":"current user","status":"ok","time":"2001-02-03T04:05:06Z","message":"Something happend"}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,21 @@
|
||||||
//go:build go1.7
|
|
||||||
// +build go1.7
|
// +build go1.7
|
||||||
|
|
||||||
package hlog
|
package hlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/rs/xid"
|
"reflect"
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
"tuxpa.in/a/zlog/internal/cbor"
|
"net/http/httptest"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/internal/cbor"
|
||||||
)
|
)
|
||||||
|
|
||||||
func decodeIfBinary(out *bytes.Buffer) string {
|
func decodeIfBinary(out *bytes.Buffer) string {
|
||||||
|
@ -28,7 +27,7 @@ func decodeIfBinary(out *bytes.Buffer) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewHandler(t *testing.T) {
|
func TestNewHandler(t *testing.T) {
|
||||||
log := zlog.New(nil).With().
|
log := zerolog.New(nil).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Logger()
|
Logger()
|
||||||
lh := NewHandler(log)
|
lh := NewHandler(log)
|
||||||
|
@ -50,7 +49,7 @@ func TestURLHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -66,7 +65,7 @@ func TestMethodHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"method":"POST"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"method":"POST"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -83,7 +82,7 @@ func TestRequestHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"request":"POST /path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"request":"POST /path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -99,9 +98,9 @@ func TestRemoteAddrHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"ip":"1.2.3.4:1234"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"ip":"1.2.3.4"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -115,9 +114,9 @@ func TestRemoteAddrHandlerIPv6(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"ip":"[2001:db8:a0b:12f0::1]:1234"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"ip":"2001:db8:a0b:12f0::1"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,7 +132,7 @@ func TestUserAgentHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"ua":"some user agent string"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"ua":"some user agent string"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -151,7 +150,7 @@ func TestRefererHandler(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"referer":"http://foo.com/bar"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"referer":"http://foo.com/bar"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -179,44 +178,10 @@ func TestRequestIDHandler(t *testing.T) {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(httptest.NewRecorder(), r)
|
h.ServeHTTP(httptest.NewRecorder(), r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomHeaderHandler(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
r := &http.Request{
|
|
||||||
Header: http.Header{
|
|
||||||
"X-Request-Id": []string{"514bbe5bb5251c92bd07a9846f4a1ab6"},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
h := CustomHeaderHandler("reqID", "X-Request-Id")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
l := FromRequest(r)
|
|
||||||
l.Log().Msg("")
|
|
||||||
}))
|
|
||||||
h = NewHandler(zlog.New(out))(h)
|
|
||||||
h.ServeHTTP(nil, r)
|
|
||||||
if want, got := `{"reqID":"514bbe5bb5251c92bd07a9846f4a1ab6"}`+"\n", decodeIfBinary(out); want != got {
|
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestProtoHandler(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
r := &http.Request{
|
|
||||||
Proto: "test",
|
|
||||||
}
|
|
||||||
h := ProtoHandler("proto")(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
l := FromRequest(r)
|
|
||||||
l.Log().Msg("")
|
|
||||||
}))
|
|
||||||
h = NewHandler(zlog.New(out))(h)
|
|
||||||
h.ServeHTTP(nil, r)
|
|
||||||
if want, got := `{"proto":"test"}`+"\n", decodeIfBinary(out); want != got {
|
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCombinedHandlers(t *testing.T) {
|
func TestCombinedHandlers(t *testing.T) {
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
r := &http.Request{
|
r := &http.Request{
|
||||||
|
@ -227,7 +192,7 @@ func TestCombinedHandlers(t *testing.T) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
}))))
|
}))))
|
||||||
h = NewHandler(zlog.New(out))(h)
|
h = NewHandler(zerolog.New(out))(h)
|
||||||
h.ServeHTTP(nil, r)
|
h.ServeHTTP(nil, r)
|
||||||
if want, got := `{"method":"POST","request":"POST /path?foo=bar","url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
if want, got := `{"method":"POST","request":"POST /path?foo=bar","url":"/path?foo=bar"}`+"\n", decodeIfBinary(out); want != got {
|
||||||
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
t.Errorf("Invalid log output, got: %s, want: %s", got, want)
|
||||||
|
@ -245,10 +210,10 @@ func BenchmarkHandlers(b *testing.B) {
|
||||||
}))
|
}))
|
||||||
h2 := MethodHandler("method")(RequestHandler("request")(h1))
|
h2 := MethodHandler("method")(RequestHandler("request")(h1))
|
||||||
handlers := map[string]http.Handler{
|
handlers := map[string]http.Handler{
|
||||||
"Single": NewHandler(zlog.New(ioutil.Discard))(h1),
|
"Single": NewHandler(zerolog.New(ioutil.Discard))(h1),
|
||||||
"Combined": NewHandler(zlog.New(ioutil.Discard))(h2),
|
"Combined": NewHandler(zerolog.New(ioutil.Discard))(h2),
|
||||||
"SingleDisabled": NewHandler(zlog.New(ioutil.Discard).Level(zlog.Disabled))(h1),
|
"SingleDisabled": NewHandler(zerolog.New(ioutil.Discard).Level(zerolog.Disabled))(h1),
|
||||||
"CombinedDisabled": NewHandler(zlog.New(ioutil.Discard).Level(zlog.Disabled))(h2),
|
"CombinedDisabled": NewHandler(zerolog.New(ioutil.Discard).Level(zerolog.Disabled))(h2),
|
||||||
}
|
}
|
||||||
for name := range handlers {
|
for name := range handlers {
|
||||||
h := handlers[name]
|
h := handlers[name]
|
||||||
|
@ -261,13 +226,13 @@ func BenchmarkHandlers(b *testing.B) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkDataRace(b *testing.B) {
|
func BenchmarkDataRace(b *testing.B) {
|
||||||
log := zlog.New(nil).With().
|
log := zerolog.New(nil).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Logger()
|
Logger()
|
||||||
lh := NewHandler(log)
|
lh := NewHandler(log)
|
||||||
h := lh(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
h := lh(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
l := FromRequest(r)
|
l := FromRequest(r)
|
||||||
l.UpdateContext(func(c zlog.Context) zlog.Context {
|
l.UpdateContext(func(c zerolog.Context) zerolog.Context {
|
||||||
return c.Str("bar", "baz")
|
return c.Str("bar", "baz")
|
||||||
})
|
})
|
||||||
l.Log().Msg("")
|
l.Log().Msg("")
|
||||||
|
@ -279,16 +244,3 @@ func BenchmarkDataRace(b *testing.B) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCtxWithID(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
id, _ := xid.FromString(`c0umremcie6smuu506pg`)
|
|
||||||
|
|
||||||
want := context.Background()
|
|
||||||
want = context.WithValue(want, idKey{}, id)
|
|
||||||
|
|
||||||
if got := CtxWithID(ctx, id); !reflect.DeepEqual(got, want) {
|
|
||||||
t.Errorf("CtxWithID() = %v, want %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
Copyright (c) 2014, 2015, 2016 Carl Jackson (carl@avtok.com)
|
|
||||||
|
|
||||||
MIT License
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
|
||||||
the Software without restriction, including without limitation the rights to
|
|
||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
||||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
||||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
@ -1,6 +0,0 @@
|
||||||
// Package mutil contains various functions that are helpful when writing http
|
|
||||||
// middleware.
|
|
||||||
//
|
|
||||||
// It has been vendored from Goji v1.0, with the exception of the code for Go 1.8:
|
|
||||||
// https://github.com/zenazn/goji/
|
|
||||||
package mutil
|
|
|
@ -1,154 +0,0 @@
|
||||||
package mutil
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bufio"
|
|
||||||
"io"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// WriterProxy is a proxy around an http.ResponseWriter that allows you to hook
|
|
||||||
// into various parts of the response process.
|
|
||||||
type WriterProxy interface {
|
|
||||||
http.ResponseWriter
|
|
||||||
// Status returns the HTTP status of the request, or 0 if one has not
|
|
||||||
// yet been sent.
|
|
||||||
Status() int
|
|
||||||
// BytesWritten returns the total number of bytes sent to the client.
|
|
||||||
BytesWritten() int
|
|
||||||
// Tee causes the response body to be written to the given io.Writer in
|
|
||||||
// addition to proxying the writes through. Only one io.Writer can be
|
|
||||||
// tee'd to at once: setting a second one will overwrite the first.
|
|
||||||
// Writes will be sent to the proxy before being written to this
|
|
||||||
// io.Writer. It is illegal for the tee'd writer to be modified
|
|
||||||
// concurrently with writes.
|
|
||||||
Tee(io.Writer)
|
|
||||||
// Unwrap returns the original proxied target.
|
|
||||||
Unwrap() http.ResponseWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
// WrapWriter wraps an http.ResponseWriter, returning a proxy that allows you to
|
|
||||||
// hook into various parts of the response process.
|
|
||||||
func WrapWriter(w http.ResponseWriter) WriterProxy {
|
|
||||||
_, cn := w.(http.CloseNotifier)
|
|
||||||
_, fl := w.(http.Flusher)
|
|
||||||
_, hj := w.(http.Hijacker)
|
|
||||||
_, rf := w.(io.ReaderFrom)
|
|
||||||
|
|
||||||
bw := basicWriter{ResponseWriter: w}
|
|
||||||
if cn && fl && hj && rf {
|
|
||||||
return &fancyWriter{bw}
|
|
||||||
}
|
|
||||||
if fl {
|
|
||||||
return &flushWriter{bw}
|
|
||||||
}
|
|
||||||
return &bw
|
|
||||||
}
|
|
||||||
|
|
||||||
// basicWriter wraps a http.ResponseWriter that implements the minimal
|
|
||||||
// http.ResponseWriter interface.
|
|
||||||
type basicWriter struct {
|
|
||||||
http.ResponseWriter
|
|
||||||
wroteHeader bool
|
|
||||||
code int
|
|
||||||
bytes int
|
|
||||||
tee io.Writer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) WriteHeader(code int) {
|
|
||||||
if !b.wroteHeader {
|
|
||||||
b.code = code
|
|
||||||
b.wroteHeader = true
|
|
||||||
b.ResponseWriter.WriteHeader(code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) Write(buf []byte) (int, error) {
|
|
||||||
b.WriteHeader(http.StatusOK)
|
|
||||||
n, err := b.ResponseWriter.Write(buf)
|
|
||||||
if b.tee != nil {
|
|
||||||
_, err2 := b.tee.Write(buf[:n])
|
|
||||||
// Prefer errors generated by the proxied writer.
|
|
||||||
if err == nil {
|
|
||||||
err = err2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b.bytes += n
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) maybeWriteHeader() {
|
|
||||||
if !b.wroteHeader {
|
|
||||||
b.WriteHeader(http.StatusOK)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) Status() int {
|
|
||||||
return b.code
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) BytesWritten() int {
|
|
||||||
return b.bytes
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) Tee(w io.Writer) {
|
|
||||||
b.tee = w
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *basicWriter) Unwrap() http.ResponseWriter {
|
|
||||||
return b.ResponseWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
// fancyWriter is a writer that additionally satisfies http.CloseNotifier,
|
|
||||||
// http.Flusher, http.Hijacker, and io.ReaderFrom. It exists for the common case
|
|
||||||
// of wrapping the http.ResponseWriter that package http gives you, in order to
|
|
||||||
// make the proxied object support the full method set of the proxied object.
|
|
||||||
type fancyWriter struct {
|
|
||||||
basicWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fancyWriter) CloseNotify() <-chan bool {
|
|
||||||
cn := f.basicWriter.ResponseWriter.(http.CloseNotifier)
|
|
||||||
return cn.CloseNotify()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fancyWriter) Flush() {
|
|
||||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
|
||||||
fl.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fancyWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
||||||
hj := f.basicWriter.ResponseWriter.(http.Hijacker)
|
|
||||||
return hj.Hijack()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *fancyWriter) ReadFrom(r io.Reader) (int64, error) {
|
|
||||||
if f.basicWriter.tee != nil {
|
|
||||||
n, err := io.Copy(&f.basicWriter, r)
|
|
||||||
f.bytes += int(n)
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
rf := f.basicWriter.ResponseWriter.(io.ReaderFrom)
|
|
||||||
f.basicWriter.maybeWriteHeader()
|
|
||||||
|
|
||||||
n, err := rf.ReadFrom(r)
|
|
||||||
f.bytes += int(n)
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
type flushWriter struct {
|
|
||||||
basicWriter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *flushWriter) Flush() {
|
|
||||||
fl := f.basicWriter.ResponseWriter.(http.Flusher)
|
|
||||||
fl.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ http.CloseNotifier = &fancyWriter{}
|
|
||||||
_ http.Flusher = &fancyWriter{}
|
|
||||||
_ http.Hijacker = &fancyWriter{}
|
|
||||||
_ io.ReaderFrom = &fancyWriter{}
|
|
||||||
_ http.Flusher = &flushWriter{}
|
|
||||||
)
|
|
8
hook.go
8
hook.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
// Hook defines an interface to a log hook.
|
// Hook defines an interface to a log hook.
|
||||||
type Hook interface {
|
type Hook interface {
|
||||||
|
@ -17,16 +17,12 @@ func (h HookFunc) Run(e *Event, level Level, message string) {
|
||||||
|
|
||||||
// LevelHook applies a different hook for each level.
|
// LevelHook applies a different hook for each level.
|
||||||
type LevelHook struct {
|
type LevelHook struct {
|
||||||
NoLevelHook, TraceHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
|
NoLevelHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run implements the Hook interface.
|
// Run implements the Hook interface.
|
||||||
func (h LevelHook) Run(e *Event, level Level, message string) {
|
func (h LevelHook) Run(e *Event, level Level, message string) {
|
||||||
switch level {
|
switch level {
|
||||||
case TraceLevel:
|
|
||||||
if h.TraceHook != nil {
|
|
||||||
h.TraceHook.Run(e, level, message)
|
|
||||||
}
|
|
||||||
case DebugLevel:
|
case DebugLevel:
|
||||||
if h.DebugHook != nil {
|
if h.DebugHook != nil {
|
||||||
h.DebugHook.Run(e, level, message)
|
h.DebugHook.Run(e, level, message)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
package cbor
|
package cbor
|
||||||
|
|
||||||
// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice.
|
|
||||||
// Making it package level instead of embedded in Encoder brings
|
|
||||||
// some extra efforts at importing, but avoids value copy when the functions
|
|
||||||
// of Encoder being invoked.
|
|
||||||
// DO REMEMBER to set this variable at importing, or
|
|
||||||
// you might get a nil pointer dereference panic at runtime.
|
|
||||||
var JSONMarshalFunc func(v interface{}) ([]byte, error)
|
|
||||||
|
|
||||||
type Encoder struct{}
|
type Encoder struct{}
|
||||||
|
|
||||||
// AppendKey adds a key (string) to the binary encoded log message
|
// AppendKey adds a key (string) to the binary encoded log message
|
||||||
|
|
|
@ -27,7 +27,6 @@ const (
|
||||||
|
|
||||||
// Tag Sub-types.
|
// Tag Sub-types.
|
||||||
additionalTypeTimestamp byte = 01
|
additionalTypeTimestamp byte = 01
|
||||||
additionalTypeEmbeddedCBOR byte = 63
|
|
||||||
|
|
||||||
// Extended Tags - from https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
|
// Extended Tags - from https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml
|
||||||
additionalTypeTagNetworkAddr uint16 = 260
|
additionalTypeTagNetworkAddr uint16 = 260
|
||||||
|
@ -92,8 +91,7 @@ func appendCborTypePrefix(dst []byte, major byte, number uint64) []byte {
|
||||||
minor = additionalTypeIntUint64
|
minor = additionalTypeIntUint64
|
||||||
|
|
||||||
}
|
}
|
||||||
|
dst = append(dst, byte(major|minor))
|
||||||
dst = append(dst, major|minor)
|
|
||||||
byteCount--
|
byteCount--
|
||||||
for ; byteCount >= 0; byteCount-- {
|
for ; byteCount >= 0; byteCount-- {
|
||||||
dst = append(dst, byte(number>>(uint(byteCount)*8)))
|
dst = append(dst, byte(number>>(uint(byteCount)*8)))
|
||||||
|
|
|
@ -5,7 +5,6 @@ package cbor
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
@ -44,7 +43,7 @@ func readByte(src *bufio.Reader) byte {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodeIntAdditionalType(src *bufio.Reader, minor byte) int64 {
|
func decodeIntAdditonalType(src *bufio.Reader, minor byte) int64 {
|
||||||
val := int64(0)
|
val := int64(0)
|
||||||
if minor <= 23 {
|
if minor <= 23 {
|
||||||
val = int64(minor)
|
val = int64(minor)
|
||||||
|
@ -78,7 +77,7 @@ func decodeInteger(src *bufio.Reader) int64 {
|
||||||
if major != majorTypeUnsignedInt && major != majorTypeNegativeInt {
|
if major != majorTypeUnsignedInt && major != majorTypeNegativeInt {
|
||||||
panic(fmt.Errorf("Major type is: %d in decodeInteger!! (expected 0 or 1)", major))
|
panic(fmt.Errorf("Major type is: %d in decodeInteger!! (expected 0 or 1)", major))
|
||||||
}
|
}
|
||||||
val := decodeIntAdditionalType(src, minor)
|
val := decodeIntAdditonalType(src, minor)
|
||||||
if major == 0 {
|
if major == 0 {
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
@ -205,7 +204,7 @@ func decodeString(src *bufio.Reader, noQuotes bool) []byte {
|
||||||
if !noQuotes {
|
if !noQuotes {
|
||||||
result = append(result, '"')
|
result = append(result, '"')
|
||||||
}
|
}
|
||||||
length := decodeIntAdditionalType(src, minor)
|
length := decodeIntAdditonalType(src, minor)
|
||||||
len := int(length)
|
len := int(length)
|
||||||
pbs := readNBytes(src, len)
|
pbs := readNBytes(src, len)
|
||||||
result = append(result, pbs...)
|
result = append(result, pbs...)
|
||||||
|
@ -214,31 +213,6 @@ func decodeString(src *bufio.Reader, noQuotes bool) []byte {
|
||||||
}
|
}
|
||||||
return append(result, '"')
|
return append(result, '"')
|
||||||
}
|
}
|
||||||
func decodeStringToDataUrl(src *bufio.Reader, mimeType string) []byte {
|
|
||||||
pb := readByte(src)
|
|
||||||
major := pb & maskOutAdditionalType
|
|
||||||
minor := pb & maskOutMajorType
|
|
||||||
if major != majorTypeByteString {
|
|
||||||
panic(fmt.Errorf("Major type is: %d in decodeString", major))
|
|
||||||
}
|
|
||||||
length := decodeIntAdditionalType(src, minor)
|
|
||||||
l := int(length)
|
|
||||||
enc := base64.StdEncoding
|
|
||||||
lEnc := enc.EncodedLen(l)
|
|
||||||
result := make([]byte, len("\"data:;base64,\"")+len(mimeType)+lEnc)
|
|
||||||
dest := result
|
|
||||||
u := copy(dest, "\"data:")
|
|
||||||
dest = dest[u:]
|
|
||||||
u = copy(dest, mimeType)
|
|
||||||
dest = dest[u:]
|
|
||||||
u = copy(dest, ";base64,")
|
|
||||||
dest = dest[u:]
|
|
||||||
pbs := readNBytes(src, l)
|
|
||||||
enc.Encode(dest, pbs)
|
|
||||||
dest = dest[lEnc:]
|
|
||||||
dest[0] = '"'
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func decodeUTF8String(src *bufio.Reader) []byte {
|
func decodeUTF8String(src *bufio.Reader) []byte {
|
||||||
pb := readByte(src)
|
pb := readByte(src)
|
||||||
|
@ -248,7 +222,7 @@ func decodeUTF8String(src *bufio.Reader) []byte {
|
||||||
panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major))
|
panic(fmt.Errorf("Major type is: %d in decodeUTF8String", major))
|
||||||
}
|
}
|
||||||
result := []byte{'"'}
|
result := []byte{'"'}
|
||||||
length := decodeIntAdditionalType(src, minor)
|
length := decodeIntAdditonalType(src, minor)
|
||||||
len := int(length)
|
len := int(length)
|
||||||
pbs := readNBytes(src, len)
|
pbs := readNBytes(src, len)
|
||||||
|
|
||||||
|
@ -264,7 +238,7 @@ func decodeUTF8String(src *bufio.Reader) []byte {
|
||||||
return append(dst, '"')
|
return append(dst, '"')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The string has no need for encoding and therefore is directly
|
// The string has no need for encoding an therefore is directly
|
||||||
// appended to the byte slice.
|
// appended to the byte slice.
|
||||||
result = append(result, pbs...)
|
result = append(result, pbs...)
|
||||||
return append(result, '"')
|
return append(result, '"')
|
||||||
|
@ -283,7 +257,7 @@ func array2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if minor == additionalTypeInfiniteCount {
|
if minor == additionalTypeInfiniteCount {
|
||||||
unSpecifiedCount = true
|
unSpecifiedCount = true
|
||||||
} else {
|
} else {
|
||||||
length := decodeIntAdditionalType(src, minor)
|
length := decodeIntAdditonalType(src, minor)
|
||||||
len = int(length)
|
len = int(length)
|
||||||
}
|
}
|
||||||
for i := 0; unSpecifiedCount || i < len; i++ {
|
for i := 0; unSpecifiedCount || i < len; i++ {
|
||||||
|
@ -292,7 +266,7 @@ func array2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
}
|
}
|
||||||
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
|
if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||||
readByte(src)
|
readByte(src)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -303,7 +277,7 @@ func array2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
}
|
}
|
||||||
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
|
if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||||
readByte(src)
|
readByte(src)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -327,7 +301,7 @@ func map2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if minor == additionalTypeInfiniteCount {
|
if minor == additionalTypeInfiniteCount {
|
||||||
unSpecifiedCount = true
|
unSpecifiedCount = true
|
||||||
} else {
|
} else {
|
||||||
length := decodeIntAdditionalType(src, minor)
|
length := decodeIntAdditonalType(src, minor)
|
||||||
len = int(length)
|
len = int(length)
|
||||||
}
|
}
|
||||||
dst.Write([]byte{'{'})
|
dst.Write([]byte{'{'})
|
||||||
|
@ -337,7 +311,7 @@ func map2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
}
|
}
|
||||||
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
|
if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||||
readByte(src)
|
readByte(src)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -352,7 +326,7 @@ func map2Json(src *bufio.Reader, dst io.Writer) {
|
||||||
if e != nil {
|
if e != nil {
|
||||||
panic(e)
|
panic(e)
|
||||||
}
|
}
|
||||||
if pb[0] == majorTypeSimpleAndFloat|additionalTypeBreak {
|
if pb[0] == byte(majorTypeSimpleAndFloat|additionalTypeBreak) {
|
||||||
readByte(src)
|
readByte(src)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
@ -375,24 +349,10 @@ func decodeTagData(src *bufio.Reader) []byte {
|
||||||
switch minor {
|
switch minor {
|
||||||
case additionalTypeTimestamp:
|
case additionalTypeTimestamp:
|
||||||
return decodeTimeStamp(src)
|
return decodeTimeStamp(src)
|
||||||
case additionalTypeIntUint8:
|
|
||||||
val := decodeIntAdditionalType(src, minor)
|
|
||||||
switch byte(val) {
|
|
||||||
case additionalTypeEmbeddedCBOR:
|
|
||||||
pb := readByte(src)
|
|
||||||
dataMajor := pb & maskOutAdditionalType
|
|
||||||
if dataMajor != majorTypeByteString {
|
|
||||||
panic(fmt.Errorf("Unsupported embedded Type: %d in decodeEmbeddedCBOR", dataMajor))
|
|
||||||
}
|
|
||||||
src.UnreadByte()
|
|
||||||
return decodeStringToDataUrl(src, "application/cbor")
|
|
||||||
default:
|
|
||||||
panic(fmt.Errorf("Unsupported Additional Tag Type: %d in decodeTagData", val))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tag value is larger than 256 (so uint16).
|
// Tag value is larger than 256 (so uint16).
|
||||||
case additionalTypeIntUint16:
|
case additionalTypeIntUint16:
|
||||||
val := decodeIntAdditionalType(src, minor)
|
val := decodeIntAdditonalType(src, minor)
|
||||||
|
|
||||||
switch uint16(val) {
|
switch uint16(val) {
|
||||||
case additionalTypeEmbeddedJSON:
|
case additionalTypeEmbeddedJSON:
|
||||||
|
@ -423,7 +383,7 @@ func decodeTagData(src *bufio.Reader) []byte {
|
||||||
|
|
||||||
case additionalTypeTagNetworkPrefix:
|
case additionalTypeTagNetworkPrefix:
|
||||||
pb := readByte(src)
|
pb := readByte(src)
|
||||||
if pb != majorTypeMap|0x1 {
|
if pb != byte(majorTypeMap|0x1) {
|
||||||
panic(fmt.Errorf("IP Prefix is NOT of MAP of 1 elements as expected"))
|
panic(fmt.Errorf("IP Prefix is NOT of MAP of 1 elements as expected"))
|
||||||
}
|
}
|
||||||
octets := decodeString(src, true)
|
octets := decodeString(src, true)
|
||||||
|
|
|
@ -8,7 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writeLog(fname string, count int, useCompress bool) {
|
func writeLog(fname string, count int, useCompress bool) {
|
||||||
|
@ -34,8 +34,8 @@ func writeLog(fname string, count int, useCompress bool) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
zlog.TimestampFunc = func() time.Time { return time.Now().Round(time.Second) }
|
zerolog.TimestampFunc = func() time.Time { return time.Now().Round(time.Second) }
|
||||||
log := zlog.New(f).With().
|
log := zerolog.New(f).With().
|
||||||
Timestamp().
|
Timestamp().
|
||||||
Logger()
|
Logger()
|
||||||
for i := 0; i < count; i++ {
|
for i := 0; i < count; i++ {
|
||||||
|
|
|
@ -1,14 +1,12 @@
|
||||||
package cbor
|
package cbor
|
||||||
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
// AppendStrings encodes and adds an array of strings to the dst byte array.
|
// AppendStrings encodes and adds an array of strings to the dst byte array.
|
||||||
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
|
func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
|
||||||
major := majorTypeArray
|
major := majorTypeArray
|
||||||
l := len(vals)
|
l := len(vals)
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -25,38 +23,13 @@ func (Encoder) AppendString(dst []byte, s string) []byte {
|
||||||
l := len(s)
|
l := len(s)
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l))
|
dst = appendCborTypePrefix(dst, majorTypeUtf8String, uint64(l))
|
||||||
}
|
}
|
||||||
return append(dst, s...)
|
return append(dst, s...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendStringers encodes and adds an array of Stringer values
|
|
||||||
// to the dst byte array.
|
|
||||||
func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte {
|
|
||||||
if len(vals) == 0 {
|
|
||||||
return e.AppendArrayEnd(e.AppendArrayStart(dst))
|
|
||||||
}
|
|
||||||
dst = e.AppendArrayStart(dst)
|
|
||||||
dst = e.AppendStringer(dst, vals[0])
|
|
||||||
if len(vals) > 1 {
|
|
||||||
for _, val := range vals[1:] {
|
|
||||||
dst = e.AppendStringer(dst, val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return e.AppendArrayEnd(dst)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendStringer encodes and adds the Stringer value to the dst
|
|
||||||
// byte array.
|
|
||||||
func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte {
|
|
||||||
if val == nil {
|
|
||||||
return e.AppendNil(dst)
|
|
||||||
}
|
|
||||||
return e.AppendString(dst, val.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendBytes encodes and adds an array of bytes to the dst byte array.
|
// AppendBytes encodes and adds an array of bytes to the dst byte array.
|
||||||
func (Encoder) AppendBytes(dst, s []byte) []byte {
|
func (Encoder) AppendBytes(dst, s []byte) []byte {
|
||||||
major := majorTypeByteString
|
major := majorTypeByteString
|
||||||
|
@ -64,7 +37,7 @@ func (Encoder) AppendBytes(dst, s []byte) []byte {
|
||||||
l := len(s)
|
l := len(s)
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -77,7 +50,7 @@ func AppendEmbeddedJSON(dst, s []byte) []byte {
|
||||||
minor := additionalTypeEmbeddedJSON
|
minor := additionalTypeEmbeddedJSON
|
||||||
|
|
||||||
// Append the TAG to indicate this is Embedded JSON.
|
// Append the TAG to indicate this is Embedded JSON.
|
||||||
dst = append(dst, major|additionalTypeIntUint16)
|
dst = append(dst, byte(major|additionalTypeIntUint16))
|
||||||
dst = append(dst, byte(minor>>8))
|
dst = append(dst, byte(minor>>8))
|
||||||
dst = append(dst, byte(minor&0xff))
|
dst = append(dst, byte(minor&0xff))
|
||||||
|
|
||||||
|
@ -87,29 +60,7 @@ func AppendEmbeddedJSON(dst, s []byte) []byte {
|
||||||
l := len(s)
|
l := len(s)
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
|
||||||
}
|
|
||||||
return append(dst, s...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendEmbeddedCBOR adds a tag and embeds input CBOR as such.
|
|
||||||
func AppendEmbeddedCBOR(dst, s []byte) []byte {
|
|
||||||
major := majorTypeTags
|
|
||||||
minor := additionalTypeEmbeddedCBOR
|
|
||||||
|
|
||||||
// Append the TAG to indicate this is Embedded JSON.
|
|
||||||
dst = append(dst, major|additionalTypeIntUint8)
|
|
||||||
dst = append(dst, minor)
|
|
||||||
|
|
||||||
// Append the CBOR Object as Byte String.
|
|
||||||
major = majorTypeByteString
|
|
||||||
|
|
||||||
l := len(s)
|
|
||||||
if l <= additionalMax {
|
|
||||||
lb := byte(l)
|
|
||||||
dst = append(dst, major|lb)
|
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
|
func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
|
||||||
major := majorTypeTags
|
major := majorTypeTags
|
||||||
minor := additionalTypeTimestamp
|
minor := additionalTypeTimestamp
|
||||||
dst = append(dst, major|minor)
|
dst = append(dst, byte(major|minor))
|
||||||
secs := t.Unix()
|
secs := t.Unix()
|
||||||
var val uint64
|
var val uint64
|
||||||
if secs < 0 {
|
if secs < 0 {
|
||||||
|
@ -17,18 +17,18 @@ func appendIntegerTimestamp(dst []byte, t time.Time) []byte {
|
||||||
major = majorTypeUnsignedInt
|
major = majorTypeUnsignedInt
|
||||||
val = uint64(secs)
|
val = uint64(secs)
|
||||||
}
|
}
|
||||||
dst = appendCborTypePrefix(dst, major, val)
|
dst = appendCborTypePrefix(dst, major, uint64(val))
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
func (e Encoder) appendFloatTimestamp(dst []byte, t time.Time) []byte {
|
||||||
major := majorTypeTags
|
major := majorTypeTags
|
||||||
minor := additionalTypeTimestamp
|
minor := additionalTypeTimestamp
|
||||||
dst = append(dst, major|minor)
|
dst = append(dst, byte(major|minor))
|
||||||
secs := t.Unix()
|
secs := t.Unix()
|
||||||
nanos := t.Nanosecond()
|
nanos := t.Nanosecond()
|
||||||
var val float64
|
var val float64
|
||||||
val = float64(secs)*1.0 + float64(nanos)*1e-9
|
val = float64(secs)*1.0 + float64(nanos)*1E-9
|
||||||
return e.AppendFloat64(dst, val)
|
return e.AppendFloat64(dst, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ func (e Encoder) AppendTimes(dst []byte, vals []time.Time, unused string) []byte
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@ func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Dur
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,25 +1,25 @@
|
||||||
package cbor
|
package cbor
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AppendNil inserts a 'Nil' object into the dst byte array.
|
// AppendNil inserts a 'Nil' object into the dst byte array.
|
||||||
func (Encoder) AppendNil(dst []byte) []byte {
|
func (Encoder) AppendNil(dst []byte) []byte {
|
||||||
return append(dst, majorTypeSimpleAndFloat|additionalTypeNull)
|
return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeNull))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendBeginMarker inserts a map start into the dst byte array.
|
// AppendBeginMarker inserts a map start into the dst byte array.
|
||||||
func (Encoder) AppendBeginMarker(dst []byte) []byte {
|
func (Encoder) AppendBeginMarker(dst []byte) []byte {
|
||||||
return append(dst, majorTypeMap|additionalTypeInfiniteCount)
|
return append(dst, byte(majorTypeMap|additionalTypeInfiniteCount))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendEndMarker inserts a map end into the dst byte array.
|
// AppendEndMarker inserts a map end into the dst byte array.
|
||||||
func (Encoder) AppendEndMarker(dst []byte) []byte {
|
func (Encoder) AppendEndMarker(dst []byte) []byte {
|
||||||
return append(dst, majorTypeSimpleAndFloat|additionalTypeBreak)
|
return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendObjectData takes an object in form of a byte array and appends to dst.
|
// AppendObjectData takes an object in form of a byte array and appends to dst.
|
||||||
|
@ -31,12 +31,12 @@ func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
|
||||||
|
|
||||||
// AppendArrayStart adds markers to indicate the start of an array.
|
// AppendArrayStart adds markers to indicate the start of an array.
|
||||||
func (Encoder) AppendArrayStart(dst []byte) []byte {
|
func (Encoder) AppendArrayStart(dst []byte) []byte {
|
||||||
return append(dst, majorTypeArray|additionalTypeInfiniteCount)
|
return append(dst, byte(majorTypeArray|additionalTypeInfiniteCount))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendArrayEnd adds markers to indicate the end of an array.
|
// AppendArrayEnd adds markers to indicate the end of an array.
|
||||||
func (Encoder) AppendArrayEnd(dst []byte) []byte {
|
func (Encoder) AppendArrayEnd(dst []byte) []byte {
|
||||||
return append(dst, majorTypeSimpleAndFloat|additionalTypeBreak)
|
return append(dst, byte(majorTypeSimpleAndFloat|additionalTypeBreak))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendArrayDelim adds markers to indicate end of a particular array element.
|
// AppendArrayDelim adds markers to indicate end of a particular array element.
|
||||||
|
@ -57,7 +57,7 @@ func (Encoder) AppendBool(dst []byte, val bool) []byte {
|
||||||
if val {
|
if val {
|
||||||
b = additionalTypeBoolTrue
|
b = additionalTypeBoolTrue
|
||||||
}
|
}
|
||||||
return append(dst, majorTypeSimpleAndFloat|b)
|
return append(dst, byte(majorTypeSimpleAndFloat|b))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendBools encodes and inserts an array of boolean values into the dst byte array.
|
// AppendBools encodes and inserts an array of boolean values into the dst byte array.
|
||||||
|
@ -69,7 +69,7 @@ func (e Encoder) AppendBools(dst []byte, vals []bool) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,7 @@ func (Encoder) AppendInt(dst []byte, val int) []byte {
|
||||||
}
|
}
|
||||||
if contentVal <= additionalMax {
|
if contentVal <= additionalMax {
|
||||||
lb := byte(contentVal)
|
lb := byte(contentVal)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
|
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ func (e Encoder) AppendInts(dst []byte, vals []int) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -129,7 +129,7 @@ func (e Encoder) AppendInts8(dst []byte, vals []int8) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ func (e Encoder) AppendInts16(dst []byte, vals []int16) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -177,7 +177,7 @@ func (e Encoder) AppendInts32(dst []byte, vals []int32) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,7 @@ func (Encoder) AppendInt64(dst []byte, val int64) []byte {
|
||||||
}
|
}
|
||||||
if contentVal <= additionalMax {
|
if contentVal <= additionalMax {
|
||||||
lb := byte(contentVal)
|
lb := byte(contentVal)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
|
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
|
||||||
}
|
}
|
||||||
|
@ -213,7 +213,7 @@ func (e Encoder) AppendInts64(dst []byte, vals []int64) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -237,7 +237,7 @@ func (e Encoder) AppendUints(dst []byte, vals []uint) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -261,7 +261,7 @@ func (e Encoder) AppendUints8(dst []byte, vals []uint8) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -285,7 +285,7 @@ func (e Encoder) AppendUints16(dst []byte, vals []uint16) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -309,7 +309,7 @@ func (e Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -325,9 +325,9 @@ func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
||||||
contentVal := val
|
contentVal := val
|
||||||
if contentVal <= additionalMax {
|
if contentVal <= additionalMax {
|
||||||
lb := byte(contentVal)
|
lb := byte(contentVal)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, contentVal)
|
dst = appendCborTypePrefix(dst, major, uint64(contentVal))
|
||||||
}
|
}
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,7 @@ func (e Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -368,7 +368,7 @@ func (Encoder) AppendFloat32(dst []byte, val float32) []byte {
|
||||||
for i := uint(0); i < 4; i++ {
|
for i := uint(0); i < 4; i++ {
|
||||||
buf[i] = byte(n >> ((3 - i) * 8))
|
buf[i] = byte(n >> ((3 - i) * 8))
|
||||||
}
|
}
|
||||||
return append(append(dst, major|subType), buf[0], buf[1], buf[2], buf[3])
|
return append(append(dst, byte(major|subType)), buf[0], buf[1], buf[2], buf[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.
|
// AppendFloats32 encodes and inserts an array of single precision float value into the dst byte array.
|
||||||
|
@ -380,7 +380,7 @@ func (e Encoder) AppendFloats32(dst []byte, vals []float32) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -403,7 +403,7 @@ func (Encoder) AppendFloat64(dst []byte, val float64) []byte {
|
||||||
major := majorTypeSimpleAndFloat
|
major := majorTypeSimpleAndFloat
|
||||||
subType := additionalTypeFloat64
|
subType := additionalTypeFloat64
|
||||||
n := math.Float64bits(val)
|
n := math.Float64bits(val)
|
||||||
dst = append(dst, major|subType)
|
dst = append(dst, byte(major|subType))
|
||||||
for i := uint(1); i <= 8; i++ {
|
for i := uint(1); i <= 8; i++ {
|
||||||
b := byte(n >> ((8 - i) * 8))
|
b := byte(n >> ((8 - i) * 8))
|
||||||
dst = append(dst, b)
|
dst = append(dst, b)
|
||||||
|
@ -420,7 +420,7 @@ func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||||
}
|
}
|
||||||
if l <= additionalMax {
|
if l <= additionalMax {
|
||||||
lb := byte(l)
|
lb := byte(l)
|
||||||
dst = append(dst, major|lb)
|
dst = append(dst, byte(major|lb))
|
||||||
} else {
|
} else {
|
||||||
dst = appendCborTypePrefix(dst, major, uint64(l))
|
dst = appendCborTypePrefix(dst, major, uint64(l))
|
||||||
}
|
}
|
||||||
|
@ -432,24 +432,16 @@ func (e Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||||
|
|
||||||
// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.
|
// AppendInterface takes an arbitrary object and converts it to JSON and embeds it dst.
|
||||||
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
||||||
marshaled, err := JSONMarshalFunc(i)
|
marshaled, err := json.Marshal(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
||||||
}
|
}
|
||||||
return AppendEmbeddedJSON(dst, marshaled)
|
return AppendEmbeddedJSON(dst, marshaled)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendType appends the parameter type (as a string) to the input byte slice.
|
|
||||||
func (e Encoder) AppendType(dst []byte, i interface{}) []byte {
|
|
||||||
if i == nil {
|
|
||||||
return e.AppendString(dst, "<nil>")
|
|
||||||
}
|
|
||||||
return e.AppendString(dst, reflect.TypeOf(i).String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6).
|
// AppendIPAddr encodes and inserts an IP Address (IPv4 or IPv6).
|
||||||
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
||||||
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
|
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
||||||
return e.AppendBytes(dst, ip)
|
return e.AppendBytes(dst, ip)
|
||||||
|
@ -457,21 +449,21 @@ func (e Encoder) AppendIPAddr(dst []byte, ip net.IP) []byte {
|
||||||
|
|
||||||
// AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length).
|
// AppendIPPrefix encodes and inserts an IP Address Prefix (Address + Mask Length).
|
||||||
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
|
func (e Encoder) AppendIPPrefix(dst []byte, pfx net.IPNet) []byte {
|
||||||
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
|
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8))
|
dst = append(dst, byte(additionalTypeTagNetworkPrefix>>8))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff))
|
dst = append(dst, byte(additionalTypeTagNetworkPrefix&0xff))
|
||||||
|
|
||||||
// Prefix is a tuple (aka MAP of 1 pair of elements) -
|
// Prefix is a tuple (aka MAP of 1 pair of elements) -
|
||||||
// first element is prefix, second is mask length.
|
// first element is prefix, second is mask length.
|
||||||
dst = append(dst, majorTypeMap|0x1)
|
dst = append(dst, byte(majorTypeMap|0x1))
|
||||||
dst = e.AppendBytes(dst, pfx.IP)
|
dst = e.AppendBytes(dst, pfx.IP)
|
||||||
maskLen, _ := pfx.Mask.Size()
|
maskLen, _ := pfx.Mask.Size()
|
||||||
return e.AppendUint8(dst, uint8(maskLen))
|
return e.AppendUint8(dst, uint8(maskLen))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendMACAddr encodes and inserts a Hardware (MAC) address.
|
// AppendMACAddr encodes and inserts an Hardware (MAC) address.
|
||||||
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
||||||
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
|
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
dst = append(dst, byte(additionalTypeTagNetworkAddr>>8))
|
||||||
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
dst = append(dst, byte(additionalTypeTagNetworkAddr&0xff))
|
||||||
return e.AppendBytes(dst, ha)
|
return e.AppendBytes(dst, ha)
|
||||||
|
@ -479,7 +471,7 @@ func (e Encoder) AppendMACAddr(dst []byte, ha net.HardwareAddr) []byte {
|
||||||
|
|
||||||
// AppendHex adds a TAG and inserts a hex bytes as a string.
|
// AppendHex adds a TAG and inserts a hex bytes as a string.
|
||||||
func (e Encoder) AppendHex(dst []byte, val []byte) []byte {
|
func (e Encoder) AppendHex(dst []byte, val []byte) []byte {
|
||||||
dst = append(dst, majorTypeTags|additionalTypeIntUint16)
|
dst = append(dst, byte(majorTypeTags|additionalTypeIntUint16))
|
||||||
dst = append(dst, byte(additionalTypeTagHexString>>8))
|
dst = append(dst, byte(additionalTypeTagHexString>>8))
|
||||||
dst = append(dst, byte(additionalTypeTagHexString&0xff))
|
dst = append(dst, byte(additionalTypeTagHexString&0xff))
|
||||||
return e.AppendBytes(dst, val)
|
return e.AppendBytes(dst, val)
|
||||||
|
|
|
@ -1,34 +0,0 @@
|
||||||
// +build !386
|
|
||||||
|
|
||||||
package cbor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/hex"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var enc2 = Encoder{}
|
|
||||||
|
|
||||||
var integerTestCases_64bit = []struct {
|
|
||||||
val int
|
|
||||||
binary string
|
|
||||||
}{
|
|
||||||
// Value in 8 bytes.
|
|
||||||
{0xabcd100000000, "\x1b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
|
|
||||||
{1000000000000, "\x1b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
|
|
||||||
// Value in 8 bytes.
|
|
||||||
{-0xabcd100000001, "\x3b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
|
|
||||||
{-1000000000001, "\x3b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAppendInt_64bit(t *testing.T) {
|
|
||||||
for _, tc := range integerTestCases_64bit {
|
|
||||||
s := enc2.AppendInt([]byte{}, tc.val)
|
|
||||||
got := string(s)
|
|
||||||
if got != tc.binary {
|
|
||||||
t.Errorf("AppendInt(0x%x)=0x%s, want: 0x%s",
|
|
||||||
tc.val, hex.EncodeToString(s),
|
|
||||||
hex.EncodeToString([]byte(tc.binary)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -88,8 +88,11 @@ var integerTestCases = []struct {
|
||||||
{0xFFFF, "\x19\xff\xff"},
|
{0xFFFF, "\x19\xff\xff"},
|
||||||
// Value in 4 bytes.
|
// Value in 4 bytes.
|
||||||
{0x10000, "\x1a\x00\x01\x00\x00"},
|
{0x10000, "\x1a\x00\x01\x00\x00"},
|
||||||
{0x7FFFFFFE, "\x1a\x7f\xff\xff\xfe"},
|
{0xFFFFFFFE, "\x1a\xff\xff\xff\xfe"},
|
||||||
{1000000, "\x1a\x00\x0f\x42\x40"},
|
{1000000, "\x1a\x00\x0f\x42\x40"},
|
||||||
|
// Value in 8 bytes.
|
||||||
|
{0xabcd100000000, "\x1b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
|
||||||
|
{1000000000000, "\x1b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
|
||||||
// Negative number test cases.
|
// Negative number test cases.
|
||||||
// Value included in the type.
|
// Value included in the type.
|
||||||
{-1, "\x20"},
|
{-1, "\x20"},
|
||||||
|
@ -113,8 +116,11 @@ var integerTestCases = []struct {
|
||||||
{-1000, "\x39\x03\xe7"},
|
{-1000, "\x39\x03\xe7"},
|
||||||
// Value in 4 bytes.
|
// Value in 4 bytes.
|
||||||
{-0x10001, "\x3a\x00\x01\x00\x00"},
|
{-0x10001, "\x3a\x00\x01\x00\x00"},
|
||||||
{-0x7FFFFFFE, "\x3a\x7f\xff\xff\xfd"},
|
{-0xFFFFFFFE, "\x3a\xff\xff\xff\xfd"},
|
||||||
{-1000000, "\x3a\x00\x0f\x42\x3f"},
|
{-1000000, "\x3a\x00\x0f\x42\x3f"},
|
||||||
|
// Value in 8 bytes.
|
||||||
|
{-0xabcd100000001, "\x3b\x00\x0a\xbc\xd1\x00\x00\x00\x00"},
|
||||||
|
{-1000000000001, "\x3b\x00\x00\x00\xe8\xd4\xa5\x10\x00"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAppendInt(t *testing.T) {
|
func TestAppendInt(t *testing.T) {
|
||||||
|
@ -211,7 +217,7 @@ var macAddrTestCases = []struct {
|
||||||
{net.HardwareAddr{0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3}, "\"20:01:0d:b8:85:a3\"", "\xd9\x01\x04\x46\x20\x01\x0d\xb8\x85\xa3"},
|
{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) {
|
func TestAppendMacAddr(t *testing.T) {
|
||||||
for _, tc := range macAddrTestCases {
|
for _, tc := range macAddrTestCases {
|
||||||
s := enc.AppendMACAddr([]byte{}, tc.macaddr)
|
s := enc.AppendMACAddr([]byte{}, tc.macaddr)
|
||||||
got := string(s)
|
got := string(s)
|
||||||
|
|
|
@ -1,19 +1,12 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
// JSONMarshalFunc is used to marshal interface to JSON encoded byte slice.
|
|
||||||
// Making it package level instead of embedded in Encoder brings
|
|
||||||
// some extra efforts at importing, but avoids value copy when the functions
|
|
||||||
// of Encoder being invoked.
|
|
||||||
// DO REMEMBER to set this variable at importing, or
|
|
||||||
// you might get a nil pointer dereference panic at runtime.
|
|
||||||
var JSONMarshalFunc func(v interface{}) ([]byte, error)
|
|
||||||
|
|
||||||
type Encoder struct{}
|
type Encoder struct{}
|
||||||
|
|
||||||
// AppendKey appends a new key to the output JSON.
|
// AppendKey appends a new key to the output JSON.
|
||||||
func (e Encoder) AppendKey(dst []byte, key string) []byte {
|
func (e Encoder) AppendKey(dst []byte, key string) []byte {
|
||||||
if dst[len(dst)-1] != '{' {
|
if len(dst) > 1 && dst[len(dst)-1] != '{' {
|
||||||
dst = append(dst, ',')
|
dst = append(dst, ',')
|
||||||
}
|
}
|
||||||
return append(e.AppendString(dst, key), ':')
|
dst = e.AppendString(dst, key)
|
||||||
|
return append(dst, ':')
|
||||||
}
|
}
|
|
@ -1,9 +1,6 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import "unicode/utf8"
|
||||||
"fmt"
|
|
||||||
"unicode/utf8"
|
|
||||||
)
|
|
||||||
|
|
||||||
const hex = "0123456789abcdef"
|
const hex = "0123456789abcdef"
|
||||||
|
|
||||||
|
@ -37,7 +34,7 @@ func (e Encoder) AppendStrings(dst []byte, vals []string) []byte {
|
||||||
//
|
//
|
||||||
// The operation loops though each byte in the string looking
|
// The operation loops though each byte in the string looking
|
||||||
// for characters that need json or utf8 encoding. If the string
|
// for characters that need json or utf8 encoding. If the string
|
||||||
// does not need encoding, then the string is appended in its
|
// does not need encoding, then the string is appended in it's
|
||||||
// entirety to the byte slice.
|
// entirety to the byte slice.
|
||||||
// If we encounter a byte that does need encoding, switch up
|
// If we encounter a byte that does need encoding, switch up
|
||||||
// the operation and perform a byte-by-byte read-encode-append.
|
// the operation and perform a byte-by-byte read-encode-append.
|
||||||
|
@ -56,39 +53,14 @@ func (Encoder) AppendString(dst []byte, s string) []byte {
|
||||||
return append(dst, '"')
|
return append(dst, '"')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The string has no need for encoding and therefore is directly
|
// The string has no need for encoding an therefore is directly
|
||||||
// appended to the byte slice.
|
// appended to the byte slice.
|
||||||
dst = append(dst, s...)
|
dst = append(dst, s...)
|
||||||
// End with a double quote
|
// End with a double quote
|
||||||
return append(dst, '"')
|
return append(dst, '"')
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendStringers encodes the provided Stringer list to json and
|
// appendStringComplex is used by appendString to take over an in
|
||||||
// appends the encoded Stringer list to the input byte slice.
|
|
||||||
func (e Encoder) AppendStringers(dst []byte, vals []fmt.Stringer) []byte {
|
|
||||||
if len(vals) == 0 {
|
|
||||||
return append(dst, '[', ']')
|
|
||||||
}
|
|
||||||
dst = append(dst, '[')
|
|
||||||
dst = e.AppendStringer(dst, vals[0])
|
|
||||||
if len(vals) > 1 {
|
|
||||||
for _, val := range vals[1:] {
|
|
||||||
dst = e.AppendStringer(append(dst, ','), val)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return append(dst, ']')
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendStringer encodes the input Stringer to json and appends the
|
|
||||||
// encoded Stringer value to the input byte slice.
|
|
||||||
func (e Encoder) AppendStringer(dst []byte, val fmt.Stringer) []byte {
|
|
||||||
if val == nil {
|
|
||||||
return e.AppendInterface(dst, nil)
|
|
||||||
}
|
|
||||||
return e.AppendString(dst, val.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
//// appendStringComplex is used by appendString to take over an in
|
|
||||||
// progress JSON string encoding that encountered a character that needs
|
// progress JSON string encoding that encountered a character that needs
|
||||||
// to be encoded.
|
// to be encoded.
|
||||||
func appendStringComplex(dst []byte, s string, i int) []byte {
|
func appendStringComplex(dst []byte, s string, i int) []byte {
|
||||||
|
@ -99,7 +71,7 @@ func appendStringComplex(dst []byte, s string, i int) []byte {
|
||||||
r, size := utf8.DecodeRuneInString(s[i:])
|
r, size := utf8.DecodeRuneInString(s[i:])
|
||||||
if r == utf8.RuneError && size == 1 {
|
if r == utf8.RuneError && size == 1 {
|
||||||
// In case of error, first append previous simple characters to
|
// In case of error, first append previous simple characters to
|
||||||
// the byte slice if any and append a replacement character code
|
// the byte slice if any and append a remplacement character code
|
||||||
// in place of the invalid sequence.
|
// in place of the invalid sequence.
|
||||||
if start < i {
|
if start < i {
|
||||||
dst = append(dst, s[start:i]...)
|
dst = append(dst, s[start:i]...)
|
||||||
|
|
|
@ -5,26 +5,11 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
// Import from zlog/global.go
|
|
||||||
timeFormatUnix = ""
|
|
||||||
timeFormatUnixMs = "UNIXMS"
|
|
||||||
timeFormatUnixMicro = "UNIXMICRO"
|
|
||||||
timeFormatUnixNano = "UNIXNANO"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AppendTime formats the input time with the given format
|
// AppendTime formats the input time with the given format
|
||||||
// and appends the encoded string to the input byte slice.
|
// and appends the encoded string to the input byte slice.
|
||||||
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
||||||
switch format {
|
if format == "" {
|
||||||
case timeFormatUnix:
|
|
||||||
return e.AppendInt64(dst, t.Unix())
|
return e.AppendInt64(dst, t.Unix())
|
||||||
case timeFormatUnixMs:
|
|
||||||
return e.AppendInt64(dst, t.UnixNano()/1000000)
|
|
||||||
case timeFormatUnixMicro:
|
|
||||||
return e.AppendInt64(dst, t.UnixNano()/1000)
|
|
||||||
case timeFormatUnixNano:
|
|
||||||
return e.AppendInt64(dst, t.UnixNano())
|
|
||||||
}
|
}
|
||||||
return append(t.AppendFormat(append(dst, '"'), format), '"')
|
return append(t.AppendFormat(append(dst, '"'), format), '"')
|
||||||
}
|
}
|
||||||
|
@ -32,15 +17,8 @@ func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
||||||
// AppendTimes converts the input times with the given format
|
// AppendTimes converts the input times with the given format
|
||||||
// and appends the encoded string list to the input byte slice.
|
// and appends the encoded string list to the input byte slice.
|
||||||
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
||||||
switch format {
|
if format == "" {
|
||||||
case timeFormatUnix:
|
|
||||||
return appendUnixTimes(dst, vals)
|
return appendUnixTimes(dst, vals)
|
||||||
case timeFormatUnixMs:
|
|
||||||
return appendUnixNanoTimes(dst, vals, 1000000)
|
|
||||||
case timeFormatUnixMicro:
|
|
||||||
return appendUnixNanoTimes(dst, vals, 1000)
|
|
||||||
case timeFormatUnixNano:
|
|
||||||
return appendUnixNanoTimes(dst, vals, 1)
|
|
||||||
}
|
}
|
||||||
if len(vals) == 0 {
|
if len(vals) == 0 {
|
||||||
return append(dst, '[', ']')
|
return append(dst, '[', ']')
|
||||||
|
@ -71,21 +49,6 @@ func appendUnixTimes(dst []byte, vals []time.Time) []byte {
|
||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
|
|
||||||
if len(vals) == 0 {
|
|
||||||
return append(dst, '[', ']')
|
|
||||||
}
|
|
||||||
dst = append(dst, '[')
|
|
||||||
dst = strconv.AppendInt(dst, vals[0].UnixNano()/div, 10)
|
|
||||||
if len(vals) > 1 {
|
|
||||||
for _, t := range vals[1:] {
|
|
||||||
dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/div, 10)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dst = append(dst, ']')
|
|
||||||
return dst
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendDuration formats the input duration with the given unit & format
|
// AppendDuration formats the input duration with the given unit & format
|
||||||
// and appends the encoded string to the input byte slice.
|
// and appends the encoded string to the input byte slice.
|
||||||
func (e Encoder) 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 {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -279,7 +279,7 @@ func (Encoder) AppendUints32(dst []byte, vals []uint32) []byte {
|
||||||
// AppendUint64 converts the input uint64 to a string and
|
// AppendUint64 converts the input uint64 to a string and
|
||||||
// appends the encoded string to the input byte slice.
|
// appends the encoded string to the input byte slice.
|
||||||
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
func (Encoder) AppendUint64(dst []byte, val uint64) []byte {
|
||||||
return strconv.AppendUint(dst, val, 10)
|
return strconv.AppendUint(dst, uint64(val), 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendUints64 encodes the input uint64s to json and
|
// AppendUints64 encodes the input uint64s to json and
|
||||||
|
@ -301,7 +301,7 @@ func (Encoder) AppendUints64(dst []byte, vals []uint64) []byte {
|
||||||
|
|
||||||
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
|
// 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 through so we
|
// with an error, but a logging library wants the data to get thru so we
|
||||||
// make a tradeoff and store those types as string.
|
// make a tradeoff and store those types as string.
|
||||||
switch {
|
switch {
|
||||||
case math.IsNaN(val):
|
case math.IsNaN(val):
|
||||||
|
@ -350,7 +350,7 @@ func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||||
return append(dst, '[', ']')
|
return append(dst, '[', ']')
|
||||||
}
|
}
|
||||||
dst = append(dst, '[')
|
dst = append(dst, '[')
|
||||||
dst = appendFloat(dst, vals[0], 64)
|
dst = appendFloat(dst, vals[0], 32)
|
||||||
if len(vals) > 1 {
|
if len(vals) > 1 {
|
||||||
for _, val := range vals[1:] {
|
for _, val := range vals[1:] {
|
||||||
dst = appendFloat(append(dst, ','), val, 64)
|
dst = appendFloat(append(dst, ','), val, 64)
|
||||||
|
@ -363,34 +363,22 @@ func (Encoder) AppendFloats64(dst []byte, vals []float64) []byte {
|
||||||
// AppendInterface marshals the input interface to a string and
|
// AppendInterface marshals the input interface to a string and
|
||||||
// appends the encoded string to the input byte slice.
|
// appends the encoded string to the input byte slice.
|
||||||
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
func (e Encoder) AppendInterface(dst []byte, i interface{}) []byte {
|
||||||
marshaled, err := JSONMarshalFunc(i)
|
marshaled, err := json.Marshal(i)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
return e.AppendString(dst, fmt.Sprintf("marshaling error: %v", err))
|
||||||
}
|
}
|
||||||
return append(dst, marshaled...)
|
return append(dst, marshaled...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AppendType appends the parameter type (as a string) to the input byte slice.
|
|
||||||
func (e Encoder) AppendType(dst []byte, i interface{}) []byte {
|
|
||||||
if i == nil {
|
|
||||||
return e.AppendString(dst, "<nil>")
|
|
||||||
}
|
|
||||||
return e.AppendString(dst, reflect.TypeOf(i).String())
|
|
||||||
}
|
|
||||||
|
|
||||||
// AppendObjectData takes in an object that is already in a byte array
|
// AppendObjectData takes in an object that is already in a byte array
|
||||||
// and adds it to the dst.
|
// and adds it to the dst.
|
||||||
func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
|
func (Encoder) AppendObjectData(dst []byte, o []byte) []byte {
|
||||||
// Three conditions apply here:
|
// Two conditions we want to put a ',' between existing content and
|
||||||
// 1. new content starts with '{' - which should be dropped OR
|
// new content:
|
||||||
// 2. new content starts with '{' - which should be replaced with ','
|
// 1. new content starts with '{' - which shd be dropped OR
|
||||||
// to separate with existing content OR
|
// 2. existing content has already other fields
|
||||||
// 3. existing content has already other fields
|
|
||||||
if o[0] == '{' {
|
if o[0] == '{' {
|
||||||
if len(dst) > 1 {
|
o[0] = ','
|
||||||
dst = append(dst, ',')
|
|
||||||
}
|
|
||||||
o = o[1:]
|
|
||||||
} else if len(dst) > 1 {
|
} else if len(dst) > 1 {
|
||||||
dst = append(dst, ',')
|
dst = append(dst, ',')
|
||||||
}
|
}
|
||||||
|
|
|
@ -165,45 +165,3 @@ func Test_appendMac(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_appendType(t *testing.T) {
|
|
||||||
typeTests := []struct {
|
|
||||||
label string
|
|
||||||
input interface{}
|
|
||||||
want []byte
|
|
||||||
}{
|
|
||||||
{"int", 42, []byte(`"int"`)},
|
|
||||||
{"MAC", net.HardwareAddr{0x12, 0x34, 0x00, 0x00, 0x90, 0xab}, []byte(`"net.HardwareAddr"`)},
|
|
||||||
{"float64", float64(2.50), []byte(`"float64"`)},
|
|
||||||
{"nil", nil, []byte(`"<nil>"`)},
|
|
||||||
{"bool", true, []byte(`"bool"`)},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range typeTests {
|
|
||||||
t.Run(tt.label, func(t *testing.T) {
|
|
||||||
if got := enc.AppendType([]byte{}, tt.input); !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("appendType() = %s, want %s", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_appendObjectData(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
dst []byte
|
|
||||||
obj []byte
|
|
||||||
want []byte
|
|
||||||
}{
|
|
||||||
{[]byte{}, []byte(`{"foo":"bar"}`), []byte(`"foo":"bar"}`)},
|
|
||||||
{[]byte(`{"qux":"quz"`), []byte(`{"foo":"bar"}`), []byte(`{"qux":"quz","foo":"bar"}`)},
|
|
||||||
{[]byte{}, []byte(`"foo":"bar"`), []byte(`"foo":"bar"`)},
|
|
||||||
{[]byte(`{"qux":"quz"`), []byte(`"foo":"bar"`), []byte(`{"qux":"quz","foo":"bar"`)},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run("ObjectData", func(t *testing.T) {
|
|
||||||
if got := enc.AppendObjectData(tt.dst, tt.obj); !reflect.DeepEqual(got, tt.want) {
|
|
||||||
t.Errorf("appendObjectData() = %s, want %s", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
//go:build !windows
|
|
||||||
// +build !windows
|
// +build !windows
|
||||||
|
|
||||||
// Package journald provides a io.Writer to send the logs
|
// Package journald provides a io.Writer to send the logs
|
||||||
|
@ -6,8 +5,8 @@
|
||||||
|
|
||||||
package journald
|
package journald
|
||||||
|
|
||||||
// This file provides a zlog writer so that logs printed
|
// This file provides a zerolog writer so that logs printed
|
||||||
// using zlog library can be sent to a journalD.
|
// using zerolog library can be sent to a journalD.
|
||||||
|
|
||||||
// Zerolog's Top level key/Value Pairs are translated to
|
// Zerolog's Top level key/Value Pairs are translated to
|
||||||
// journald's args - all Values are sent to journald as strings.
|
// journald's args - all Values are sent to journald as strings.
|
||||||
|
@ -21,17 +20,16 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/coreos/go-systemd/journal"
|
||||||
|
"github.com/rs/zerolog"
|
||||||
|
"github.com/rs/zerolog/internal/cbor"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/go-systemd/v22/journal"
|
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
"tuxpa.in/a/zlog/internal/cbor"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultJournalDPrio = journal.PriNotice
|
const defaultJournalDPrio = journal.PriNotice
|
||||||
|
|
||||||
// NewJournalDWriter returns a zlog log destination
|
// NewJournalDWriter returns a zerolog log destination
|
||||||
// to be used as parameter to New() calls. Writing logs
|
// to be used as parameter to New() calls. Writing logs
|
||||||
// to this writer will send the log messages to journalD
|
// to this writer will send the log messages to journalD
|
||||||
// running in this system.
|
// running in this system.
|
||||||
|
@ -42,46 +40,47 @@ func NewJournalDWriter() io.Writer {
|
||||||
type journalWriter struct {
|
type journalWriter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// levelToJPrio converts zlog Level string into
|
// levelToJPrio converts zerolog Level string into
|
||||||
// journalD's priority values. JournalD has more
|
// journalD's priority values. JournalD has more
|
||||||
// priorities than zlog.
|
// priorities than zerolog.
|
||||||
func levelToJPrio(zLevel string) journal.Priority {
|
func levelToJPrio(zLevel string) journal.Priority {
|
||||||
lvl, _ := zlog.ParseLevel(zLevel)
|
lvl, _ := zerolog.ParseLevel(zLevel)
|
||||||
|
|
||||||
switch lvl {
|
switch lvl {
|
||||||
case zlog.TraceLevel:
|
case zerolog.DebugLevel:
|
||||||
return journal.PriDebug
|
return journal.PriDebug
|
||||||
case zlog.DebugLevel:
|
case zerolog.InfoLevel:
|
||||||
return journal.PriDebug
|
|
||||||
case zlog.InfoLevel:
|
|
||||||
return journal.PriInfo
|
return journal.PriInfo
|
||||||
case zlog.WarnLevel:
|
case zerolog.WarnLevel:
|
||||||
return journal.PriWarning
|
return journal.PriWarning
|
||||||
case zlog.ErrorLevel:
|
case zerolog.ErrorLevel:
|
||||||
return journal.PriErr
|
return journal.PriErr
|
||||||
case zlog.FatalLevel:
|
case zerolog.FatalLevel:
|
||||||
return journal.PriCrit
|
return journal.PriCrit
|
||||||
case zlog.PanicLevel:
|
case zerolog.PanicLevel:
|
||||||
return journal.PriEmerg
|
return journal.PriEmerg
|
||||||
case zlog.NoLevel:
|
case zerolog.NoLevel:
|
||||||
return journal.PriNotice
|
return journal.PriNotice
|
||||||
}
|
}
|
||||||
return defaultJournalDPrio
|
return defaultJournalDPrio
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w journalWriter) Write(p []byte) (n int, err error) {
|
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{}
|
var event map[string]interface{}
|
||||||
origPLen := len(p)
|
|
||||||
p = cbor.DecodeIfBinaryToBytes(p)
|
p = cbor.DecodeIfBinaryToBytes(p)
|
||||||
d := json.NewDecoder(bytes.NewReader(p))
|
d := json.NewDecoder(bytes.NewReader(p))
|
||||||
d.UseNumber()
|
d.UseNumber()
|
||||||
err = d.Decode(&event)
|
err = d.Decode(&event)
|
||||||
jPrio := defaultJournalDPrio
|
jPrio := defaultJournalDPrio
|
||||||
args := make(map[string]string)
|
args := make(map[string]string, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if l, ok := event[zlog.LevelFieldName].(string); ok {
|
if l, ok := event[zerolog.LevelFieldName].(string); ok {
|
||||||
jPrio = levelToJPrio(l)
|
jPrio = levelToJPrio(l)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,20 +88,20 @@ func (w journalWriter) Write(p []byte) (n int, err error) {
|
||||||
for key, value := range event {
|
for key, value := range event {
|
||||||
jKey := strings.ToUpper(key)
|
jKey := strings.ToUpper(key)
|
||||||
switch key {
|
switch key {
|
||||||
case zlog.LevelFieldName, zlog.TimestampFieldName:
|
case zerolog.LevelFieldName, zerolog.TimestampFieldName:
|
||||||
continue
|
continue
|
||||||
case zlog.MessageFieldName:
|
case zerolog.MessageFieldName:
|
||||||
msg, _ = value.(string)
|
msg, _ = value.(string)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
switch v := value.(type) {
|
switch value.(type) {
|
||||||
case string:
|
case string:
|
||||||
args[jKey] = v
|
args[jKey], _ = value.(string)
|
||||||
case json.Number:
|
case json.Number:
|
||||||
args[jKey] = fmt.Sprint(value)
|
args[jKey] = fmt.Sprint(value)
|
||||||
default:
|
default:
|
||||||
b, err := zlog.InterfaceMarshalFunc(value)
|
b, err := json.Marshal(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
args[jKey] = fmt.Sprintf("[error: %v]", err)
|
args[jKey] = fmt.Sprintf("[error: %v]", err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -112,10 +111,5 @@ func (w journalWriter) Write(p []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
args["JSON"] = string(p)
|
args["JSON"] = string(p)
|
||||||
err = journal.Send(msg, jPrio, args)
|
err = journal.Send(msg, jPrio, args)
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
n = origPLen
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,12 @@
|
||||||
// +build linux
|
// +build !windows
|
||||||
|
|
||||||
package journald_test
|
package journald_test
|
||||||
|
|
||||||
import (
|
import "github.com/rs/zerolog"
|
||||||
"bytes"
|
import "github.com/rs/zerolog/journald"
|
||||||
"io"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
"tuxpa.in/a/zlog/journald"
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExampleNewJournalDWriter() {
|
func ExampleNewJournalDWriter() {
|
||||||
log := zlog.New(journald.NewJournalDWriter())
|
log := zerolog.New(journald.NewJournalDWriter())
|
||||||
log.Info().Str("foo", "bar").Uint64("small", 123).Float64("float", 3.14).Uint64("big", 1152921504606846976).Msg("Journal Test")
|
log.Info().Str("foo", "bar").Uint64("small", 123).Float64("float", 3.14).Uint64("big", 1152921504606846976).Msg("Journal Test")
|
||||||
// Output:
|
// Output:
|
||||||
}
|
}
|
||||||
|
@ -48,39 +42,3 @@ Thu 2018-04-26 22:30:20.768136 PDT [s=3284d695bde946e4b5017c77a399237f;i=329f0;b
|
||||||
_PID=27103
|
_PID=27103
|
||||||
_SOURCE_REALTIME_TIMESTAMP=1524807020768136
|
_SOURCE_REALTIME_TIMESTAMP=1524807020768136
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func TestWriteReturnsNoOfWrittenBytes(t *testing.T) {
|
|
||||||
input := []byte(`{"level":"info","time":1570912626,"message":"Starting..."}`)
|
|
||||||
wr := journald.NewJournalDWriter()
|
|
||||||
want := len(input)
|
|
||||||
got, err := wr.Write(input)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Unexpected error %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if want != got {
|
|
||||||
t.Errorf("Expected %d bytes to be written got %d", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMultiWrite(t *testing.T) {
|
|
||||||
var (
|
|
||||||
w1 = new(bytes.Buffer)
|
|
||||||
w2 = new(bytes.Buffer)
|
|
||||||
w3 = journald.NewJournalDWriter()
|
|
||||||
)
|
|
||||||
|
|
||||||
zlog.ErrorHandler = func(err error) {
|
|
||||||
if err == io.ErrShortWrite {
|
|
||||||
t.Errorf("Unexpected ShortWriteError")
|
|
||||||
t.FailNow()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
log := zlog.New(io.MultiWriter(w1, w2, w3)).With().Logger()
|
|
||||||
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
log.Info().Msg("Tick!")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
166
log.go
166
log.go
|
@ -1,13 +1,13 @@
|
||||||
// Package zlog provides a lightweight logging library dedicated to JSON logging.
|
// Package zerolog provides a lightweight logging library dedicated to JSON logging.
|
||||||
//
|
//
|
||||||
// A global Logger can be use for simple logging:
|
// A global Logger can be use for simple logging:
|
||||||
//
|
//
|
||||||
// import "tuxpa.in/a/zlog/log"
|
// import "github.com/rs/zerolog/log"
|
||||||
//
|
//
|
||||||
// log.Info().Msg("hello world")
|
// log.Info().Msg("hello world")
|
||||||
// // Output: {"time":1494567715,"level":"info","message":"hello world"}
|
// // Output: {"time":1494567715,"level":"info","message":"hello world"}
|
||||||
//
|
//
|
||||||
// NOTE: To import the global logger, import the "log" subpackage "tuxpa.in/a/zlog/log".
|
// NOTE: To import the global logger, import the "log" subpackage "github.com/rs/zerolog/log".
|
||||||
//
|
//
|
||||||
// Fields can be added to log messages:
|
// Fields can be added to log messages:
|
||||||
//
|
//
|
||||||
|
@ -16,7 +16,7 @@
|
||||||
//
|
//
|
||||||
// Create logger instance to manage different outputs:
|
// Create logger instance to manage different outputs:
|
||||||
//
|
//
|
||||||
// logger := zlog.New(os.Stderr).With().Timestamp().Logger()
|
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
|
||||||
// logger.Info().
|
// logger.Info().
|
||||||
// Str("foo", "bar").
|
// Str("foo", "bar").
|
||||||
// Msg("hello world")
|
// Msg("hello world")
|
||||||
|
@ -30,7 +30,7 @@
|
||||||
//
|
//
|
||||||
// Level logging
|
// Level logging
|
||||||
//
|
//
|
||||||
// zlog.SetGlobalLevel(zlog.InfoLevel)
|
// zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
//
|
//
|
||||||
// log.Debug().Msg("filtered out message")
|
// log.Debug().Msg("filtered out message")
|
||||||
// log.Info().Msg("routed message")
|
// log.Info().Msg("routed message")
|
||||||
|
@ -62,7 +62,7 @@
|
||||||
//
|
//
|
||||||
// Sample logs:
|
// Sample logs:
|
||||||
//
|
//
|
||||||
// sampled := log.Sample(&zlog.BasicSampler{N: 10})
|
// sampled := log.Sample(&zerolog.BasicSampler{N: 10})
|
||||||
// sampled.Info().Msg("will be logged every 10 messages")
|
// sampled.Info().Msg("will be logged every 10 messages")
|
||||||
//
|
//
|
||||||
// Log with contextual hooks:
|
// Log with contextual hooks:
|
||||||
|
@ -70,45 +70,44 @@
|
||||||
// // Create the hook:
|
// // Create the hook:
|
||||||
// type SeverityHook struct{}
|
// type SeverityHook struct{}
|
||||||
//
|
//
|
||||||
// func (h SeverityHook) Run(e *zlog.Event, level zlog.Level, msg string) {
|
// func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
|
||||||
// if level != zlog.NoLevel {
|
// if level != zerolog.NoLevel {
|
||||||
// e.Str("severity", level.String())
|
// e.Str("severity", level.String())
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// // And use it:
|
// // And use it:
|
||||||
// var h SeverityHook
|
// var h SeverityHook
|
||||||
// log := zlog.New(os.Stdout).Hook(h)
|
// log := zerolog.New(os.Stdout).Hook(h)
|
||||||
// log.Warn().Msg("")
|
// log.Warn().Msg("")
|
||||||
// // Output: {"level":"warn","severity":"warn"}
|
// // Output: {"level":"warn","severity":"warn"}
|
||||||
//
|
//
|
||||||
// # Caveats
|
//
|
||||||
|
// Caveats
|
||||||
//
|
//
|
||||||
// There is no fields deduplication out-of-the-box.
|
// There is no fields deduplication out-of-the-box.
|
||||||
// Using the same key multiple times creates new key in final JSON each time.
|
// Using the same key multiple times creates new key in final JSON each time.
|
||||||
//
|
//
|
||||||
// logger := zlog.New(os.Stderr).With().Timestamp().Logger()
|
// logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
|
||||||
// logger.Info().
|
// logger.Info().
|
||||||
// Timestamp().
|
// Timestamp().
|
||||||
// Msg("dup")
|
// Msg("dup")
|
||||||
// // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
|
// // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
|
||||||
//
|
//
|
||||||
// In this case, many consumers will take the last value,
|
// However, it’s not a big deal though as JSON accepts dup keys,
|
||||||
// but this is not guaranteed; check yours if in doubt.
|
// the last one prevails.
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Level defines log levels.
|
// Level defines log levels.
|
||||||
type Level int8
|
type Level uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DebugLevel defines debug log level.
|
// DebugLevel defines debug log level.
|
||||||
|
@ -127,87 +126,53 @@ const (
|
||||||
NoLevel
|
NoLevel
|
||||||
// Disabled disables the logger.
|
// Disabled disables the logger.
|
||||||
Disabled
|
Disabled
|
||||||
|
|
||||||
// TraceLevel defines trace log level.
|
|
||||||
TraceLevel Level = -1
|
|
||||||
// Values less than TraceLevel are handled as numbers.
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l Level) String() string {
|
func (l Level) String() string {
|
||||||
switch l {
|
switch l {
|
||||||
case TraceLevel:
|
|
||||||
return LevelTraceValue
|
|
||||||
case DebugLevel:
|
case DebugLevel:
|
||||||
return LevelDebugValue
|
return "debug"
|
||||||
case InfoLevel:
|
case InfoLevel:
|
||||||
return LevelInfoValue
|
return "info"
|
||||||
case WarnLevel:
|
case WarnLevel:
|
||||||
return LevelWarnValue
|
return "warn"
|
||||||
case ErrorLevel:
|
case ErrorLevel:
|
||||||
return LevelErrorValue
|
return "error"
|
||||||
case FatalLevel:
|
case FatalLevel:
|
||||||
return LevelFatalValue
|
return "fatal"
|
||||||
case PanicLevel:
|
case PanicLevel:
|
||||||
return LevelPanicValue
|
return "panic"
|
||||||
case Disabled:
|
|
||||||
return "disabled"
|
|
||||||
case NoLevel:
|
case NoLevel:
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return strconv.Itoa(int(l))
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseLevel converts a level string into a zlog Level value.
|
// ParseLevel converts a level string into a zerolog Level value.
|
||||||
// returns an error if the input string does not match known values.
|
// returns an error if the input string does not match known values.
|
||||||
func ParseLevel(levelStr string) (Level, error) {
|
func ParseLevel(levelStr string) (Level, error) {
|
||||||
switch {
|
switch levelStr {
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(TraceLevel)):
|
case DebugLevel.String():
|
||||||
return TraceLevel, nil
|
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(DebugLevel)):
|
|
||||||
return DebugLevel, nil
|
return DebugLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(InfoLevel)):
|
case InfoLevel.String():
|
||||||
return InfoLevel, nil
|
return InfoLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(WarnLevel)):
|
case WarnLevel.String():
|
||||||
return WarnLevel, nil
|
return WarnLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(ErrorLevel)):
|
case ErrorLevel.String():
|
||||||
return ErrorLevel, nil
|
return ErrorLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(FatalLevel)):
|
case FatalLevel.String():
|
||||||
return FatalLevel, nil
|
return FatalLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(PanicLevel)):
|
case PanicLevel.String():
|
||||||
return PanicLevel, nil
|
return PanicLevel, nil
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(Disabled)):
|
case NoLevel.String():
|
||||||
return Disabled, nil
|
|
||||||
case strings.EqualFold(levelStr, LevelFieldMarshalFunc(NoLevel)):
|
|
||||||
return NoLevel, nil
|
return NoLevel, nil
|
||||||
}
|
}
|
||||||
i, err := strconv.Atoi(levelStr)
|
|
||||||
if err != nil {
|
|
||||||
return NoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
|
return NoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
|
||||||
}
|
}
|
||||||
if i > 127 || i < -128 {
|
|
||||||
return NoLevel, fmt.Errorf("Out-Of-Bounds Level: '%d', defaulting to NoLevel", i)
|
|
||||||
}
|
|
||||||
return Level(i), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalText implements encoding.TextUnmarshaler to allow for easy reading from toml/yaml/json formats
|
|
||||||
func (l *Level) UnmarshalText(text []byte) error {
|
|
||||||
if l == nil {
|
|
||||||
return errors.New("can't unmarshal a nil *Level")
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
*l, err = ParseLevel(string(text))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// MarshalText implements encoding.TextMarshaler to allow for easy writing into toml/yaml/json formats
|
|
||||||
func (l Level) MarshalText() ([]byte, error) {
|
|
||||||
return []byte(LevelFieldMarshalFunc(l)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// A Logger represents an active logging object that generates lines
|
// A Logger represents an active logging object that generates lines
|
||||||
// of JSON output to an io.Writer. Each logging operation makes a single
|
// of JSON output to an io.Writer. Each logging operation makes a single
|
||||||
// call to the Writer's Write method. There is no guarantee on access
|
// call to the Writer's Write method. There is no guaranty on access
|
||||||
// serialization to the Writer. If your Writer is not thread safe,
|
// serialization to the Writer. If your Writer is not thread safe,
|
||||||
// you may consider a sync wrapper.
|
// you may consider a sync wrapper.
|
||||||
type Logger struct {
|
type Logger struct {
|
||||||
|
@ -216,7 +181,6 @@ type Logger struct {
|
||||||
sampler Sampler
|
sampler Sampler
|
||||||
context []byte
|
context []byte
|
||||||
hooks []Hook
|
hooks []Hook
|
||||||
stack bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a root logger with given output writer. If the output writer implements
|
// New creates a root logger with given output writer. If the output writer implements
|
||||||
|
@ -224,7 +188,7 @@ type Logger struct {
|
||||||
// one.
|
// one.
|
||||||
//
|
//
|
||||||
// Each logging operation makes a single call to the Writer's Write method. There is no
|
// Each logging operation makes a single call to the Writer's Write method. There is no
|
||||||
// guarantee on access serialization to the Writer. If your Writer is not thread safe,
|
// guaranty on access serialization to the Writer. If your Writer is not thread safe,
|
||||||
// you may consider using sync wrapper.
|
// you may consider using sync wrapper.
|
||||||
func New(w io.Writer) Logger {
|
func New(w io.Writer) Logger {
|
||||||
if w == nil {
|
if w == nil {
|
||||||
|
@ -234,7 +198,7 @@ func New(w io.Writer) Logger {
|
||||||
if !ok {
|
if !ok {
|
||||||
lw = levelWriterAdapter{w}
|
lw = levelWriterAdapter{w}
|
||||||
}
|
}
|
||||||
return Logger{w: lw, level: TraceLevel}
|
return Logger{w: lw}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Nop returns a disabled logger for which all operation are no-op.
|
// Nop returns a disabled logger for which all operation are no-op.
|
||||||
|
@ -247,7 +211,6 @@ func (l Logger) Output(w io.Writer) Logger {
|
||||||
l2 := New(w)
|
l2 := New(w)
|
||||||
l2.level = l.level
|
l2.level = l.level
|
||||||
l2.sampler = l.sampler
|
l2.sampler = l.sampler
|
||||||
l2.stack = l.stack
|
|
||||||
if len(l.hooks) > 0 {
|
if len(l.hooks) > 0 {
|
||||||
l2.hooks = append(l2.hooks, l.hooks...)
|
l2.hooks = append(l2.hooks, l.hooks...)
|
||||||
}
|
}
|
||||||
|
@ -264,10 +227,6 @@ func (l Logger) With() Context {
|
||||||
l.context = make([]byte, 0, 500)
|
l.context = make([]byte, 0, 500)
|
||||||
if context != nil {
|
if context != nil {
|
||||||
l.context = append(l.context, context...)
|
l.context = append(l.context, context...)
|
||||||
} else {
|
|
||||||
// This is needed for AppendKey to not check len of input
|
|
||||||
// thus making it inlinable
|
|
||||||
l.context = enc.AppendBeginMarker(l.context)
|
|
||||||
}
|
}
|
||||||
return Context{l}
|
return Context{l}
|
||||||
}
|
}
|
||||||
|
@ -282,9 +241,6 @@ func (l *Logger) UpdateContext(update func(c Context) Context) {
|
||||||
if cap(l.context) == 0 {
|
if cap(l.context) == 0 {
|
||||||
l.context = make([]byte, 0, 500)
|
l.context = make([]byte, 0, 500)
|
||||||
}
|
}
|
||||||
if len(l.context) == 0 {
|
|
||||||
l.context = enc.AppendBeginMarker(l.context)
|
|
||||||
}
|
|
||||||
c := update(Context{*l})
|
c := update(Context{*l})
|
||||||
l.context = c.l.context
|
l.context = c.l.context
|
||||||
}
|
}
|
||||||
|
@ -295,11 +251,6 @@ func (l Logger) Level(lvl Level) Logger {
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetLevel returns the current Level of l.
|
|
||||||
func (l Logger) GetLevel() Level {
|
|
||||||
return l.level
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sample returns a logger with the s sampler.
|
// Sample returns a logger with the s sampler.
|
||||||
func (l Logger) Sample(s Sampler) Logger {
|
func (l Logger) Sample(s Sampler) Logger {
|
||||||
l.sampler = s
|
l.sampler = s
|
||||||
|
@ -308,19 +259,10 @@ func (l Logger) Sample(s Sampler) Logger {
|
||||||
|
|
||||||
// Hook returns a logger with the h Hook.
|
// Hook returns a logger with the h Hook.
|
||||||
func (l Logger) Hook(h Hook) Logger {
|
func (l Logger) Hook(h Hook) Logger {
|
||||||
newHooks := make([]Hook, len(l.hooks), len(l.hooks)+1)
|
l.hooks = append(l.hooks, h)
|
||||||
copy(newHooks, l.hooks)
|
|
||||||
l.hooks = append(newHooks, h)
|
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trace starts a new message with trace level.
|
|
||||||
//
|
|
||||||
// You must call Msg on the returned event in order to send the event.
|
|
||||||
func (l *Logger) Trace() *Event {
|
|
||||||
return l.newEvent(TraceLevel, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug starts a new message with debug level.
|
// Debug starts a new message with debug level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
|
@ -349,18 +291,6 @@ func (l *Logger) Error() *Event {
|
||||||
return l.newEvent(ErrorLevel, nil)
|
return l.newEvent(ErrorLevel, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err starts a new message with error level with err as a field if not nil or
|
|
||||||
// with info level if err is nil.
|
|
||||||
//
|
|
||||||
// You must call Msg on the returned event in order to send the event.
|
|
||||||
func (l *Logger) Err(err error) *Event {
|
|
||||||
if err != nil {
|
|
||||||
return l.Error().Err(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return l.Info()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
||||||
// is called by the Msg method, which terminates the program immediately.
|
// is called by the Msg method, which terminates the program immediately.
|
||||||
//
|
//
|
||||||
|
@ -379,13 +309,11 @@ func (l *Logger) Panic() *Event {
|
||||||
|
|
||||||
// WithLevel starts a new message with level. Unlike Fatal and Panic
|
// WithLevel starts a new message with level. Unlike Fatal and Panic
|
||||||
// methods, WithLevel does not terminate the program or stop the ordinary
|
// methods, WithLevel does not terminate the program or stop the ordinary
|
||||||
// flow of a goroutine when used with their respective levels.
|
// flow of a gourotine when used with their respective levels.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func (l *Logger) WithLevel(level Level) *Event {
|
func (l *Logger) WithLevel(level Level) *Event {
|
||||||
switch level {
|
switch level {
|
||||||
case TraceLevel:
|
|
||||||
return l.Trace()
|
|
||||||
case DebugLevel:
|
case DebugLevel:
|
||||||
return l.Debug()
|
return l.Debug()
|
||||||
case InfoLevel:
|
case InfoLevel:
|
||||||
|
@ -403,7 +331,7 @@ func (l *Logger) WithLevel(level Level) *Event {
|
||||||
case Disabled:
|
case Disabled:
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
return l.newEvent(level, nil)
|
panic("zerolog: WithLevel(): invalid level: " + strconv.Itoa(int(level)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -419,7 +347,7 @@ func (l *Logger) Log() *Event {
|
||||||
// Arguments are handled in the manner of fmt.Print.
|
// Arguments are handled in the manner of fmt.Print.
|
||||||
func (l *Logger) Print(v ...interface{}) {
|
func (l *Logger) Print(v ...interface{}) {
|
||||||
if e := l.Debug(); e.Enabled() {
|
if e := l.Debug(); e.Enabled() {
|
||||||
e.CallerSkipFrame(1).Msg(fmt.Sprint(v...))
|
e.Msg(fmt.Sprint(v...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -427,7 +355,7 @@ func (l *Logger) Print(v ...interface{}) {
|
||||||
// Arguments are handled in the manner of fmt.Printf.
|
// Arguments are handled in the manner of fmt.Printf.
|
||||||
func (l *Logger) Printf(format string, v ...interface{}) {
|
func (l *Logger) Printf(format string, v ...interface{}) {
|
||||||
if e := l.Debug(); e.Enabled() {
|
if e := l.Debug(); e.Enabled() {
|
||||||
e.CallerSkipFrame(1).Msg(fmt.Sprintf(format, v...))
|
e.Msg(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,30 +367,24 @@ func (l Logger) Write(p []byte) (n int, err error) {
|
||||||
// Trim CR added by stdlog.
|
// Trim CR added by stdlog.
|
||||||
p = p[0 : n-1]
|
p = p[0 : n-1]
|
||||||
}
|
}
|
||||||
l.Log().CallerSkipFrame(1).Msg(string(p))
|
l.Log().Msg(string(p))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) newEvent(level Level, done func(string)) *Event {
|
func (l *Logger) newEvent(level Level, done func(string)) *Event {
|
||||||
enabled := l.should(level)
|
enabled := l.should(level)
|
||||||
if !enabled {
|
if !enabled {
|
||||||
if done != nil {
|
|
||||||
done("")
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
e := newEvent(l.w, level)
|
e := newEvent(l.w, level)
|
||||||
e.done = done
|
e.done = done
|
||||||
e.ch = l.hooks
|
e.ch = l.hooks
|
||||||
if level != NoLevel && LevelFieldName != "" {
|
if level != NoLevel {
|
||||||
e.Str(LevelFieldName, LevelFieldMarshalFunc(level))
|
e.Str(LevelFieldName, level.String())
|
||||||
}
|
}
|
||||||
if l.context != nil && len(l.context) > 1 {
|
if l.context != nil && len(l.context) > 0 {
|
||||||
e.buf = enc.AppendObjectData(e.buf, l.context)
|
e.buf = enc.AppendObjectData(e.buf, l.context)
|
||||||
}
|
}
|
||||||
if l.stack {
|
|
||||||
e.Stack()
|
|
||||||
}
|
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
95
log/log.go
95
log/log.go
|
@ -1,168 +1,115 @@
|
||||||
// Package log provides a global logger for zlog.
|
// Package log provides a global logger for zerolog.
|
||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Logger is the global logger.
|
// Logger is the global logger.
|
||||||
var Logger = zlog.New(nil).Output(
|
var Logger = zerolog.New(os.Stderr).With().Timestamp().Logger()
|
||||||
zerolog.ConsoleWriter{
|
|
||||||
Out: os.Stderr,
|
|
||||||
TimeFormat: time.RFC3339,
|
|
||||||
},
|
|
||||||
).With().Timestamp().Logger()
|
|
||||||
|
|
||||||
// Output duplicates the global logger and sets w as its output.
|
// Output duplicates the global logger and sets w as its output.
|
||||||
func Output(w io.Writer) zlog.Logger {
|
func Output(w io.Writer) zerolog.Logger {
|
||||||
return Logger.Output(w)
|
return Logger.Output(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
// With creates a child logger with the field added to its context.
|
// With creates a child logger with the field added to its context.
|
||||||
func With() zlog.Context {
|
func With() zerolog.Context {
|
||||||
return Logger.With()
|
return Logger.With()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Level creates a child logger with the minimum accepted level set to level.
|
// Level creates a child logger with the minimum accepted level set to level.
|
||||||
func Level(level zlog.Level) zlog.Logger {
|
func Level(level zerolog.Level) zerolog.Logger {
|
||||||
return Logger.Level(level)
|
return Logger.Level(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sample returns a logger with the s sampler.
|
// Sample returns a logger with the s sampler.
|
||||||
func Sample(s zlog.Sampler) zlog.Logger {
|
func Sample(s zerolog.Sampler) zerolog.Logger {
|
||||||
return Logger.Sample(s)
|
return Logger.Sample(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hook returns a logger with the h Hook.
|
// Hook returns a logger with the h Hook.
|
||||||
func Hook(h zlog.Hook) zlog.Logger {
|
func Hook(h zerolog.Hook) zerolog.Logger {
|
||||||
return Logger.Hook(h)
|
return Logger.Hook(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Err starts a new message with error level with err as a field if not nil or
|
|
||||||
// with info level if err is nil.
|
|
||||||
//
|
|
||||||
// You must call Msg on the returned event in order to send the event.
|
|
||||||
func Err(err error) *zlog.Event {
|
|
||||||
return Logger.Err(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trace starts a new message with trace level.
|
|
||||||
//
|
|
||||||
// You must call Msg on the returned event in order to send the event.
|
|
||||||
func Trace() *zlog.Event {
|
|
||||||
return Logger.Trace()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Debug starts a new message with debug level.
|
// Debug starts a new message with debug level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Debug() *zlog.Event {
|
func Debug() *zerolog.Event {
|
||||||
return Logger.Debug()
|
return Logger.Debug()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info starts a new message with info level.
|
// Info starts a new message with info level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Info() *zlog.Event {
|
func Info() *zerolog.Event {
|
||||||
return Logger.Info()
|
return Logger.Info()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn starts a new message with warn level.
|
// Warn starts a new message with warn level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Warn() *zlog.Event {
|
func Warn() *zerolog.Event {
|
||||||
return Logger.Warn()
|
return Logger.Warn()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error starts a new message with error level.
|
// Error starts a new message with error level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Error() *zlog.Event {
|
func Error() *zerolog.Event {
|
||||||
return Logger.Error()
|
return Logger.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf sends a log event using debug level and no extra field.
|
|
||||||
// Arguments are handled in the manner of fmt.Errorf.
|
|
||||||
func Errorf(format string, v ...interface{}) {
|
|
||||||
Logger.Error().CallerSkipFrame(1).Msgf(format, v...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Errorln(args ...interface{}) {
|
|
||||||
Logger.Error().Msg(fmt.Sprintln(args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fatal starts a new message with fatal level. The os.Exit(1) function
|
// 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.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Fatal() *zlog.Event {
|
func Fatal() *zerolog.Event {
|
||||||
return Logger.Fatal()
|
return Logger.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fatalf(format string, args ...interface{}) {
|
|
||||||
Logger.Fatal().Msgf(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Fatalln(args ...interface{}) {
|
|
||||||
Logger.Fatal().Msg(fmt.Sprintln(args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Panic starts a new message with panic level. The message is also sent
|
// Panic starts a new message with panic level. The message is also sent
|
||||||
// to the panic function.
|
// to the panic function.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Panic() *zlog.Event {
|
func Panic() *zerolog.Event {
|
||||||
return Logger.Panic()
|
return Logger.Panic()
|
||||||
}
|
}
|
||||||
|
|
||||||
func Panicf(format string, args ...interface{}) {
|
|
||||||
Logger.Panic().Msgf(format, args...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func Panicln(args ...interface{}) {
|
|
||||||
Logger.Panic().Msg(fmt.Sprintln(args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
// WithLevel starts a new message with level.
|
// WithLevel starts a new message with level.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func WithLevel(level zlog.Level) *zlog.Event {
|
func WithLevel(level zerolog.Level) *zerolog.Event {
|
||||||
return Logger.WithLevel(level)
|
return Logger.WithLevel(level)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log starts a new message with no level. Setting zlog.GlobalLevel to
|
// Log starts a new message with no level. Setting zerolog.GlobalLevel to
|
||||||
// zlog.Disabled will still disable events produced by this method.
|
// zerolog.Disabled will still disable events produced by this method.
|
||||||
//
|
//
|
||||||
// You must call Msg on the returned event in order to send the event.
|
// You must call Msg on the returned event in order to send the event.
|
||||||
func Log() *zlog.Event {
|
func Log() *zerolog.Event {
|
||||||
return Logger.Log()
|
return Logger.Log()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print sends a log event using debug level and no extra field.
|
// Print sends a log event using debug level and no extra field.
|
||||||
// Arguments are handled in the manner of fmt.Print.
|
// Arguments are handled in the manner of fmt.Print.
|
||||||
func Print(v ...interface{}) {
|
func Print(v ...interface{}) {
|
||||||
Logger.Debug().CallerSkipFrame(1).Msg(fmt.Sprint(v...))
|
Logger.Print(v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf sends a log event using debug level and no extra field.
|
// Printf sends a log event using debug level and no extra field.
|
||||||
// Arguments are handled in the manner of fmt.Printf.
|
// Arguments are handled in the manner of fmt.Printf.
|
||||||
func Printf(format string, v ...interface{}) {
|
func Printf(format string, v ...interface{}) {
|
||||||
Logger.Debug().CallerSkipFrame(1).Msgf(format, v...)
|
Logger.Printf(format, v...)
|
||||||
}
|
|
||||||
|
|
||||||
func Println(args ...interface{}) {
|
|
||||||
Logger.Debug().Msg(fmt.Sprintln(args...))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ctx returns the Logger associated with the ctx. If no logger
|
// Ctx returns the Logger associated with the ctx. If no logger
|
||||||
// is associated, a disabled logger is returned.
|
// is associated, a disabled logger is returned.
|
||||||
func Ctx(ctx context.Context) *zlog.Logger {
|
func Ctx(ctx context.Context) *zerolog.Logger {
|
||||||
return zlog.Ctx(ctx)
|
return zerolog.Ctx(ctx)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
//go:build !binary_log
|
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
|
|
||||||
package log_test
|
package log_test
|
||||||
|
@ -9,8 +8,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
"tuxpa.in/a/zlog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// setup would normally be an init() function, however, there seems
|
// setup would normally be an init() function, however, there seems
|
||||||
|
@ -18,16 +17,16 @@ import (
|
||||||
// global Logger from an init()
|
// global Logger from an init()
|
||||||
func setup() {
|
func setup() {
|
||||||
// UNIX Time is faster and smaller than most timestamps
|
// UNIX Time is faster and smaller than most timestamps
|
||||||
// If you set zlog.TimeFieldFormat to an empty string,
|
// If you set zerolog.TimeFieldFormat to an empty string,
|
||||||
// logs will write with UNIX time
|
// logs will write with UNIX time
|
||||||
zlog.TimeFieldFormat = ""
|
zerolog.TimeFieldFormat = ""
|
||||||
// In order to always output a static time to stdout for these
|
// In order to always output a static time to stdout for these
|
||||||
// examples to pass, we need to override zlog.TimestampFunc
|
// examples to pass, we need to override zerolog.TimestampFunc
|
||||||
// and log.Logger globals -- you would not normally need to do this
|
// and log.Logger globals -- you would not normally need to do this
|
||||||
zlog.TimestampFunc = func() time.Time {
|
zerolog.TimestampFunc = func() time.Time {
|
||||||
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
|
return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
|
||||||
}
|
}
|
||||||
log.Logger = zlog.New(os.Stdout).With().Timestamp().Logger()
|
log.Logger = zerolog.New(os.Stdout).With().Timestamp().Logger()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Simple logging example using the Print function in the log package
|
// Simple logging example using the Print function in the log package
|
||||||
|
@ -55,25 +54,6 @@ func ExampleLog() {
|
||||||
// Output: {"time":1199811905,"message":"hello world"}
|
// Output: {"time":1199811905,"message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example of a conditional level based on the presence of an error.
|
|
||||||
func ExampleErr() {
|
|
||||||
setup()
|
|
||||||
err := errors.New("some error")
|
|
||||||
log.Err(err).Msg("hello world")
|
|
||||||
log.Err(nil).Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"level":"error","error":"some error","time":1199811905,"message":"hello world"}
|
|
||||||
// {"level":"info","time":1199811905,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Example of a log at a particular "level" (in this case, "trace")
|
|
||||||
func ExampleTrace() {
|
|
||||||
setup()
|
|
||||||
log.Trace().Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"level":"trace","time":1199811905,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Example of a log at a particular "level" (in this case, "debug")
|
// Example of a log at a particular "level" (in this case, "debug")
|
||||||
func ExampleDebug() {
|
func ExampleDebug() {
|
||||||
setup()
|
setup()
|
||||||
|
@ -131,9 +111,9 @@ func Example() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Default level for this example is info, unless debug flag is present
|
// Default level for this example is info, unless debug flag is present
|
||||||
zlog.SetGlobalLevel(zlog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
if *debug {
|
if *debug {
|
||||||
zlog.SetGlobalLevel(zlog.DebugLevel)
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug().Msg("This message appears only when log level set to Debug")
|
log.Debug().Msg("This message appears only when log level set to Debug")
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
|
|
||||||
package zlog_test
|
package zerolog_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -10,18 +10,18 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleNew() {
|
func ExampleNew() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Info().Msg("hello world")
|
log.Info().Msg("hello world")
|
||||||
// Output: {"level":"info","message":"hello world"}
|
// Output: {"level":"info","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_With() {
|
func ExampleLogger_With() {
|
||||||
log := zlog.New(os.Stdout).
|
log := zerolog.New(os.Stdout).
|
||||||
With().
|
With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -32,7 +32,7 @@ func ExampleLogger_With() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Level() {
|
func ExampleLogger_Level() {
|
||||||
log := zlog.New(os.Stdout).Level(zlog.WarnLevel)
|
log := zerolog.New(os.Stdout).Level(zerolog.WarnLevel)
|
||||||
|
|
||||||
log.Info().Msg("filtered out message")
|
log.Info().Msg("filtered out message")
|
||||||
log.Error().Msg("kept message")
|
log.Error().Msg("kept message")
|
||||||
|
@ -41,7 +41,7 @@ func ExampleLogger_Level() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Sample() {
|
func ExampleLogger_Sample() {
|
||||||
log := zlog.New(os.Stdout).Sample(&zlog.BasicSampler{N: 2})
|
log := zerolog.New(os.Stdout).Sample(&zerolog.BasicSampler{N: 2})
|
||||||
|
|
||||||
log.Info().Msg("message 1")
|
log.Info().Msg("message 1")
|
||||||
log.Info().Msg("message 2")
|
log.Info().Msg("message 2")
|
||||||
|
@ -54,8 +54,8 @@ func ExampleLogger_Sample() {
|
||||||
|
|
||||||
type LevelNameHook struct{}
|
type LevelNameHook struct{}
|
||||||
|
|
||||||
func (h LevelNameHook) Run(e *zlog.Event, l zlog.Level, msg string) {
|
func (h LevelNameHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
|
||||||
if l != zlog.NoLevel {
|
if l != zerolog.NoLevel {
|
||||||
e.Str("level_name", l.String())
|
e.Str("level_name", l.String())
|
||||||
} else {
|
} else {
|
||||||
e.Str("level_name", "NoLevel")
|
e.Str("level_name", "NoLevel")
|
||||||
|
@ -64,7 +64,7 @@ func (h LevelNameHook) Run(e *zlog.Event, l zlog.Level, msg string) {
|
||||||
|
|
||||||
type MessageHook string
|
type MessageHook string
|
||||||
|
|
||||||
func (h MessageHook) Run(e *zlog.Event, l zlog.Level, msg string) {
|
func (h MessageHook) Run(e *zerolog.Event, l zerolog.Level, msg string) {
|
||||||
e.Str("the_message", msg)
|
e.Str("the_message", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@ func ExampleLogger_Hook() {
|
||||||
var levelNameHook LevelNameHook
|
var levelNameHook LevelNameHook
|
||||||
var messageHook MessageHook = "The message"
|
var messageHook MessageHook = "The message"
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).Hook(levelNameHook).Hook(messageHook)
|
log := zerolog.New(os.Stdout).Hook(levelNameHook).Hook(messageHook)
|
||||||
|
|
||||||
log.Info().Msg("hello world")
|
log.Info().Msg("hello world")
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ func ExampleLogger_Hook() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Print() {
|
func ExampleLogger_Print() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Print("hello world")
|
log.Print("hello world")
|
||||||
|
|
||||||
|
@ -88,26 +88,15 @@ func ExampleLogger_Print() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Printf() {
|
func ExampleLogger_Printf() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Printf("hello %s", "world")
|
log.Printf("hello %s", "world")
|
||||||
|
|
||||||
// Output: {"level":"debug","message":"hello world"}
|
// Output: {"level":"debug","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Trace() {
|
|
||||||
log := zlog.New(os.Stdout)
|
|
||||||
|
|
||||||
log.Trace().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Int("n", 123).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"level":"trace","foo":"bar","n":123,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleLogger_Debug() {
|
func ExampleLogger_Debug() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -118,7 +107,7 @@ func ExampleLogger_Debug() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Info() {
|
func ExampleLogger_Info() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Info().
|
log.Info().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -129,7 +118,7 @@ func ExampleLogger_Info() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Warn() {
|
func ExampleLogger_Warn() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Warn().
|
log.Warn().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -139,7 +128,7 @@ func ExampleLogger_Warn() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Error() {
|
func ExampleLogger_Error() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Error().
|
log.Error().
|
||||||
Err(errors.New("some error")).
|
Err(errors.New("some error")).
|
||||||
|
@ -149,16 +138,16 @@ func ExampleLogger_Error() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_WithLevel() {
|
func ExampleLogger_WithLevel() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.WithLevel(zlog.InfoLevel).
|
log.WithLevel(zerolog.InfoLevel).
|
||||||
Msg("hello world")
|
Msg("hello world")
|
||||||
|
|
||||||
// Output: {"level":"info","message":"hello world"}
|
// Output: {"level":"info","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Write() {
|
func ExampleLogger_Write() {
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
|
@ -171,7 +160,7 @@ func ExampleLogger_Write() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleLogger_Log() {
|
func ExampleLogger_Log() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -182,11 +171,11 @@ func ExampleLogger_Log() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Dict() {
|
func ExampleEvent_Dict() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Dict("dict", zlog.Dict().
|
Dict("dict", zerolog.Dict().
|
||||||
Str("bar", "baz").
|
Str("bar", "baz").
|
||||||
Int("n", 1),
|
Int("n", 1),
|
||||||
).
|
).
|
||||||
|
@ -201,7 +190,7 @@ type User struct {
|
||||||
Created time.Time
|
Created time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u User) MarshalZerologObject(e *zlog.Event) {
|
func (u User) MarshalZerologObject(e *zerolog.Event) {
|
||||||
e.Str("name", u.Name).
|
e.Str("name", u.Name).
|
||||||
Int("age", u.Age).
|
Int("age", u.Age).
|
||||||
Time("created", u.Created)
|
Time("created", u.Created)
|
||||||
|
@ -213,7 +202,7 @@ type Price struct {
|
||||||
unit string
|
unit string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Price) MarshalZerologObject(e *zlog.Event) {
|
func (p Price) MarshalZerologObject(e *zerolog.Event) {
|
||||||
denom := uint64(1)
|
denom := uint64(1)
|
||||||
for i := 0; i < p.prec; i++ {
|
for i := 0; i < p.prec; i++ {
|
||||||
denom *= 10
|
denom *= 10
|
||||||
|
@ -225,34 +214,30 @@ func (p Price) MarshalZerologObject(e *zlog.Event) {
|
||||||
|
|
||||||
type Users []User
|
type Users []User
|
||||||
|
|
||||||
func (uu Users) MarshalZerologArray(a *zlog.Array) {
|
func (uu Users) MarshalZerologArray(a *zerolog.Array) {
|
||||||
for _, u := range uu {
|
for _, u := range uu {
|
||||||
a.Object(u)
|
a.Object(u)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Array() {
|
func ExampleEvent_Array() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Array("array", zlog.Arr().
|
Array("array", zerolog.Arr().
|
||||||
Str("baz").
|
Str("baz").
|
||||||
Int(1).
|
Int(1),
|
||||||
Dict(zlog.Dict().
|
|
||||||
Str("bar", "baz").
|
|
||||||
Int("n", 1),
|
|
||||||
),
|
|
||||||
).
|
).
|
||||||
Msg("hello world")
|
Msg("hello world")
|
||||||
|
|
||||||
// Output: {"foo":"bar","array":["baz",1,{"bar":"baz","n":1}],"message":"hello world"}
|
// Output: {"foo":"bar","array":["baz",1],"message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Array_object() {
|
func ExampleEvent_Array_object() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
// Users implements zlog.LogArrayMarshaler
|
// Users implements zerolog.LogArrayMarshaler
|
||||||
u := Users{
|
u := Users{
|
||||||
User{"John", 35, time.Time{}},
|
User{"John", 35, time.Time{}},
|
||||||
User{"Bob", 55, time.Time{}},
|
User{"Bob", 55, time.Time{}},
|
||||||
|
@ -267,9 +252,9 @@ func ExampleEvent_Array_object() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Object() {
|
func ExampleEvent_Object() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
// User implements zlog.LogObjectMarshaler
|
// User implements zerolog.LogObjectMarshaler
|
||||||
u := User{"John", 35, time.Time{}}
|
u := User{"John", 35, time.Time{}}
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
|
@ -281,7 +266,7 @@ func ExampleEvent_Object() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_EmbedObject() {
|
func ExampleEvent_EmbedObject() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
price := Price{val: 6449, prec: 2, unit: "$"}
|
price := Price{val: 6449, prec: 2, unit: "$"}
|
||||||
|
|
||||||
|
@ -294,7 +279,7 @@ func ExampleEvent_EmbedObject() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Interface() {
|
func ExampleEvent_Interface() {
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
obj := struct {
|
obj := struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
@ -311,9 +296,9 @@ func ExampleEvent_Interface() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Dur() {
|
func ExampleEvent_Dur() {
|
||||||
d := 10 * time.Second
|
d := time.Duration(10 * time.Second)
|
||||||
|
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -325,11 +310,11 @@ func ExampleEvent_Dur() {
|
||||||
|
|
||||||
func ExampleEvent_Durs() {
|
func ExampleEvent_Durs() {
|
||||||
d := []time.Duration{
|
d := []time.Duration{
|
||||||
10 * time.Second,
|
time.Duration(10 * time.Second),
|
||||||
20 * time.Second,
|
time.Duration(20 * time.Second),
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout)
|
log := zerolog.New(os.Stdout)
|
||||||
|
|
||||||
log.Log().
|
log.Log().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
|
@ -339,42 +324,10 @@ func ExampleEvent_Durs() {
|
||||||
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
// Output: {"foo":"bar","durs":[10000,20000],"message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleEvent_Fields_map() {
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"bar": "baz",
|
|
||||||
"n": 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
log := zlog.New(os.Stdout)
|
|
||||||
|
|
||||||
log.Log().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleEvent_Fields_slice() {
|
|
||||||
fields := []interface{}{
|
|
||||||
"bar", "baz",
|
|
||||||
"n", 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
log := zlog.New(os.Stdout)
|
|
||||||
|
|
||||||
log.Log().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleContext_Dict() {
|
func ExampleContext_Dict() {
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Dict("dict", zlog.Dict().
|
Dict("dict", zerolog.Dict().
|
||||||
Str("bar", "baz").
|
Str("bar", "baz").
|
||||||
Int("n", 1),
|
Int("n", 1),
|
||||||
).Logger()
|
).Logger()
|
||||||
|
@ -385,9 +338,9 @@ func ExampleContext_Dict() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Array() {
|
func ExampleContext_Array() {
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Array("array", zlog.Arr().
|
Array("array", zerolog.Arr().
|
||||||
Str("baz").
|
Str("baz").
|
||||||
Int(1),
|
Int(1),
|
||||||
).Logger()
|
).Logger()
|
||||||
|
@ -398,13 +351,13 @@ func ExampleContext_Array() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Array_object() {
|
func ExampleContext_Array_object() {
|
||||||
// Users implements zlog.LogArrayMarshaler
|
// Users implements zerolog.LogArrayMarshaler
|
||||||
u := Users{
|
u := Users{
|
||||||
User{"John", 35, time.Time{}},
|
User{"John", 35, time.Time{}},
|
||||||
User{"Bob", 55, time.Time{}},
|
User{"Bob", 55, time.Time{}},
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Array("users", u).
|
Array("users", u).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -415,10 +368,10 @@ func ExampleContext_Array_object() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Object() {
|
func ExampleContext_Object() {
|
||||||
// User implements zlog.LogObjectMarshaler
|
// User implements zerolog.LogObjectMarshaler
|
||||||
u := User{"John", 35, time.Time{}}
|
u := User{"John", 35, time.Time{}}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Object("user", u).
|
Object("user", u).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -432,7 +385,7 @@ func ExampleContext_EmbedObject() {
|
||||||
|
|
||||||
price := Price{val: 6449, prec: 2, unit: "$"}
|
price := Price{val: 6449, prec: 2, unit: "$"}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
EmbedObject(price).
|
EmbedObject(price).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -449,7 +402,7 @@ func ExampleContext_Interface() {
|
||||||
Name: "john",
|
Name: "john",
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Interface("obj", obj).
|
Interface("obj", obj).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -460,9 +413,9 @@ func ExampleContext_Interface() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Dur() {
|
func ExampleContext_Dur() {
|
||||||
d := 10 * time.Second
|
d := time.Duration(10 * time.Second)
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Dur("dur", d).
|
Dur("dur", d).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -474,11 +427,11 @@ func ExampleContext_Dur() {
|
||||||
|
|
||||||
func ExampleContext_Durs() {
|
func ExampleContext_Durs() {
|
||||||
d := []time.Duration{
|
d := []time.Duration{
|
||||||
10 * time.Second,
|
time.Duration(10 * time.Second),
|
||||||
20 * time.Second,
|
time.Duration(20 * time.Second),
|
||||||
}
|
}
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
Str("foo", "bar").
|
Str("foo", "bar").
|
||||||
Durs("durs", d).
|
Durs("durs", d).
|
||||||
Logger()
|
Logger()
|
||||||
|
@ -490,7 +443,7 @@ func ExampleContext_Durs() {
|
||||||
|
|
||||||
func ExampleContext_IPAddr() {
|
func ExampleContext_IPAddr() {
|
||||||
hostIP := net.IP{192, 168, 0, 100}
|
hostIP := net.IP{192, 168, 0, 100}
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
IPAddr("HostIP", hostIP).
|
IPAddr("HostIP", hostIP).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
|
@ -501,7 +454,7 @@ func ExampleContext_IPAddr() {
|
||||||
|
|
||||||
func ExampleContext_IPPrefix() {
|
func ExampleContext_IPPrefix() {
|
||||||
route := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
|
route := net.IPNet{IP: net.IP{192, 168, 0, 0}, Mask: net.CIDRMask(24, 32)}
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
IPPrefix("Route", route).
|
IPPrefix("Route", route).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
|
@ -510,9 +463,9 @@ func ExampleContext_IPPrefix() {
|
||||||
// Output: {"Route":"192.168.0.0/24","message":"hello world"}
|
// Output: {"Route":"192.168.0.0/24","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_MACAddr() {
|
func ExampleContext_MacAddr() {
|
||||||
mac := net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}
|
mac := net.HardwareAddr{0x00, 0x14, 0x22, 0x01, 0x23, 0x45}
|
||||||
log := zlog.New(os.Stdout).With().
|
log := zerolog.New(os.Stdout).With().
|
||||||
MACAddr("hostMAC", mac).
|
MACAddr("hostMAC", mac).
|
||||||
Logger()
|
Logger()
|
||||||
|
|
||||||
|
@ -520,35 +473,3 @@ func ExampleContext_MACAddr() {
|
||||||
|
|
||||||
// Output: {"hostMAC":"00:14:22:01:23:45","message":"hello world"}
|
// Output: {"hostMAC":"00:14:22:01:23:45","message":"hello world"}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleContext_Fields_map() {
|
|
||||||
fields := map[string]interface{}{
|
|
||||||
"bar": "baz",
|
|
||||||
"n": 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
log.Log().Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
||||||
func ExampleContext_Fields_slice() {
|
|
||||||
fields := []interface{}{
|
|
||||||
"bar", "baz",
|
|
||||||
"n", 1,
|
|
||||||
}
|
|
||||||
|
|
||||||
log := zlog.New(os.Stdout).With().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Fields(fields).
|
|
||||||
Logger()
|
|
||||||
|
|
||||||
log.Log().Msg("hello world")
|
|
||||||
|
|
||||||
// Output: {"foo":"bar","bar":"baz","n":1,"message":"hello world"}
|
|
||||||
}
|
|
||||||
|
|
382
log_test.go
382
log_test.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -7,8 +7,6 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -77,30 +75,10 @@ func TestInfo(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEmptyLevelFieldName(t *testing.T) {
|
|
||||||
fieldName := LevelFieldName
|
|
||||||
LevelFieldName = ""
|
|
||||||
|
|
||||||
t.Run("empty setting", func(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
log.Info().
|
|
||||||
Str("foo", "bar").
|
|
||||||
Int("n", 123).
|
|
||||||
Msg("")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","n":123}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
LevelFieldName = fieldName
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWith(t *testing.T) {
|
func TestWith(t *testing.T) {
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
ctx := New(out).With().
|
ctx := New(out).With().
|
||||||
Str("string", "foo").
|
Str("string", "foo").
|
||||||
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
|
||||||
Stringer("stringer_nil", nil).
|
|
||||||
Bytes("bytes", []byte("bar")).
|
Bytes("bytes", []byte("bar")).
|
||||||
Hex("hex", []byte{0x12, 0xef}).
|
Hex("hex", []byte{0x12, 0xef}).
|
||||||
RawJSON("json", []byte(`{"some":"json"}`)).
|
RawJSON("json", []byte(`{"some":"json"}`)).
|
||||||
|
@ -124,21 +102,7 @@ func TestWith(t *testing.T) {
|
||||||
caller := fmt.Sprintf("%s:%d", file, line+3)
|
caller := fmt.Sprintf("%s:%d", file, line+3)
|
||||||
log := ctx.Caller().Logger()
|
log := ctx.Caller().Logger()
|
||||||
log.Log().Msg("")
|
log.Log().Msg("")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","stringer":"127.0.0.1","stringer_nil":null,"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 {
|
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate CallerWithSkipFrameCount.
|
|
||||||
out.Reset()
|
|
||||||
_, file, line, _ = runtime.Caller(0)
|
|
||||||
caller = fmt.Sprintf("%s:%d", file, line+5)
|
|
||||||
log = ctx.CallerWithSkipFrameCount(3).Logger()
|
|
||||||
func() {
|
|
||||||
log.Log().Msg("")
|
|
||||||
}()
|
|
||||||
// The above line is a little contrived, but the line above should be the line due
|
|
||||||
// to the extra frame skip.
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","stringer":"127.0.0.1","stringer_nil":null,"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)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -246,66 +210,6 @@ func TestFieldsMapNilPnt(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFieldsSlice(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
log.Log().Fields([]interface{}{
|
|
||||||
"nil", nil,
|
|
||||||
"string", "foo",
|
|
||||||
"bytes", []byte("bar"),
|
|
||||||
"error", errors.New("some error"),
|
|
||||||
"bool", true,
|
|
||||||
"int", int(1),
|
|
||||||
"int8", int8(2),
|
|
||||||
"int16", int16(3),
|
|
||||||
"int32", int32(4),
|
|
||||||
"int64", int64(5),
|
|
||||||
"uint", uint(6),
|
|
||||||
"uint8", uint8(7),
|
|
||||||
"uint16", uint16(8),
|
|
||||||
"uint32", uint32(9),
|
|
||||||
"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()), `{"nil":null,"string":"foo","bytes":"bar","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,"ipv6":"2001:db8:85a3::8a2e:370:7334","dur":1000,"time":"0001-01-01T00:00:00Z","obj":{"Pub":"a","Tag":"b","priv":1}}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFieldsSliceExtraneous(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
log.Log().Fields([]interface{}{
|
|
||||||
"string", "foo",
|
|
||||||
"error", errors.New("some error"),
|
|
||||||
32, "valueForNonStringKey",
|
|
||||||
"bool", true,
|
|
||||||
"int", int(1),
|
|
||||||
"keyWithoutValue",
|
|
||||||
}).Msg("")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":"foo","error":"some error","bool":true,"int":1}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFieldsNotMapSlice(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
log.Log().
|
|
||||||
Fields(obj{"a", "b", 1}).
|
|
||||||
Fields("string").
|
|
||||||
Fields(1).
|
|
||||||
Msg("")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestFields(t *testing.T) {
|
func TestFields(t *testing.T) {
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
log := New(out)
|
log := New(out)
|
||||||
|
@ -315,13 +219,9 @@ func TestFields(t *testing.T) {
|
||||||
log.Log().
|
log.Log().
|
||||||
Caller().
|
Caller().
|
||||||
Str("string", "foo").
|
Str("string", "foo").
|
||||||
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
|
||||||
Stringer("stringer_nil", nil).
|
|
||||||
Bytes("bytes", []byte("bar")).
|
Bytes("bytes", []byte("bar")).
|
||||||
Hex("hex", []byte{0x12, 0xef}).
|
Hex("hex", []byte{0x12, 0xef}).
|
||||||
RawJSON("json", []byte(`{"some":"json"}`)).
|
RawJSON("json", []byte(`{"some":"json"}`)).
|
||||||
RawCBOR("cbor", []byte{0x83, 0x01, 0x82, 0x02, 0x03, 0x82, 0x04, 0x05}).
|
|
||||||
Func(func(e *Event) { e.Str("func", "func_output") }).
|
|
||||||
AnErr("some_err", nil).
|
AnErr("some_err", nil).
|
||||||
Err(errors.New("some error")).
|
Err(errors.New("some error")).
|
||||||
Bool("bool", true).
|
Bool("bool", true).
|
||||||
|
@ -345,7 +245,7 @@ func TestFields(t *testing.T) {
|
||||||
Time("time", time.Time{}).
|
Time("time", time.Time{}).
|
||||||
TimeDiff("diff", now, now.Add(-10*time.Second)).
|
TimeDiff("diff", now, now.Add(-10*time.Second)).
|
||||||
Msg("")
|
Msg("")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"cbor":"data:application/cbor;base64,gwGCAgOCBAU=","func":"func_output","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 {
|
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)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -355,7 +255,6 @@ func TestFieldsArrayEmpty(t *testing.T) {
|
||||||
log := New(out)
|
log := New(out)
|
||||||
log.Log().
|
log.Log().
|
||||||
Strs("string", []string{}).
|
Strs("string", []string{}).
|
||||||
Stringers("stringer", []fmt.Stringer{}).
|
|
||||||
Errs("err", []error{}).
|
Errs("err", []error{}).
|
||||||
Bools("bool", []bool{}).
|
Bools("bool", []bool{}).
|
||||||
Ints("int", []int{}).
|
Ints("int", []int{}).
|
||||||
|
@ -373,7 +272,7 @@ func TestFieldsArrayEmpty(t *testing.T) {
|
||||||
Durs("dur", []time.Duration{}).
|
Durs("dur", []time.Duration{}).
|
||||||
Times("time", []time.Time{}).
|
Times("time", []time.Time{}).
|
||||||
Msg("")
|
Msg("")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":[],"stringer":[],"err":[],"bool":[],"int":[],"int8":[],"int16":[],"int32":[],"int64":[],"uint":[],"uint8":[],"uint16":[],"uint32":[],"uint64":[],"float32":[],"float64":[],"dur":[],"time":[]}`+"\n"; got != want {
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":[],"err":[],"bool":[],"int":[],"int8":[],"int16":[],"int32":[],"int64":[],"uint":[],"uint8":[],"uint16":[],"uint32":[],"uint64":[],"float32":[],"float64":[],"dur":[],"time":[]}`+"\n"; got != want {
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,7 +282,6 @@ func TestFieldsArraySingleElement(t *testing.T) {
|
||||||
log := New(out)
|
log := New(out)
|
||||||
log.Log().
|
log.Log().
|
||||||
Strs("string", []string{"foo"}).
|
Strs("string", []string{"foo"}).
|
||||||
Stringers("stringer", []fmt.Stringer{net.IP{127, 0, 0, 1}}).
|
|
||||||
Errs("err", []error{errors.New("some error")}).
|
Errs("err", []error{errors.New("some error")}).
|
||||||
Bools("bool", []bool{true}).
|
Bools("bool", []bool{true}).
|
||||||
Ints("int", []int{1}).
|
Ints("int", []int{1}).
|
||||||
|
@ -399,9 +297,9 @@ func TestFieldsArraySingleElement(t *testing.T) {
|
||||||
Floats32("float32", []float32{11}).
|
Floats32("float32", []float32{11}).
|
||||||
Floats64("float64", []float64{12}).
|
Floats64("float64", []float64{12}).
|
||||||
Durs("dur", []time.Duration{1 * time.Second}).
|
Durs("dur", []time.Duration{1 * time.Second}).
|
||||||
Times("time", []time.Time{{}}).
|
Times("time", []time.Time{time.Time{}}).
|
||||||
Msg("")
|
Msg("")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo"],"stringer":["127.0.0.1"],"err":["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"]}`+"\n"; got != want {
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo"],"err":["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"]}`+"\n"; got != want {
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -411,7 +309,6 @@ func TestFieldsArrayMultipleElement(t *testing.T) {
|
||||||
log := New(out)
|
log := New(out)
|
||||||
log.Log().
|
log.Log().
|
||||||
Strs("string", []string{"foo", "bar"}).
|
Strs("string", []string{"foo", "bar"}).
|
||||||
Stringers("stringer", []fmt.Stringer{nil, net.IP{127, 0, 0, 1}}).
|
|
||||||
Errs("err", []error{errors.New("some error"), nil}).
|
Errs("err", []error{errors.New("some error"), nil}).
|
||||||
Bools("bool", []bool{true, false}).
|
Bools("bool", []bool{true, false}).
|
||||||
Ints("int", []int{1, 0}).
|
Ints("int", []int{1, 0}).
|
||||||
|
@ -427,9 +324,9 @@ func TestFieldsArrayMultipleElement(t *testing.T) {
|
||||||
Floats32("float32", []float32{11, 0}).
|
Floats32("float32", []float32{11, 0}).
|
||||||
Floats64("float64", []float64{12, 0}).
|
Floats64("float64", []float64{12, 0}).
|
||||||
Durs("dur", []time.Duration{1 * time.Second, 0}).
|
Durs("dur", []time.Duration{1 * time.Second, 0}).
|
||||||
Times("time", []time.Time{{}, {}}).
|
Times("time", []time.Time{time.Time{}, time.Time{}}).
|
||||||
Msg("")
|
Msg("")
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo","bar"],"stringer":[null,"127.0.0.1"],"err":["some error",null],"bool":[true,false],"int":[1,0],"int8":[2,0],"int16":[3,0],"int32":[4,0],"int64":[5,0],"uint":[6,0],"uint8":[7,0],"uint16":[8,0],"uint32":[9,0],"uint64":[10,0],"float32":[11,0],"float64":[12,0],"dur":[1000,0],"time":["0001-01-01T00:00:00Z","0001-01-01T00:00:00Z"]}`+"\n"; got != want {
|
if got, want := decodeIfBinaryToString(out.Bytes()), `{"string":["foo","bar"],"err":["some error",null],"bool":[true,false],"int":[1,0],"int8":[2,0],"int16":[3,0],"int32":[4,0],"int64":[5,0],"uint":[6,0],"uint8":[7,0],"uint16":[8,0],"uint32":[9,0],"uint64":[10,0],"float32":[11,0],"float64":[12,0],"dur":[1000,0],"time":["0001-01-01T00:00:00Z","0001-01-01T00:00:00Z"]}`+"\n"; got != want {
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -440,12 +337,10 @@ func TestFieldsDisabled(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("string", "foo").
|
Str("string", "foo").
|
||||||
Stringer("stringer", net.IP{127, 0, 0, 1}).
|
|
||||||
Bytes("bytes", []byte("bar")).
|
Bytes("bytes", []byte("bar")).
|
||||||
Hex("hex", []byte{0x12, 0xef}).
|
Hex("hex", []byte{0x12, 0xef}).
|
||||||
AnErr("some_err", nil).
|
AnErr("some_err", nil).
|
||||||
Err(errors.New("some error")).
|
Err(errors.New("some error")).
|
||||||
Func(func(e *Event) { e.Str("func", "func_output") }).
|
|
||||||
Bool("bool", true).
|
Bool("bool", true).
|
||||||
Int("int", 1).
|
Int("int", 1).
|
||||||
Int8("int8", 2).
|
Int8("int8", 2).
|
||||||
|
@ -542,24 +437,6 @@ func TestLevel(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetLevel(t *testing.T) {
|
|
||||||
levels := []Level{
|
|
||||||
DebugLevel,
|
|
||||||
InfoLevel,
|
|
||||||
WarnLevel,
|
|
||||||
ErrorLevel,
|
|
||||||
FatalLevel,
|
|
||||||
PanicLevel,
|
|
||||||
NoLevel,
|
|
||||||
Disabled,
|
|
||||||
}
|
|
||||||
for _, level := range levels {
|
|
||||||
if got, want := New(nil).Level(level).GetLevel(), level; got != want {
|
|
||||||
t.Errorf("GetLevel() = %v, want: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSampling(t *testing.T) {
|
func TestSampling(t *testing.T) {
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
log := New(out).Sample(&BasicSampler{N: 2})
|
log := New(out).Sample(&BasicSampler{N: 2})
|
||||||
|
@ -614,45 +491,32 @@ func TestLevelWriter(t *testing.T) {
|
||||||
p string
|
p string
|
||||||
}{},
|
}{},
|
||||||
}
|
}
|
||||||
|
log := New(lw)
|
||||||
// Allow extra-verbose logs.
|
|
||||||
SetGlobalLevel(TraceLevel - 1)
|
|
||||||
log := New(lw).Level(TraceLevel - 1)
|
|
||||||
|
|
||||||
log.Trace().Msg("0")
|
|
||||||
log.Debug().Msg("1")
|
log.Debug().Msg("1")
|
||||||
log.Info().Msg("2")
|
log.Info().Msg("2")
|
||||||
log.Warn().Msg("3")
|
log.Warn().Msg("3")
|
||||||
log.Error().Msg("4")
|
log.Error().Msg("4")
|
||||||
log.Log().Msg("nolevel-1")
|
log.Log().Msg("nolevel-1")
|
||||||
log.WithLevel(TraceLevel).Msg("5")
|
log.WithLevel(DebugLevel).Msg("5")
|
||||||
log.WithLevel(DebugLevel).Msg("6")
|
log.WithLevel(InfoLevel).Msg("6")
|
||||||
log.WithLevel(InfoLevel).Msg("7")
|
log.WithLevel(WarnLevel).Msg("7")
|
||||||
log.WithLevel(WarnLevel).Msg("8")
|
log.WithLevel(ErrorLevel).Msg("8")
|
||||||
log.WithLevel(ErrorLevel).Msg("9")
|
|
||||||
log.WithLevel(NoLevel).Msg("nolevel-2")
|
log.WithLevel(NoLevel).Msg("nolevel-2")
|
||||||
log.WithLevel(-1).Msg("-1") // Same as TraceLevel
|
|
||||||
log.WithLevel(-2).Msg("-2") // Will log
|
|
||||||
log.WithLevel(-3).Msg("-3") // Will not log
|
|
||||||
|
|
||||||
want := []struct {
|
want := []struct {
|
||||||
l Level
|
l Level
|
||||||
p string
|
p string
|
||||||
}{
|
}{
|
||||||
{TraceLevel, `{"level":"trace","message":"0"}` + "\n"},
|
|
||||||
{DebugLevel, `{"level":"debug","message":"1"}` + "\n"},
|
{DebugLevel, `{"level":"debug","message":"1"}` + "\n"},
|
||||||
{InfoLevel, `{"level":"info","message":"2"}` + "\n"},
|
{InfoLevel, `{"level":"info","message":"2"}` + "\n"},
|
||||||
{WarnLevel, `{"level":"warn","message":"3"}` + "\n"},
|
{WarnLevel, `{"level":"warn","message":"3"}` + "\n"},
|
||||||
{ErrorLevel, `{"level":"error","message":"4"}` + "\n"},
|
{ErrorLevel, `{"level":"error","message":"4"}` + "\n"},
|
||||||
{NoLevel, `{"message":"nolevel-1"}` + "\n"},
|
{NoLevel, `{"message":"nolevel-1"}` + "\n"},
|
||||||
{TraceLevel, `{"level":"trace","message":"5"}` + "\n"},
|
{DebugLevel, `{"level":"debug","message":"5"}` + "\n"},
|
||||||
{DebugLevel, `{"level":"debug","message":"6"}` + "\n"},
|
{InfoLevel, `{"level":"info","message":"6"}` + "\n"},
|
||||||
{InfoLevel, `{"level":"info","message":"7"}` + "\n"},
|
{WarnLevel, `{"level":"warn","message":"7"}` + "\n"},
|
||||||
{WarnLevel, `{"level":"warn","message":"8"}` + "\n"},
|
{ErrorLevel, `{"level":"error","message":"8"}` + "\n"},
|
||||||
{ErrorLevel, `{"level":"error","message":"9"}` + "\n"},
|
|
||||||
{NoLevel, `{"message":"nolevel-2"}` + "\n"},
|
{NoLevel, `{"message":"nolevel-2"}` + "\n"},
|
||||||
{Level(-1), `{"level":"trace","message":"-1"}` + "\n"},
|
|
||||||
{Level(-2), `{"level":"-2","message":"-2"}` + "\n"},
|
|
||||||
}
|
}
|
||||||
if got := lw.ops; !reflect.DeepEqual(got, want) {
|
if got := lw.ops; !reflect.DeepEqual(got, want) {
|
||||||
t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want)
|
t.Errorf("invalid ops:\ngot:\n%v\nwant:\n%v", got, want)
|
||||||
|
@ -777,75 +641,6 @@ func TestErrorMarshalFunc(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCallerMarshalFunc(t *testing.T) {
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
|
|
||||||
// test default behaviour this is really brittle due to the line numbers
|
|
||||||
// actually mattering for validation
|
|
||||||
pc, file, line, _ := runtime.Caller(0)
|
|
||||||
caller := fmt.Sprintf("%s:%d", file, line+2)
|
|
||||||
log.Log().Caller().Msg("msg")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
out.Reset()
|
|
||||||
|
|
||||||
// test custom behavior. In this case we'll take just the last directory
|
|
||||||
origCallerMarshalFunc := CallerMarshalFunc
|
|
||||||
defer func() { CallerMarshalFunc = origCallerMarshalFunc }()
|
|
||||||
CallerMarshalFunc = func(pc uintptr, file string, line int) string {
|
|
||||||
parts := strings.Split(file, "/")
|
|
||||||
if len(parts) > 1 {
|
|
||||||
return strings.Join(parts[len(parts)-2:], "/") + ":" + strconv.Itoa(line)
|
|
||||||
}
|
|
||||||
|
|
||||||
return runtime.FuncForPC(pc).Name() + ":" + file + ":" + strconv.Itoa(line)
|
|
||||||
}
|
|
||||||
pc, file, line, _ = runtime.Caller(0)
|
|
||||||
caller = CallerMarshalFunc(pc, file, line+2)
|
|
||||||
log.Log().Caller().Msg("msg")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","message":"msg"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLevelFieldMarshalFunc(t *testing.T) {
|
|
||||||
origLevelFieldMarshalFunc := LevelFieldMarshalFunc
|
|
||||||
LevelFieldMarshalFunc = func(l Level) string {
|
|
||||||
return strings.ToUpper(l.String())
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
LevelFieldMarshalFunc = origLevelFieldMarshalFunc
|
|
||||||
}()
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := New(out)
|
|
||||||
|
|
||||||
log.Debug().Msg("test")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"DEBUG","message":"test"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
out.Reset()
|
|
||||||
|
|
||||||
log.Info().Msg("test")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"INFO","message":"test"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
out.Reset()
|
|
||||||
|
|
||||||
log.Warn().Msg("test")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"WARN","message":"test"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
out.Reset()
|
|
||||||
|
|
||||||
log.Error().Msg("test")
|
|
||||||
if got, want := decodeIfBinaryToString(out.Bytes()), `{"level":"ERROR","message":"test"}`+"\n"; got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
out.Reset()
|
|
||||||
}
|
|
||||||
|
|
||||||
type errWriter struct {
|
type errWriter struct {
|
||||||
error
|
error
|
||||||
}
|
}
|
||||||
|
@ -866,146 +661,3 @@ func TestErrorHandler(t *testing.T) {
|
||||||
t.Errorf("ErrorHandler err = %#v, want %#v", got, want)
|
t.Errorf("ErrorHandler err = %#v, want %#v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateEmptyContext(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
log := New(&buf)
|
|
||||||
|
|
||||||
log.UpdateContext(func(c Context) Context {
|
|
||||||
return c.Str("foo", "bar")
|
|
||||||
})
|
|
||||||
log.Info().Msg("no panic")
|
|
||||||
|
|
||||||
want := `{"level":"info","foo":"bar","message":"no panic"}` + "\n"
|
|
||||||
|
|
||||||
if got := decodeIfBinaryToString(buf.Bytes()); got != want {
|
|
||||||
t.Errorf("invalid log output:\ngot: %q\nwant: %q", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLevel_String(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
l Level
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"trace", TraceLevel, "trace"},
|
|
||||||
{"debug", DebugLevel, "debug"},
|
|
||||||
{"info", InfoLevel, "info"},
|
|
||||||
{"warn", WarnLevel, "warn"},
|
|
||||||
{"error", ErrorLevel, "error"},
|
|
||||||
{"fatal", FatalLevel, "fatal"},
|
|
||||||
{"panic", PanicLevel, "panic"},
|
|
||||||
{"disabled", Disabled, "disabled"},
|
|
||||||
{"nolevel", NoLevel, ""},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got := tt.l.String(); got != tt.want {
|
|
||||||
t.Errorf("String() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLevel_MarshalText(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
l Level
|
|
||||||
want string
|
|
||||||
}{
|
|
||||||
{"trace", TraceLevel, "trace"},
|
|
||||||
{"debug", DebugLevel, "debug"},
|
|
||||||
{"info", InfoLevel, "info"},
|
|
||||||
{"warn", WarnLevel, "warn"},
|
|
||||||
{"error", ErrorLevel, "error"},
|
|
||||||
{"fatal", FatalLevel, "fatal"},
|
|
||||||
{"panic", PanicLevel, "panic"},
|
|
||||||
{"disabled", Disabled, "disabled"},
|
|
||||||
{"nolevel", NoLevel, ""},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got, err := tt.l.MarshalText(); err != nil {
|
|
||||||
t.Errorf("MarshalText couldn't marshal: %v", tt.l)
|
|
||||||
} else if string(got) != tt.want {
|
|
||||||
t.Errorf("String() = %v, want %v", string(got), tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestParseLevel(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
levelStr string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want Level
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{"trace", args{"trace"}, TraceLevel, false},
|
|
||||||
{"debug", args{"debug"}, DebugLevel, false},
|
|
||||||
{"info", args{"info"}, InfoLevel, false},
|
|
||||||
{"warn", args{"warn"}, WarnLevel, false},
|
|
||||||
{"error", args{"error"}, ErrorLevel, false},
|
|
||||||
{"fatal", args{"fatal"}, FatalLevel, false},
|
|
||||||
{"panic", args{"panic"}, PanicLevel, false},
|
|
||||||
{"disabled", args{"disabled"}, Disabled, false},
|
|
||||||
{"nolevel", args{""}, NoLevel, false},
|
|
||||||
{"-1", args{"-1"}, TraceLevel, false},
|
|
||||||
{"-2", args{"-2"}, Level(-2), false},
|
|
||||||
{"-3", args{"-3"}, Level(-3), false},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
got, err := ParseLevel(tt.args.levelStr)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("ParseLevel() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if got != tt.want {
|
|
||||||
t.Errorf("ParseLevel() got = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestUnmarshalTextLevel(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
levelStr string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want Level
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{"trace", args{"trace"}, TraceLevel, false},
|
|
||||||
{"debug", args{"debug"}, DebugLevel, false},
|
|
||||||
{"info", args{"info"}, InfoLevel, false},
|
|
||||||
{"warn", args{"warn"}, WarnLevel, false},
|
|
||||||
{"error", args{"error"}, ErrorLevel, false},
|
|
||||||
{"fatal", args{"fatal"}, FatalLevel, false},
|
|
||||||
{"panic", args{"panic"}, PanicLevel, false},
|
|
||||||
{"disabled", args{"disabled"}, Disabled, false},
|
|
||||||
{"nolevel", args{""}, NoLevel, false},
|
|
||||||
{"-1", args{"-1"}, TraceLevel, false},
|
|
||||||
{"-2", args{"-2"}, Level(-2), false},
|
|
||||||
{"-3", args{"-3"}, Level(-3), false},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
var l Level
|
|
||||||
err := l.UnmarshalText([]byte(tt.args.levelStr))
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if l != tt.want {
|
|
||||||
t.Errorf("UnmarshalText() got = %v, want %v", l, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
// +build !go1.12
|
|
||||||
|
|
||||||
package zlog
|
|
||||||
|
|
||||||
const contextCallerSkipFrameCount = 3
|
|
|
@ -42,32 +42,15 @@ func frameField(f errors.Frame, s *state, c rune) string {
|
||||||
|
|
||||||
// MarshalStack implements pkg/errors stack trace marshaling.
|
// MarshalStack implements pkg/errors stack trace marshaling.
|
||||||
//
|
//
|
||||||
// zlog.ErrorStackMarshaler = MarshalStack
|
// zerolog.ErrorStackMarshaler = MarshalStack
|
||||||
func MarshalStack(err error) interface{} {
|
func MarshalStack(err error) interface{} {
|
||||||
type stackTracer interface {
|
type stackTracer interface {
|
||||||
StackTrace() errors.StackTrace
|
StackTrace() errors.StackTrace
|
||||||
}
|
}
|
||||||
var sterr stackTracer
|
sterr, ok := err.(stackTracer)
|
||||||
var ok bool
|
|
||||||
for err != nil {
|
|
||||||
sterr, ok = err.(stackTracer)
|
|
||||||
if ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
u, ok := err.(interface {
|
|
||||||
Unwrap() error
|
|
||||||
})
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = u.Unwrap()
|
|
||||||
}
|
|
||||||
if sterr == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
st := sterr.StackTrace()
|
st := sterr.StackTrace()
|
||||||
s := &state{}
|
s := &state{}
|
||||||
out := make([]map[string]string, 0, len(st))
|
out := make([]map[string]string, 0, len(st))
|
||||||
|
|
|
@ -4,50 +4,33 @@ package pkgerrors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"tuxpa.in/a/zlog"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogStack(t *testing.T) {
|
func TestLogStack(t *testing.T) {
|
||||||
zlog.ErrorStackMarshaler = MarshalStack
|
zerolog.ErrorStackMarshaler = MarshalStack
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
log := zlog.New(out)
|
log := zerolog.New(out)
|
||||||
|
|
||||||
err := fmt.Errorf("from error: %w", errors.New("error message"))
|
err := errors.Wrap(errors.New("error message"), "from error")
|
||||||
log.Log().Stack().Err(err).Msg("")
|
log.Log().Stack().Err(err).Msg("")
|
||||||
|
|
||||||
got := out.String()
|
got := out.String()
|
||||||
want := `\{"stack":\[\{"func":"TestLogStack","line":"21","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
|
want := `\{"stack":\[\{"func":"TestLogStack","line":"20","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
|
||||||
if ok, _ := regexp.MatchString(want, got); !ok {
|
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLogStackFromContext(t *testing.T) {
|
|
||||||
zlog.ErrorStackMarshaler = MarshalStack
|
|
||||||
|
|
||||||
out := &bytes.Buffer{}
|
|
||||||
log := zlog.New(out).With().Stack().Logger() // calling Stack() on log context instead of event
|
|
||||||
|
|
||||||
err := fmt.Errorf("from error: %w", errors.New("error message"))
|
|
||||||
log.Log().Err(err).Msg("") // not explicitly calling Stack()
|
|
||||||
|
|
||||||
got := out.String()
|
|
||||||
want := `\{"stack":\[\{"func":"TestLogStackFromContext","line":"37","source":"stacktrace_test.go"\},.*\],"error":"from error: error message"\}\n`
|
|
||||||
if ok, _ := regexp.MatchString(want, got); !ok {
|
if ok, _ := regexp.MatchString(want, got); !ok {
|
||||||
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkLogStack(b *testing.B) {
|
func BenchmarkLogStack(b *testing.B) {
|
||||||
zlog.ErrorStackMarshaler = MarshalStack
|
zerolog.ErrorStackMarshaler = MarshalStack
|
||||||
out := &bytes.Buffer{}
|
out := &bytes.Buffer{}
|
||||||
log := zlog.New(out)
|
log := zerolog.New(out)
|
||||||
err := errors.Wrap(errors.New("error message"), "from error")
|
err := errors.Wrap(errors.New("error message"), "from error")
|
||||||
b.ReportAllocs()
|
b.ReportAllocs()
|
||||||
|
|
||||||
|
|
BIN
pretty.png
BIN
pretty.png
Binary file not shown.
Before Width: | Height: | Size: 82 KiB After Width: | Height: | Size: 141 KiB |
16
sampler.go
16
sampler.go
|
@ -1,4 +1,4 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
@ -38,7 +38,7 @@ func (s RandomSampler) Sample(lvl Level) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// BasicSampler is a sampler that will send every Nth events, regardless of
|
// BasicSampler is a sampler that will send every Nth events, regardless of
|
||||||
// their level.
|
// there level.
|
||||||
type BasicSampler struct {
|
type BasicSampler struct {
|
||||||
N uint32
|
N uint32
|
||||||
counter uint32
|
counter uint32
|
||||||
|
@ -46,12 +46,8 @@ type BasicSampler struct {
|
||||||
|
|
||||||
// Sample implements the Sampler interface.
|
// Sample implements the Sampler interface.
|
||||||
func (s *BasicSampler) Sample(lvl Level) bool {
|
func (s *BasicSampler) Sample(lvl Level) bool {
|
||||||
n := s.N
|
|
||||||
if n == 1 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
c := atomic.AddUint32(&s.counter, 1)
|
c := atomic.AddUint32(&s.counter, 1)
|
||||||
return c%n == 1
|
return c%s.N == s.N-1
|
||||||
}
|
}
|
||||||
|
|
||||||
// BurstSampler lets Burst events pass per Period then pass the decision to
|
// BurstSampler lets Burst events pass per Period then pass the decision to
|
||||||
|
@ -104,15 +100,11 @@ func (s *BurstSampler) inc() uint32 {
|
||||||
|
|
||||||
// LevelSampler applies a different sampler for each level.
|
// LevelSampler applies a different sampler for each level.
|
||||||
type LevelSampler struct {
|
type LevelSampler struct {
|
||||||
TraceSampler, DebugSampler, InfoSampler, WarnSampler, ErrorSampler Sampler
|
DebugSampler, InfoSampler, WarnSampler, ErrorSampler Sampler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s LevelSampler) Sample(lvl Level) bool {
|
func (s LevelSampler) Sample(lvl Level) bool {
|
||||||
switch lvl {
|
switch lvl {
|
||||||
case TraceLevel:
|
|
||||||
if s.TraceSampler != nil {
|
|
||||||
return s.TraceSampler.Sample(lvl)
|
|
||||||
}
|
|
||||||
case DebugLevel:
|
case DebugLevel:
|
||||||
if s.DebugSampler != nil {
|
if s.DebugSampler != nil {
|
||||||
return s.DebugSampler.Sample(lvl)
|
return s.DebugSampler.Sample(lvl)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
45
syslog.go
45
syslog.go
|
@ -1,16 +1,12 @@
|
||||||
// +build !windows
|
// +build !windows
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// See http://cee.mitre.org/language/1.0-beta1/clt.html#syslog
|
|
||||||
// or https://www.rsyslog.com/json-elasticsearch/
|
|
||||||
const ceePrefix = "@cee:"
|
|
||||||
|
|
||||||
// SyslogWriter is an interface matching a syslog.Writer struct.
|
// SyslogWriter is an interface matching a syslog.Writer struct.
|
||||||
type SyslogWriter interface {
|
type SyslogWriter interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
|
@ -24,57 +20,38 @@ type SyslogWriter interface {
|
||||||
|
|
||||||
type syslogWriter struct {
|
type syslogWriter struct {
|
||||||
w SyslogWriter
|
w SyslogWriter
|
||||||
prefix string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyslogLevelWriter wraps a SyslogWriter and call the right syslog level
|
// SyslogLevelWriter wraps a SyslogWriter and call the right syslog level
|
||||||
// method matching the zlog level.
|
// method matching the zerolog level.
|
||||||
func SyslogLevelWriter(w SyslogWriter) LevelWriter {
|
func SyslogLevelWriter(w SyslogWriter) LevelWriter {
|
||||||
return syslogWriter{w, ""}
|
return syslogWriter{w}
|
||||||
}
|
|
||||||
|
|
||||||
// SyslogCEEWriter wraps a SyslogWriter with a SyslogLevelWriter that adds a
|
|
||||||
// MITRE CEE prefix for JSON syslog entries, compatible with rsyslog
|
|
||||||
// and syslog-ng JSON logging support.
|
|
||||||
// See https://www.rsyslog.com/json-elasticsearch/
|
|
||||||
func SyslogCEEWriter(w SyslogWriter) LevelWriter {
|
|
||||||
return syslogWriter{w, ceePrefix}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sw syslogWriter) Write(p []byte) (n int, err error) {
|
func (sw syslogWriter) Write(p []byte) (n int, err error) {
|
||||||
var pn int
|
return sw.w.Write(p)
|
||||||
if sw.prefix != "" {
|
|
||||||
pn, err = sw.w.Write([]byte(sw.prefix))
|
|
||||||
if err != nil {
|
|
||||||
return pn, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
n, err = sw.w.Write(p)
|
|
||||||
return pn + n, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteLevel implements LevelWriter interface.
|
// WriteLevel implements LevelWriter interface.
|
||||||
func (sw syslogWriter) WriteLevel(level Level, p []byte) (n int, err error) {
|
func (sw syslogWriter) WriteLevel(level Level, p []byte) (n int, err error) {
|
||||||
switch level {
|
switch level {
|
||||||
case TraceLevel:
|
|
||||||
case DebugLevel:
|
case DebugLevel:
|
||||||
err = sw.w.Debug(sw.prefix + string(p))
|
err = sw.w.Debug(string(p))
|
||||||
case InfoLevel:
|
case InfoLevel:
|
||||||
err = sw.w.Info(sw.prefix + string(p))
|
err = sw.w.Info(string(p))
|
||||||
case WarnLevel:
|
case WarnLevel:
|
||||||
err = sw.w.Warning(sw.prefix + string(p))
|
err = sw.w.Warning(string(p))
|
||||||
case ErrorLevel:
|
case ErrorLevel:
|
||||||
err = sw.w.Err(sw.prefix + string(p))
|
err = sw.w.Err(string(p))
|
||||||
case FatalLevel:
|
case FatalLevel:
|
||||||
err = sw.w.Emerg(sw.prefix + string(p))
|
err = sw.w.Emerg(string(p))
|
||||||
case PanicLevel:
|
case PanicLevel:
|
||||||
err = sw.w.Crit(sw.prefix + string(p))
|
err = sw.w.Crit(string(p))
|
||||||
case NoLevel:
|
case NoLevel:
|
||||||
err = sw.w.Info(sw.prefix + string(p))
|
err = sw.w.Info(string(p))
|
||||||
default:
|
default:
|
||||||
panic("invalid level")
|
panic("invalid level")
|
||||||
}
|
}
|
||||||
// Any CEE prefix is not part of the message, so we don't include its length
|
|
||||||
n = len(p)
|
n = len(p)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,10 @@
|
||||||
// +build !binary_log
|
// +build !binary_log
|
||||||
// +build !windows
|
// +build !windows
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"bytes"
|
import "reflect"
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
type syslogEvent struct {
|
type syslogEvent struct {
|
||||||
level string
|
level string
|
||||||
|
@ -21,10 +17,6 @@ type syslogTestWriter struct {
|
||||||
func (w *syslogTestWriter) Write(p []byte) (int, error) {
|
func (w *syslogTestWriter) Write(p []byte) (int, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
func (w *syslogTestWriter) Trace(m string) error {
|
|
||||||
w.events = append(w.events, syslogEvent{"Trace", m})
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
func (w *syslogTestWriter) Debug(m string) error {
|
func (w *syslogTestWriter) Debug(m string) error {
|
||||||
w.events = append(w.events, syslogEvent{"Debug", m})
|
w.events = append(w.events, syslogEvent{"Debug", m})
|
||||||
return nil
|
return nil
|
||||||
|
@ -53,7 +45,6 @@ func (w *syslogTestWriter) Crit(m string) error {
|
||||||
func TestSyslogWriter(t *testing.T) {
|
func TestSyslogWriter(t *testing.T) {
|
||||||
sw := &syslogTestWriter{}
|
sw := &syslogTestWriter{}
|
||||||
log := New(SyslogLevelWriter(sw))
|
log := New(SyslogLevelWriter(sw))
|
||||||
log.Trace().Msg("trace")
|
|
||||||
log.Debug().Msg("debug")
|
log.Debug().Msg("debug")
|
||||||
log.Info().Msg("info")
|
log.Info().Msg("info")
|
||||||
log.Warn().Msg("warn")
|
log.Warn().Msg("warn")
|
||||||
|
@ -70,39 +61,3 @@ func TestSyslogWriter(t *testing.T) {
|
||||||
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type testCEEwriter struct {
|
|
||||||
buf *bytes.Buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only implement one method as we're just testing the prefixing
|
|
||||||
func (c testCEEwriter) Debug(m string) error { return nil }
|
|
||||||
|
|
||||||
func (c testCEEwriter) Info(m string) error {
|
|
||||||
_, err := c.buf.Write([]byte(m))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c testCEEwriter) Warning(m string) error { return nil }
|
|
||||||
|
|
||||||
func (c testCEEwriter) Err(m string) error { return nil }
|
|
||||||
|
|
||||||
func (c testCEEwriter) Emerg(m string) error { return nil }
|
|
||||||
|
|
||||||
func (c testCEEwriter) Crit(m string) error { return nil }
|
|
||||||
|
|
||||||
func (c testCEEwriter) Write(b []byte) (int, error) {
|
|
||||||
return c.buf.Write(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSyslogWriter_WithCEE(t *testing.T) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
sw := testCEEwriter{&buf}
|
|
||||||
log := New(SyslogCEEWriter(sw))
|
|
||||||
log.Info().Str("key", "value").Msg("message string")
|
|
||||||
got := string(buf.Bytes())
|
|
||||||
want := "@cee:{"
|
|
||||||
if !strings.HasPrefix(got, want) {
|
|
||||||
t.Errorf("Bad CEE message start: want %v, got %v", want, got)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
94
writer.go
94
writer.go
|
@ -1,12 +1,7 @@
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"io"
|
"io"
|
||||||
"path"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,9 +26,11 @@ type syncWriter struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SyncWriter wraps w so that each call to Write is synchronized with a mutex.
|
// SyncWriter wraps w so that each call to Write is synchronized with a mutex.
|
||||||
// This syncer can be used to wrap the call to writer's Write method if it is
|
// This syncer can be the call to writer's Write method is not thread safe.
|
||||||
// not thread safe. Note that you do not need this wrapper for os.File Write
|
// Note that os.File Write operation is using write() syscall which is supposed
|
||||||
// operations on POSIX and Windows systems as they are already thread-safe.
|
// to be thread-safe on POSIX systems. So there is no need to use this with
|
||||||
|
// os.File on such systems as zerolog guaranties to issue a single Write call
|
||||||
|
// per log event.
|
||||||
func SyncWriter(w io.Writer) io.Writer {
|
func SyncWriter(w io.Writer) io.Writer {
|
||||||
if lw, ok := w.(LevelWriter); ok {
|
if lw, ok := w.(LevelWriter); ok {
|
||||||
return &syncWriter{lw: lw}
|
return &syncWriter{lw: lw}
|
||||||
|
@ -61,30 +58,30 @@ type multiLevelWriter struct {
|
||||||
|
|
||||||
func (t multiLevelWriter) Write(p []byte) (n int, err error) {
|
func (t multiLevelWriter) Write(p []byte) (n int, err error) {
|
||||||
for _, w := range t.writers {
|
for _, w := range t.writers {
|
||||||
if _n, _err := w.Write(p); err == nil {
|
n, err = w.Write(p)
|
||||||
n = _n
|
if err != nil {
|
||||||
if _err != nil {
|
return
|
||||||
err = _err
|
}
|
||||||
} else if _n != len(p) {
|
if n != len(p) {
|
||||||
err = io.ErrShortWrite
|
err = io.ErrShortWrite
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return len(p), nil
|
||||||
return n, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) {
|
func (t multiLevelWriter) WriteLevel(l Level, p []byte) (n int, err error) {
|
||||||
for _, w := range t.writers {
|
for _, w := range t.writers {
|
||||||
if _n, _err := w.WriteLevel(l, p); err == nil {
|
n, err = w.WriteLevel(l, p)
|
||||||
n = _n
|
if err != nil {
|
||||||
if _err != nil {
|
return
|
||||||
err = _err
|
}
|
||||||
} else if _n != len(p) {
|
if n != len(p) {
|
||||||
err = io.ErrShortWrite
|
err = io.ErrShortWrite
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return len(p), nil
|
||||||
return n, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MultiLevelWriter creates a writer that duplicates its writes to all the
|
// MultiLevelWriter creates a writer that duplicates its writes to all the
|
||||||
|
@ -101,54 +98,3 @@ func MultiLevelWriter(writers ...io.Writer) LevelWriter {
|
||||||
}
|
}
|
||||||
return multiLevelWriter{lwriters}
|
return multiLevelWriter{lwriters}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestingLog is the logging interface of testing.TB.
|
|
||||||
type TestingLog interface {
|
|
||||||
Log(args ...interface{})
|
|
||||||
Logf(format string, args ...interface{})
|
|
||||||
Helper()
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestWriter is a writer that writes to testing.TB.
|
|
||||||
type TestWriter struct {
|
|
||||||
T TestingLog
|
|
||||||
|
|
||||||
// Frame skips caller frames to capture the original file and line numbers.
|
|
||||||
Frame int
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewTestWriter creates a writer that logs to the testing.TB.
|
|
||||||
func NewTestWriter(t TestingLog) TestWriter {
|
|
||||||
return TestWriter{T: t}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to testing.TB.
|
|
||||||
func (t TestWriter) Write(p []byte) (n int, err error) {
|
|
||||||
t.T.Helper()
|
|
||||||
|
|
||||||
n = len(p)
|
|
||||||
|
|
||||||
// Strip trailing newline because t.Log always adds one.
|
|
||||||
p = bytes.TrimRight(p, "\n")
|
|
||||||
|
|
||||||
// Try to correct the log file and line number to the caller.
|
|
||||||
if t.Frame > 0 {
|
|
||||||
_, origFile, origLine, _ := runtime.Caller(1)
|
|
||||||
_, frameFile, frameLine, ok := runtime.Caller(1 + t.Frame)
|
|
||||||
if ok {
|
|
||||||
erase := strings.Repeat("\b", len(path.Base(origFile))+len(strconv.Itoa(origLine))+3)
|
|
||||||
t.T.Logf("%s%s:%d: %s", erase, path.Base(frameFile), frameLine, p)
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.T.Log(string(p))
|
|
||||||
|
|
||||||
return n, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConsoleTestWriter creates an option that correctly sets the file frame depth for testing.TB log.
|
|
||||||
func ConsoleTestWriter(t TestingLog) func(w *ConsoleWriter) {
|
|
||||||
return func(w *ConsoleWriter) {
|
|
||||||
w.Out = TestWriter{T: t, Frame: 6}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
154
writer_test.go
154
writer_test.go
|
@ -1,13 +1,9 @@
|
||||||
//go:build !binary_log && !windows
|
// +build !binary_log
|
||||||
// +build !binary_log,!windows
|
// +build !windows
|
||||||
|
|
||||||
package zlog
|
package zerolog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
@ -31,147 +27,3 @@ func TestMultiSyslogWriter(t *testing.T) {
|
||||||
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
t.Errorf("Invalid syslog message routing: want %v, got %v", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var writeCalls int
|
|
||||||
|
|
||||||
type mockedWriter struct {
|
|
||||||
wantErr bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c mockedWriter) Write(p []byte) (int, error) {
|
|
||||||
writeCalls++
|
|
||||||
|
|
||||||
if c.wantErr {
|
|
||||||
return -1, errors.New("Expected error")
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(p), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tests that a new writer is only used if it actually works.
|
|
||||||
func TestResilientMultiWriter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
writers []io.Writer
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "All valid writers",
|
|
||||||
writers: []io.Writer{
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "All invalid writers",
|
|
||||||
writers: []io.Writer{
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "First invalid writer",
|
|
||||||
writers: []io.Writer{
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "First valid writer",
|
|
||||||
writers: []io.Writer{
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
mockedWriter{
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
writers := tt.writers
|
|
||||||
multiWriter := MultiLevelWriter(writers...)
|
|
||||||
|
|
||||||
logger := New(multiWriter).With().Timestamp().Logger().Level(InfoLevel)
|
|
||||||
logger.Info().Msg("Test msg")
|
|
||||||
|
|
||||||
if len(writers) != writeCalls {
|
|
||||||
t.Errorf("Expected %d writers to have been called but only %d were.", len(writers), writeCalls)
|
|
||||||
}
|
|
||||||
writeCalls = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type testingLog struct {
|
|
||||||
testing.TB
|
|
||||||
buf bytes.Buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *testingLog) Log(args ...interface{}) {
|
|
||||||
if _, err := t.buf.WriteString(fmt.Sprint(args...)); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *testingLog) Logf(format string, args ...interface{}) {
|
|
||||||
if _, err := t.buf.WriteString(fmt.Sprintf(format, args...)); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestTestWriter(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
write []byte
|
|
||||||
want []byte
|
|
||||||
}{{
|
|
||||||
name: "newline",
|
|
||||||
write: []byte("newline\n"),
|
|
||||||
want: []byte("newline"),
|
|
||||||
}, {
|
|
||||||
name: "oneline",
|
|
||||||
write: []byte("oneline"),
|
|
||||||
want: []byte("oneline"),
|
|
||||||
}, {
|
|
||||||
name: "twoline",
|
|
||||||
write: []byte("twoline\n\n"),
|
|
||||||
want: []byte("twoline"),
|
|
||||||
}}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
tb := &testingLog{TB: t} // Capture TB log buffer.
|
|
||||||
w := TestWriter{T: tb}
|
|
||||||
|
|
||||||
n, err := w.Write(tt.write)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if n != len(tt.write) {
|
|
||||||
t.Errorf("Expected %d write length but got %d", len(tt.write), n)
|
|
||||||
}
|
|
||||||
p := tb.buf.Bytes()
|
|
||||||
if !bytes.Equal(tt.want, p) {
|
|
||||||
t.Errorf("Expected %q, got %q.", tt.want, p)
|
|
||||||
}
|
|
||||||
|
|
||||||
log := New(NewConsoleWriter(ConsoleTestWriter(t)))
|
|
||||||
log.Info().Str("name", tt.name).Msg("Success!")
|
|
||||||
|
|
||||||
tb.buf.Reset()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue