Allow lazy Logger execution (#103)
This commit is contained in:
@@ -3,11 +3,18 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { Logger } from './types';
|
||||
import type { Logger, Logger$Data } from './types';
|
||||
|
||||
type ConsoleType = 'error' | 'log' | 'warn';
|
||||
|
||||
function apply (log: ConsoleType, type: string, values: Array<mixed>): void {
|
||||
const isFunction = require('./is/function');
|
||||
|
||||
function apply (log: ConsoleType, type: string, values: Logger$Data): void {
|
||||
if (values.length === 1 && isFunction(values[0])) {
|
||||
// flowlint-next-line unclear-type:off
|
||||
return apply(log, type, ((values[0]: any): Function)());
|
||||
}
|
||||
|
||||
console[log].apply(console, [new Date().toString(), type].concat(values));
|
||||
}
|
||||
|
||||
@@ -37,10 +44,10 @@ module.exports = function logger (_type: string): Logger {
|
||||
|
||||
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)
|
||||
? (...values: Logger$Data): void => apply('log', type, values)
|
||||
: (...values: Logger$Data): void => void 0,
|
||||
error: (...values: Logger$Data): void => apply('error', type, values),
|
||||
log: (...values: Logger$Data): void => apply('log', type, values),
|
||||
warn: (...values: Logger$Data): void => apply('warn', type, values)
|
||||
};
|
||||
};
|
||||
|
||||
@@ -38,6 +38,38 @@ describe('logger', () => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('logs to console.log with .log', () => {
|
||||
l.log('console.log test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'console.log test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs with mulptiple values', () => {
|
||||
l.log('test', 'a', 2);
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'test',
|
||||
'a',
|
||||
2
|
||||
);
|
||||
});
|
||||
|
||||
it('logs when called with function', () => {
|
||||
l.log(() => 'a function test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'a function test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to console.error with .error', () => {
|
||||
l.error('console.error test');
|
||||
|
||||
@@ -58,28 +90,6 @@ describe('logger', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to console.log with .log', () => {
|
||||
l.log('console.log test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'console.log test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to with mulptiple values', () => {
|
||||
l.log('test', 'a', 2);
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'test',
|
||||
'a',
|
||||
2
|
||||
);
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG specified', () => {
|
||||
l.debug('test');
|
||||
|
||||
|
||||
@@ -10,9 +10,11 @@ export interface ExtErrorInterface {
|
||||
stack: string;
|
||||
}
|
||||
|
||||
export type Logger$Data = Array<mixed | () => Array<mixed>>;
|
||||
|
||||
export type Logger = {
|
||||
debug: (...values: Array<mixed>) => void,
|
||||
error: (...values: Array<mixed>) => void,
|
||||
log: (...values: Array<mixed>) => void,
|
||||
warn: (...values: Array<mixed>) => void
|
||||
debug: (...values: Logger$Data) => void,
|
||||
error: (...values: Logger$Data) => void,
|
||||
log: (...values: Logger$Data) => void,
|
||||
warn: (...values: Logger$Data) => void
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user