Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write fields textformatter #3

Merged
merged 2 commits into from
Feb 4, 2020
Merged
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
15 changes: 15 additions & 0 deletions formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,28 @@ func (t TextFormatter) Format(w io.Writer, logLevel LogLevel, wl WLogger, msg st
// Append log message to buffer
writeString(w, msg)

if len(wl.GetFields()) > 0 {
writeString(w, " [ ")
writeFields(w, wl.GetFields())
writeString(w, "]")
}

if len(msg) == 0 || msg[len(msg)-1] != '\n' {
writeString(w, "\n")
}

return nil
}

func writeFields(w io.Writer, fields Fields) {
for key, value := range fields {
writeString(w, key)
writeString(w, "=")
writeString(w, fmt.Sprintf("%v", value))
writeString(w, ", ")
}
}

func getTimestamp(now time.Time) string {
year, month, day := now.Date()
hour, min, sec := now.Clock()
Expand Down
3 changes: 1 addition & 2 deletions wlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (l *Logger) WithScope(fields Fields) *ScopedLogger {
for k, v := range l.fields {
scopeFields[k] = v
}
return &ScopedLogger{scopeFields, l, JSONFormatter{}}
return &ScopedLogger{scopeFields, l, l.formatter}
}

// SetGlobalFields set fields in a global wlog instance. These fields will be appended to any
Expand All @@ -156,7 +156,6 @@ func (l *Logger) SetGlobalFields(f Fields) {
l.lock.Lock()
defer l.lock.Unlock()
l.fields = fields
l.formatter = JSONFormatter{}
}

// Debugf formats and logs a debug message
Expand Down