Skip to content

Commit dae50c2

Browse files
committed
fix: colorJson working
1 parent 4e9ae55 commit dae50c2

7 files changed

+63
-38
lines changed

debug.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import (
1515
)
1616

1717
var (
18-
writer io.Writer = os.Stderr
19-
reg *regexp.Regexp
20-
neg []*regexp.Regexp
21-
m sync.Mutex
22-
enabled = false
23-
cache *goCache.Cache
24-
HAS_COLORS = true
25-
HAS_TIME = true
26-
formatter Formatter = &TextFormatter{HasColor: true}
27-
negRegEx = regexp.MustCompile(`^-.*?`)
28-
textRegex = regexp.MustCompile(`(?i)text`)
29-
jsonRegex = regexp.MustCompile(`(?i)json`)
18+
writer io.Writer = os.Stderr
19+
reg *regexp.Regexp
20+
neg []*regexp.Regexp
21+
m sync.Mutex
22+
enabled = false
23+
cache *goCache.Cache
24+
HAS_COLORS = true
25+
HAS_TIME = true
26+
defaultFormatter = &TextFormatter{HasColor: true}
27+
formatter Formatter = defaultFormatter
28+
negRegEx = regexp.MustCompile(`^-.*?`)
29+
textRegex = regexp.MustCompile(`(?i)text`)
30+
jsonRegex = regexp.MustCompile(`(?i)json`)
3031
)
3132

3233
type Fields map[string]interface{}
@@ -92,6 +93,9 @@ func Disable() {
9293
}
9394

9495
func SetFormatter(f Formatter) {
96+
if f == nil {
97+
f = defaultFormatter
98+
}
9599
m.Lock()
96100
defer m.Unlock()
97101
formatter = f
@@ -105,7 +109,11 @@ func SetFormatterString(s string) {
105109

106110
if jsonRegex.MatchString(s) {
107111
// default to PRETTY true
108-
_f = &JSONFormatter{PrettyPrint: os.Getenv("DEBUG_FORMATTER_PRETTY_OFF") == ""}
112+
j := JSONFormatter{PrettyPrint: os.Getenv("DEBUG_FORMATTER_PRETTY_OFF") == ""}
113+
if j.PrettyPrint && j.Indent == 0 {
114+
j.Indent = 2
115+
}
116+
_f = &j
109117
}
110118

111119
SetFormatter(_f)

debug_bench_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ func BenchmarkMatch(b *testing.B) {
2121
}
2222
}
2323

24+
func BenchmarkMatchJson(b *testing.B) {
25+
SetFormatter(&JSONFormatter{})
26+
SetHasColors(false)
27+
debug := Debug("something")
28+
Enable("something")
29+
for i := 0; i < b.N; i++ {
30+
debug.Log("stuff")
31+
}
32+
SetFormatter(nil)
33+
SetHasColors(true)
34+
}
35+
36+
func BenchmarkMatchWithFields(b *testing.B) {
37+
debug := Debug("something")
38+
Enable("something")
39+
debug.WithFields(map[string]interface{}{
40+
"a": "one",
41+
"b": "two",
42+
})
43+
for i := 0; i < b.N; i++ {
44+
debug.Log("stuff")
45+
}
46+
}
47+
2448
func BenchmarkMatchError(b *testing.B) {
2549
debug := Debug("something")
2650
Enable("error*")

debug_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,5 @@ func TestBuildPattern(t *testing.T) {
342342
ar := *actualNegs[i]
343343
assert.Equal(t, er, ar, "pattern matches")
344344
}
345-
346345
}
347346
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/nmccready/go-debug
33
go 1.14
44

55
require (
6-
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2
7-
github.com/fatih/color v1.9.0 // indirect
6+
github.com/fatih/color v1.9.0
7+
github.com/nmccready/colorjson v0.0.4
88
github.com/patrickmn/go-cache v2.1.0+incompatible
99
github.com/stretchr/testify v1.6.1
1010
)

go.sum

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
2-
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
31
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
42
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
53
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
64
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
5+
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
6+
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
77
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
88
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
99
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
1010
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
1111
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
12+
github.com/nmccready/colorjson v0.0.4 h1:g+oCvz7hw20bKIeCrR8eju4XXRcku2y+tY6N6R5luHQ=
13+
github.com/nmccready/colorjson v0.0.4/go.mod h1:n65NtVgGymxWEJz0pX5HHBJIM4GW/wHvkMUG4l83u5o=
1214
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
1315
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
1416
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -19,6 +21,7 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
1921
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
2022
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
2123
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2225
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2326
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2427
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

json_formatter.go

+5-20
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"runtime"
66
"strconv"
77

8-
"github.com/TylerBrock/colorjson"
98
"github.com/fatih/color"
9+
"github.com/nmccready/colorjson"
1010
)
1111

