-
-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './collections'; | ||
export * from './json'; | ||
export * from './logging'; | ||
export * from './misc'; | ||
export * from './time'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { MockWritable } from 'stdio-mock'; | ||
import { createProjectLogger, createModuleLogger } from './logging'; | ||
|
||
describe('logging', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers({ now: new Date('2022-01-01') }); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.useRealTimers(); | ||
}); | ||
|
||
describe('createProjectLogger', () => { | ||
it('returns an object that can be used for logging', () => { | ||
const writer = new MockWritable(); | ||
const log = createProjectLogger('some-project'); | ||
log.log = writer.write.bind(writer); | ||
log.enabled = true; | ||
// Typecast: The Debugger type is wrong and does not include a `useColors` | ||
// property. | ||
(log as any).useColors = false; | ||
|
||
log('Some message'); | ||
|
||
expect(writer.data()).toStrictEqual([ | ||
'2022-01-01T00:00:00.000Z metamask:some-project Some message', | ||
]); | ||
}); | ||
}); | ||
|
||
describe('createModuleLogger', () => { | ||
it('returns an object that can be used for logging', () => { | ||
const writer = new MockWritable(); | ||
const projectLogger = createProjectLogger('some-project'); | ||
const log = createModuleLogger(projectLogger, 'some-module'); | ||
log.log = writer.write.bind(writer); | ||
log.enabled = true; | ||
// Typecast: The Debugger type is wrong and does not include a `useColors` | ||
// property. | ||
(log as any).useColors = false; | ||
|
||
log('Some message'); | ||
|
||
expect(writer.data()).toStrictEqual([ | ||
'2022-01-01T00:00:00.000Z metamask:some-project:some-module Some message', | ||
]); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import debug, { Debugger } from 'debug'; | ||
|
||
const globalLogger = debug('metamask'); | ||
|
||
/** | ||
* Creates a logger via the `debug` library whose log messages will be tagged | ||
* using the name of your project. By default, such messages will be | ||
* suppressed, but you can reveal them by setting the `DEBUG` environment | ||
* variable to `metamask:<projectName>`. You can also set this variable to | ||
* `metamask:*` if you want to see log messages from all MetaMask projects that | ||
* are also using this function to create their loggers. | ||
* | ||
* @param projectName - The name of your project. This should be the name of | ||
* your NPM package if you're developing one. | ||
* @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 commentThe reason will be displayed to describe this comment to others. Learn more. I'm choosing to wrap |
||
} | ||
|
||
/** | ||
* Creates a logger via the `debug` library which is derived from the logger for | ||
* the whole project whose log messages will be tagged using the name of your | ||
* module. By default, such messages will be suppressed, but you can reveal them | ||
* by setting the `DEBUG` environment variable to | ||
* `metamask:<projectName>:<moduleName>`. You can also set this variable to | ||
* `metamask:<projectName>:*` if you want to see log messages from the project, | ||
* or `metamask:*` if you want to see log messages from all MetaMask projects. | ||
* | ||
* @param projectLogger - The logger created via {@link createProjectLogger}. | ||
* @param moduleName - The name of your module. You could use the name of the | ||
* file where you're using this logger or some other name. | ||
* @returns An instance of `debug`. | ||
*/ | ||
export function createModuleLogger( | ||
projectLogger: Debugger, | ||
moduleName: string, | ||
): Debugger { | ||
return projectLogger.extend(moduleName); | ||
} |
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:
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.