英文:
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('[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?
答案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('something', 'products =', 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('1'); // indented
console.groupEnd();
console.log('2'); // not indented
console.dir() is another useful tool for logging objects. It adds colors to property values, which usually helps readability.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论