1
1
# Logfmt Encoder
2
2
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 )
5
12
6
13
## Usage
7
14
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.
9
17
10
18
``` go
11
19
package main
12
20
13
21
import (
14
22
" os"
15
23
16
- " github.com/jsternberg /zap-logfmt"
24
+ " github.com/sykesm /zap-logfmt"
17
25
" go.uber.org/zap"
18
26
" go.uber.org/zap/zapcore"
19
27
)
@@ -29,44 +37,40 @@ func main() {
29
37
}
30
38
```
31
39
32
- To use RFC3339 output for the time instead of an integer timestamp, you
33
- can do this:
40
+ ## Arrays, Objects, and Reflected Fields
34
41
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.
37
45
38
- import (
39
- " os"
46
+ ### Arrays
40
47
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.
45
52
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
59
61
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...
61
64
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 ` .
67
68
68
69
## Namespaces
69
70
70
71
Namespaces are supported. If a namespace is opened, all of the keys will
71
72
be prepended with the namespace name. For example, with the namespace
72
73
` 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