๋ฐ˜์‘ํ˜•

console.log๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ œ๊ณตํ•˜๋Š” console API์ค‘ ํ•˜๋‚˜. console ๊ฐ์ฒด์—๋Š” log ์™ธ์—๋„ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

 

log ๋ ˆ๋ฒจ ์ข…๋ฅ˜


์ •๋ง ํ•„์š”ํ•œ ์ •๋ณด๊ฐ€ ์•„๋‹ˆ๋ฉด log์™€ info๋Š” ์ œํ’ˆ ๋ฐฐํฌ ๋‹จ๊ณ„์—์„  ์ถœ๋ ฅ๋˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค. ๋ฐฐํฌํ•  ๋•Œ log๋‚˜ info๋Š” ์ถœ๋ ฅํ•˜์ง€ ์•Š๋„๋ก ํ•˜๊ฑฐ๋‚˜ ๋„คํŠธ์›Œํฌ ์š”์ฒญ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๋“ฑ์˜ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ค‘์š”๋„ ๋ ˆ๋ฒจ์— ๋งž๋Š” console ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

 

console.log

const dog = { type: "๐Ÿถ", name: "์ธ„์ธ„", owner: { name: "John" } };
console.log("logging", dog);

 

๊ฐ€์žฅ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” console ๋ฉ”์†Œ๋“œ๋กœ ๊ฐœ๋ฐœ ๋„์ค‘ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. , ์ฝค๋งˆ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

๐Ÿ”๏ธ console.log๋Š” ์ฐธ์กฐ๋ฅผ ๋กœ๊น…ํ•œ๋‹ค. ๊ฐ์ฒด ๋‚ด์šฉ์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์žˆ๋‹ค๋ฉด ๋กœ๊น…์‹œ์—๋„ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.

๐Ÿ”๏ธ ์„œ์‹๋ฌธ์ž %c์™€ CSS ์†์„ฑ์„ ์ด์šฉํ•ด ์ถœ๋ ฅ ๋‚ด์šฉ์˜ ์ƒ‰์ด๋‚˜ ํฌ๊ธฐ ๋“ฑ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

console.log("%c์ƒ‰์ƒ ๋ณ€๊ฒฝ", "color: yellow; font-size: large;")

 

์ฝ˜์†” ์ถœ๋ ฅ ๋‚ด์šฉ์˜ ํ…์ŠคํŠธ ์ƒ‰์ƒ๊ณผ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•œ ํ™”๋ฉด

 

console.info

ํŠน์ •ํ•œ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ.

 

console.warn

๊ฒฝ๊ณ  ์ˆ˜์ค€์˜ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ. ๋…ธ๋ž€์ƒ‰์œผ๋กœ ์ถœ๋ ฅ๋จ.

 

console.error

์‹ฌ๊ฐํ•œ ์ˆ˜์ค€์˜ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ(์˜ˆ์ƒํ•˜์ง€ ๋ชปํ•œ ์—๋Ÿฌ๋‚˜ ์‹œ์Šคํ…œ ์—๋Ÿฌ ๋“ฑ). ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ์ถœ๋ ฅ๋จ.

 

log, info, warn, error ์ฝ˜์†” ์ถœ๋ ฅ ํ™”๋ฉด

 

ํŠน์ • ์กฐ๊ฑด์ผ ๋•Œ๋งŒ ์ฝ˜์†” ์ถœ๋ ฅํ•˜๊ธฐ — console.assert


console.assert(expression, msg)
console.assert(expression, obj)

 

console.assert ๋ฉ”์„œ๋“œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ช…์‹œํ•œ ์กฐ๊ฑด์ด falsy์ผ ๋•Œ๋งŒ ์ฝ˜์†”์— ์˜ค๋ฅ˜ ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. truthy ์ผ ๋•Œ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ถœ๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ด๋ฅผ ํ™œ์šฉํ•ด ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๋•Œ๋งŒ ์ฝ˜์†”์„ ์ถœ๋ ฅํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.

