티스토리 뷰
728x90
Call stack(stack trace)를 출력하여 에러 로그를 출력하고, 디버깅하는 방법을 소개합니다.
가장 간단한 방법으로 로그(console.log())를 출력하여 특정 시간에 어떤 변수가 어떤 값을 갖고 있는지 확인할 수 있고, 문제 원인을 확인해볼 수 있습니다.
그런데 만약 어떤 함수가 호출되서 문제가 발생했는데, 어떤 경로로 호출되었는지 알고 싶을 때 로그 한줄만으로 찾기 어려울 수 있습니다. 이럴 때, Call stack을 출력하여 해당 코드까지 어떤 stack으로 함수가 호출되었는지 확인할 수 있습니다.
console.trace()으로 콜스택 출력
console.trace(message)는 message와 함께 콜스택을 출력합니다. 콜스택을 출력하면 해당 코드 위치까지 어떤 스택으로 함수가 호출되었는지 알 수 있습니다.
아래 예제는 aaa() 함수를 호출합니다. aaa() 내부에서 trace() 함수를 호출하고, bbb()를 호출하며, 그 내부에서 다시 trace()를 출력합니다. 결과를 보시면 어떻게 콜스택이 출력되는지 알 수 있습니다.
function aaa() {
console.trace("Trace from aaa():");
console.log("Inside aaa()");
bbb();
}
function bbb() {
console.trace("Trace from bbb():");
console.log("Inside bbb()");
}
aaa();
Output:
Error:
Trace: Trace from aaa():
at aaa (/home/mjs/js_project/examples/node_29eba7c610466.tmp.js:2:11)
at Object.<anonymous> (/home/mjs/js_project/examples/node_29eba7c610466.tmp.js:12:1)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Inside aaa()
Inside bbb()
Error:
Trace: Trace from bbb():
at bbb (/home/mjs/js_project/examples/node_29eba7c610466.tmp.js:8:11)
at aaa (/home/mjs/js_project/examples/node_29eba7c610466.tmp.js:4:3)
at Object.<anonymous> (/home/mjs/js_project/examples/node_29eba7c610466.tmp.js:12:1)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
console.log()로 콜스택 출력
console.log(message, error)는 message와 함께 error를 출력합니다. 에러 정보를 아래와 같이 출력하면 에러가 발생한 지점에 대한 콜스택도 함께 출력됩니다.
따라서, 어떤 에러가 발생했을 때 그 지점의 콜스택 정보를 보고 싶을 때는 에러 핸들링을 통해 에러의 콜스택을 출력하는 방법을 사용할 수 있습니다.
function aaa() {
throw new Error("unknown error");
}
try {
aaa();
} catch (error) {
console.error("An error occurred:", error);
}
Output:
Error:
An error occurred: Error: unknown error
at aaa (/home/mjs/js_project/examples/node_907f6eb5110a0.tmp.js:2:9)
at Object.<anonymous> (/home/mjs/js_project/examples/node_907f6eb5110a0.tmp.js:6:3)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at Module._load (node:internal/modules/cjs/loader:922:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
728x90
'개발 > Javascript' 카테고리의 다른 글
[Javascript] 문자열 사이에 문자열 삽입하기 (0) | 2023.11.30 |
---|---|
[Javascript] String을 Int로 변환하는 방법 (0) | 2023.11.29 |
[Javascript] 배열 마지막 값 제거하기 (0) | 2023.11.23 |
[Javascript] 배열 첫번째 값 제거하기 (0) | 2023.11.22 |
[Javascript] 배열 중복 값 제거 (0) | 2023.11.21 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- DART
- 주황버섯
- 모루정원
- 파란버섯
- Python
- 리스보아 카드
- 와일드보어의 땅
- 앞자리 0 제거
- 티스토리챌린지
- 나무던전
- 포르투
- java
- Unreal Engine
- 와보땅
- 오블완
- javascript
- 원숭이의숲
- 다크스텀프
- 12e 트램
- 리스본
- 메이플랜드
- aosp 빌드
- adb
- 15e 트램
- Flutter
- 파이썬
- android
- 네키
- 포르투갈
- Skia
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함