@@ -42,11 +42,58 @@ func TestDebug(t *testing.T) {
4242
4343 d .Debug (testMessage )
4444
45- assert .True (t , hasANSI (buf .String ()), "Must have no colors" )
45+ assert .True (t , hasANSI (buf .String ()))
4646
4747 output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
4848 expected := strings .TrimSpace (testMessageExpected )
49- assert .Equal (t , expected , output , "Must have no colors" )
49+ assert .Equal (t , expected , output )
50+ }
51+
52+ func TestDebugJSON (t * testing.T ) {
53+ var buf bytes.Buffer
54+ d := getDebugger ()
55+ d .SetOutput (& buf )
56+ SetFormat (Json )
57+ d .Debug (testMessage )
58+ SetFormat (Plain )
59+
60+ assert .False (t , hasANSI (buf .String ()))
61+ output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
62+ expected := strings .TrimSpace ("{\" namespace\" :\" " + namespace + "\" ,\" message\" :\" " + testMessage + "\" }" )
63+ assert .Equal (t , expected , output )
64+ }
65+
66+ func TestDebugJSONWithField (t * testing.T ) {
67+ var buf bytes.Buffer
68+ d := getDebugger ()
69+ d .SetOutput (& buf )
70+ d = d .With ("json" , true ).With ("number" , 123 )
71+ SetFormat (Json )
72+ d .Debug (testMessage )
73+ SetFormat (Plain )
74+
75+ assert .False (t , hasANSI (buf .String ()))
76+ output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
77+ expected := strings .TrimSpace ("{\" namespace\" :\" " + namespace + "\" ,\" fields\" :{\" json\" :true,\" number\" :123},\" message\" :\" " + testMessage + "\" }" )
78+ assert .Equal (t , expected , output )
79+ }
80+
81+ func TestDebugJSONWithFieldTimestamp (t * testing.T ) {
82+ var buf bytes.Buffer
83+ d := getDebugger ()
84+ d .SetOutput (& buf )
85+
86+ d = d .With ("json" , true ).With ("number" , 123 )
87+ SetTimestamp (& Timestamp {Format : "2006" })
88+ SetFormat (Json )
89+ d .Debug (testMessage )
90+ SetFormat (Plain )
91+ SetTimestamp (nil )
92+
93+ assert .False (t , hasANSI (buf .String ()))
94+ output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
95+ expected := strings .TrimSpace ("{\" timestamp\" :\" " + fmt .Sprint (time .Now ().Year ()) + "\" ,\" namespace\" :\" " + namespace + "\" ,\" fields\" :{\" json\" :true,\" number\" :123},\" message\" :\" " + testMessage + "\" }" )
96+ assert .Equal (t , expected , output )
5097}
5198
5299func TestDebugWithFields (t * testing.T ) {
@@ -58,7 +105,7 @@ func TestDebugWithFields(t *testing.T) {
58105
59106 x .Debug (testMessage )
60107
61- assert .True (t , hasANSI (buf .String ()), "Must have no colors" )
108+ assert .True (t , hasANSI (buf .String ()))
62109
63110 output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
64111 expected := strings .TrimSpace (fmt .Sprintf ("%s key1=value1 key2=42 %s +0ms\n " , namespace , testMessage ))
@@ -74,11 +121,11 @@ func TestDebugGlobalOutput(t *testing.T) {
74121
75122 d .Debug (testMessage )
76123
77- assert .True (t , hasANSI (buf .String ()), "Must have no colors" )
124+ assert .True (t , hasANSI (buf .String ()))
78125
79126 output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
80127 expected := strings .TrimSpace (testMessageExpected )
81- assert .Equal (t , expected , output , "Must have no colors" )
128+ assert .Equal (t , expected , output )
82129}
83130
84131func TestDebugNoColors (t * testing.T ) {
@@ -89,7 +136,7 @@ func TestDebugNoColors(t *testing.T) {
89136
90137 d .Debug (testMessage )
91138
92- assert .False (t , hasANSI (buf .String ()), "Must have no colors" )
139+ assert .False (t , hasANSI (buf .String ()))
93140}
94141
95142func TestDebugNonMatchingNamespace (t * testing.T ) {
@@ -100,7 +147,7 @@ func TestDebugNonMatchingNamespace(t *testing.T) {
100147
101148 d .Debug ("" )
102149
103- assert .Empty (t , buf .String (), "Must have no message" )
150+ assert .Empty (t , buf .String ())
104151}
105152
106153func TestDebugEmptyMessage (t * testing.T ) {
@@ -112,7 +159,7 @@ func TestDebugEmptyMessage(t *testing.T) {
112159 SetNamespace ("does:not:exist" )
113160 d .Debug ("test" )
114161
115- assert .Empty (t , buf .String (), "Must have no message" )
162+ assert .Empty (t , buf .String ())
116163}
117164
118165func TestDebugWithColors (t * testing.T ) {
@@ -123,7 +170,7 @@ func TestDebugWithColors(t *testing.T) {
123170
124171 d .Debug (testMessage )
125172
126- assert .True (t , hasANSI (buf .String ()), "Must have colors" )
173+ assert .True (t , hasANSI (buf .String ()))
127174}
128175
129176func TestDebugf (t * testing.T ) {
0 commit comments