libs/log: [JSON format] include timestamp (#6174)

Closes #6146
This commit is contained in:
Anton Kaliaev
2021-02-25 11:06:24 +04:00
committed by GitHub
parent e9e5026dac
commit fc5a108d53
5 changed files with 16 additions and 6 deletions

View File

@@ -71,6 +71,7 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
- [rpc/client/http] \#6163 Do not drop events even if the `out` channel is full (@melekes)
- [node] \#6059 Validate and complete genesis doc before saving to state store (@silasdavis)
- [state] \#6067 Batch save state data (@githubsands & @cmwaters)
- [libs/log] \#6174 Include timestamp (`ts` field; `time.RFC3339Nano` format) in JSON logger output (@melekes)
### BUG FIXES

View File

@@ -15,7 +15,7 @@ const (
func TestParseLogLevel(t *testing.T) {
var buf bytes.Buffer
jsonLogger := log.NewTMJSONLogger(&buf)
jsonLogger := log.NewTMJSONLoggerNoTS(&buf)
correctLogLevels := []struct {
lvl string

View File

@@ -58,7 +58,7 @@ func TestVariousLevels(t *testing.T) {
tc := tc
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
logger := log.NewFilter(log.NewTMJSONLogger(&buf), tc.allowed)
logger := log.NewFilter(log.NewTMJSONLoggerNoTS(&buf), tc.allowed)
logger.Debug("here", "this is", "debug log")
logger.Info("here", "this is", "info log")
@@ -74,7 +74,7 @@ func TestVariousLevels(t *testing.T) {
func TestLevelContext(t *testing.T) {
var buf bytes.Buffer
logger := log.NewTMJSONLogger(&buf)
logger := log.NewTMJSONLoggerNoTS(&buf)
logger = log.NewFilter(logger, log.AllowError())
logger = logger.With("context", "value")
@@ -96,7 +96,7 @@ func TestLevelContext(t *testing.T) {
func TestVariousAllowWith(t *testing.T) {
var buf bytes.Buffer
logger := log.NewTMJSONLogger(&buf)
logger := log.NewTMJSONLoggerNoTS(&buf)
logger1 := log.NewFilter(logger, log.AllowError(), log.AllowInfoWith("context", "value"))
logger1.With("context", "value").Info("foo", "bar", "baz")

View File

@@ -11,5 +11,14 @@ import (
// w.Write. The passed Writer must be safe for concurrent use by multiple
// goroutines if the returned Logger will be used concurrently.
func NewTMJSONLogger(w io.Writer) Logger {
return &tmLogger{kitlog.NewJSONLogger(w)}
logger := kitlog.NewJSONLogger(w)
logger = kitlog.With(logger, "ts", kitlog.DefaultTimestampUTC)
return &tmLogger{logger}
}
// NewTMJSONLoggerNoTS is the same as NewTMJSONLogger, but without the
// timestamp.
func NewTMJSONLoggerNoTS(w io.Writer) Logger {
logger := kitlog.NewJSONLogger(w)
return &tmLogger{logger}
}

View File

@@ -15,7 +15,7 @@ import (
func TestTracingLogger(t *testing.T) {
var buf bytes.Buffer
logger := log.NewTMJSONLogger(&buf)
logger := log.NewTMJSONLoggerNoTS(&buf)
logger1 := log.NewTracingLogger(logger)
err1 := errors.New("courage is grace under pressure")