Skip to content

Commit

Permalink
Merge pull request #1 from sdual/fix-comment
Browse files Browse the repository at this point in the history
Add comment
  • Loading branch information
sdual authored Sep 5, 2024
2 parents 5b8ae4b + 752e2eb commit 9989eaf
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,31 @@
# roachslog

[![Go](https://github.com/sdual/roachslog/actions/workflows/go-test.yml/badge.svg)](https://github.com/sdual/roachslog/actions/workflows/go-test.yml)

## Examples

```go
package main

import (
"log/slog"
"os"

"github.com/cockroachdb/errors"
"github.com/sdual/roachslog"
)

func main() {
ops := slog.HandlerOptions{
AddSource: true,
Level: slog.LevelInfo,
}

handler := slog.NewJSONHandler(os.Stdout, &ops)
rsHandler := roachslog.NewReachSlogHandler(handler)
slog.SetDefault(slog.New(rsHandler))

err := errors.New("error")
slog.Error("error occurred", roachslog.Err(err))
}
```
11 changes: 11 additions & 0 deletions arr_attr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package roachslog

import (
"log/slog"
)

const errAttrKey = "error"

func Err(err error) slog.Attr {
return slog.Any(errAttrKey, err)
}
41 changes: 13 additions & 28 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,29 @@ import (
"github.com/cockroachdb/errors"
)

const (
defaultErrorAttrKey = "error"
defaultStactraceAttrKey = "stacktrace"
)
const defaultStactraceAttrKey = "stacktrace"

type (
// RoachSlogHandler is the slog handler to format stacktrace of logs cockroachdb/errors.
RoachSlogHandler struct {
handler slog.Handler
errAttrKey string
stacktraceKey string
}

opt func(*RoachSlogHandler)
)

// NewReachSlogHandler creates a new RoachSlogHandler instance.
func NewReachSlogHandler(handler slog.Handler, opts ...opt) RoachSlogHandler {

func NewReachSlogHandler(handler slog.Handler, opts ...opt) slog.Handler {
rs := &RoachSlogHandler{
handler: handler,
errAttrKey: defaultErrorAttrKey,
stacktraceKey: defaultStactraceAttrKey,
}

for _, option := range opts {
option(rs)
}
return *rs
}

func WithErrorAttrKey(key string) opt {
return func(rs *RoachSlogHandler) {
rs.errAttrKey = key
}
return rs
}

func WithStacktraceAttrKey(key string) opt {
Expand All @@ -56,22 +45,18 @@ func (rs *RoachSlogHandler) Enabled(ctx context.Context, level slog.Level) bool
func (rs *RoachSlogHandler) Handle(ctx context.Context, record slog.Record) error {
var stacktrace string
record.Attrs(func(attr slog.Attr) bool {
if attr.Key == rs.errAttrKey {
err, ok := attr.Value.Any().(error)
// If the cast to type error fails, no information about the error is logged.
if attr.Key == errAttrKey {
// If the cast to error data type fails, no information about the error is logged.
// The value of the error data type should be available as the value corresponding to the specified key (errAttrKey).
if !ok {
return false
}
stacktrace, ok = extractStacktrace(err)
if !ok {
return false
if err, ok := attr.Value.Any().(error); ok {
stacktrace = extractStacktrace(err)
record.AddAttrs(slog.String(rs.stacktraceKey, stacktrace))
}
return false
}
return true
})

record.AddAttrs(slog.String(rs.stacktraceKey, stacktrace))
return rs.handler.Handle(ctx, record)
}

Expand All @@ -82,10 +67,10 @@ func (rs *RoachSlogHandler) WithGroup(g string) slog.Handler {
return &RoachSlogHandler{handler: rs.handler.WithGroup(g)}
}

func extractStacktrace(err error) (string, bool) {
func extractStacktrace(err error) string {
safeDetails := errors.GetSafeDetails(err).SafeDetails
if len(safeDetails) > 0 {
return safeDetails[0], true
return safeDetails[0]
}
return "", false
return ""
}

0 comments on commit 9989eaf

Please sign in to comment.