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 function
Visual Studio Code (또는 Webstorm)의 디버그 콘솔 창에서 와 같은 stdout 예외가 표시되면 앱을 내부 콘솔 대신 외부 터미널 애플리케이션으로 실행합니다. 그 이유는 콘솔 디버그 창이 TTY가 아니기 때문입니다 ( process.stdout.isTTY
is false). 따라서 귀하의 실행 구성을 업데이트 launch.json
와 "console": "externalTerminal"
옵션을 선택합니다.
'IT TIP' 카테고리의 다른 글
jQuery + 클라이언트 측 템플릿 = "구문 오류, 인식 할 수없는 표현" (0) | 2020.12.06 |
---|---|
iOS에서 Facebook SDK 버전 찾기 (0) | 2020.12.06 |
SSL 프록시 / Charles 및 Android 문제 (0) | 2020.12.06 |
setLatestEventInfo 메서드를 확인할 수 없습니다. (0) | 2020.12.06 |
'isPresent ()'확인없이 'Optional.get ()' (0) | 2020.12.06 |