node.js console.log没有新行[重复]

时间:2021-07-08 16:55:08

This question already has an answer here:

这个问题在这里已有答案:

For logging to console for node.js, currently I do

要记录到node.js的控制台,目前我这样做

var log = function(msg)
{
  var util = require('util');
  console.log('some prefix(like "core-module")---------'),
  console.log(util.inspect(msg,
  {
    depth: 99,
    colors: true
  }));
};

In this manner, object depth is logged correctly. If I do,

以这种方式,正确记录对象深度。如果我做,

console.log('some-module: '+ 
util.inspect(msg,
      {
        depth: 99,
        colors: true
      })
) 

Although this is logged as a single line, but since the content becomes String, the object depth is presented merely [object] which is not good.

虽然这是作为单行记录的,但由于内容变为String,因此对象深度仅仅是[object],这是不好的。

So, my question here is is there any way to console.log in a single line as

所以,我的问题是有没有办法将console.log作为一行

'some-prefix: '+ detailed log?

'some-prefix:'+详细日志?

In another words, is it possible to suppress new-line after console.log?

换句话说,是否有可能在console.log之后抑制换行?

Thanks.

1 个解决方案

#1


2  

Ok, I do

好的,我知道

var log = function(msg)
{
  var util = require('util');
  process.stdout.write('CORE: ');
  process.stdout.write(util.inspect(msg,
  {
    depth: 99,
    colors: true
  }));
  process.stdout.write('\n');
};

#1


2  

Ok, I do

好的,我知道

var log = function(msg)
{
  var util = require('util');
  process.stdout.write('CORE: ');
  process.stdout.write(util.inspect(msg,
  {
    depth: 99,
    colors: true
  }));
  process.stdout.write('\n');
};