aboutsummaryrefslogtreecommitdiffstats
path: root/log/format.go
diff options
context:
space:
mode:
authorKurkó Mihály <kurkomisi@users.noreply.github.com>2018-07-11 15:59:04 +0800
committerPéter Szilágyi <peterke@gmail.com>2018-07-11 15:59:04 +0800
commita9835c1816bc49ee54c82b4f2a5b05cbcd89881b (patch)
treee1badefd627aa3a7c4e1937eab22b8fe3eb204d1 /log/format.go
parent2eedbe799f5eb8766e4808d8a1810cc1c90c4b93 (diff)
downloadgo-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar.gz
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar.bz2
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar.lz
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar.xz
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.tar.zst
go-tangerine-a9835c1816bc49ee54c82b4f2a5b05cbcd89881b.zip
cmd, dashboard, log: log collection and exploration (#17097)
* cmd, dashboard, internal, log, node: logging feature * cmd, dashboard, internal, log: requested changes * dashboard, vendor: gofmt, govendor, use vendored file watcher * dashboard, log: gofmt -s -w, goimports * dashboard, log: gosimple
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.