Skip to content

Improve rendering of pointers to structs inside of slices and maps #186

@mbark

Description

@mbark

Description

It would be great to be able to render pointers to structs contained inside slices and maps better.

This could be done either by an improved rendering by default or through some way to configure the way that attributes are rendered.

I wrote a simple helper to compare devslog, log, slog with text formatter and slog with json formatter:

go run telnesstech.com/cmd
[13:58:41]  INFO  devslog
S slice: 1 []*main.testingStruct
    0: *main.testingStruct
      Name: slice1
S ptr  : *main.testingStruct
    Name: test
M map  : 1 map[string]*main.testingStruct
    key: main.testingStruct
      Name: test
2025/09/26 13:58:41 INFO charmbracelet/log slice=[0x140001260c0] ptr=&{Name:test} map=map[key:0x140001260e0]
time=2025-09-26T13:58:41.584+02:00 level=INFO msg=slog slice=[0x140001260c0] ptr=&{Name:test} map=map[key:0x140001260e0]
{"time":"2025-09-26T13:58:41.584822+02:00","level":"INFO","msg":"slog","slice":[{"Name":"slice1"}],"ptr":{"Name":"test"},"map":{"key":{"Name":"test"}}}

devslog does this by default which is very useful, especially as it mirrors what is likely to mirror the production-behavior when using json formatter. The relevant code from devslogs starts around here.

Another alternative would also be to have an option similar to slog's ReplaceAttr to allow the user themselves to configure how attributes are rendered, making this possible to implement yourself.

Code to print the demo above.

package main

import (
	"log/slog"
	"os"

	"github.com/charmbracelet/log"
	"github.com/golang-cz/devslog"
)

func main() {
	slice := []*testingStruct{{Name: "slice1"}}
	ptr := &testingStruct{Name: "test"}
	m := map[string]*testingStruct{"key": {Name: "test"}}

	slog.New(devslog.NewHandler(os.Stdout, nil)).With("slice", slice, "ptr", ptr, "map", m).Info("devslog")
	log.With("slice", slice, "ptr", ptr, "map", m).Info("charmbracelet/slog")
	slog.New(slog.NewTextHandler(os.Stdout, nil)).With("slice", slice, "ptr", ptr, "map", m).Info("slog")
	slog.New(slog.NewJSONHandler(os.Stdout, nil)).With("slice", slice, "ptr", ptr, "map", m).Info("slog")
}

type testingStruct struct {
	Name string
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions