Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions log/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion log/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion log/examples/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 5 additions & 21 deletions log/standardLogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"

Expand All @@ -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()))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting idea, does this provide a performance improvement?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's really just because I was frustrated with managing the whitespaces though I remembered I tried comparing Join and using the Builder (my test was small) and there wasn't really any difference, at least with small cases.


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will incur a performance hit due to copying the final string into a byte array. Use bytes.Buffer instead. Maybe also NewBytesBuffer(make([]byte, 0, aReasonableDefaultCapacity)).

}

func (jf jsonFormat) Format(entry *logrus.Entry) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions log/standardLogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down