console.assert(2 === 3, "not same!"); // false์ด๋ฏ€๋กœ ์ฝ˜์†” ์ถœ๋ ฅ
console.assert(2 === 2, "same!"); // true์ด๋ฏ€๋กœ ์ฝ˜์†” ์ถœ๋ ฅ ์•ˆํ•จ

 

๋‘๋ฒˆ์งธ ์ธ์ž์— ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ๊ฐ์ฒด๋ฅผ ๋„˜๊ธธ ์ˆ˜๋„ ์žˆ๋‹ค.

const errorMsg = "the # is not even";
for (let number = 2; number <= 5; number += 1) {
  console.assert(number % 2 === 0, { number: number, errorMsg: errorMsg });
}
// ์–ด์„ค์…˜์— ์‹คํŒจํ•จ: {number: 3, errorMsg: 'the # is not even'}
// ์–ด์„ค์…˜์— ์‹คํŒจํ•จ: {number: 5, errorMsg: 'the # is not even'}

 

๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•  ๋•Œ ์œ ์šฉํ•œ ๋ฉ”์†Œ๋“œ


console.table

console.table(data [, columns])

 

๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด์„ ํ…Œ์ด๋ธ” ํ˜•์‹์œผ๋กœ ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ ํ–‰(row)์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋‚˜์—ดํ•˜์ง€๋งŒ ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ํ†ตํ•ด ํŠน์ • ์—ด(column)๋งŒ ํ‘œ์‹œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

const names = ["Tyrone", "Janet", "Maria"];
console.table(names);

 

๋ฐฐ์—ด์„ ๋„˜๊ฒผ์„ ๋•Œ

 

const tyrone = { firstName: "Tyrone", lastName: "Jones" };
console.table(tyrone);

 

๊ฐ์ฒด๋ฅผ ๋„˜๊ฒผ์„ ๋•Œ
 
const tyrone = { firstName: "Tyrone", lastName: "Jones" };
const janet = { firstName: "Janet", lastName: "Smith" };
const maria = { firstName: "Maria", lastName: "Cruz" };

console.table([tyrone, janet, maria]);

 

 
๊ฐ์ฒด๋กœ ๊ตฌ์„ฑ๋œ ๋ฐฐ์—ด์„ ๋„˜๊ฒผ์„ ๋•Œ
 
const tyrone = { firstName: "Tyrone", lastName: "Jones" };
const janet = { firstName: "Janet", lastName: "Smith" };
const maria = { firstName: "Maria", lastName: "Cruz" };

console.table([tyrone, janet, maria], "firstName");

 

ํŠน์ • ์—ด(column)๋งŒ ํ‘œ์‹œํ•  ๋•Œ

 

console.dir

console.dir(object)

 

DOM ์š”์†Œ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค. console.log๋กœ DOM ์š”์†Œ๋ฅผ ๋กœ๊น…ํ•ด๋ณด๋ฉด ํƒœ๊ทธ๋งŒ ๋ณด์ด์ง€๋งŒ console.dir์„ ์‚ฌ์šฉํ•˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐ์ฒด ํ˜•์‹(ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํŠธ๋ฆฌ ๊ตฌ์กฐ)์œผ๋กœ ํ‘œํ˜„ํ•ด์ค€๋‹ค.

 

document.body ์š”์†Œ๋ฅผ log์™€ dir๋กœ ์ถœ๋ ฅํ–ˆ์„ ๋•Œ ๋น„๊ต

 

๊ทธ ์™ธ์—” console.log์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๋‘๋ฒˆ์งธ ์ธ์ž์— colors depth ์˜ต์…˜์„ ๋„˜๊ธธ ์ˆ˜ ์žˆ๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

