英文:
How can I make NodeJS's console.log always print output within a single line no matter what?
问题
抱歉,由于技术限制,我无法提供特定的代码翻译。
英文:
Is there any way to format the JSON logged through console.log
in the terminal?
I'm logging a lot of debug data and if the the logged data exceeds a certain length, the terminal logs it prettified in many lines. I'd like to change it to log in one line, no matter the length of the data. Is there any way to do that?
In summary, I want to change this log style:
[12:34:56][DEBUG][CODE] - {
data: {
action: 'action',
url: '/path/to/my/api?variableOne=valueOne&variableTwo=valueTwo'
}
}
To this log style:
[12:34:56][DEBUG][CODE] - { data: { action: 'action', url: '/path/to/my/api?variableOne=valueOne&variableTwo=valueTwo' } }
答案1
得分: 0
这是您要翻译的部分:
Is there any way to format the JSON logged through console.log in the terminal?
有的。创建一个自定义的控制台对象。查看文档以了解如何进行操作以及可以指定的选项。特别是,请查看inspectOptions
文档。
您要寻找的特定inspectOptions
选项是breaklength
和compact
:
breakLength
:<integer>
输入值在哪个长度上拆分成多行。将其设置为Infinity,以将输入格式化为单行(与compact
设置为true
或任何大于等于1的数字结合使用)。 默认值:80
。
compact
:<boolean>
|<integer>
将此设置为false
会导致每个对象键显示在新行上。对于超过breakLength
的文本,它将在新行上中断。如果设置为一个数字,只要所有属性适合breakLength
,就将大多数内部元素合并在一行上。短数组元素也会被组合在一起。有关更多信息,请参见下面的示例。 默认值:3
。
因此,由于您要求
我想将其更改为一行记录,无论数据的长度如何
那么您可能想要这样做:
const { Console } = require('node:console')
console = new Console({
stdout: process.stdout,
stderr: process.stderr,
// ignoreErrors, colorMode, groupIndentation
inspectOptions: {
// ...
breakLength: Infinity,
compact: true,
// ...
}
});
然后,您可以使用console.log
测试它,例如:console.log({a:1,b:2,c:3,hello:"world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"});
。
您还可以只对要生成格式化字符串的特定对象使用util.inspect函数,然后在默认的全局console
对象上使用console.log
,传递返回的字符串。
英文:
> Is there any way to format the JSON logged through console.log in the terminal?
Yes there is. Create a custom console object. See the docs for how to do that and what options you can specify. In particular, see also the inspectOptions
docs.
The particular inspectOptions
option you are looking for are breaklength
and compact
:
> breakLength
: <integer>
The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with compact
set to true
or any number >= 1
). Default: 80
.
>
> compact
: <boolean>
| <integer>
Setting this to false
causes each object key to be displayed on a new line. It will break on new lines in text that is longer than breakLength
. If set to a number, the most n
inner elements are united on a single line as long as all properties fit into breakLength
. Short array elements are also grouped together. For more information, see the example below. Default: 3
.
So since you asked
> I'd like to change it to log in one line, no matter the length of the data
Then you probably want to do something like this:
const { Console } = require('node:console')
console = new Console({
stdout: process.stdout,
stderr: process.stderr,
// ignoreErrors, colorMode, groupIndentation
inspectOptions: {
// ...
breakLength: Infinity,
compact: true,
// ...
}
});
And then you can test it with console.log({a:1,b:2,c:3,hello:"world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"});
.
You can also just use the util.inspect
function on specific objects you want to make a formatted string for, and then do console.log
on the default global console
object, passing the returned string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论