Skip to content

Commit

Permalink
Add a stacktrace-inducing template token
Browse files Browse the repository at this point in the history
Having `err` objects respond to `%+v` is quite widespread
within the golang ecosystem. Add a logger template unit
supporting this behavior.
  • Loading branch information
ribasushi committed Apr 29, 2024
1 parent 226e4f0 commit 4d65bf7
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions middleware/logger.go
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
Expand Down Expand Up @@ -41,6 +42,7 @@ type (
// - user_agent
// - status
// - error
// - error_stacktrace (err passed through Sprintf's '%+v')
// - latency (In nanoseconds)
// - latency_human (Human readable)
// - bytes_in (Bytes received)
Expand Down Expand Up @@ -198,8 +200,12 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
if err != nil {
// Error may contain invalid JSON e.g. `"`
b, _ := json.Marshal(err.Error())
b = b[1 : len(b)-1]
return buf.Write(b)
return buf.Write(b[1 : len(b)-1])
}
case "error_stacktrace":
if err != nil {
b, _ := json.Marshal(fmt.Sprintf("%+v", err))
return buf.Write(b[1 : len(b)-1])
}
case "latency":
l := stop.Sub(start)
Expand Down

0 comments on commit 4d65bf7

Please sign in to comment.