我的node.js应用程序有很多控制台日志,这对我来说很重要(它是一个非常大的应用程序,因此运行了很长时间,我需要知道事情仍在进行中)但我最终会有数千行控制台日志.
是否可能以某种方式console.update擦除/替换控制台线而不是创建新线?
在“Main.js”中,我使用spawn创建一个child_process。我想从“work.js”获取数据,但它显示错误
\n\nTypeError: out.clearLine is not a function\nRun Code Online (Sandbox Code Playgroud)\n\n如果node work.js在终端中使用\xef\xbc\x8cit 工作正常\xe3\x80\x82\n看起来 process.stdout 在 childProcess 中没有函数“clearLine”。
main.js
\n\nvar spawn = require(\'child_process\').spawn;\nvar child = spawn(\'node\',[\'work.js\']);\nchild.stdout.on(\'data\', function (data) {\n console.log(\'data:\',data.toString());\n})\nchild.stderr.on(\'data\', function (data) {\n console.log("stderr:", data.toString());\n});\nRun Code Online (Sandbox Code Playgroud)\n\n工作.js
\n\nvar out = process.stdout;\nvar idx = 0;\nvar id = setInterval(()=>{\n idx++;\n out.clearLine();\n out.cursorTo(0);\n out.write(\'workTime:\' + idx);\n if(idx>3){\n clearInterval(id); \n console.log();\n console.log(\'end\')\n }\n},100)\nRun Code Online (Sandbox Code Playgroud)\n\n这只是一个演示,我无法更改 work.js。我该如何解决 main.js 中的问题,谢谢
\n我读了一个nodejs的文件,遇到了一个方法cursorTo,我不明白它。请有人解释一下。
function refreshConsole () {
if (Settings.properties.preventMessageFlicker) {
readline.cursorTo(process.stdout, 0, 0);
} else {
process.stdout.write('\033c');
}
}
Run Code Online (Sandbox Code Playgroud) process.stdout.clearLine()删除最新行.如何删除所有行stdout?
var out = process.stdout;
out.write("1\n"); // prints `1` and new line
out.write("2"); // prints `2`
setTimeout(function () {
process.stdout.clearLine(); // clears the latest line (`2`)
out.cursorTo(0); // moves the cursor at the beginning of line
out.write("3"); // prints `3`
out.write("\n4"); // prints new line and `4`
console.log();
}, 1000);
Run Code Online (Sandbox Code Playgroud)
输出是:
1
3
4
Run Code Online (Sandbox Code Playgroud)
我想删除所有行stdout而不是最新行,因此输出将是:
3
4
Run Code Online (Sandbox Code Playgroud)