Expand logger for debug (#60)
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ logger (type: string): Logger
|
||||
```
|
||||
|
||||
|
||||
Returns a `Logger` that has `.log`, `.error` and `.warn` methods. Loggins is done with a consistent prefix (type of logger, date) followed by the actual message uning the underlying console.
|
||||
Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (constrolled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.
|
||||
|
||||
```js
|
||||
const l = require('@polkadot/util/logger')('test');
|
||||
|
||||
+16
-8
@@ -1,15 +1,11 @@
|
||||
// ISC, Copyright 2017-2018 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
type Logger = {
|
||||
error: (...values: Array<mixed>) => void,
|
||||
log: (...values: Array<mixed>) => void,
|
||||
warn: (...values: Array<mixed>) => void
|
||||
};
|
||||
import type { Logger } from './types';
|
||||
|
||||
type LogType = 'error' | 'log' | 'warn';
|
||||
type ConsoleType = 'error' | 'log' | 'warn';
|
||||
|
||||
function apply (log: LogType, type: string, values: Array<mixed>): void {
|
||||
function apply (log: ConsoleType, type: string, values: Array<mixed>): void {
|
||||
console[log].apply(console, [new Date().toString(), type].concat(values));
|
||||
}
|
||||
|
||||
@@ -18,7 +14,7 @@ function apply (log: LogType, type: string, values: Array<mixed>): void {
|
||||
@signature logger (type: string): Logger
|
||||
@summary Creates a consistent log interface for messages
|
||||
@description
|
||||
Returns a `Logger` that has `.log`, `.error` and `.warn` methods. Loggins is done with a consistent prefix (type of logger, date) followed by the actual message uning the underlying console.
|
||||
Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (constrolled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.
|
||||
@example
|
||||
const l = require('@polkadot/util/logger')('test');
|
||||
|
||||
@@ -26,8 +22,20 @@ function apply (log: LogType, type: string, values: Array<mixed>): void {
|
||||
*/
|
||||
module.exports = function logger (_type: string): Logger {
|
||||
const type = ` ${_type.toUpperCase()}:`.slice(-16);
|
||||
let isDebug;
|
||||
|
||||
try {
|
||||
const debugList = process.env.DEBUG.split(',');
|
||||
|
||||
isDebug = !!debugList.find((entry) => _type.indexOf(entry) === 0);
|
||||
} catch (error) {
|
||||
isDebug = false;
|
||||
}
|
||||
|
||||
return {
|
||||
debug: isDebug
|
||||
? (...values: Array<mixed>): void => apply('log', type, values)
|
||||
: (...values: Array<mixed>): void => void 0,
|
||||
error: (...values: Array<mixed>): void => apply('error', type, values),
|
||||
log: (...values: Array<mixed>): void => apply('log', type, values),
|
||||
warn: (...values: Array<mixed>): void => apply('warn', type, values)
|
||||
|
||||
@@ -6,9 +6,14 @@ describe('logger', () => {
|
||||
let dateMatch;
|
||||
let prefixMatch;
|
||||
let l;
|
||||
let ln;
|
||||
let spy;
|
||||
|
||||
beforeEach(() => {
|
||||
ln = logger('notDebug');
|
||||
|
||||
process.env.DEBUG = 'test';
|
||||
|
||||
l = logger('test');
|
||||
|
||||
spy = {
|
||||
@@ -67,4 +72,40 @@ describe('logger', () => {
|
||||
2
|
||||
);
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG specified', () => {
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'test'
|
||||
);
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG partial specified', () => {
|
||||
l = logger('testing');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
expect.stringMatching(/TESTING:/),
|
||||
'test'
|
||||
);
|
||||
});
|
||||
|
||||
it('does not debug log when non-matching DEBUG specified', () => {
|
||||
process.env.DEBUG = 'blah';
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not debug log when no DEBUG specified', () => {
|
||||
ln.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,3 +7,10 @@ export interface ExtErrorInterface {
|
||||
message: string;
|
||||
stack: string;
|
||||
}
|
||||
|
||||
export type Logger = {
|
||||
debug: (...values: Array<mixed>) => void,
|
||||
error: (...values: Array<mixed>) => void,
|
||||
log: (...values: Array<mixed>) => void,
|
||||
warn: (...values: Array<mixed>) => void
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user