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, 2023
1 parent 0d47b7e commit bc358b8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions middleware/logger.go
Expand Up @@ -41,6 +41,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 +199,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 bc358b8

Please sign in to comment.