Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces logging via the `debug` library. One key feature of this library is that it allows you to assign a label to log messages. All log messages are suppressed and will not be shown by default, but the label you choose can be namespaced, and you can use these namespaces to selectively show the log messages you're interested in. With that in mind this commit defines at least two logging namespaces: a global `metamask` namespace for all projects that need logging and a project-level namespace. The way it works is this. Say your project is called `eth-block-tracker`. To add logging to your project, you'd add a file (call it `logging-utils.ts`) which contains: import { createProjectLogger } from "@metamask/utils"; export const projectLogger = createProjectLogger("eth-block-tracker"); You could either use `projectLogger` anywhere in your project like this: import { projectLogger as log } from "./logging-utils"; log("This is a log message"); Then you could run your tests, or whatever command you want to run, by setting the `DEBUG` environment variable like this: DEBUG=metamask:eth-block-tracker <command goes here> And in the output you'd see something like: metamask:eth-block-tracker This is a log message +0ms However if you wanted to namespace your log messages further — say you wanted to only show log messages for a `polling-block-tracker.ts` file — you could update `logging-utils.ts` like this: import { createProjectLogger, createModuleLogger } from "@metamask/utils"; export const projectLogger = createProjectLogger("eth-block-tracker"); export { createModuleLogger }; Then add the following to `polling-block-tracker.ts`: import { projectLogger, createModuleLogger } from "./logging-utils"; const log = createModuleLogger(projectLogger, "polling-block-tracker"); log("This is a log message"); Now you could run your command with: DEBUG=metamask:eth-block-tracker:polling-block-tracker <command goes here> or, for all `eth-block-tracker` log messages: DEBUG=metamask:eth-block-tracker:* <command goes here> And in the output you'd see something like: metamask:eth-block-tracker:polling-block-message This is a log message +0ms Finally if you wanted to show all log messages across all MetaMask projects that are also making use of this logging mechanism, you could say: DEBUG=metamask:* <command goes here>
- Loading branch information