add labels/env log option for jsonfile
this allows jsonfile logger to collect extra metadata from containers with `--log-opt labels=label1,label2 --log-opt env=env1,env2`. Extra attributes are saved into `attrs` attributes for each log data. Signed-off-by: Daniel Dao <dqminh@cloudflare.com>
This commit is contained in:
parent
5b56e85c9d
commit
154434f5a2
2 changed files with 15 additions and 0 deletions
|
@ -2,6 +2,7 @@ package jsonlog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,6 +13,9 @@ type JSONLogs struct {
|
||||||
Log []byte `json:"log,omitempty"`
|
Log []byte `json:"log,omitempty"`
|
||||||
Stream string `json:"stream,omitempty"`
|
Stream string `json:"stream,omitempty"`
|
||||||
Created string `json:"time"`
|
Created string `json:"time"`
|
||||||
|
|
||||||
|
// json-encoded bytes
|
||||||
|
RawAttrs json.RawMessage `json:"attrs,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSONBuf is based on the same method from JSONLog
|
// MarshalJSONBuf is based on the same method from JSONLog
|
||||||
|
@ -34,6 +38,15 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
||||||
buf.WriteString(`"stream":`)
|
buf.WriteString(`"stream":`)
|
||||||
ffjsonWriteJSONString(buf, mj.Stream)
|
ffjsonWriteJSONString(buf, mj.Stream)
|
||||||
}
|
}
|
||||||
|
if len(mj.RawAttrs) > 0 {
|
||||||
|
if first == true {
|
||||||
|
first = false
|
||||||
|
} else {
|
||||||
|
buf.WriteString(`,`)
|
||||||
|
}
|
||||||
|
buf.WriteString(`"attrs":`)
|
||||||
|
buf.Write(mj.RawAttrs)
|
||||||
|
}
|
||||||
if first == true {
|
if first == true {
|
||||||
first = false
|
first = false
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -21,6 +21,8 @@ func TestJSONLogsMarshalJSONBuf(t *testing.T) {
|
||||||
&JSONLogs{Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":}$`,
|
&JSONLogs{Log: []byte("\u2028 \u2029")}: `^{\"log\":\"\\u2028 \\u2029\",\"time\":}$`,
|
||||||
&JSONLogs{Log: []byte{0xaF}}: `^{\"log\":\"\\ufffd\",\"time\":}$`,
|
&JSONLogs{Log: []byte{0xaF}}: `^{\"log\":\"\\ufffd\",\"time\":}$`,
|
||||||
&JSONLogs{Log: []byte{0x7F}}: `^{\"log\":\"\x7f\",\"time\":}$`,
|
&JSONLogs{Log: []byte{0x7F}}: `^{\"log\":\"\x7f\",\"time\":}$`,
|
||||||
|
// with raw attributes
|
||||||
|
&JSONLogs{Log: []byte("A log line"), RawAttrs: []byte(`{"hello":"world","value":1234}`)}: `^{\"log\":\"A log line\",\"attrs\":{\"hello\":\"world\",\"value\":1234},\"time\":}$`,
|
||||||
}
|
}
|
||||||
for jsonLog, expression := range logs {
|
for jsonLog, expression := range logs {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
|
Loading…
Reference in a new issue