dongzhe3573 2016-08-15 21:26
浏览 22
已采纳

虹膜-如何记录错误/异常?

I'm testing a Go application using Iris. I want to log every error or exception to my Rollbar account.

For instance, if the endpoint takes too long to respond and there is a timeout, I want to log it. How can I capture errors like that?

Update

I found in the documentation the OnError method, and I thought I could use it like this:

iris.OnError(iris.StatusServiceUnavailable, func(c *iris.Context) {
    c.Write("503")
    params := string(c.RequestCtx.Request.Body())
    rollbar.Error("error", errors.New("503 Service Unavailable"), &rollbar.Field{Name: "request_body", Data: params})
})

But it is not logging the error to Rollbar.

  • 写回答

1条回答 默认 最新

  • dongroufan6846 2016-08-15 22:48
    关注

    You'll need to write your own iris middleware.

    Should look something like this:

    func irisMiddlewareFunc(ctx *iris.Context) {
        startedAt := time.Now()
        ctx.Next()
        timeTaken := time.Since(startedAt)
    
        // now check time taken and log as required
        if timeTaken.Seconds() > 2 {
            fmt.Println("Taken too long")
        }
    }
    

    The you use it like this:

        iris.UseFunc(irisMiddlewareFunc)
    

    And if you need to handle panics as well, just use recover() as you would usually do to handle panics. See github.com/iris-contrib/middleware/recovery for an example.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度