Debug like a PRO with the ๐๐๐ถ๐น.๐ฑ๐ฒ๐ฏ๐๐ด๐น๐ผ๐ด method.
It allows you write conditional debug messages to ๐๐๐ฑ๐ฒ๐ฟ๐ฟ based on the existence of the ๐ก๐ข๐๐_๐๐๐๐จ๐ environment variable.
When the value passed match the one defined in your code, messages will be logged. Otherwise, these log statements will have no effect.
Obviously, this is a perfect feature for collecting debug logs in order to identify problems. Library authors use it, and I'm sure a lot of you noticed it with some of the libraries in the community.
As you can see in the snap, I'm running the code like this:
๐ก๐ข๐๐_๐๐๐๐จ๐=๐ฎ๐ฝ๐ฝ ๐ป๐ผ๐ฑ๐ฒ ๐๐ฎ๐บ๐ฝ๐น๐ฒ.๐ท๐
The options for the ๐๐๐ถ๐น.๐ฑ๐ฒ๐ฏ๐๐ด๐น๐ผ๐ด method include:
๐๐ฒ๐ฐ๐๐ถ๐ผ๐ป: Name of the debug function for this portion of app.
๐ฐ๐ฎ๐น๐น๐ฏ๐ฎ๐ฐ๐ธ: Called only once. Can be used to replace the logging function.
Notes on the ๐ก๐ข๐๐_๐๐๐๐จ๐ env var:
- It supports wildcard, so ๐ฎ๐ฝ๐ฝ* and * work.
- You can specify multiple sections like this: ๐ฎ๐ฝ๐ฝ,๐ณ๐,๐ป๐ฒ๐,๐๐น๐ Which helps in filtering the debug logs to what you only care about.
Did you learn something new today?
Like and share this post, and follow me for more!
Top comments (0)