'How do I know the level of nesting of my code?

I am writing a custom log function and it would be amazing if it could add some indentation to the log based on the level of nesting of the log.

For example this:

logger.log("level1")
if (true) {
  logger.log("level2")
}

would print this:

level1
  level2

I looked in process and in the content of an Error but I can't find sufficient information to know the level of nesting of my code. Of course I could manually add a parameter to the function each time I use the function to specify the level of nesting, but it would be way better if it was calculated automatically. Is there a way to do it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source