@@ -56,19 +56,20 @@ func (h *handler) Handle(ctx context.Context, record slog.Record) error {
5656 h .formatTime (ctx , record , builder , options )
5757 h .formatLevel (ctx , record , builder , options )
5858 h .formatCaller (ctx , record , builder , options )
59+ h .formatGroup (ctx , record , builder , options )
5960 h .formatMessage (ctx , record , builder , options )
6061
6162 // fixed attrs
6263 num := record .NumAttrs ()
6364 fixedNum := len (h .attrs )
6465 for i , attr := range h .attrs {
65- h .formatAttr (ctx , h . group , record .Level , attr , builder , num + fixedNum == i + 1 , options )
66+ h .formatAttr (ctx , record .Level , attr , builder , num + fixedNum == i + 1 , options )
6667 }
6768
6869 idx := 0
6970 record .Attrs (func (attr slog.Attr ) bool {
7071 idx ++
71- h .formatAttr (ctx , h . group , record .Level , attr , builder , num == idx , options )
72+ h .formatAttr (ctx , record .Level , attr , builder , num == idx , options )
7273 return true
7374 })
7475
@@ -89,7 +90,11 @@ func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {
8990
9091func (h * handler ) WithGroup (name string ) slog.Handler {
9192 n := h .clone ()
92- n .group = name
93+ if h .group == "" {
94+ n .group = name
95+ } else {
96+ n .group = fmt .Sprintf ("%s.%s" , h .group , name )
97+ }
9398 return n
9499}
95100
@@ -161,6 +166,18 @@ func (h *handler) formatCaller(ctx context.Context, record slog.Record, builder
161166 Write (' ' )
162167}
163168
169+ func (h * handler ) formatGroup (ctx context.Context , record slog.Record , builder * colorbuilder.Builder , options LoggerOptionsFetcher ) {
170+ if h .group == "" {
171+ return
172+ }
173+
174+ h .loadAttrKeyWithOptions (builder , AttrKeyMessage , options )
175+ h .loadColorWithOptions (builder , ColorTypeMessage , options ).
176+ WriteString (h .group ).
177+ DisableColor ().
178+ WriteString (" " )
179+ }
180+
164181func (h * handler ) formatMessage (ctx context.Context , record slog.Record , builder * colorbuilder.Builder , options LoggerOptionsFetcher ) {
165182 if record .Message == "" {
166183 return
@@ -177,17 +194,13 @@ func (h *handler) formatMessage(ctx context.Context, record slog.Record, builder
177194 Write (' ' )
178195}
179196
180- func (h * handler ) formatAttr (ctx context.Context , group string , level slog.Level , attr slog.Attr , builder * colorbuilder.Builder , last bool , options LoggerOptionsFetcher ) {
181- var key = attr .Key
182- if group != "" {
183- key = group + "." + key
184- }
197+ func (h * handler ) formatAttr (ctx context.Context , level slog.Level , attr slog.Attr , builder * colorbuilder.Builder , last bool , options LoggerOptionsFetcher ) {
185198
186199 switch attr .Value .Kind () {
187200 case slog .KindGroup :
188201 groupAttr := attr .Value .Group ()
189202 for _ , a := range groupAttr {
190- h .formatAttr (ctx , key , level , a , builder , last , options )
203+ h .formatAttr (ctx , level , a , builder , last , options )
191204 }
192205 return
193206 default :
@@ -209,18 +222,18 @@ func (h *handler) formatAttr(ctx context.Context, group string, level slog.Level
209222 }
210223 }
211224 attr = slog .Group (attr .Key , slog .Any ("info" , stackError {v }), slog .Any ("stack" , stacks ))
212- h .formatAttr (ctx , group , level , attr , builder , false , options )
225+ h .formatAttr (ctx , level , attr , builder , false , options )
213226 return
214227 }
215228 h .loadColorWithOptions (builder , ColorTypeAttrErrorKey , options )
216229 }
217230 }
218231
219232 builder .
220- WriteString (key ).
233+ WriteString (attr . Key ).
221234 SetColor (options .FetchColorType (ColorTypeAttrDelimiter )).
222235 WriteString (options .FetchDelimiter ())
223- h .formatAttrValue (ctx , level , key , attr , builder , last , options )
236+ h .formatAttrValue (ctx , level , attr . Key , attr , builder , last , options )
224237}
225238
226239func (h * handler ) formatAttrValue (ctx context.Context , level slog.Level , fullKey string , attr slog.Attr , builder * colorbuilder.Builder , last bool , options LoggerOptionsFetcher ) {
0 commit comments