在NodeJS的console.log每一行前添加制表符。

huangapple go评论63阅读模式
英文:

Add tabs at the beginning of every line in console.log for NodeJS

问题

I use console.log a lot in my code for better debugging. For example, in my cart.js file in the controllers folder, inside getProductsInCart function, I do:

controllers/cart.js

console.log('[controllers/cart.js].getProductsInCart', '\nproducts =', products);

which gives me the following result in the terminal:

[controllers/cart.js].getProductsInCart // <-- where `products` object is located 
        products = [
            {
                productId: new ObjectId("645a54116710e60e28b96903"),
                quantity: 1,
                _id: new ObjectId("645a6e2e119c195fd8b32f99")
            }
        ]

My products object is fetched from MongoDB. However, when I start having more and more functions and variables in different files, the log would be a bit hard to read.

Therefore, I expect every variable being logged in the terminal to look as follows:

[controllers/cart.js].getProductsInCart
        products = [
            {
                productId: new ObjectId("645a54116710e60e28b96903"),
                quantity: 1,
                _id: new ObjectId("645a6e2e119c195fd8b32f99")
            }
        ]

How can I add 2 tabs at the beginning of every line for my products value to look like that?

英文:

I use console.log a lot in my code for better debugging. For example, in my cart.js file in the controllers folder, inside getProductsInCart function, I do:

controllers/cart.js

console.log(&#39;[controllers/cart.js].getProductsInCart&#39;, &#39;\nproducts =&#39;, products);`

which gives me the following result in the terminal:

[controllers/cart.js].getProductsInCart // &lt;-- where `products` object is located 
products = [
  {
    productId: new ObjectId(&quot;645a54116710e60e28b96903&quot;),
    quantity: 1,
    _id: new ObjectId(&quot;645a6e2e119c195fd8b32f99&quot;)
  }
]

My products object is fetched from MongoDB. However, when I start having more and more functions and variables in different files, the log would be a bit hard to read.

Therefore, I expect every variable being logged in the terminal to look as follows:

[controllers/cart.js].getProductsInCart
        products = [
            {
                productId: new ObjectId(&quot;645a54116710e60e28b96903&quot;),
                quantity: 1,
                _id: new ObjectId(&quot;645a6e2e119c195fd8b32f99&quot;)
            }
        ]

How can I add 2 tabs at the beginning of every line for my products value to look like that?

答案1

得分: 2

以下是翻译好的部分:

如果您不想在项目中引入日志记录库,请考虑使用 JSON.stringify() 来为产品对象添加额外的缩进,或者使用 console.group()/console.groupEnd() 来为之间打印的所有行添加缩进,或者两者组合使用。

JSON.stringify() 的第三个参数会为结果字符串中每个对象嵌套级别添加一定数量的空格:

console.log('something', 'products =', JSON.stringify(products, null, 5))

需要注意的是,空格的数量会随着嵌套级别的增加而成倍增加。

console.group() / groupEnd() 允许您在它们之间缩进行:

console.group();
console.log('1'); // 缩进
console.groupEnd();
console.log('2'); // 不缩进

console.dir() 是另一个用于记录对象的有用工具。它为属性值添加颜色,通常有助于提高可读性。

英文:

If you're against bringing a logging library into your project, consider using JSON.stringify() to add extra indentation to the products object or console.group()/console.groupEnd() to add indentation to all lines printed in between, or the combination of both.

JSON.stringify()'s third parameter adds a number of spaces to each level of the object nesting in the resulting string:

console.log(&#39;something&#39;, &#39;products =&#39;, JSON.stringify(products, null, 5))

The caveat is that the number of spaces multiplies with each level of nesting.

console.group() / groupEnd() let you indent the lines between them:

console.group();
console.log(&#39;1&#39;); // indented
console.groupEnd();
console.log(&#39;2&#39;); // not indented

console.dir() is another useful tool for logging objects. It adds colors to property values, which usually helps readability.

huangapple
  • 本文由 发表于 2023年5月10日 20:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定