Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(logger): fix regression #651 * fix(types): better types for logger
- Loading branch information
Showing
4 changed files
with
186 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
interface Logger { | ||
info: (message: string, additional: Object) => void | ||
debug: (message: string, additional: Object) => void | ||
error: (message: string, additional: Object) => void | ||
export interface Logger { | ||
info: (message: string, ...additional: Object[]) => void; | ||
debug: (message: string, ...additional: Object[]) => void; | ||
error: (message: string, ...additional: Object[]) => void; | ||
warn: (message: string, ...additional: Object[]) => void; | ||
verbose: (message: string, ...additional: Object[]) => void; | ||
} | ||
interface Options { | ||
level?: 'error' | 'warn' | 'info' | 'verbose' | 'debug' | string; | ||
} | ||
|
||
export default Logger | ||
export default function logger(options?: Options): Logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,64 @@ | ||
const lazyPrint = (value) => { | ||
if (typeof value === 'function') { return value() } | ||
const lazyPrint = value => { | ||
if (typeof value === 'function') { | ||
return value() | ||
} | ||
|
||
return value | ||
} | ||
|
||
function logger ({ | ||
level = 'error' | ||
} = {}) { | ||
return { | ||
error (message, ...additional) { | ||
if (!level.includes('debug', 'verbose', 'info', 'warn', 'error')) return | ||
console.error({ | ||
message: lazyPrint(message), | ||
...additional.map(lazyPrint) | ||
}) | ||
}, | ||
warn (message, ...additional) { | ||
if (!level.includes('debug', 'verbose', 'info', 'warn')) return | ||
console.warn({ | ||
message: lazyPrint(message), | ||
...additional.map(lazyPrint) | ||
}) | ||
}, | ||
info (message, additional) { | ||
if (!level.includes('debug', 'verbose', 'info')) return | ||
console.info({ | ||
message: lazyPrint(message), | ||
...additional.map(lazyPrint) | ||
}) | ||
}, | ||
verbose (message, additional) { | ||
if (!level.includes('debug', 'verbose')) return | ||
console.debug({ | ||
message: lazyPrint(message), | ||
...additional.map(lazyPrint) | ||
}) | ||
}, | ||
debug (message, additional) { | ||
if (level !== 'debug') return | ||
console.debug({ | ||
const logLevels = { | ||
debug: [ | ||
['debug', 'debug'], | ||
['verbose', 'debug'], | ||
['info', 'info'], | ||
['error', 'error'], | ||
['warn', 'warn'] | ||
], | ||
verbose: [ | ||
['verbose', 'debug'], | ||
['info', 'info'], | ||
['error', 'error'], | ||
['warn', 'warn'] | ||
], | ||
info: [ | ||
['info', 'info'], | ||
['error', 'error'], | ||
['warn', 'warn'] | ||
], | ||
warn: [ | ||
['warn', 'warn'], | ||
['error', 'error'] | ||
], | ||
error: [['error', 'error']], | ||
none: [] | ||
} | ||
|
||
const print = | ||
fn => | ||
(message, ...additional) => | ||
console[fn]({ | ||
message: lazyPrint(message), | ||
...additional.map(lazyPrint) | ||
...(Array.isArray(additional) ? additional.map(lazyPrint) : []) | ||
}) | ||
} | ||
|
||
const NO_OP = () => {} | ||
|
||
function logger ({ level = 'error' } = {}) { | ||
const levels = logLevels[level] | ||
|
||
const logger = { | ||
error: NO_OP, | ||
debug: NO_OP, | ||
info: NO_OP, | ||
verbose: NO_OP, | ||
warn: NO_OP | ||
} | ||
|
||
if (!levels) return logger | ||
|
||
for (const [level2, consoleMethod] of levels) logger[level2] = print(consoleMethod) | ||
|
||
return logger | ||
} | ||
|
||
module.exports = logger |