如何使 NodeJS 的 console.log 无论如何都始终在单行中打印输出?

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

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选项是breaklengthcompact

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: &lt;integer&gt; 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: &lt;boolean&gt; | &lt;integer&gt; 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(&#39;node:console&#39;)
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:&quot;world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!&quot;});.

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.

huangapple
  • 本文由 发表于 2023年2月8日 21:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386491.html
匿名

发表评论

匿名网友

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

确定