最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Indented, multi-line logging in NodeJS - Stack Overflow

programmeradmin7浏览0评论

I want to print JSON.stringify()'d objects to the console, for context as part of a Mocha test suite output.

As the tests indent, I'd like the object log lines to be indented far enough rightwards (say, 3-4 tab spaces) that they are recognisably in the right describe() group.

How could I achieve this with something like console.log or process.stdout.write?

I want to print JSON.stringify()'d objects to the console, for context as part of a Mocha test suite output.

As the tests indent, I'd like the object log lines to be indented far enough rightwards (say, 3-4 tab spaces) that they are recognisably in the right describe() group.

How could I achieve this with something like console.log or process.stdout.write?

Share Improve this question asked Sep 12, 2015 at 16:11 yellow-saintyellow-saint 9342 gold badges14 silver badges40 bronze badges 4
  • Look at the extra parameters of JSON.stringify on the MDN. – MinusFour Commented Sep 12, 2015 at 16:15
  • 1 @MinusFour That leaves the initial curly brackets at column 0, and creates spaces at all depths of the object. I want a normal JSON.stringify(obj,null,2) output, all shifted right by three tabspaces. – yellow-saint Commented Sep 12, 2015 at 16:53
  • 1 Then you want to shift the whole string by 3 tabspaces? – MinusFour Commented Sep 12, 2015 at 16:57
  • @MinusFour - yes. Every line needs to shift. – yellow-saint Commented Sep 12, 2015 at 16:58
Add a ment  | 

3 Answers 3

Reset to default 15

If just used for console.log you might want to look up for groups, check this on your console:

var obj = {
  a : 1,
  b : 2,
  c: 3
  }

console.log('Non-tabbed');
console.group();
console.log(JSON.stringify(obj, null, 2));
console.groupEnd();
console.log('Back to non-tabbed');

Works fine on current browsers and latest node. There's also a package on npm that might just work for that if you are working with older node versions.

node-console-group

This shifts the whole JSON string by 3 spaces. It breaks the JSON string by new lines, then adds on each line 3 spaces to a new string which will hold every line shifted.

var results = document.getElementById('results');
var obj = {
    a: 5,
    b: 3,
    c: 4
};

var string = JSON.stringify(obj, null, 2);

var stringShifted = '';
string.split('\n').forEach(function(line){
    stringShifted += '   ' + line + '\n';
});
console.log(string);
console.log(stringShifted);

results.innerHTML = 'Before : \n' + string;
results.innerHTML += '\n\nAfter : \n' + stringShifted;
<pre id="results"></pre>

You can add a short reg-ex replace on the JSON.stringify output to get the effect that you're after.

var myObject = _buildAnObject();   

it("Should log my entire JSON.stringify indented", function(){
   console.log("My log message: \n\t" 
        + JSON.stringify(myObject, null, 2).replace(/\n\r?/g, '\n\t'));
   assert(true);
});

I've put a working mocha example on codepen.io.

under nodejs you can try util.inspect(obj, {depth:10, colors:true ...}) i used it under mocha tested to indent logs

const log = (...args:unknown[])=>{
    setTimeout(()=>{
        let indent = "\t";
        args = args.map(a=>util.inspect(a, {colors:true, depth:10}).replace(/^\x1B\[32m['"`]/ig, '\x1B[32m').replace(/['"`]\x1B\[39m$/ig, '\x1B[39m').replace(/\n/g, `\n${indent} `))
        console.log.call(console, indent, ...args);
    })
}
let user = {
    name:"surinder singh",
    age:38,
    hobbies:[{
        name:"Something interesting",
        from:"birth"
    },{
        name:"Electronic DIY",
        from:"from ~primary"
    },{
        name:"Coding",
        from:"after school"
    }]
}
let args = ["a string 1234", 123 , {xyz:"xyz", num:123, arr:["xxx", 123, {345:"xxx"}, user], bool:true}];

console.log( ...args );
log( ...args  )

发布评论

评论列表(0)

  1. 暂无评论