@@ -16,6 +16,7 @@ import (
1616 "log/slog"
1717 "strings"
1818 "testing"
19+ "time"
1920
2021 "github.com/stretchr/testify/assert"
2122 "github.com/stretchr/testify/require"
@@ -345,3 +346,66 @@ func TestFormatOverride(t *testing.T) {
345346 })
346347 }
347348}
349+
350+ func TestDurationFormatting (t * testing.T ) {
351+ tests := []struct {
352+ name string
353+ duration time.Duration
354+ expectedString string
355+ }{
356+ {
357+ name : "hours" ,
358+ duration : 6 * time .Hour ,
359+ expectedString : "6h0m0s" ,
360+ },
361+ {
362+ name : "minutes and seconds" ,
363+ duration : 5 * time .Minute + 30 * time .Second ,
364+ expectedString : "5m30s" ,
365+ },
366+ {
367+ name : "milliseconds" ,
368+ duration : 150 * time .Millisecond ,
369+ expectedString : "150ms" ,
370+ },
371+ {
372+ name : "complex duration" ,
373+ duration : 2 * time .Hour + 30 * time .Minute + 45 * time .Second ,
374+ expectedString : "2h30m45s" ,
375+ },
376+ }
377+
378+ for _ , tt := range tests {
379+ t .Run (tt .name , func (t * testing.T ) {
380+ var buf bytes.Buffer
381+ logger := NewSlogLogger (
382+ WithOutput (& buf ),
383+ WithFormat (FormatJSON ),
384+ )
385+
386+ logger .Info ("test message" , slog .Duration ("duration" , tt .duration ))
387+
388+ // Parse the JSON output
389+ var logEntry map [string ]any
390+ err := json .Unmarshal (buf .Bytes (), & logEntry )
391+ require .NoError (t , err )
392+
393+ // Duration should be formatted as human-readable string, not nanoseconds
394+ assert .Equal (t , tt .expectedString , logEntry ["duration" ], "duration should be human-readable" )
395+ })
396+ }
397+ }
398+
399+ func TestDurationFormattingTextFormat (t * testing.T ) {
400+ var buf bytes.Buffer
401+ logger := NewSlogLogger (
402+ WithOutput (& buf ),
403+ WithFormat (FormatText ),
404+ )
405+
406+ logger .Info ("test message" , slog .Duration ("duration" , 6 * time .Hour ))
407+
408+ output := buf .String ()
409+ // Text format should also show human-readable duration
410+ assert .Contains (t , output , "duration=6h0m0s" )
411+ }
0 commit comments