-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logging functions #20
Conversation
* @returns An instance of `debug`. | ||
*/ | ||
export function createProjectLogger(projectName: string): Debugger { | ||
return globalLogger.extend(projectName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm choosing to wrap debug
in case we want to swap this out for something else in the future. UPDATE: Well, I guess the interface of the object that this functions returns will probably change if we do go with a different library, but hopefully we won't have to change this name.
bf4b91c
to
beb1e1a
Compare
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>
"prettier": "^2.2.1", | ||
"prettier-plugin-packagejson": "^2.2.11", | ||
"rimraf": "^3.0.2", | ||
"ts-jest": "^26.3.0", | ||
"stdio-mock": "^1.2.0", | ||
"ts-jest": "^28.0.8", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Getting this peer dependency warning:
warning " > ts-jest@28.0.8" has incorrect peer dependency "typescript@>=4.3".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed this by bumping TypeScript: #21
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is merged now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! I can approve it after the peer dependency warning is resolved
Ready for review again. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
For context behind this change: In order to write tests for the provider API that the extension exposes (MetaMask/metamask-extension#15556) I've found adding logging to the various libraries that the network layer uses to be useful. After discussing this with @Gudahtt we determined that it might be nice to have a unified logging solution across all of our libraries.
This commit introduces logging via the
debug
library. One key featureof 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 aproject-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:You could either use
projectLogger
anywhere in your project like this:Then you could run your tests, or whatever command you want to run, by
setting the
DEBUG
environment variable like this:And in the output you'd see something like:
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:Then add the following to
polling-block-tracker.ts
:Now you could run your command with:
or, for all
eth-block-tracker
log messages:And in the output you'd see something like:
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: