@@ -42,27 +42,55 @@ 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 )
5050}
5151
52- func TestDebugWithFields (t * testing.T ) {
52+ func TestDebugJSON (t * testing.T ) {
5353 var buf bytes.Buffer
5454 d := getDebugger ()
5555 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+ }
5665
57- x := d .With ("key1" , "value1" ).With ("key2" , 42 )
66+ func TestDebugJSONWithField (t * testing.T ) {
67+ var buf bytes.Buffer
68+ d := getDebugger ()
69+ d .SetOutput (& buf )
70+ SetFormat (JSON )
71+ d .Debug (testMessage )
72+ SetFormat (Plain )
5873
59- x .Debug (testMessage )
74+ assert .False (t , hasANSI (buf .String ()))
75+ output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
76+ expected := strings .TrimSpace ("{\" namespace\" :\" " + namespace + "\" ,\" message\" :\" " + testMessage + "\" }" )
77+ assert .Equal (t , expected , output )
78+ }
6079
61- assert .True (t , hasANSI (buf .String ()), "Must have no colors" )
80+ func TestDebugJSONWithFieldTimestamp (t * testing.T ) {
81+ var buf bytes.Buffer
82+ d := getDebugger ()
83+ SetOutput (& buf )
84+ SetTimestamp (& Timestamp {Format : "2006" })
85+ SetFormat (JSON )
86+ d .Debug (testMessage )
87+ SetFormat (Plain )
88+ SetTimestamp (nil )
6289
90+ assert .False (t , hasANSI (buf .String ()))
6391 output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
64- expected := strings .TrimSpace (fmt .Sprintf ( "%s key1=value1 key2=42 %s +0ms \n " , namespace , testMessage ) )
65- assert .Equal (t , expected , output , "Must have fields" )
92+ expected := strings .TrimSpace ("{ \" timestamp \" : \" " + fmt .Sprint ( time . Now (). Year ()) + " \" , \" namespace \" : \" " + namespace + " \" , \" message \" : \" " + testMessage + " \" }" )
93+ assert .Equal (t , expected , output )
6694}
6795
6896func TestDebugGlobalOutput (t * testing.T ) {
@@ -74,11 +102,11 @@ func TestDebugGlobalOutput(t *testing.T) {
74102
75103 d .Debug (testMessage )
76104
77- assert .True (t , hasANSI (buf .String ()), "Must have no colors" )
105+ assert .True (t , hasANSI (buf .String ()))
78106
79107 output := strings .TrimSpace (stripANSI (buf .String ())) // Strip colors and trim whitespace
80108 expected := strings .TrimSpace (testMessageExpected )
81- assert .Equal (t , expected , output , "Must have no colors" )
109+ assert .Equal (t , expected , output )
82110}
83111
84112func TestDebugNoColors (t * testing.T ) {
@@ -89,7 +117,7 @@ func TestDebugNoColors(t *testing.T) {
89117
90118 d .Debug (testMessage )
91119
92- assert .False (t , hasANSI (buf .String ()), "Must have no colors" )
120+ assert .False (t , hasANSI (buf .String ()))
93121}
94122
95123func TestDebugNonMatchingNamespace (t * testing.T ) {
@@ -100,7 +128,7 @@ func TestDebugNonMatchingNamespace(t *testing.T) {
100128
101129 d .Debug ("" )
102130
103- assert .Empty (t , buf .String (), "Must have no message" )
131+ assert .Empty (t , buf .String ())
104132}
105133
106134func TestDebugEmptyMessage (t * testing.T ) {
@@ -112,7 +140,7 @@ func TestDebugEmptyMessage(t *testing.T) {
112140 SetNamespace ("does:not:exist" )
113141 d .Debug ("test" )
114142
115- assert .Empty (t , buf .String (), "Must have no message" )
143+ assert .Empty (t , buf .String ())
116144}
117145
118146func TestDebugWithColors (t * testing.T ) {
@@ -123,7 +151,7 @@ func TestDebugWithColors(t *testing.T) {
123151
124152 d .Debug (testMessage )
125153
126- assert .True (t , hasANSI (buf .String ()), "Must have colors" )
154+ assert .True (t , hasANSI (buf .String ()))
127155}
128156
129157func TestDebugf (t * testing.T ) {
0 commit comments