【问题标题】:How can I output this in JSON format by overriding?如何通过覆盖以 JSON 格式输出?
【发布时间】:2015-10-17 12:21:43
【问题描述】:

我正在尝试使用覆盖来控制台记录此内容,但出现错误。当我删除 JSON.stringify(arguments[0]) 时,代码可以正常工作,但我需要 JSON 格式的代码。

app.use(function(req, res, next) {
    var t_log= console.log;

    console.log = function(){
        arguments[0] = JSON.stringify(arguments[0])+" sessionID " + req.sessionID + " requestID=" + req.id +" timestamp"+ new Date();
        t_log.apply(console, arguments);

    }
    console.log("call");

    next();

});

【问题讨论】:

  • arguments[0] 的开头是什么? arguments.length 是什么?你遇到了什么错误?
  • @Paul 我在控制台中得到了无限的 '\'。

标签: javascript json node.js express


【解决方案1】:

将通用对象转换为真正的 JSON:

JSON.parse(JSON.stringify(my_generic_object))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 2019-02-21
    • 2021-06-26
    • 2020-06-15
    • 1970-01-01
    相关资源
    最近更新 更多