Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js console.log - Is it possible to update a line rather than create a new line?

My node.js application has a lot of console logs, which are important for me to see (it's quite a big app so runs for a long time and I need to know that things are still progressing) but I'm ending up with thousands of lines of console logs.

Is it somehow possible to do a console.update that erases/replaces a console line rather than creating a new line?

like image 406
JVG Avatar asked Jun 26 '13 00:06

JVG


People also ask

Does console log make a new line?

To create a multi line strings when printing to the JavaScript console, you need to add the new line character represented by the \n symbol. Alternatively, you can also add a new line break using Enter when you use the template literals syntax.

How do I make the console log not print a newline?

stdout. write() method to print to console without trailing newline.

What does console log do in node js?

The most basic and most used method is console. log() , which prints the string you pass to it to the console. If you pass an object, it will render it as a string.

How do I use the console to log into a different line?

To display a console. log in multiple lines we need to break the line with \n console. log("Hello \n world!"); That will display it like this: Hello world!


2 Answers

Try playing with process.stdout methods instead on console:

process.stdout.write("Hello, World"); process.stdout.clearLine(0); process.stdout.cursorTo(0); process.stdout.write("\n"); // end the line 

TypeScript: clearLine() takes -1, 0, or 1 as a direction parameter with the following meanings:

-1: to the left from cursor.
0: the entire line.
1 - to the right from cursor

like image 79
michelek Avatar answered Oct 17 '22 16:10

michelek


Following @michelek's answer, you can use a function somewhat like this:

function printProgress(progress){     process.stdout.clearLine();     process.stdout.cursorTo(0);     process.stdout.write(progress + '%'); } 
like image 26
Bruno Peres Avatar answered Oct 17 '22 17:10

Bruno Peres