0
0

More than 3 years have passed since last update.

【メモ】console.trace を閉じた状態でログ出力する

Last updated at Posted at 2020-12-25

1個だけトレースするならまだしも、
複数回トレースしたい場合もデカデカとログを占有して邪魔くさい・・・
というわけで、console.trace() を閉じた状態で出力したい。

といっても、これだけでいい。

console.groupCollapsed("trace...");
console.trace();
console.groupEnd();

個人的には、console.log の用途のついでにトレースもしたいニーズがあるので、
こんな関数を作ったら割とデバッグが捗る。

console.tracedLog = (label, ...log) => {
  {
    console.group(`-- ${label} --`);
    console.log(...log);
    {
      console.groupCollapsed("trace...");
      console.trace();
      console.groupEnd();
    }
    console.groupEnd();
  }
};

chrome-devtools でこれを使うとこんな感じの出力になる

> console.tracedLog("title", [1, 2, 3]);
▼ -- title --
    > (3) [1, 2, 3]
  > trace...
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0