diff --git a/devslog.go b/devslog.go index 126d368..1f48f9e 100644 --- a/devslog.go +++ b/devslog.go @@ -505,6 +505,9 @@ func (h *developHandler) formatStruct(st reflect.Type, sv reflect.Value, l int) pr := h.structKeyPadding(sv, nil) for i := 0; i < sv.NumField(); i++ { + if !sv.Type().Field(i).IsExported() { + continue + } v := sv.Field(i) t := v.Type() @@ -624,6 +627,9 @@ func (h *developHandler) mapKeyPadding(rv reflect.Value, fgColor *foregroundColo func (h *developHandler) structKeyPadding(sv reflect.Value, fgColor *foregroundColor) (p int) { st := sv.Type() for i := 0; i < sv.NumField(); i++ { + if !st.Field(i).IsExported() { + continue + } c := len(st.Field(i).Name) if fgColor != nil { c = len(cs([]byte(st.Field(i).Name), *fgColor)) diff --git a/devslog_test.go b/devslog_test.go index 68f32d5..1241d00 100644 --- a/devslog_test.go +++ b/devslog_test.go @@ -679,21 +679,23 @@ func test_Struct(t *testing.T, o *Options) { logger := slog.New(NewHandler(w, o)) type StructTest struct { - Slice []int - Map map[int]int - Struct struct{ B bool } - SliceP *[]int - MapP *map[int]int - StructP *struct{ B bool } + Slice []int + Map map[int]int + Struct struct{ B bool } + SliceP *[]int + MapP *map[int]int + StructP *struct{ B bool } + unexported int } s := &StructTest{ - Slice: []int{}, - Map: map[int]int{}, - Struct: struct{ B bool }{}, - SliceP: &[]int{}, - MapP: &map[int]int{}, - StructP: &struct{ B bool }{}, + Slice: []int{}, + Map: map[int]int{}, + Struct: struct{ B bool }{}, + SliceP: &[]int{}, + MapP: &map[int]int{}, + StructP: &struct{ B bool }{}, + unexported: 5, } logger.Info("msg",