aboutsummaryrefslogtreecommitdiffstats
path: root/log/format.go
diff options
context:
space:
mode:
Diffstat (limited to 'log/format.go')
-rw-r--r--log/format.go46
1 files changed, 44 insertions, 2 deletions
diff --git a/log/format.go b/log/format.go
index 0d4732cc0..7902b296e 100644
--- a/log/format.go
+++ b/log/format.go
@@ -77,11 +77,11 @@ type TerminalStringer interface {
// a terminal with color-coded level output and terser human friendly timestamp.
// This format should only be used for interactive programs or while developing.
//
-// [TIME] [LEVEL] MESAGE key=value key=value ...
+// [LEVEL] [TIME] MESAGE key=value key=value ...
//
// Example:
//
-// [May 16 20:58:45] [DBUG] remove route ns=haproxy addr=127.0.0.1:50002
+// [DBUG] [May 16 20:58:45] remove route ns=haproxy addr=127.0.0.1:50002
//
func TerminalFormat(usecolor bool) Format {
return FormatFunc(func(r *Record) []byte {
@@ -202,6 +202,48 @@ func JSONFormat() Format {
return JSONFormatEx(false, true)
}
+// JSONFormatOrderedEx formats log records as JSON arrays. If pretty is true,
+// records will be pretty-printed. If lineSeparated is true, records
+// will be logged with a new line between each record.
+func JSONFormatOrderedEx(pretty, lineSeparated bool) Format {
+ jsonMarshal := json.Marshal
+ if pretty {
+ jsonMarshal = func(v interface{}) ([]byte, error) {
+ return json.MarshalIndent(v, "", " ")
+ }
+ }
+ return FormatFunc(func(r *Record) []byte {
+ props := make(map[string]interface{})
+
+ props[r.KeyNames.Time] = r.Time
+ props[r.KeyNames.Lvl] = r.Lvl.String()
+ props[r.KeyNames.Msg] = r.Msg
+
+ ctx := make([]string, len(r.Ctx))
+ for i := 0; i < len(r.Ctx); i += 2 {
+ k, ok := r.Ctx[i].(string)
+ if !ok {
+ props[errorKey] = fmt.Sprintf("%+v is not a string key,", r.Ctx[i])
+ }
+ ctx[i] = k
+ ctx[i+1] = formatLogfmtValue(r.Ctx[i+1], true)
+ }
+ props[r.KeyNames.Ctx] = ctx
+
+ b, err := jsonMarshal(props)
+ if err != nil {
+ b, _ = jsonMarshal(map[string]string{
+ errorKey: err.Error(),
+ })
+ return b
+ }
+ if lineSeparated {
+ b = append(b, '\n')
+ }
+ return b
+ })
+}
+
// JSONFormatEx formats log records as JSON objects. If pretty is true,
// records will be pretty-printed. If lineSeparated is true, records
// will be logged with a new line between each record.