Node.js console.log - 줄을 새로 만들지 않고 줄을 업데이트할 수 있습니까?
나의node.js
애플리케이션에는 콘솔 로그가 많이 있는데, 제가 보기에 중요합니다. (이 앱은 꽤 큰 앱이므로 장시간 실행되므로 작업이 여전히 진행 중이라는 것을 알아야 합니다.) 하지만 수천 줄의 콘솔 로그가 생성되고 있습니다.
어떻게든 가능한 일인가요?console.update
새로운 라인을 만드는 것보다 콘솔 라인을 지우는 것?
콘솔에서 대신 process.stdout 메서드를 사용하여 재생해 보십시오.
process.stdout.write("Hello, World");
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write("\n"); // end the line
스크립트 입력:clearLine()
는 -1, 0 또는 1을 다음과 같은 의미의 방향 매개 변수로 사용합니다.
-1: 커서에서 왼쪽으로.
0: 전체 라인.
1 - 커서에서 오른쪽으로
@michellek의 답변에 따라 다음과 같은 기능을 사용할 수 있습니다.
function printProgress(progress){
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write(progress + '%');
}
물론입니다. 제가 만든 모듈을 사용하여 이 작업을 수행할 수 있습니다. fknsrs/jety
다음을 통해 설치
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");
제티는 단독으로 사용할 경우 매우 유용하지 않습니다.그 위에 추상적인 것을 쌓아놓으면 좀 더 효과적이어서 좀 덜 장황하게 표현할 수 있습니다.
\r을 사용하여 회선을 종료합니다.
process.stdout.write('text\r');
간단한 예(벽시계)는 다음과 같습니다.
setInterval(() => process.stdout.write(`clock: ${new Date()}\r`), 1000);
부분 라인 쓰기.
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.includes(section) && 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
로그 업데이트를 사용할 수 있습니다.
const logUpdate = require('log-update');
logUpdate('this will be gone');
logUpdate('this will stay');
만약 당신이 stdout 예외를 본다면, 예를 들어 다음과 같이.TypeError: process.stdout.clearLine is not a function
Visual Studio Code(또는 Webstorm)의 Debug Console 창에서 내부 콘솔 대신 외부 터미널 응용 프로그램으로 앱을 실행합니다.그 이유는 디버그 콘솔 창이 TTY가 아니기 때문입니다(process.stdout.isTTY
false)입니다.따라서 에서 시작 구성을 업데이트합니다.launch.json
와 함께"console": "externalTerminal"
선택.
그 중에서도 @michellek에 의한 대답이 효과가 있습니다.그러나 이를 사용하기 시작하면 출력이 파일로 리디렉션되거나 디버거에 있거나 Linux 화면 세션에서 실행되는 등의 경우 예외 문제가 발생할 수 있습니다.다음과 같은 메시지가 표시될 수 있습니다.process.stdout.clearLine is not a function
.
따라서 적어도 테스트를 추가하여 출력이 'TTY'이고 'clearLine()', 'cursorTo()' 등을 수행할 수 있는지 확인해야 합니다.
if (process.stdout.isTTY) {
process.stdout.write("Hello, World");
process.stdout.clearLine(0);
process.stdout.cursorTo(0);
process.stdout.write("\n"); // end the line
}
언급URL : https://stackoverflow.com/questions/17309749/node-js-console-log-is-it-possible-to-update-a-line-rather-than-create-a-new-l
'programing' 카테고리의 다른 글
Angular에서 라우터를 사용하는 구성 요소를 단위 테스트하려면 어떻게 해야 합니까? (0) | 2023.09.14 |
---|---|
포스트 텍스트에서 블록 따옴표 제거 - Wordpress (0) | 2023.09.14 |
var_graphics를 텍스트 파일에 저장합니다. (0) | 2023.09.14 |
윈도우 엣지와 관련하여 팝오버의 X 위치를 기준으로 부트스트랩 팝오버의 위치를 변경? (0) | 2023.09.14 |
Swift #selector 구문으로 컴파일 오류의 "모호한 사용"을 해결하려면 어떻게 해야 합니까? (0) | 2023.09.14 |