IT TIP

Node.js console.log-새 줄을 만드는 대신 줄을 업데이트 할 수 있습니까?

itqueen 2020. 12. 6. 22:27
반응형

Node.js console.log-새 줄을 만드는 대신 줄을 업데이트 할 수 있습니까?


node.js응용 프로그램에는 많은 콘솔 로그가 있습니다. 이것은 제가보기에 중요한 것입니다 (아주 큰 앱이므로 오랫동안 실행되고 작업이 계속 진행되고 있음을 알아야 함). 콘솔 로그.

console.update새 줄을 만드는 대신 콘솔 줄을 지우거나 대체하는 것이 어떻게 든 가능 합니까?


콘솔에서 대신 process.stdout 메서드를 사용해보십시오.

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

@michelek의 대답에 따라 다음과 같은 함수를 사용할 수 있습니다.

function printProgress(progress){
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(progress + '%');
}

물론 내가 만든 모듈을 사용하여이 작업을 수행 할 수 있습니다. fknsrs / jetty

다음을 통해 설치

npm install jetty

다음은 사용 예입니다.

// Yeah, Jetty!
var Jetty = require("jetty");

// Create a new Jetty object. This is a through stream with some additional
// methods on it. Additionally, connect it to process.stdout
var jetty = new Jetty(process.stdout);

// Clear the screen
jetty.clear();

// write something
jetty.text("hello world");
jetty.moveTo([0,0]);
jetty.text("hello panda");

Jetty는 단독으로 사용할 때 유용하지 않습니다. 부두 호출을 덜 장황하게 만들기 위해 추상화를 구축 할 때 훨씬 더 효과적입니다.

더 많은 사용 예를 보려면 fknsrs / bento 에서 jetty를 사용하는 방법을 확인하세요 .


부분 라인을 작성합니다.

process.stdout.write("text");
process.stdout.write("more");
process.stdout.write("\n"); // end the line

출력의 양이 실제 문제인 경우 로깅을 다시 생각해야 할 것입니다. 선택적 런타임 로깅을 허용하는 로깅 시스템을 사용하여 출력을 필요한 것으로 좁힐 수 있습니다.

// The sections we want to log and the minimum level
var LOG_LEVEL = 4;
var LOG_SECTIONS = ["section1","section2","section3"];

function logit(msg, section, level) {
  if (LOG_SECTIONS.indexOf(section) > -1 && LOG_LEVEL >= level) {
    console.log(section + ":" + msg);
  }
}

logit("message 1", "section1", 4); // will log
logit("message 2", "section2", 4); // will log
logit("message 3", "section3", 2); // wont log, below log level
logit("message 4", "section4", 4); // wont log, not in a log section

TypeError: process.stdout.clearLine is not a functionVisual Studio Code (또는 Webstorm)의 디버그 콘솔 창에서 와 같은 stdout 예외가 표시되면 앱을 내부 콘솔 대신 외부 터미널 애플리케이션으로 실행합니다. 그 이유는 콘솔 디버그 창이 TTY가 아니기 때문입니다 ( process.stdout.isTTYis false). 따라서 귀하의 실행 구성을 업데이트 launch.json"console": "externalTerminal"옵션을 선택합니다.

참고URL : https://stackoverflow.com/questions/17309749/node-js-console-log-is-it-possible-to-update-a-line-rather-than-create-a-new-l

반응형