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.

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

报告相同问题?

悬赏问题

  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算