Skip to content

log.Error() mishandles error printing #170

@thomas-maurice

Description

@thomas-maurice

Describe the bug

The log.Error function misprints error messages using the JSONFormatter, when called like log.Error("some message", "error", err) the error field of the log is empty.

However it seems to work properly when using the TextFormatter

Setup

Please complete the following information along with version numbers, if applicable.

  • macOS
  • Ghostty
  • zsh

To Reproduce

Create a main.go with the snippet below, run it with go run main.go. You will see the following output:

{"time":"2025-05-12T11:49:59+02:00","level":"error","caller":"charmslog/main.go:21","msg":"some message","error":{}}

When what would have been expected is

{"time":"2025-05-12T11:49:59+02:00","level":"error","caller":"charmslog/main.go:21","msg":"some message","error":"some error happened"}

Notice in the second line the "error": {} field.

Source Code

package main

import (
        "fmt"
        "log/slog"
        "os"
        "time"

        "github.com/charmbracelet/log"
)

func main() {
        l := log.NewWithOptions(os.Stderr, log.Options{
                TimeFormat:      time.RFC3339,
                ReportTimestamp: true,
                ReportCaller:    true,
                Formatter:       log.JSONFormatter,
        })

        logger := slog.New(l)
        logger.Error("some message", "error", fmt.Errorf("some error happened"))
}

Expected behavior

The error should be printed in the error field

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions