有没有Node.js console.log长度限制?

Node.js中console.log输出的长度是否有限制? 以下打印数字高达56462,然后停止。 这是因为我们返回来自MySQL的数据集,输出只会在327K字符后退出。

var out = ""; for (i = 0; i < 100000; i++) { out += " " + i; } console.log(out); 

string本身似乎很好,因为这将返回99999的最后几个数字:

 console.log(out.substring(out.length - 23)); 

返回:

 99996 99997 99998 99999 

这是使用节点v0.6.14。

你有没有试过在有更多内存的机器上写这么多?
根据Node源代码控制台写入stream: https : //github.com/joyent/node/blob/cfcb1de130867197cbc9c6012b7e84e08e53d032/lib/console.js#L55
stream可以将数据caching到内存中: http : //nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback

所以,如果你把大量的数据放到一个stream中,你可能会达到内存上限。 我build议你分割你的数据,并将其馈入process.stdout.write方法,这里是一个例子: http : //nodejs.org/api/stream.html#stream_event_drain

我会build议使用节点> 6.0时使用输出到文件

 const output = fs.createWriteStream('./stdout.log'); const errorOutput = fs.createWriteStream('./stderr.log'); // custom simple logger const logger = new Console(output, errorOutput); // use it like console var count = 5; logger.log('count: %d', count); // in stdout.log: count 5