本文介绍了有Node.js console.log长度限制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js中console.log输出的长度是否有限制?以下将打印数字,直到56462,然后停止。之所以出现这种情况,是因为我们要从MySQL返回数据集,并且输出将在327k个字符后退出。

Is there a limit the length of console.log output in Node.js? The following prints numbers up to 56462, then stops. This came up because we were returning datasets from MySQL and the output would just quit after 327k characters.

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

console.log(out);

字符串本身看起来很好,因为它返回最后的数字,直到99999:

The string itself seems fine, as this returns the last few numbers up to 99999:

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

返回值:

99996 99997 99998 99999

这是使用Node v0.6.14。

This is using Node v0.6.14.

推荐答案

您是否曾尝试在具有更多内存的计算机上写这么多内容?

根据Node源代码控制台正在写入流中:

并且流会将数据缓冲到内存中:

Have you tried writing that much on a machine with more memory?
According to Node source code console is writing into a stream: https://github.com/joyent/node/blob/cfcb1de130867197cbc9c6012b7e84e08e53d032/lib/console.js#L55
And streams may buffer the data into memory: http://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback

因此,如果您把将大量数据重新存储到流中,可能会达到内存上限。
我建议您拆分数据并将其提供给 process.stdout.write 方法,下面是一个示例:

So if you put reeeaally a lot of data into a stream, you may hit the memory ceiling.I'd recommend you split up your data and feed it into process.stdout.write method, here's an example: http://nodejs.org/api/stream.html#stream_event_drain

这篇关于有Node.js console.log长度限制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 03:21