Skip to content

Commit

Permalink
Fix colors in debug logs (#3592)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorefnon authored and kibertoad committed Dec 28, 2019
1 parent 9b37c94 commit 3914bf5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/logger.js
Expand Up @@ -7,9 +7,17 @@ const { isFunction, isNil, isString } = require('lodash');
class Logger {
constructor(config) {
const {
log: { debug, warn, error, deprecate, inspectionDepth } = {},
log: {
debug,
warn,
error,
deprecate,
inspectionDepth,
enableColors,
} = {},
} = config;
this._inspectionDepth = inspectionDepth || 5;
this._enableColors = resolveIsEnabledColors(enableColors);
this._debug = debug;
this._warn = warn;
this._error = error;
Expand All @@ -27,7 +35,10 @@ class Logger {
}

if (!isString(message)) {
message = inspect(message, { depth: this._inspectionDepth });
message = inspect(message, {
depth: this._inspectionDepth,
colors: this._enableColors,
});
}

console.log(colorFn ? colorFn(message) : message);
Expand All @@ -52,4 +63,16 @@ class Logger {
}
}

function resolveIsEnabledColors(enableColorsParam) {
if (!isNil(enableColorsParam)) {
return enableColorsParam;
}

if (process && process.stdout) {
return process.stdout.isTTY;
}

return false;
}

module.exports = Logger;
1 change: 1 addition & 0 deletions types/index.d.ts
Expand Up @@ -1854,6 +1854,7 @@ declare namespace Knex {
error?: LogFn;
debug?: LogFn;
inspectionDepth?: number;
enableColors?: boolean;
deprecate?: (method: string, alternative: string) => void;
}

Expand Down

0 comments on commit 3914bf5

Please sign in to comment.