1212
// JSONFormatter formats logs into parsable json
@@ -20,6 +20,7 @@ type JSONFormatter struct {
2020
// HTMLEscape allows disabling html escaping in output
2121
HTMLEscape bool
2222

23+
Indent int
2324
// FieldMap allows users to customize the names of keys for default fields.
2425
// As an example:
2526
// formatter := &JSONFormatter{
@@ -52,29 +53,13 @@ func (f *JSONFormatter) Format(dbg *Debugger, msg string) string {
5253
"time": "20:53:24.798"
5354
}
5455
*/
55-
finalized := finalizeFields(dbg, msg, false, func(k string, v interface{}) interface{} {
56-
switch v := v.(type) {
57-
case error:
58-
// Otherwise errors are ignored by `encoding/json`
59-
// https://github.com/sirupsen/logrus/issues/137
60-
return v.Error()
61-
default:
62-
return nil
63-
}
64-
})
56+
finalized := finalizeFields(dbg, msg, false, nil)
6557

66-
// b := &bytes.Buffer{}
67-
68-
// encoder := json.NewEncoder(b)
69-
// encoder.SetEscapeHTML(!f.DisableHTMLEscape)
70-
// if f.PrettyPrint {
71-
// encoder.SetIndent("", " ")
72-
// }
73-
// err := encoder.Encode(finalized.Fields)
7458
_f := colorjson.NewFormatter()
7559
_f.DisabledColor = !HAS_COLORS
7660
_f.HTMLEscape = f.HTMLEscape
77-
_f.Indent = 2
61+
_f.Indent = f.Indent
62+
7863
intColor, _ := strconv.Atoi(dbg.color)
7964
_f.KeyMapColors["namespace"] = color.New(color.Attribute(intColor))
8065
b, err := _f.Marshal(map[string]interface{}(finalized.Fields))

json_formatter_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
func TestErrorNotLost(t *testing.T) {
13+
SetHasColors(false)
1314
SetFormatter(&JSONFormatter{})
1415

1516
s := formatter.Format(Debug("error_not_lost").WithField("error", errors.New("wild walrus")), "hi")
@@ -27,6 +28,7 @@ func TestErrorNotLost(t *testing.T) {
2728
}
2829

2930
func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
31+
SetHasColors(false)
3032
SetFormatter(&JSONFormatter{})
3133

3234
s := formatter.Format(Debug("mapped_field_error").WithField("omg", errors.New("wild walrus")), "hi")
@@ -44,6 +46,7 @@ func TestErrorNotLostOnFieldNotNamedError(t *testing.T) {
4446
}
4547

4648
func TestFieldClashWithTime(t *testing.T) {
49+
SetHasColors(false)
4750
SetFormatter(&JSONFormatter{})
4851

4952
s := formatter.Format(Debug("clash_time").WithField("time", "right now!"), "hi")
@@ -60,6 +63,7 @@ func TestFieldClashWithTime(t *testing.T) {
6063
}
6164

6265
func TestFieldClashWithMsg(t *testing.T) {
66+
SetHasColors(false)
6367
SetFormatter(&JSONFormatter{})
6468

6569
s := formatter.Format(Debug("clash_msg").WithField("msg", errors.New("wild walrus")), "hi")
@@ -72,6 +76,7 @@ func TestFieldClashWithMsg(t *testing.T) {
7276
}
7377

7478
func TestFieldClashWithNamespace(t *testing.T) {
79+
SetHasColors(false)
7580
SetFormatter(&JSONFormatter{})
7681

7782
s := formatter.Format(Debug("clash_namespace").WithField("namespace", errors.New("wild walrus")), "hi")
@@ -97,6 +102,7 @@ func TestJSONEntryEndsWithNewline(t *testing.T) {
97102

98103
func TestJSONPretty(t *testing.T) {
99104
HAS_TIME = false
105+
SetHasColors(false)
100106
SetFormatter(&JSONFormatter{PrettyPrint: true})
101107

102108
s := formatter.Format(Debug("pretty").WithField("dog", "wild walrus"), "hi")

0 commit comments

Comments
 (0)