在go-json-rest中关闭访问日志记录器。

huangapple go评论76阅读模式
英文:

Turn off access logger in go-json-rest

问题

我正在尝试使用Go编写REST服务。由于Go有不同的框架可以用来编写REST服务,所以我们想在选择框架之前进行一些性能测试。

目前我使用了go-json-rest来做一个示例。但是当我执行这个示例的REST请求时,我在终端上看到以下日志打印出来。

24/Nov/2015:12:30:35 +0530 200 29μs "GET /countries HTTP/1.1" - "Java/1.8.0_45"

这个日志记录会花费一些时间,并影响我的性能报告。我该如何关闭这个日志记录呢?

英文:

I am trying to write REST services in Go. As there are different frameworks for writing REST service in Go, we would like to do some performance testing before choosing the framework.

Currently I did a sample using go-json-rest.
But when I execute this sample REST request, I see the following logs printed in my terminal.

24/Nov/2015:12:30:35 +0530 200 29μs "GET /countries HTTP/1.1" - "Java/1.8.0_45"

This logging will take some time and impact my performance report.
How can I switch of this logging?

答案1

得分: 1

你应该知道,控制日志记录的是AccessLogApacheMiddleware,而DefaultDevStack包含AccessLogApacheMiddleware

阻止日志记录的两种方法:

  1. 可以使用预定义的变量,例如DefaultCommonStack,它不包含AccessLogApacheMiddleware
  2. 构建自己的...Middlewares。由于Use函数是一个可变参数函数,可以将多个中间件传递给该函数。如果不需要AccessLogApacheMiddleware,可以忽略它。这样可以更好地控制所需的中间件。

例如:

api := rest.NewApi()
api.Use(&rest.TimerMiddleware{},
    &rest.RecorderMiddleware{},
    &rest.PoweredByMiddleware{},
    &rest.RecoverMiddleware{})

希望对你有所帮助。

英文:

You should know that it's AccessLogApacheMiddleware that controls your logging, and DefaultDevStack contains the AccessLogApacheMiddleware.

Two ways to prevent logging:

  1. You can use a predefined variable like DefaultCommonStack which doesn't contain the AccessLogApacheMiddleware;
  2. Construct your own ...Middlewares. Since the Use function is a Variadic Functions, which pass the multiple middlewares into the function. If you don't need AccessLogApacheMiddleware, you just ignore it. It gives you more power to control whatever middlewares you like.

e.g.

api := rest.NewApi()
api.Use(&rest.TimerMiddleware{},
    &rest.RecorderMiddleware{},
    &rest.PoweredByMiddleware{},
    &rest.RecoverMiddleware{})

May this be helpful.

huangapple
  • 本文由 发表于 2015年11月24日 15:24:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/33887656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定