UNPKG

999 BJavaScriptView Raw
1/* eslint no-console:0 */
2
3const color = require('colorette');
4const { isFunction, isNil } = require('lodash');
5
6function log(message, userFn, colorFn) {
7 if (!isNil(userFn) && !isFunction(userFn)) {
8 throw new TypeError('Extensions to knex logger must be functions!');
9 }
10
11 if (isFunction(userFn)) {
12 userFn(message);
13 return;
14 }
15
16 console.log(colorFn ? colorFn(message) : message);
17}
18
19class Logger {
20 constructor(config) {
21 const { log: { debug, warn, error, deprecate } = {} } = config;
22
23 this._debug = debug;
24 this._warn = warn;
25 this._error = error;
26 this._deprecate = deprecate;
27 }
28
29 debug(message) {
30 log(message, this._debug);
31 }
32
33 warn(message) {
34 log(message, this._warn, color.yellow);
35 }
36
37 error(message) {
38 log(message, this._error, color.red);
39 }
40
41 deprecate(method, alternative) {
42 const message = `${method} is deprecated, please use ${alternative}`;
43
44 log(message, this._deprecate, color.yellow);
45 }
46}
47
48module.exports = Logger;