Javascript/Node.js

[Node.js] console.log

Rainbow🌈Coder 2022. 5. 14. 09:53
728x90

1. console.clear();


console.log("logging.....");
console.clear();

 

μ½˜μ†”μ°½μ„ μ§€μ›Œμ€€λ‹€.



2. log level : μ„œλ²„μ—μ„œ λ°œμƒν•˜λŠ” μ—¬λŸ¬ μ΄λ²€νŠΈμ— λŒ€ν•΄μ„œ(μ—λŸ¬μ˜ μƒνƒœμ— λ”°λΌμ„œ)


console.log("log"); //개발
console.info("info"); //개발
console.warn("warn"); //경보
console.error("error"); //μ—λŸ¬, μ‚¬μš©μž μ—λŸ¬, μ‹œμŠ€ν…œ μ—λŸ¬


3. assert : 첫번째 μΈμžκ°€ false μΌλ•Œλ§Œ 좜λ ₯

console.assert(2 === 3, "not same!");  //Assertion failed: not same!
console.assert(2 === 2, "same!");       //무응닡


4. print object : 보기 μ’‹κ²Œ 좜λ ₯ 


const student = { name: "홍길동", age: 20, company: { name: "AC" } };
console.log(student);
console.table(student); //더 λ³΄κΈ° μ’‹κ²Œ ν…Œμ΄λΈ” ν˜•νƒœλ‘œ μΆœλ ₯ κ°€λŠ₯
console.dir(student, { showHidden: true, colors: false, depth: 2 }); //depth둜 μ€‘μ²©λœ μ˜€λΈŒμ νŠΈλ₯Ό μ–΄λА κΉŠμ΄ κΉŒμ§€ λ³΄μ—¬μ€„ κ±΄μ§€ μ‘°μ ˆ κ°€λŠ₯
console.dir(student, { showHidden: true, colors: false, depth: 0 });

{ name: '홍길동', age: 20, company: { name: 'AC' } }
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ name β”‚  Values  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  name   β”‚      β”‚ '홍길동' β”‚
β”‚   age   β”‚      β”‚    20    β”‚
β”‚ company β”‚ 'AC' β”‚          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
{ name: '홍길동', age: 20, company: { name: 'AC' } }
{ name: '홍길동', age: 20, company: [Object] }


4. μ„±λŠ₯확인 : νŠΉμ • μ½”λ“œ μˆ˜ν–‰μ‹œκ°„ 확인

//measuring time
console.time("for loop");
for (let i = 0; i < 10; i++) {
  i++;
}
console.timeEnd("for loop"); //for loop: 0.129ms



5. counting(ν•¨μˆ˜ 호좜 횟수)


function a() {
  console.count("a function");
}

a();
a();

console.countReset("a function"); 
a();

a function: 1
a function: 2
a function: 1



6. trace : λ””λ²„κΉ…μ‹œ μ•„μ£Ό μœ λ¦¬ν•¨!!!


function f1() {
  f2();
}

function f2() {
  f3();
}

function f3() {
  console.log("f3");
  console.trace(); //이 ν•¨μˆ˜κ°€ μ–΄λ””μ„œλΆ€ν„° μ–΄λ–»κ²Œ λˆ„κ°€ ν˜ΈμΆœν–ˆλŠ”μ§€λ₯Ό λ³Ό μˆ˜ μžˆμŒ
}

f1();

Trace
    at f3 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:53:11)
    at f2 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:48:3)
    at f1 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:44:3)
    at Object.<anonymous> (C:\Users\User\gitprogect\NodeJs\1-global\app.js:56:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

 

 

ν’€μ½”λ“œ

const fs = require("fs");

console.log("logging.....");
console.clear();

// log level : μ„œλ²„μ—μ„œ λ°œμƒν•˜λŠ” μ—¬λŸ¬ μ΄λ²€νŠΈμ— λŒ€ν•΄μ„œ(μ—λŸ¬μ˜ μƒνƒœμ— λ”°λΌμ„œ)
console.log("log"); //개발
console.info("info"); //개발
console.warn("warn"); //경보
console.error("error"); //μ—λŸ¬, μ‚¬μš©μž μ—λŸ¬, μ‹œμŠ€ν…œ μ—λŸ¬

//assert : 첫번째 μΈμžκ°€ false μΌλ•Œλ§Œ 좜λ ₯
console.assert(2 === 3, "not same!");
console.assert(2 === 2, "same!");

//print object : 보기 μ’‹κ²Œ 좜λ ₯ 
const student = { name: "홍길동", age: 20, company: { name: "AC" } };
console.log(student);
console.table(student); //더 보기 μ’‹κ²Œ ν…Œμ΄λΈ” ν˜•νƒœλ‘œ 좜λ ₯ κ°€λŠ₯
console.dir(student, { showHidden: true, colors: false, depth: 2 }); //depth둜 μ€‘μ²©λœ 였브젝트λ₯Ό μ–΄λŠ 깊이 κΉŒμ§€ 보여쀄 건지 쑰절 κ°€λŠ₯
console.dir(student, { showHidden: true, colors: false, depth: 0 });

//μ„±λŠ₯확인 : νŠΉμ • μ½”λ“œ μˆ˜ν–‰μ‹œκ°„ 확인
//measuring time
console.time("for loop");
for (let i = 0; i < 10; i++) {
  i++;
}
console.timeEnd("for loop");

//counting(ν•¨μˆ˜ 호좜 횟수)
function a() {
  console.count("a function");
}

a();
a();

console.countReset("a function");
a();

//trace : λ””λ²„κΉ…μ‹œ μ•„μ£Ό μœ λ¦¬ν•¨!!!
function f1() {
  f2();
}

function f2() {
  f3();
}

function f3() {
  console.log("f3");
  console.trace(); //이 ν•¨μˆ˜κ°€ μ–΄λ””μ„œλΆ€ν„° μ–΄λ–»κ²Œ λˆ„κ°€ ν˜ΈμΆœν–ˆλŠ”μ§€λ₯Ό λ³Ό 수 있음
}

f1();

 

둜그

log
info
warn
error
Assertion failed: not same!
{ name: '홍길동', age: 20, company: { name: 'AC' } }
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ name β”‚  Values  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  name   β”‚      β”‚ '홍길동' β”‚
β”‚   age   β”‚      β”‚    20    β”‚
β”‚ company β”‚ 'AC' β”‚          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
{ name: '홍길동', age: 20, company: { name: 'AC' } }
{ name: '홍길동', age: 20, company: [Object] }
for loop: 0.129ms
a function: 1
a function: 2
a function: 1
f3
Trace
    at f3 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:53:11)
    at f2 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:48:3)
    at f1 (C:\Users\User\gitprogect\NodeJs\1-global\app.js:44:3)
    at Object.<anonymous> (C:\Users\User\gitprogect\NodeJs\1-global\app.js:56:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

 

728x90