是否有Node.js console.log长度限制?

时间:2022-09-26 16:55:35

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.

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

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

console.log(out); 

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

字符串本身似乎很好,因为这会返回最后几个数字,最多为99999:

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

Returns:

99996 99997 99998 99999

This is using Node v0.6.14.

这是使用Node v0.6.14。

2 个解决方案

#1


4  

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

你有没有尝试在内存更多的机器上写这么多?根据Node源代码控制台写入流:https://github.com/joyent/node/blob/cfcb1de130867197cbc9c6012b7e84e08e53d032/lib/console.js#L55并且流可以将数据缓冲到内存中:http:// nodejs。组织/ API / stream.html#stream_writable_write_chunk_encoding_callback

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

因此,如果您将大量数据放入流中,则可能会达到内存上限。我建议你拆分数据并将其提供给process.stdout.write方法,这是一个例子:http://nodejs.org/api/stream.html#stream_event_drain

#2


-1  

I would recommend using output to file when using node > 6.0

我建议在使用node> 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

#1


4  

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

你有没有尝试在内存更多的机器上写这么多?根据Node源代码控制台写入流:https://github.com/joyent/node/blob/cfcb1de130867197cbc9c6012b7e84e08e53d032/lib/console.js#L55并且流可以将数据缓冲到内存中:http:// nodejs。组织/ API / stream.html#stream_writable_write_chunk_encoding_callback

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

因此,如果您将大量数据放入流中,则可能会达到内存上限。我建议你拆分数据并将其提供给process.stdout.write方法,这是一个例子:http://nodejs.org/api/stream.html#stream_event_drain

#2


-1  

I would recommend using output to file when using node > 6.0

我建议在使用node> 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