const dog = { type: "๐Ÿถ", name: "์ธ„์ธ„", owner: { name: "John" } };
console.dir(dog, { colors: true, depth: 1 });

 

  • colors : ์ฝ˜์†” ์ถœ๋ ฅ ๋‚ด์šฉ์— ์ƒ‰์ƒ ํ‘œํ˜„ ์—ฌ๋ถ€. ๊ฐ’ boolean
  • depth : ์–ผ๋งˆ๋งŒํผ์˜ ๊นŠ์ด๊นŒ์ง€ ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ• ์ง€ ์„ค์ •. ๊ฐ’ number / ๊ธฐ๋ณธ๊ฐ’ 2

 

colors: false, depth: 0์œผ๋กœ ์ง€์ •ํ•œ ํ›„ node ์ถœ๋ ฅ ํ™”๋ฉด

 

์ฝ”๋“œ ์ˆ˜ํ–‰์‹œ๊ฐ„ ์ธก์ • — console.time


๐Ÿ’ก ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •์„ ์œ„ํ•œ ํ‘œ์ค€ ๋ฉ”์„œ๋“œ๋Š” `performance.now()` ์ด๋‹ค(์ฐธ๊ณ ๊ธ€). `console.time()`์€ ํ‘œ์ค€์ด ์•„๋‹ˆ๋‹ค. `performance.now()`๋Š” 1/1000 ๋ฐ€๋ฆฌ์ดˆ ๋‹จ์œ„๋กœ ์ธก์ •ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ •ํ™•๋„๊ฐ€ ๋” ์šฐ์ˆ˜ํ•˜๋‹ค. `performance.now()`๋Š” ์›น ํŽ˜์ด์ง€ ํ˜น์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ™˜๊ฒฝ์„ ์‹œ์ž‘ํ•˜๊ฑฐ๋‚˜ ๋กœ๋“œํ•œ ์‹œ์ ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ ์ƒ๋Œ€์ ์ธ ์‹œ๊ฐ„(ํƒ€์ž„์Šคํƒฌํ”„)์„ ๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฆ‰, ํŽ˜์ด์ง€ ์ƒˆ๋กœ๊ณ ์นจ์ด๋‚˜ ์ดˆ๊ธฐํ™”ํ•  ๋•Œ๋งˆ๋‹ค `performance.now()` ๊ฐ’์€ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋˜์–ด ๊ฒฝ๊ณผ ์‹œ๊ฐ„์„ ์ธก์ •ํ•œ๋‹ค.

 

console.time([label])
console.timeEnd([label])
console.timeLog([label][, ...data])

 

์ฝ”๋“œ ์ˆ˜ํ–‰ ์‹œ๊ฐ„์„ ์ธก์ •ํ•˜๊ณ  ์‹ถ์„๋•Œ console.time() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. time() ๋ถ€ํ„ฐ timeEnd() ๊นŒ์ง€ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„์„ ์ฝ˜์†”์— ์ถœ๋ ฅํ•œ๋‹ค. ์ด๋•Œ time() ๊ณผ timeEnd() ๋ฉ”์„œ๋“œ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ label ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค.

console.time("totalExecutionTime"); // Time started
for (let i = 0; i < 10; i += 1) {}
console.timeEnd("totalExecutionTime"); // Printing total execution time

 

 

console.timeLog()๋ฅผ ์ด์šฉํ•ด for loop ๋“ฑ์—์„œ ๊ฐ ๋ฐ˜๋ณต์˜ ์‹คํ–‰ ์‹œ๊ฐ„์„ ํ‘œ์‹œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

console.time("totalExecutionTime"); // Time started
console.time("executionTime"); // Time started
for (let i = 0; i < 10; i += 1) {
  console.timeLog("executionTime", i); // Printing execution time for each
}
console.timeLog("totalExecutionTime"); // Printing total execution time

 

 

ํ˜ธ์ถœ ํšŸ์ˆ˜ ๋กœ๊น… — console.count


console.count([label])
console.countReset([label])

 

์ธ์ž์— ์ „๋‹ฌํ•œ ๋ ˆ์ด๋ธ”์ด ๋ช‡ ๋ฒˆ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. console.countReset ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ˜ธ์ถœ ํšŸ์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

function a() {
  console.count("a ํ•จ์ˆ˜ ์‹คํ–‰");
}

a();
a();
console.countReset("a ํ•จ์ˆ˜ ์‹คํ–‰"); // ํ˜ธ์ถœ ํšŸ์ˆ˜ ์ดˆ๊ธฐํ™”
a();

 

 

๊ฐ’ ์ž์ฒด๋ฅผ ๋ ˆ์ด๋ธ”๋กœ ์‚ฌ์šฉํ•ด์„œ, ํ•ด๋‹น ๊ฐ’์„ ์นด์šดํ„ฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

console.count(NaN); // NaN: 1
console.count(NaN + 3); // NaN: 2

let age = 30;
console.count(age / 2); // 15: 1

 

ํ•จ์ˆ˜ ํ˜ธ์ถœ ์œ„์น˜๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์„ ๋•Œ — console.trace


๐Ÿ’ก ์Šคํƒ ํŠธ๋ ˆ์ด์Šค ๊ด€๋ จ ๋” ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋…ธํŠธ ๋งํฌ ์ฐธ๊ณ 

 

console.trace([data])

 

console.trace๋Š” ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„์˜ stack trace๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. stack trace๋ž€ ํŠน์ • ์‹œ์ ๋ถ€ํ„ฐ~ํŠน์  ์‹œ์ ๊นŒ์ง€ ๊ฑฐ์ณ์˜จ ํ•จ์ˆ˜๋“ค์˜ ์ˆœ์„œ ์žˆ๋Š” ์ง‘ํ•ฉ์ด๋‹ค. ํ”„๋กœ์„ธ์Šค๊ฐ€ OS๋กœ๋ถ€ํ„ฐ ๋ฉ”๋ชจ๋ฆฌ์˜ ํŠน์ • ์˜์—ญ์„ ํ• ๋‹น ๋ฐ›์œผ๋ฉด, ์ด (๋ฉ”๋ชจ๋ฆฌ)์˜์—ญ์€ ๋‹ค์‹œ (ํ…์ŠคํŠธ)์ฝ”๋“œ / ๋ฐ์ดํ„ฐ / Heap / Stack 4๊ฐ€์ง€ ์˜์—ญ์œผ๋กœ ๋‚˜๋‰œ๋‹ค. Stack ์˜์—ญ์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ปค์ง€๋ฉฐ ํ•จ์ˆ˜์˜ ์ง€์—ญ ๋ณ€์ˆ˜ / ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ์ด ๋‹ด๊ธด๋‹ค. stack trace๊ฐ€ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๋ฐ”๋กœ ์ด Stack์˜์—ญ.

function f1() {
  f2();
}

function f2() {
  f3();
}

function f3() {
  console.trace();
  console.log("hi: :)");
}

f1();

 

์œ„ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค. โžŠf3 ํ•จ์ˆ˜๋Š” 6๋ฒˆ์งธ ์ค„์— ์žˆ๋Š” f2 ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœํ–ˆ๊ณ , โž‹f2 ํ•จ์ˆ˜๋Š” 2๋ฒˆ์งธ ์ค„์— ์žˆ๋Š” f1 ํ•จ์ˆ˜์—์„œ ํ˜ธ์ถœํ–ˆ๊ณ , โžŒf1 ํ•จ์ˆ˜๋Š” 14๋ฒˆ์งธ ์ค„์—์„œ ํ˜ธ์ถœํ•œ ๊ฒƒ์„ ์ฐจ๋ก€๋Œ€๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

์ฝ˜์†” ๊ทธ๋ฃนํ™” — console.groups


console.group([label])
console.groupCollapsed([label])
console.groupEnd([label])

 

console.group()์„ ์‚ฌ์šฉํ•ด ์ ‘๊ณ  ํŽผ์น  ์ˆ˜ ์žˆ๋Š” ์ธ๋ผ์ธ ๊ทธ๋ฃน์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. console.group() ์œผ๋กœ ๊ทธ๋ฃน์„ ์‹œ์ž‘ํ•˜๊ณ  console.groupEnd()๋กœ ๊ทธ๋ฃน์„ ์ข…๋ฃŒํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์„ ์ค‘์ฒฉํ•ด์„œ ๋งŒ๋“ค ์ˆ˜๋„ ์žˆ๋‹ค. ๊ธฐ๋ณธ์ ์œผ๋กœ ํŽผ์นจ ์ƒํƒœ์˜ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•œ๋‹ค. console.groupCollapsed()๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ ‘ํž˜ ์ƒํƒœ์˜ ๊ทธ๋ฃน์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

console.group("URL Info");
console.log("Protocol", window.location.protocol);
console.log("Host", window.origin);
console.log("Path", window.location.pathname);
console.groupCollapsed("Meta Info"); // ์ค‘์ฒฉ ๊ทธ๋ฃน ์‹œ์ž‘
console.log("Date Fetched", new Date().getTime());
console.log("OS", navigator["platform"]);
console.log("Browser", navigator["appCodeName"]);
console.log("Language", navigator["language"]);
console.groupEnd(); // ์ค‘์ฒฉ ๊ทธ๋ฃน ์ข…๋ฃŒ
console.groupEnd();

 

URL Info๋Š” console.group์„ ์‚ฌ์šฉํ•ด์„œ ํŽผ์ณ์žˆ๊ณ  Meta Info๋Š” console.groupCollapsed๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘ํ˜€์žˆ๋‹ค

 

๋””๋ฒ„๊ทธ ์ฝ˜์†” — console.debug


console.debug(object [, object, ...])

 

console.debug()๋Š” console.log()์™€ ๋™์ผํ•˜์ง€๋งŒ ๋นŒ๋“œ ์‹œ์Šคํ…œ์—์„  ์ œ๊ฑฐ๋˜๊ณ , ํ”„๋กœ๋•์…˜ ๋ชจ๋“œ์—์„  ๋น„ํ™œ์„ฑํ™”๋œ๋‹ค. console.debug() ์ถœ๋ ฅ ๋‚ด์šฉ์„ ํ™•์ธํ•˜๋ ค๋ฉด ํฌ๋กฌ ์ฝ˜์†” ์ถœ๋ ฅ ์„ค์ •์—์„œ ์ƒ์„ธ(Verbose) ์˜ต์…˜์ด ์ฒดํฌ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

 

์ฝ˜์†”์— ์Šคํƒ€์ผ ์ž…ํžˆ๊ธฐ


์ฝ˜์†” ์ถœ๋ ฅ ๊ฒฐ๊ณผ์—๋„ CSS ์Šคํƒ€์ผ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ์€ ๊ณณ์— %c ์„œ์‹๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ์›ํ•˜๋Š” ์Šคํƒ€์ผ์„ ์ •์˜ํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๋Ÿฌ ์Šคํƒ€์ผ์„ ์ ์šฉํ•  ๋• %c ์„œ์‹ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ ๋งŒํผ ์Šคํƒ€์ผ์ด ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

console.log(
  "%cHello %cWorld",
  "color: red;", // %cHello์— ๋Œ€ํ•œ ์Šคํƒ€์ผ
  "color: yellow; background: black;", // %cWorld์— ๋Œ€ํ•œ ์Šคํƒ€์ผ
);

 

 

์Šคํƒ€์ผ์ด ์ •์˜๋œ ๋ณ€์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ง€์ •ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

// ์Šคํƒ€์ผ ๋ณ€์ˆ˜ ์ง€์ •
const helloStyle = ["color: red"];
const worldStyle = ["color: yellow", "background: black"].join(";");

console.log("%cHello %cWorld", helloStyle, worldStyle);

 

๋”๋ณด๊ธฐ

 

์ฝ”๋“œ ์ถœ์ฒ˜ : https://github.com/Lissy93/dashy/blob/master/src/utils/CoolConsole.js

