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 (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
|
@ -12,6 +13,9 @@ type JSONLogs struct {
|
|||
Log []byte `json:"log,omitempty"`
|
||||
Stream string `json:"stream,omitempty"`
|
||||
Created string `json:"time"`
|
||||
|
||||
// json-encoded bytes
|
||||
RawAttrs json.RawMessage `json:"attrs,omitempty"`
|
||||
}
|
||||
|
||||
// MarshalJSONBuf is based on the same method from JSONLog
|
||||
|
@ -34,6 +38,15 @@ func (mj *JSONLogs) MarshalJSONBuf(buf *bytes.Buffer) error {
|
|||
buf.WriteString(`"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 {
|
||||
first = false
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue