如何插入 socket.io 的内置日志系统来生成我自己的消息?
socket.io
似乎对其所有内部结构都有一个基本合理的日志系统。我如何自己获取该日志记录对象,以便可以在适当的级别生成自己的日志消息?令我烦恼的是,我的 console.log()
消息没有时间戳、未分级,而且与 socket.io
消息相比很难看。我在 socket.io
代码中做了很多探索,但目前我对 Node 还不够了解,无法理解对象层次结构是什么样的,也不知道如何获取对象我想从我的代码中得到。
从长远来看,我可能需要一个更强大的日志系统模块(能够记录到文件、自动轮换、按模块管理级别、自定义日志级别等)。 Winston 看起来很合理,但我也可以让 socket.io
使用它吗?如果一切都集中在一处就好了。
socket.io
seems to have a basically sensible logging system for all its internals. How do I get at that logging object myself so I can generate my own log messages at appropriate levels? It bugs me that my console.log()
messages are un-timestamped, un-leveled, and ugly next to the socket.io
messages. I've done a bunch of spelunking in the socket.io
code and I'm just not savvy enough about node at this point to understand what the object hierarchies look like to know how to get at the object I want from my code.
Longer term, I'm probably going to want a more robust logging system module (with the ability to log to files, auto-rotate, manage levels on a per-module basis, custom log levels, etc.). Winston looks sensible, but can I get socket.io
to use it, too? It'd be nice to have everything in one place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在使用 socket.io 时,我能够像这样插入现有的记录器模块:
配置记录器也非常简单:
While using socket.io, I was able to plug into the existing logger module like so:
Configuring the logger is also very simple:
您是否考虑过使用 Connect 中的记录器中间件?看起来有人已经为您想要的内容创建了一个名为 socket.IO-connect 的库。我在我的 Express 程序中使用类似的东西:
Have you looked into using the logger middleware from Connect? It looks like someone already created a library for what you want called socket.IO-connect. I use something similar in my Express program: