如何通过重写以JSON格式输出

How can I output this in JSON format by overriding?

本文关键字:格式 输出 JSON 何通过 重写      更新时间:2023-09-26

我正在尝试使用覆盖来控制日志,但我得到错误。代码工作良好,当我删除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();
});

将泛型对象转换为真正的JSON:

JSON.parse(JSON.stringify(my_generic_object))