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

fix: stack trace method #28

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions adapters/slog/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
github.com/danteay/golog/fields v0.1.0 h1:/W3Gh3PrVoJsY53sear+yzyLRkIMkM039lOfSNfUFj0=
github.com/danteay/golog/fields v0.1.0/go.mod h1:ACO2Sinx9OSYgwgUVTSZtWaT1q0VzHes6cDVgJtOS4Q=
github.com/danteay/golog/levels v0.1.0 h1:vJqNEKlij90z6b7PSq6bqxmV9KbKn1tj8nlkASl0r14=
github.com/danteay/golog/levels v0.1.0/go.mod h1:eWSbOC3D2TEvsl/Ngmyh1NngmX9ZNnywPTa5kVpN8Ew=
github.com/danteay/golog/levels v0.1.1 h1:cG6KT6bdmfZ7I6Q4TKGtQu+/SK2SY9FJTYzC6JBjzZo=
github.com/danteay/golog/levels v0.1.1/go.mod h1:eWSbOC3D2TEvsl/Ngmyh1NngmX9ZNnywPTa5kVpN8Ew=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
10 changes: 8 additions & 2 deletions adapters/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"io"
"log/slog"
"os"
"runtime/debug"
"strings"

"github.com/danteay/golog/fields"
"github.com/danteay/golog/internal/errors"
"github.com/danteay/golog/levels"
)

Expand Down Expand Up @@ -106,12 +107,17 @@ func getErrFields(level levels.Level, err error, curFields []any, withTrace bool
curFields = append(curFields, slog.Any("error", err))

if level == levels.TraceLevel || withTrace {
curFields = append(curFields, slog.Any("stacktrace", errors.GetStackTrace()))
curFields = append(curFields, slog.Any("stack", getStackTrace()))
}

return curFields
}

func getStackTrace() []string {
stack := strings.ReplaceAll(string(debug.Stack()), "\t", "")
return strings.Split(stack, "\n")
}

func getSlogInstance(level levels.Level, writer io.Writer) *slog.Logger {
handler := slog.NewJSONHandler(writer, &slog.HandlerOptions{
AddSource: false,
Expand Down