javascript - Node.js:如何解决终端 UI 中的异步日志重叠?

标签 javascript node.js logging asynchronous terminal

我正在尝试做什么

我正在构建一个node.js日志工具logminer了解日志/监控服务并使其过期。我发现了一个有趣的问题,但我还无法解决:

问题

假设我们想要在发出 HTTP 请求时记录客户端的 URL、IP 和位置,以查看用户在多功能栏中输入的内容(或出于任何原因)。

假设 URL 立即被记录,但对于 IP,我们执行异步请求(也许我们想根据 IP 获取位置)

// hypothetical http request handler
function httpRequestHandler(req, res){
    // first log (after 0 seconds)
    var log = new Log('Request')

    if(req.url = 'home'){
        // second log (after 0.1 seconds)
        log.message('welcome home')
        // ... do something
    } else {
        // second log (after 0.1 seconds);
        log.message('the url is', req.url)

        asyncFunction(req, function(value){
             // third log (after 5 seconds)
             log.message('the ip is ', value.ip)
             log.message('the location is ', value.location)
             // ... do something
        })
    }
}

输出很快就会变成这样:

(+0 秒)用户请求:http://example.com/other

Request
 in Request: welcome home

(+5 秒)用户请求:http://example.com/

Request
 in Request: the url is http://example.com/other
 in Request: the ip is 127.0.0.1
 in Request: the location is 'San Francisco'
<小时/>

进入此:

.

// at +0 second
Request
 in Request: welcome home

// at +1 second
Request
 in Request: the url is http://example.com/other

// at +2 second
Request
 in Request: welcome home

// at +3 second
Request
 in Request: welcome home


// at +5 second
 in Request: the ip is 127.0.0.1
 in Request: the location is 'San Francisco'

正如您所看到的,+5 秒会造成困惑,特别是因为它实际上看起来像这样:

Request
 in Request: welcome home
 in Request: the ip is 127.0.0.1
 in Request: the location is 'San Francisco'

这没有任何意义!第二行和第三行与第一行位于不同的范围内,这使得它们看起来像是相同的。对于许多并发用户来说,这是完全不可读的。这就是为什么我认为必须有一种方法来识别每行的起源,这就是为什么我在 logminer 中有一个 ID。在每一行中包含一个范围 ID 是可行的,但将各个点连接起来仍然非常困难,而且还占用大量屏幕空间。

logminer scope id

问题

现在我的问题是,是否可以根据其范围在异步环境中对事物进行分组,而无需 ID 或使用分页/过滤器而无需 GUI?

由于终端的线性特性,这是一个无法解决的设计问题,因此此时需要 GUI,还是我遗漏了一些东西?

处理这个问题的最佳方法是什么?

最佳答案

对此问题的通常解决方案是不要将消息分块输出,因为结果是异步的,这些消息在时间上是分开的。

相反,将整个消息的所有数据累积到一个字符串或各种变量中,然后立即输出整个消息。这将避免消息的某些部分被输出,然后稍后(在异步操作之后)输出消息的其他部分。

如果您想确保知道事件“何时”发生,请记录事件开始时的时间戳,并将其包含在消息中。

例如:

// hypothetical http request handler
function httpRequestHandler(req, res){
    // first log (after 0 seconds)
    var log = new Log('Request');

    if(req.url = 'home'){
        // second log (after 0.1 seconds)
        log.message('welcome home');
        // ... do something
    } else {
        asyncFunction(req, function(value){
            log.message('the url is', req.url);
            log.message('the ip is ', value.ip);
            log.message('the location is ', value.location);
            // ... do something
        })
    }
}

关于javascript - Node.js:如何解决终端 UI 中的异步日志重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270601/

相关文章:

javascript - AngularJS—— View 不会在我的浏览器中呈现?

javascript - 使用键盘向上/向下箭头导航表 <tr>

node.js - 按相关列值订购 Bookshelf.js 获取

javascript - 无法读取未定义的属性 `xxx`

.net - 记录日志的最佳方法是什么?

javascript - 如何使用 javascript 对重复对象的 ID 进行分组并删除数组中的对象

javascript - jQuery 委托(delegate)选择器问题

node.js - Node.js 的 Sequelize : cannot authenticate for postgresql

android - 手动从 Android 设备获取 LogCat

java - Spring 启动执行器 : can I change the logger level for entire package in one go?