/* eslint no-console: ["error", { allow: ["log", "info", "warn"] }] */

/* Prints the app name and version, helpful for debugging */
export const welcomeMsg = () => {
  const v = process.env.VUE_APP_VERSION
    ? `V${process.env.VUE_APP_VERSION}`
    : "";
  console.log(
    `\n%cDashy  ${v} ๐Ÿš€`,
    "color:#0dd8d8; background:#0b1021; font-size:1.5rem; padding:0.15rem 0.25rem; margin: 1rem auto; font-family: Rockwell; border: 2px solid #0dd8d8; border-radius: 4px;font-weight: bold; text-shadow: 1px 1px 1px #00af87bf;",
  );
};

/* Prints warning message, usually when there is a configuration error */
export const warningMsg = (message, stack) => {
  console.info(
    `\n%cโš ๏ธ Warning โš ๏ธ%c \n${message} \n\n%cThis is likely not an issue with Dashy, but rather your configuration. If you think it is a bug, please open a ticket on GitHub: https://git.io/JukXk`,
    "color:#ceb73f; background: #ceb73f33; font-size:1.5rem; padding:0.15rem; margin: 1rem auto; font-family: Rockwell, Tahoma, 'Trebuchet MS', Helvetica; border: 2px solid #ceb73f; border-radius: 4px; font-weight: bold; text-shadow: 1px 1px 1px #000000bf;",
    "font-weight: bold; font-size: 1rem;color: #ceb73f;",
    "color: #ceb73f; font-size: 0.75rem; font-family: Tahoma, 'Trebuchet MS', Helvetica;",
  );
  if (stack) {
    console.warn(`%cStack Trace%c\n${stack}`, "font-weight: bold;", "");
  }
};

/* Prints status message */
export const statusMsg = (title, msg) => {
  console.log(
    `%c${title || ""}\n%c${msg}`,
    "font-weight: bold; color: #0dd8d8; text-decoration: underline;",
    "color: #ceb73f;",
  );
};

/* Prints status message, with a stack trace */
export const statusErrorMsg = (title, msg, errorLog) => {
  console.log(
    `%c${title || ""}\n%c${msg} \n%c${errorLog || ""}`,
    "font-weight: bold; color: #0dd8d8; text-decoration: underline;",
    "color: #ff025a",
    "color: #ff025a80;",
  );
};

 

๋กœ๊ทธ ๋ฌธ์ž์—ด ํฌ๋งท


C์–ธ์–ด ์Šคํƒ€์ผ printf๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ž์—ด ํฌ๋งท์„ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฐธ๊ณ ๋กœ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ์‚ฌ์šฉํ•ด๋„ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์งง์€ ๋ฌธ์ž์—ด์€ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์˜ ๊ฐ€๋…์„ฑ์ด ๋” ์ข‹๋‹ค.

 

์ง€์ •์ž ์„ค๋ช…
%s ๋ฌธ์ž์—ด ๋˜๋Š” ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜๋˜๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  ํƒ€์ž…
%d ํ˜น์€ %i ์ •์ˆ˜ (integer)
%f ์‹ค์ˆ˜ (Real numbers)
%o(์†Œ๋ฌธ์ž o) ์ตœ์ ์˜ ํฌ๋งท ์‚ฌ์šฉ
%O (๋Œ€๋ฌธ์ž O) ๊ธฐ๋ณธ ํฌ๋งท ์‚ฌ์šฉ
%c ์ปค์Šคํ…€ ํฌ๋งท (์Šคํƒ€์ผ ์ง€์ •)

 

console.log(
  "Hello %s, welcome to the year %d!",
  "world", // %s(๋ฌธ์ž์—ด)์— ์ง€์ •๋จ
  new Date().getFullYear(), // %d(์ •์ˆ˜)์— ์ง€์ •๋จ
);

// Hello world, welcome to the year 2022!

 

๋ ˆํผ๋Ÿฐ์Šค


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•