Skip to content

Commit f9c81d9

Browse files
committed
Improved reflection, object and array encoders
Signed-off-by: Matthew Sykes <[email protected]>
1 parent ef1948c commit f9c81d9

9 files changed

+929
-400
lines changed

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: go
2+
3+
matrix:
4+
include:
5+
- go: "1.10.x"
6+
- go: "1.11.x"
7+
install: true
8+
env: GO111MODULE=on
9+
10+
script: go test -race ./...

LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
The MIT License (MIT)
22

33
Copyright (c) 2017 Jonathan Sternberg
4+
Copyright (c) 2019 Matthew Sykes
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy of
67
this software and associated documentation files (the "Software"), to deal in

README.md

+37-33
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
# Logfmt Encoder
22

3-
This package implements logfmt for
4-
[zap](https://github.com/uber-go/zap).
3+
This package provides a logfmt encoder for [zap][zap].
4+
5+
It is a fork of [github.com/jsternberg/zap-logfmt][jsternberg] that improves
6+
the handling of reflected fields and encodes arrays and objects instead of
7+
dropping them from logs. While logging simple fields is preferred for many
8+
reasons, having occasional ugly data in logs is better than missing data.
9+
10+
[![Build Status](https://travis-ci.org/sykesm/zap-logfmt.svg?branch=master)](https://travis-ci.org/sykesm/zap-logfmt)
11+
[![GoDoc](https://godoc.org/github.com/sykesm/zap-logfmt?status.svg)](https://godoc.org/github.com/sykesm/zap-logfmt)
512

613
## Usage
714

8-
The encoder is simple to use.
15+
The encoder is easy to configure. Simply create a new core with an instance of
16+
the logfmt encoder and use it with your preferred logging interface.
917

1018
```go
1119
package main
1220

1321
import (
1422
"os"
1523

16-
"github.com/jsternberg/zap-logfmt"
24+
"github.com/sykesm/zap-logfmt"
1725
"go.uber.org/zap"
1826
"go.uber.org/zap/zapcore"
1927
)
@@ -29,44 +37,40 @@ func main() {
2937
}
3038
```
3139

32-
To use RFC3339 output for the time instead of an integer timestamp, you
33-
can do this:
40+
## Arrays, Objects, and Reflected Fields
3441

35-
```go
36-
package main
42+
While it's best to avoid complex data types in log fields, there are times
43+
when they sneak in. When complex fields are included in log records, they will
44+
be encoded, but they won't be very pretty.
3745

38-
import (
39-
"os"
46+
### Arrays
4047

41-
"github.com/jsternberg/zap-logfmt"
42-
"go.uber.org/zap"
43-
"go.uber.org/zap/zapcore"
44-
)
48+
Arrays are encoded as a comma separated list of values within square brackets.
49+
This format is very similar to JSON encoding. Arrays of simple scalars remain
50+
quite readable but including elements that require quoting will result in very
51+
ugly records.
4552

46-
func main() {
47-
config := zap.NewProductionEncoderConfig()
48-
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
49-
encoder.AppendString(ts.UTC().Format(time.RFC3339))
50-
}
51-
logger := zap.New(zapcore.NewCore(
52-
zaplogfmt.NewEncoder(config),
53-
os.Stdout,
54-
zapcore.DebugLevel,
55-
))
56-
logger.Info("Hello World")
57-
}
58-
```
53+
### Objects
54+
55+
Objects are encoded as a space separated list of _key=value_ pairs. Because
56+
this format includes an equals sign, the encoded object will require quoting.
57+
If any value in the object requires quoting, the required escapes will make
58+
the encoded field pretty difficult for humans to read.
59+
60+
### Channels and Functions
5961

60-
## Limitations
62+
Channels and functions are encoded as their type and their address. There
63+
aren't many meaningful ways to log channels and functions...
6164

62-
It is not possible to log an array, channel, function, map, slice, or
63-
struct. Functions and channels since they don't really have a suitable
64-
representation to begin with. Logfmt does not have a method of
65-
outputting arrays or maps so arrays, slices, maps, and structs cannot be
66-
rendered.
65+
### Maps and Structs
66+
67+
Maps and structs are encoded as strings that contain the result of `fmt.Sprint`.
6768

6869
## Namespaces
6970

7071
Namespaces are supported. If a namespace is opened, all of the keys will
7172
be prepended with the namespace name. For example, with the namespace
7273
`foo` and the key `bar`, you would get a key of `foo.bar`.
74+
75+
[zap]: https://github.com/uber-go/zap
76+
[jsternberg]: https://github.com/jsternberg/zap-logfmt

0 commit comments

Comments
 (0)