Skip to content

Option to use json.MarshalIndent #25

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
49 changes: 36 additions & 13 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import (

type (
Logger struct {
prefix string
level Lvl
skip int
output io.Writer
template *fasttemplate.Template
levels []string
color *color.Color
bufferPool sync.Pool
mutex sync.Mutex
prefix string
level Lvl
skip int
output io.Writer
template *fasttemplate.Template
levels []string
color *color.Color
bufferPool sync.Pool
mutex sync.Mutex
indentEnabled bool
indent string
}

Lvl uint8
Expand Down Expand Up @@ -96,6 +98,10 @@ func (l *Logger) DisableColor() {
l.initLevels()
}

func (l *Logger) DisableIndent() {
l.indentEnabled = false
}

func (l *Logger) EnableColor() {
l.color.Enable()
l.initLevels()
Expand All @@ -109,6 +115,11 @@ func (l *Logger) SetPrefix(p string) {
l.prefix = p
}

func (l *Logger) SetIndent(i string) {
l.indentEnabled = true
l.indent = i
}

func (l *Logger) Level() Lvl {
return l.level
}
Expand Down Expand Up @@ -360,9 +371,18 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
if format == "" {
message = fmt.Sprint(args...)
} else if format == "json" {
b, err := json.Marshal(args[0])
if err != nil {
panic(err)
var b []byte
var err error
if l.indentEnabled {
b, err = json.MarshalIndent(args[0], "", l.indent)
if err != nil {
panic(err)
}
} else {
b, err = json.Marshal(args[0])
if err != nil {
panic(err)
}
}
message = string(b)
} else {
Expand Down Expand Up @@ -392,7 +412,10 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
if err == nil {
s := buf.String()
i := buf.Len() - 1
if s[i] == '}' {
if i == -1 {
// Empty header
buf.WriteString(message)
} else if s[i] == '}' {
// JSON header
buf.Truncate(i)
buf.WriteByte(',')
Expand Down
11 changes: 11 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ func TestJSON(t *testing.T) {
assert.Contains(t, b.String(), `"name":"value"`)
}

func TestJSONIndent(t *testing.T) {
l := New("test")
b := new(bytes.Buffer)
l.SetOutput(b)
l.SetLevel(DEBUG)
l.SetHeader("")
l.SetIndent(" ")
l.Debugj(JSON{"name": "value"})
assert.Contains(t, b.String(), "xxx {\n \"name\": \"value\"\n}\n")
}

func TestStringWithQuotes(t *testing.T) {
l := New("test")
b := new(bytes.Buffer)
Expand Down