diff --git a/examples/api/README.md b/examples/api/README.md index dd43d0d..a7ea347 100644 --- a/examples/api/README.md +++ b/examples/api/README.md @@ -22,18 +22,22 @@ curl http://localhost:3333/users/123 # will return a user in JSON Running each curl should produce the following logs ``` 2023/09/09 11:05:06 ERROR - db error: connection lost - examples/api/main.go:53 - Could not get user - examples/api/main.go:54 +db error: connection lost + examples/api/main.go:53 +Could not get user + examples/api/main.go:54 + + examples/api/main.go:54 http_method=GET user_id=999 request_id=It73FDo3WC-000005 request_path=/users/999 remote_ip=127.0.0.1:52246 protocol=HTTP/1.1 error="Could not get user: db error: connection lost" 2023/09/09 11:05:06 INFO API Request request_id=It73FDo3WC-000005 request_path=/users/999 remote_ip=127.0.0.1:52246 protocol=HTTP/1.1 http_method=GET status=500 latency=96.25µs 2023/09/09 11:05:25 ERROR - db error: user id[321] not found - examples/api/main.go:62 - User not found - examples/api/main.go:63 +db error: user id[321] not found + examples/api/main.go:62 +User not found + examples/api/main.go:63 + + examples/api/main.go:63 http_method=GET user_id=321 request_path=/users/321 remote_ip=127.0.0.1:52246 request_id=It73FDo3WC-000006 protocol=HTTP/1.1 error="User not found: db error: user id[321] not found" 2023/09/09 11:05:25 INFO API Request protocol=HTTP/1.1 http_method=GET request_path=/users/321 remote_ip=127.0.0.1:52246 request_id=It73FDo3WC-000006 status=404 latency=65.306µs diff --git a/examples/api/http/http.go b/examples/api/http/http.go index db7cb5e..44e27dc 100644 --- a/examples/api/http/http.go +++ b/examples/api/http/http.go @@ -3,7 +3,6 @@ package http import ( "context" "fmt" - "github.com/Southclaws/fault" "github.com/Southclaws/fault/fctx" "github.com/Southclaws/fault/fmsg" "github.com/Southclaws/fault/ftag" @@ -11,7 +10,6 @@ import ( "github.com/go-chi/chi/v5/middleware" "log/slog" "net/http" - "strings" "time" ) @@ -70,27 +68,6 @@ func LoggerRequest(logger *slog.Logger) func(next http.Handler) http.Handler { } -func isInternalString(s string) bool { - return strings.HasPrefix(s, "<") && strings.HasSuffix(s, ">") -} - -func toStackTrace(err error) string { - var sb strings.Builder - u := fault.Flatten(err) - for _, v := range u { - if isInternalString(v.Message) { - continue - } - if v.Message != "" { - sb.WriteString(fmt.Sprintf("\t%s\n", v.Message)) - } - if v.Location != "" { - sb.WriteString(fmt.Sprintf("\t\t%s\n", v.Location)) - } - } - return sb.String() -} - func RespondWithError( logger *slog.Logger, err error, @@ -100,7 +77,7 @@ func RespondWithError( tag := ftag.Get(err) attrs := fctxToSlog(r.Context()) - errStr := toStackTrace(err) + errStr := fmt.Sprintf("%+v", err) attrs = append(attrs, slog.String("error", err.Error())) logger.Error("\n"+errStr, attrs...) diff --git a/examples/api/main.go b/examples/api/main.go index 2bd6913..458b0c0 100644 --- a/examples/api/main.go +++ b/examples/api/main.go @@ -81,5 +81,6 @@ func main() { r.Get("/", GetUser) }) + fmt.Printf("Listening on :3333 ...\n") http.ListenAndServe(":3333", r) }