Skip to content

Commit

Permalink
chore!: change default timestamp and level key names
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Nov 6, 2023
1 parent 707eb77 commit cafb9c6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const (

var (
// TimestampKey is the key for the timestamp.
TimestampKey = "ts"
TimestampKey = "time"
// MessageKey is the key for the message.
MessageKey = "msg"
// LevelKey is the key for the level.
LevelKey = "lvl"
LevelKey = "level"
// CallerKey is the key for the caller.
CallerKey = "caller"
// PrefixKey is the key for the prefix.
Expand Down
30 changes: 15 additions & 15 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestJson(t *testing.T) {
}{
{
name: "default logger info with timestamp",
expected: "{\"lvl\":\"info\",\"msg\":\"info\"}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\"}\n",
msg: "info",
kvs: nil,
f: l.Info,
Expand All @@ -38,77 +38,77 @@ func TestJson(t *testing.T) {
},
{
name: "default logger error with timestamp",
expected: "{\"lvl\":\"error\",\"msg\":\"info\"}\n",
expected: "{\"level\":\"error\",\"msg\":\"info\"}\n",
msg: "info",
kvs: nil,
f: l.Error,
},
{
name: "multiline message",
expected: "{\"lvl\":\"error\",\"msg\":\"info\\ninfo\"}\n",
expected: "{\"level\":\"error\",\"msg\":\"info\\ninfo\"}\n",
msg: "info\ninfo",
kvs: nil,
f: l.Error,
},
{
name: "multiline kvs",
expected: "{\"lvl\":\"error\",\"msg\":\"info\",\"multiline\":\"info\\ninfo\"}\n",
expected: "{\"level\":\"error\",\"msg\":\"info\",\"multiline\":\"info\\ninfo\"}\n",
msg: "info",
kvs: []interface{}{"multiline", "info\ninfo"},
f: l.Error,
},
{
name: "odd number of kvs",
expected: "{\"baz\":\"missing value\",\"foo\":\"bar\",\"lvl\":\"error\",\"msg\":\"info\"}\n",
expected: "{\"baz\":\"missing value\",\"foo\":\"bar\",\"level\":\"error\",\"msg\":\"info\"}\n",
msg: "info",
kvs: []interface{}{"foo", "bar", "baz"},
f: l.Error,
},
{
name: "error field",
expected: "{\"error\":\"error message\",\"lvl\":\"error\",\"msg\":\"info\"}\n",
expected: "{\"error\":\"error message\",\"level\":\"error\",\"msg\":\"info\"}\n",
msg: "info",
kvs: []interface{}{"error", errors.New("error message")},
f: l.Error,
},
{
name: "struct field",
expected: "{\"lvl\":\"info\",\"msg\":\"info\",\"struct\":{}}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\",\"struct\":{}}\n",
msg: "info",
kvs: []interface{}{"struct", struct{ foo string }{foo: "bar"}},
f: l.Info,
},
{
name: "slice field",
expected: "{\"lvl\":\"info\",\"msg\":\"info\",\"slice\":[1,2,3]}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[1,2,3]}\n",
msg: "info",
kvs: []interface{}{"slice", []int{1, 2, 3}},
f: l.Info,
},
{
name: "slice of structs",
expected: "{\"lvl\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
msg: "info",
kvs: []interface{}{"slice", []struct{ foo string }{{foo: "bar"}, {foo: "baz"}}},
f: l.Info,
},
{
name: "slice of strings",
expected: "{\"lvl\":\"info\",\"msg\":\"info\",\"slice\":[\"foo\",\"bar\"]}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[\"foo\",\"bar\"]}\n",
msg: "info",
kvs: []interface{}{"slice", []string{"foo", "bar"}},
f: l.Info,
},
{
name: "slice of errors",
expected: "{\"lvl\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
expected: "{\"level\":\"info\",\"msg\":\"info\",\"slice\":[{},{}]}\n",
msg: "info",
kvs: []interface{}{"slice", []error{errors.New("error message1"), errors.New("error message2")}},
f: l.Info,
},
{
name: "map of strings",
expected: "{\"lvl\":\"info\",\"map\":{\"a\":\"b\",\"foo\":\"bar\"},\"msg\":\"info\"}\n",
expected: "{\"level\":\"info\",\"map\":{\"a\":\"b\",\"foo\":\"bar\"},\"msg\":\"info\"}\n",
msg: "info",
kvs: []interface{}{"map", map[string]string{"a": "b", "foo": "bar"}},
f: l.Info,
Expand Down Expand Up @@ -140,14 +140,14 @@ func TestJsonCaller(t *testing.T) {
}{
{
name: "simple caller",
expected: fmt.Sprintf("{\"caller\":\"log/%s:%d\",\"lvl\":\"info\",\"msg\":\"info\"}\n", filepath.Base(file), line+30),
expected: fmt.Sprintf("{\"caller\":\"log/%s:%d\",\"level\":\"info\",\"msg\":\"info\"}\n", filepath.Base(file), line+30),
msg: "info",
kvs: nil,
f: l.Info,
},
{
name: "nested caller",
expected: fmt.Sprintf("{\"caller\":\"log/%s:%d\",\"lvl\":\"info\",\"msg\":\"info\"}\n", filepath.Base(file), line+30),
expected: fmt.Sprintf("{\"caller\":\"log/%s:%d\",\"level\":\"info\",\"msg\":\"info\"}\n", filepath.Base(file), line+30),
msg: "info",
kvs: nil,
f: func(msg interface{}, kvs ...interface{}) {
Expand Down Expand Up @@ -177,5 +177,5 @@ func TestJsonCustomKey(t *testing.T) {
logger.SetFormatter(JSONFormatter)
logger.SetReportTimestamp(true)
logger.Info("info")
require.Equal(t, "{\"lvl\":\"info\",\"msg\":\"info\",\"time\":\"0002/01/01 00:00:00\"}\n", buf.String())
require.Equal(t, "{\"level\":\"info\",\"msg\":\"info\",\"time\":\"0002/01/01 00:00:00\"}\n", buf.String())
}
26 changes: 13 additions & 13 deletions logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestLogfmt(t *testing.T) {
}{
{
name: "simple",
expected: "lvl=info msg=info\n",
expected: "level=info msg=info\n",
msg: "info",
kvs: nil,
f: l.Info,
Expand All @@ -35,84 +35,84 @@ func TestLogfmt(t *testing.T) {
},
{
name: "message with keyvals",
expected: "lvl=info msg=info foo=bar\n",
expected: "level=info msg=info foo=bar\n",
msg: "info",
kvs: []interface{}{"foo", "bar"},
f: l.Info,
},
{
name: "message with multiline keyvals",
expected: "lvl=info msg=info foo=\"bar\\nbaz\"\n",
expected: "level=info msg=info foo=\"bar\\nbaz\"\n",
msg: "info",
kvs: []interface{}{"foo", "bar\nbaz"},
f: l.Info,
},
{
name: "multiline message",
expected: "lvl=info msg=\"info\\nfoo\"\n",
expected: "level=info msg=\"info\\nfoo\"\n",
msg: "info\nfoo",
kvs: nil,
f: l.Info,
},
{
name: "message with error",
expected: "lvl=info msg=info err=\"foo: bar\"\n",
expected: "level=info msg=info err=\"foo: bar\"\n",
msg: "info",
kvs: []interface{}{"err", errors.New("foo: bar")},
f: l.Info,
},
{
name: "odd number of keyvals",
expected: "lvl=info msg=info foo=bar baz=\"missing value\"\n",
expected: "level=info msg=info foo=bar baz=\"missing value\"\n",
msg: "info",
kvs: []interface{}{"foo", "bar", "baz"},
f: l.Info,
},
{
name: "struct field",
expected: "lvl=info msg=info foo=\"{bar:foo bar}\"\n",
expected: "level=info msg=info foo=\"{bar:foo bar}\"\n",
msg: "info",
kvs: []interface{}{"foo", struct{ bar string }{"foo bar"}},
f: l.Info,
},
{
name: "multiple struct fields",
expected: "lvl=info msg=info foo={bar:baz} foo2={bar:baz}\n",
expected: "level=info msg=info foo={bar:baz} foo2={bar:baz}\n",
msg: "info",
kvs: []interface{}{"foo", struct{ bar string }{"baz"}, "foo2", struct{ bar string }{"baz"}},
f: l.Info,
},
{
name: "slice of structs",
expected: "lvl=info msg=info foo=\"[{bar:baz} {bar:baz}]\"\n",
expected: "level=info msg=info foo=\"[{bar:baz} {bar:baz}]\"\n",
msg: "info",
kvs: []interface{}{"foo", []struct{ bar string }{{"baz"}, {"baz"}}},
f: l.Info,
},
{
name: "slice of strings",
expected: "lvl=info msg=info foo=\"[bar baz]\"\n",
expected: "level=info msg=info foo=\"[bar baz]\"\n",
msg: "info",
kvs: []interface{}{"foo", []string{"bar", "baz"}},
f: l.Info,
},
{
name: "slice of errors",
expected: "lvl=info msg=info foo=\"[error1 error2]\"\n",
expected: "level=info msg=info foo=\"[error1 error2]\"\n",
msg: "info",
kvs: []interface{}{"foo", []error{errors.New("error1"), errors.New("error2")}},
f: l.Info,
},
{
name: "map of strings",
expected: "lvl=info msg=info foo=map[bar:baz]\n",
expected: "level=info msg=info foo=map[bar:baz]\n",
msg: "info",
kvs: []interface{}{"foo", map[string]string{"bar": "baz"}},
f: l.Info,
},
{
name: "map with interface",
expected: "lvl=info msg=info foo=map[bar:baz]\n",
expected: "level=info msg=info foo=map[bar:baz]\n",
msg: "info",
kvs: []interface{}{"foo", map[string]interface{}{"bar": "baz"}},
f: l.Info,
Expand Down
2 changes: 1 addition & 1 deletion pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestFormatter(t *testing.T) {
SetReportTimestamp(false)
SetFormatter(JSONFormatter)
Info("info")
assert.Equal(t, "{\"lvl\":\"info\",\"msg\":\"info\"}\n", buf.String())
assert.Equal(t, "{\"level\":\"info\",\"msg\":\"info\"}\n", buf.String())
}

func TestWithPrefix(t *testing.T) {
Expand Down

0 comments on commit cafb9c6

Please sign in to comment.