Skip to content

Commit

Permalink
add fields to log.print() (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
SupriyaKasten authored and mergify[bot] committed Oct 15, 2019
1 parent 9f896b4 commit a58994c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
13 changes: 10 additions & 3 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func Debug() Logger {
}

// Print adds `msg` to the log at `InfoLevel`. It is a wrapper for `Info().Print(msg)`, since this is the most common use case.
func Print(msg string) {
Info().Print(msg)
func Print(msg string, fields ...field.M) {
Info().Print(msg, fields...)
}

func WithContext(ctx context.Context) Logger {
Expand All @@ -60,13 +60,20 @@ func WithError(err error) Logger {
return Info().WithError(err)
}

func (l *logger) Print(msg string) {
func (l *logger) Print(msg string, fields ...field.M) {
logFields := make(logrus.Fields)
if ctxFields := field.FromContext(l.ctx); ctxFields != nil {
for _, cf := range ctxFields.Fields() {
logFields[cf.Key()] = cf.Value()
}
}

for _, f := range fields {
for k, v := range f {
logFields[k] = v
}
}

entry := log.WithFields(logFields)
if l.err != nil {
entry = entry.WithError(l.err)
Expand Down
14 changes: 12 additions & 2 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ func (s *LogSuite) TestLogMessage(c *C) {
testLogMessage(c, text, Print)
}

func (s *LogSuite) TestLogWithFields(c *C) {
const text = "Some useful text."
entry := testLogMessage(c, text, Print, field.M{"key": "value"})
c.Assert(entry["level"], Equals, infoLevelStr)
// Error should not be set in the log entry
c.Assert(entry["error"], Equals, nil)
// A field with "key" should be set in the log entry
c.Assert(entry["key"], Equals, "value")
}

func (s *LogSuite) TestLogWithError(c *C) {
const text = "My error message"
err := errors.New("test error")
Expand Down Expand Up @@ -84,11 +94,11 @@ func (s *LogSuite) TestLogWithContextFieldsAndError(c *C) {
c.Assert(entry["key"], Equals, "value")
}

func testLogMessage(c *C, msg string, print func(string)) map[string]interface{} {
func testLogMessage(c *C, msg string, print func(string, ...field.M), fields ...field.M) map[string]interface{} {
log.SetFormatter(&logrus.JSONFormatter{TimestampFormat: time.RFC3339Nano})
var memLog bytes.Buffer
log.SetOutput(&memLog)
print(msg)
print(msg, fields...)
var entry map[string]interface{}
err := json.Unmarshal(memLog.Bytes(), &entry)
c.Assert(err, IsNil)
Expand Down
4 changes: 3 additions & 1 deletion pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package log

import (
"context"

"github.com/kanisterio/kanister/pkg/field"
)

type Logger interface {
Print(msg string)
Print(msg string, fields ...field.M)
WithContext(ctx context.Context) Logger
WithError(err error) Logger
}

0 comments on commit a58994c

Please sign in to comment.