如何插入 socket.io 的内置日志系统来生成我自己的消息?

发布于 2024-12-07 17:15:34 字数 460 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你穿错了嫁妆 2024-12-14 17:15:34

在使用 socket.io 时,我能够像这样插入现有的记录器模块:

var express = require('express'),
    app     = module.exports = express.createServer(), //just creating 'app' for io
    io      = require('socket.io').listen(app),
    logger  = io.log, // access the existing logger setup in socket.io
    util    = require('util');

logger.info(util.format("Express server listening on port %d in %s mode", 8003, app.settings.env));

配置记录器也非常简单:

io.configure('production', function(){
  io.set('log level', 1);
}

While using socket.io, I was able to plug into the existing logger module like so:

var express = require('express'),
    app     = module.exports = express.createServer(), //just creating 'app' for io
    io      = require('socket.io').listen(app),
    logger  = io.log, // access the existing logger setup in socket.io
    util    = require('util');

logger.info(util.format("Express server listening on port %d in %s mode", 8003, app.settings.env));

Configuring the logger is also very simple:

io.configure('production', function(){
  io.set('log level', 1);
}
小清晰的声音 2024-12-14 17:15:34

您是否考虑过使用 Connect 中的记录器中间件?看起来有人已经为您想要的内容创建了一个名为 socket.IO-connect 的库。我在我的 Express 程序中使用类似的东西:

var connect = require('connect');

module.exports = connect.createServer(
    connect.logger({ format: ':response-time :method :url' }),
    connect.static(__dirname + '/public)
);

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:

var connect = require('connect');

module.exports = connect.createServer(
    connect.logger({ format: ':response-time :method :url' }),
    connect.static(__dirname + '/public)
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文