@@ -20,7 +20,20 @@ func TestHandler_colors(t *testing.T) {
2020 rec := slog .NewRecord (now , slog .LevelInfo , "foobar" , 0 )
2121 AssertNoError (t , h .Handle (context .Background (), rec ))
2222
23- expected := fmt .Sprintf ("\x1b [90m%s\x1b [0m \x1b [92mINF\x1b [0m \x1b [97mfoobar\x1b [0m\r \n " , now .Format (time .DateTime ))
23+ expected := fmt .Sprintf ("\x1b [90m%s\x1b [0m \x1b [92mINF\x1b [0m \x1b [97mfoobar\x1b [0m\n " , now .Format (time .DateTime ))
24+ AssertEqual (t , expected , buf .String ())
25+ }
26+
27+ func TestHandler_TimeFormat (t * testing.T ) {
28+ buf := bytes.Buffer {}
29+ h := NewHandler (& buf , & HandlerOptions {TimeFormat : time .RFC3339Nano , NoColor : true })
30+ now := time .Now ()
31+ rec := slog .NewRecord (now , slog .LevelInfo , "foobar" , 0 )
32+ endTime := now .Add (time .Second )
33+ rec .AddAttrs (slog .Time ("endtime" , endTime ))
34+ AssertNoError (t , h .Handle (context .Background (), rec ))
35+
36+ expected := fmt .Sprintf ("%s INF foobar endtime=%s\n " , now .Format (time .RFC3339Nano ), endTime .Format (time .RFC3339Nano ))
2437 AssertEqual (t , expected , buf .String ())
2538}
2639
@@ -31,7 +44,7 @@ func TestHandler_NoColor(t *testing.T) {
3144 rec := slog .NewRecord (now , slog .LevelInfo , "foobar" , 0 )
3245 AssertNoError (t , h .Handle (context .Background (), rec ))
3346
34- expected := fmt .Sprintf ("%s INF foobar\r \ n " , now .Format (time .DateTime ))
47+ expected := fmt .Sprintf ("%s INF foobar\n " , now .Format (time .DateTime ))
3548 AssertEqual (t , expected , buf .String ())
3649}
3750
@@ -63,7 +76,7 @@ func TestHandler_Attr(t *testing.T) {
6376 )
6477 AssertNoError (t , h .Handle (context .Background (), rec ))
6578
66- expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar err=the error stringer=stringer nostringer={bar}\r \ n " , now .Format (time .DateTime ), now .Format (time .RFC3339 ))
79+ expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar err=the error stringer=stringer nostringer={bar}\n " , now .Format (time .DateTime ), now .Format (time .DateTime ))
6780 AssertEqual (t , expected , buf .String ())
6881}
6982
@@ -84,12 +97,12 @@ func TestHandler_WithAttr(t *testing.T) {
8497 })
8598 AssertNoError (t , h2 .Handle (context .Background (), rec ))
8699
87- expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar\r \ n " , now .Format (time .DateTime ), now .Format (time .RFC3339 ))
100+ expected := fmt .Sprintf ("%s INF foobar bool=true int=-12 uint=12 float=3.14 foo=bar time=%s dur=1s group.foo=bar group.subgroup.foo=bar\n " , now .Format (time .DateTime ), now .Format (time .DateTime ))
88101 AssertEqual (t , expected , buf .String ())
89102
90103 buf .Reset ()
91104 AssertNoError (t , h .Handle (context .Background (), rec ))
92- AssertEqual (t , fmt .Sprintf ("%s INF foobar\r \ n " , now .Format (time .DateTime )), buf .String ())
105+ AssertEqual (t , fmt .Sprintf ("%s INF foobar\n " , now .Format (time .DateTime )), buf .String ())
93106}
94107
95108func TestHandler_WithGroup (t * testing.T ) {
@@ -100,31 +113,31 @@ func TestHandler_WithGroup(t *testing.T) {
100113 rec .Add ("int" , 12 )
101114 h2 := h .WithGroup ("group1" ).WithAttrs ([]slog.Attr {slog .String ("foo" , "bar" )})
102115 AssertNoError (t , h2 .Handle (context .Background (), rec ))
103- expected := fmt .Sprintf ("%s INF foobar group1.foo=bar group1.int=12\r \ n " , now .Format (time .DateTime ))
116+ expected := fmt .Sprintf ("%s INF foobar group1.foo=bar group1.int=12\n " , now .Format (time .DateTime ))
104117 AssertEqual (t , expected , buf .String ())
105118 buf .Reset ()
106119
107120 h3 := h2 .WithGroup ("group2" )
108121 AssertNoError (t , h3 .Handle (context .Background (), rec ))
109- expected = fmt .Sprintf ("%s INF foobar group1.foo=bar group1.group2.int=12\r \ n " , now .Format (time .DateTime ))
122+ expected = fmt .Sprintf ("%s INF foobar group1.foo=bar group1.group2.int=12\n " , now .Format (time .DateTime ))
110123 AssertEqual (t , expected , buf .String ())
111124
112125 buf .Reset ()
113126 AssertNoError (t , h .Handle (context .Background (), rec ))
114- AssertEqual (t , fmt .Sprintf ("%s INF foobar int=12\r \ n " , now .Format (time .DateTime )), buf .String ())
127+ AssertEqual (t , fmt .Sprintf ("%s INF foobar int=12\n " , now .Format (time .DateTime )), buf .String ())
115128}
116129
117130func TestHandler_Levels (t * testing.T ) {
118131 levels := map [slog.Level ]string {
119- slog .LevelDebug - 1 : "??? " ,
132+ slog .LevelDebug - 1 : "DBG-1 " ,
120133 slog .LevelDebug : "DBG" ,
121- slog .LevelDebug + 1 : "DBG" ,
134+ slog .LevelDebug + 1 : "DBG+1 " ,
122135 slog .LevelInfo : "INF" ,
123- slog .LevelInfo + 1 : "INF" ,
136+ slog .LevelInfo + 1 : "INF+1 " ,
124137 slog .LevelWarn : "WRN" ,
125- slog .LevelWarn + 1 : "WRN" ,
138+ slog .LevelWarn + 1 : "WRN+1 " ,
126139 slog .LevelError : "ERR" ,
127- slog .LevelError + 1 : "ERR" ,
140+ slog .LevelError + 1 : "ERR+1 " ,
128141 }
129142
130143 for l := range levels {
@@ -137,7 +150,7 @@ func TestHandler_Levels(t *testing.T) {
137150 rec := slog .NewRecord (now , ll , "foobar" , 0 )
138151 if ll >= l {
139152 AssertNoError (t , h .Handle (context .Background (), rec ))
140- AssertEqual (t , fmt .Sprintf ("%s %s foobar\r \ n " , now .Format (time .DateTime ), s ), buf .String ())
153+ AssertEqual (t , fmt .Sprintf ("%s %s foobar\n " , now .Format (time .DateTime ), s ), buf .String ())
141154 buf .Reset ()
142155 }
143156 }
@@ -155,10 +168,10 @@ func TestHandler_Source(t *testing.T) {
155168 AssertNoError (t , h .Handle (context .Background (), rec ))
156169 cwd , _ := os .Getwd ()
157170 file , _ = filepath .Rel (cwd , file )
158- AssertEqual (t , fmt .Sprintf ("%s INF %s:%d > foobar\r \ n " , now .Format (time .DateTime ), file , line ), buf .String ())
171+ AssertEqual (t , fmt .Sprintf ("%s INF %s:%d > foobar\n " , now .Format (time .DateTime ), file , line ), buf .String ())
159172 buf .Reset ()
160173 AssertNoError (t , h2 .Handle (context .Background (), rec ))
161- AssertEqual (t , fmt .Sprintf ("%s INF foobar\r \ n " , now .Format (time .DateTime )), buf .String ())
174+ AssertEqual (t , fmt .Sprintf ("%s INF foobar\n " , now .Format (time .DateTime )), buf .String ())
162175}
163176
164177func TestHandler_Err (t * testing.T ) {
0 commit comments