如何在 Nodejs 调试控制台视图中更改对象的字符串表示

How to change string representation of objects in Nodejs debug console view

如何在 nodejs 调试控制台中更改对象实例的字符串表示形式。有没有我可以覆盖的方法(如 .NET 中的 toString())?

考虑以下代码:

class SomeObject{
    constructor(){
        this._varA = "some text";
        this._varB = 12345;
        this._varC = "some more text";
        this._varD = true;
        this._varE = 0.45;
    }

    toString(){
        return "custom textual rapresentation of my object";
    }
}

var array = [];

array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());

console.log(array);

这会产生以下结果:

然而,在我工作过的其他环境和编程语言中,重写 toString() 方法将显示 toString() 的结果(在上面的示例中 "custom textual representation of my object")而不是动态由调试器创建的文本表示(在上面的示例代码中是:SomeObject {_varA: "some text", _varB: 12345, _varC: "some more text", …})——我毫不怀疑它在未定义自定义替代项时非常有用。

我也意识到 console.log(array.toString()); 甚至 console.log(array.map(t=>t.toString())); 会产生类似于我所追求的东西,但是这会阻止我使用调试导航即浏览对象。深入对象图。

如果这不可能,其他人会从中受益吗?如果有足够的兴趣,我可以考虑将其定义和实现为一个功能。

有一个可以在另一个字符串上调用的 toString() 方法。

terms[200]._text.toString()

您可能也在寻找 JSON.stringify(),我发现它在调试中非常有用。由于 JavaScript 对象字面意思是 JSON,这将使将它们打印到控制台更简单。

console.log(JSON.stringify(terms[200]))

当您执行 console.log 时,它会在 util.js 内部调用 formatValue,它有一个下面的检查

const maybeCustomInspect = value[customInspectSymbol] || value.inspect;

这意味着如果您的值有一个 inspect 方法,它会被调用,然后您可以 return 和 toString 相同。因此,将您的代码更改为

class SomeObject{
    constructor(){
        this._varA = "some text";
        this._varB = 12345;
        this._varC = "some more text";
        this._varD = true;
        this._varE = 0.45;
    }

    inspect(depth, opts) {
        return this.toString();
    }

    toString(){
        return "custom textual rapresentation of my object";
    }
}

var array = [];

array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());

console.log(array);

打印出来

[ custom textual rapresentation of my object,
  custom textual rapresentation of my object,
  custom textual rapresentation of my object ]

Nodejs 也有相同的文档

https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_custom_inspection_functions_on_objects

根据文档,我使用的 inspect 方法已弃用,正确的方法如下

const util = require('util');

class SomeObject{
    constructor(){
        this._varA = "some text";
        this._varB = 12345;
        this._varC = "some more text";
        this._varD = true;
        this._varE = 0.45;
    }

    [util.inspect.custom](depth, options) {
        return this.toString();
    }

    toString(){
        return "custom textual rapresentation of my object";
    }
}

var array = [];

array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());

console.log(array);

编辑:2018 年 3 月 28 日

所以我使用下面的方法启动了脚本

$ node --inspect-brk=54223 test.js 
Debugger listening on ws://127.0.0.1:54223/81094440-716b-42a5-895e-4ea2008e0dff
For help see https://nodejs.org/en/docs/inspector

然后运行一个socat转发器使用下面

$ socat -v TCP-LISTEN:54222,fork TCP:127.0.0.1:54223

当您在调试器中调试变量 array 时,您会在 socat 终端

上得到以下输出

信息是由调试器重建的,可以为您提供有意义的表示。

现在 v8 调试 api 不知道我们想要以不同的方式表示它,就像我们对 console.log 所做的那样。现在 V8 代码中可能有类似的东西做类似的事情,但是查看源代码,我无法弄清楚是否存在这样的事情。因此,您可能需要向具有 V8 调试器 api 知识的人确认,是否存在此类内容

如果不是,您需要在 IDE 级别构建一些东西,这又不是一件容易的事

我的两分钱: 如何覆盖 console.log 函数来做你想做的事。 这是 POC,它需要在任何对象中使用 _toString 函数来更改它在日志中的显示方式。

创建一个包含以下内容的文件logger.js:

const getUpdatedLogObj = function(x){


    if(x && typeof x == 'object'){
            if(typeof x._toString === 'function'){
                return x._toString()
            } else {
                for(let i in x){
                    x[i] = getUpdatedLogObj(x[i])
                }
            }
        }
        return x;
    }



    console._log = console.log
    console.log = function(x){console._log(getUpdatedLogObj({...x}))}

将其导入 index.js

require('./logger')

console.log({a: 1, b: 2, c: {d: 5, e: 6, _toString: function(){return 'fromToString'}}})

而且您仍然可以获得导航:

const util = require('util');
class SomeObject{
constructor(){
    this._varA = "some text";
    this._varB = 12345;
    this._varC = "some more text";
    this._varD = true;
    this._varE = 0.45;
}

[util.inspect.custom](depth, options) {
    return this.toString();
}

toString(){
    return "custom textual rapresentation of my object";
}
}

var array = [];

array.push(new SomeObject());
array.push(new SomeObject());
array.push(new SomeObject());

console.log(array);

VS Code 中添加了一个新选项,可以调整调试器输出:只需将以下内容添加到您的启动配置中

"customDescriptionGenerator": "function (def) { if (this.toString) { const _v = this.toString(); if (_v.indexOf(\"[object Object]\") < 0) return _v; } return def; }",

Viola:您的实体在 watch 中使用“toString”查看,保留向下钻取等功能