<p>Do you instantiate a new logger within the HandlerFunc? Do you use a global logger (Zap's FAQ discourages this)? Do you write a wrapper around the HandlerFunc that lets you pass in a logger instance from main?</p>
<hr/>**评论:**<br/><br/>iygwaz: <pre><p>Followup question: Is there any way to group logs from a single request with some sort of reference number? I was thinking of using a goroutine ID or something like that but apparently you can't access them (<a href="https://stackoverflow.com/questions/19115273/looking-for-a-call-or-thread-id-to-use-for-logging" rel="nofollow">https://stackoverflow.com/questions/19115273/looking-for-a-call-or-thread-id-to-use-for-logging</a>)</p></pre>Rafikithewd: <pre><p>You can generate a request id and add it to the request context </p>
<p>Example: <a href="https://joeshaw.org/revisiting-context-and-http-handler-for-go-17/" rel="nofollow">https://joeshaw.org/revisiting-context-and-http-handler-for-go-17/</a></p></pre>Rafikithewd: <pre><p>I personally use either logrus or apex/log for my projects </p>
<p>They have a singleton approach so you can configure the logger in main() but to use it later you just have to import the package </p>
<p><a href="https://medium.com/@47fd6a5307cc/e8d9627f4a9a" rel="nofollow">https://medium.com/@47fd6a5307cc/e8d9627f4a9a</a></p></pre>quiI: <pre><p>Logging is just like any other dependency, like a `FooService`</p>
<p>Use DI to pass in your dependencies, then they are easy to test and dont rely on global variables.</p>
<p>This might help <a href="https://quii.gitbook.io/learn-go-with-tests/standard-lib-essentials/http-server" rel="nofollow">https://quii.gitbook.io/learn-go-with-tests/standard-lib-essentials/http-server</a> </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传