如何使node.js日志格式关闭parens以匹配开放的parens?

时间:2015-12-24 15:34:49

标签: node.js logging formatting parentheses

节点console.log(object)的最新版本返回:

{ top: 
  { child: 
    { anotherChild: 'someValue' } } }

如何让它返回更传统的方式:

{ 
  top: { 
    child: { 
      anotherChild: 'someValue'  
    } 
  } 
}

谢谢!

1 个答案:

答案 0 :(得分:0)

我可能误解了这个问题......是否有理由不能使用JSON.strinfigy格式化对象,然后console.log呢? JSON.stringify接受控制缩进级别的其他参数。

> a = { top: { child: { anotherChild: 'someValue' } } }
> console.log(JSON.stringify(a, null, 2))

{
  "top": {
    "child": {
      "anotherChild": "someValue"
    }
  }
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

  

JSON.stringify(value [,replacer [,space]])

特别是

  

space可选

     

用于将空白插入输出JSON字符串的String或Number对象,以便于阅读。如果这是一个数字,   它表示用作空格的空格字符数;   如果此数字大于10,则该数字的上限为10。值小于   1表示不应使用空格。如果这是一个String,那么   字符串(或字符串的前10个字符,如果它长于   那个)用作空白区域。如果未提供此参数(或是   null),没有使用空格。