Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement slog.LogValuer #129

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nozo-moto
Copy link

@nozo-moto nozo-moto commented Sep 18, 2023

#107

This change is Reviewable

code
package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/cockroachdb/errors"
)

func f1() error {
	return errors.Wrap(f2(), "wrapped error 1")
}

func f2() error {
	return errors.Wrap(f3(), "wrapped error 2")
}

func f3() error {
	return errors.Wrap(f4(), "wrapped error 3")
}

func f4() error {
	return errors.Wrap(f5(), "wrapped error 4")
}

func f5() error {
	return errors.New("error root")
}

func main() {
	ctx := context.Background()
	err := f1()

	logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
	logger.InfoContext(ctx, "test", slog.Any("error", err))
}
result
{
  "time": "2023-09-18T17:57:09.438517+09:00",
  "level": "INFO",
  "msg": "test",
  "error": {
    "message": "wrapped error 1: wrapped error 2: wrapped error 3: wrapped error 4: error root",
    "stacktrace": {
      "main.f1": {
        "file": "/Users/nozo/tmp/cockloaddb-slog-try/main.go",
        "name": "main.f1",
        "line": 12
      },
      "main.main": {
        "file": "/Users/nozo/tmp/cockloaddb-slog-try/main.go",
        "name": "main.main",
        "line": 33
      },
      "runtime.main": {
        "file": "/opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/proc.go",
        "name": "runtime.main",
        "line": 267
      },
      "runtime.goexit": {
        "file": "/opt/homebrew/Cellar/go/1.21.1/libexec/src/runtime/asm_arm64.s",
        "name": "runtime.goexit",
        "line": 1197
      }
    }
  }
}

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Sep 18, 2023

CLA assistant check
All committers have signed the CLA.

@nozo-moto nozo-moto changed the title implement LogValue method to WithStack Implement slog.LogValuer Sep 18, 2023
@nozo-moto nozo-moto force-pushed the implment-logvalue-method-to-withstack branch from adf5647 to 556c210 Compare September 18, 2023 09:00
@nozo-moto nozo-moto marked this pull request as ready for review September 18, 2023 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants