From 32ea86f74bf9e5af4c849303291409bca696a46d Mon Sep 17 00:00:00 2001 From: Novan Allanadi <25704935+nofun97@users.noreply.github.com> Date: Wed, 26 Feb 2020 14:20:51 +1100 Subject: [PATCH 1/2] changed position of level to be after timestamp --- log/standardLogger.go | 26 +++++--------------------- log/standardLogger_test.go | 4 ++-- 2 files changed, 7 insertions(+), 23 deletions(-) diff --git a/log/standardLogger.go b/log/standardLogger.go index 6044c43..97c515a 100644 --- a/log/standardLogger.go +++ b/log/standardLogger.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "strings" "time" @@ -14,40 +13,25 @@ import ( const keyFields = "_fields" -type logrusLevelConfig interface { - getLogrusLevel() logrus.Level -} - -type ioOutConfig interface { - getIoOut() io.Writer -} - type standardLogger struct { internal *logrus.Logger fields frozen.Map } func (sf standardFormat) Format(entry *logrus.Entry) ([]byte, error) { - message := strings.Builder{} - message.WriteString(entry.Time.Format(time.RFC3339Nano)) - message.WriteByte(' ') + sections := append(make([]string, 0, 5), entry.Time.Format(time.RFC3339Nano), strings.ToUpper(entry.Level.String())) if entry.Data[keyFields] != nil && entry.Data[keyFields].(frozen.Map).Count() != 0 { - message.WriteString(getFormattedField(entry.Data[keyFields].(frozen.Map))) - message.WriteByte(' ') + sections = append(sections, getFormattedField(entry.Data[keyFields].(frozen.Map))) } - message.WriteString(strings.ToUpper(entry.Level.String())) - message.WriteByte(' ') - if entry.Message != "" { - message.WriteString(entry.Message) - message.WriteByte(' ') + sections = append(sections, entry.Message) } // TODO: add codelinker's message here - message.WriteByte('\n') - return []byte(message.String()), nil + sections = append(sections, "\n") + return []byte(strings.Join(sections, " ")), nil } func (jf jsonFormat) Format(entry *logrus.Entry) ([]byte, error) { diff --git a/log/standardLogger_test.go b/log/standardLogger_test.go index a433ac1..1c00fd4 100644 --- a/log/standardLogger_test.go +++ b/log/standardLogger_test.go @@ -172,11 +172,11 @@ func TestInfof(t *testing.T) { } func testStandardLogOutput(t *testing.T, level logrus.Level, fields frozen.Map, logFunc func()) { - expectedOutput := strings.Join([]string{strings.ToUpper(level.String()), testMessage}, " ") actualOutput := redirectOutput(t, logFunc) // uses Contains to avoid checking timestamps - assert.Contains(t, actualOutput, expectedOutput) + assert.Contains(t, actualOutput, strings.ToUpper(level.String())) + assert.Contains(t, actualOutput, testMessage) for i := fields.Range(); i.Next(); { assert.Contains(t, actualOutput, fmt.Sprintf("%s=%v", i.Key(), i.Value())) } From e6c279ca1a50d98c793b6814a1a918350ff03de7 Mon Sep 17 00:00:00 2001 From: Novan Allanadi <25704935+nofun97@users.noreply.github.com> Date: Wed, 26 Feb 2020 14:23:23 +1100 Subject: [PATCH 2/2] updated docs --- log/README.md | 4 ++-- log/examples.md | 2 +- log/examples/example.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/log/README.md b/log/README.md index ca5b029..190f075 100644 --- a/log/README.md +++ b/log/README.md @@ -202,13 +202,13 @@ Fields will be logged as an object of the attribute `fields`. One thing to remem The standard formatter will log in the following format without the parentheses: ```text -(time in RFC3339Nano Format) (Fields) (Level) (Message) +(time in RFC3339Nano Format) (Level) (Fields) (Message) ``` For example: ```log -2020-02-05T09:05:11.041651+11:00 this=one have=fields INFO log with fields +2020-02-05T09:05:11.041651+11:00 INFO this=one have=fields log with fields ``` In the current implementation, the fields are logged in a random order. diff --git a/log/examples.md b/log/examples.md index 25210ed..eff41f0 100644 --- a/log/examples.md +++ b/log/examples.md @@ -122,7 +122,7 @@ func loggingDemo(ctx context.Context) { // by the logger. There are two levels of logging, Debug and Info. Each will also have // the format counterpart, Debugf and Infof. // Logging will log in the following format: - // (time in RFC3339Nano Format) (Fields) (Level) (Message) + // (time in RFC3339Nano Format) (Level) (Fields) (Message) // Fields themselves are logged as a space separated list of key=value log.From(ctx).Debug("This does not have any fields") diff --git a/log/examples/example.go b/log/examples/example.go index 22d8f3a..dfb62c0 100644 --- a/log/examples/example.go +++ b/log/examples/example.go @@ -103,7 +103,7 @@ func loggingDemo(ctx context.Context) { // by the logger. There are two levels of logging, Debug and Info. Each will also have // the format counterpart, Debugf and Infof. // Logging will log in the following format: - // (time in RFC3339Nano Format) (Fields) (Level) (Message) + // (time in RFC3339Nano Format) (Level) (Fields) (Message) // Fields themselves are logged as a space separated list of key=value. log.From(ctx).Debug("This does not have any fields")