Is there a way to avoid logging certain HTTP requests?

xuanbao · · 615 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Here&#39;s what the code looks like that starts my server:</p> <pre><code>func startServer() { router := getRouter() port := getPort() log.Println(&#34;Starting up app on port &#34; + port + &#34;...&#34;) log.Fatal(http.ListenAndServe(&#34;:&#34;+port, handlers.LoggingHandler(os.Stdout, router))) } </code></pre> <p>The issue is that I have a health check route that&#39;s being pinged every 10 seconds to make sure it&#39;s alive, but I don&#39;t want the app to log that request to stdout because then our logging service picks it up. I need all other routes logged normally though. Is there a way to say &#34;hey, don&#39;t log anything for this one route&#34;?</p> <hr/>**评论:**<br/><br/>shovelpost: <pre><p>You either need to add <code>handlers.LoggingHandler()</code> in all routes except the one that needs to avoid logging or add special code in <code>handlers.LoggingHandler()</code> to recognize the route that needs to avoid logging.</p></pre>aminoglycine: <pre><p>Ah, thank you. I should have checked gorilla&#39;s handlers readme, it had an example right in there: <a href="https://github.com/gorilla/handlers#example" rel="nofollow">https://github.com/gorilla/handlers#example</a></p></pre>mcouturier: <pre><p>Or register your health check handler as a Middleware just before your logging Middleware. Easy.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

615 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传