winston-format-debug
Version:
Debug formatter for Winston
136 lines • 3.91 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.dateToString = exports.applyColors = exports.rpad = exports.lpad = void 0;
const chalk_1 = __importDefault(require("chalk"));
function lpad(str, count, fill = ' ') {
str = `${str}`;
while (str.length < count) {
str = fill + str;
}
return str;
}
exports.lpad = lpad;
function rpad(str, count, fill = ' ') {
str = `${str}`;
while (str.length < count) {
str = str + fill;
}
return str;
}
exports.rpad = rpad;
// Applies one or more colors to a message, and returns the colorized message.
function applyColors(message, colorList) {
if (!message) {
return message;
}
if (typeof colorList === 'string') {
message = applyColor(message, colorList);
}
else {
for (const color of colorList) {
message = applyColor(message, color);
}
}
return message;
}
exports.applyColors = applyColors;
function applyColor(message, color) {
const chalkColor = COLOR_TRANSLATIONS[color];
if (chalkColor) {
return chalk_1.default[chalkColor](message);
}
else if (color.startsWith('#')) {
return chalk_1.default.hex(color)(message);
}
else {
return message;
}
}
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
function dateToString(date) {
const time = [
lpad(date.getHours(), 2, '0'),
lpad(date.getMinutes(), 2, '0'),
lpad(date.getSeconds(), 2, '0'),
].join(':');
return [MONTHS[date.getMonth()], date.getDate(), time].join(' ');
}
exports.dateToString = dateToString;
const COLOR_TRANSLATIONS = {
// Valid colors from winston
black: 'black',
red: 'red',
green: 'green',
yellow: 'yellow',
blue: 'blue',
magenta: 'magenta',
cyan: 'cyan',
white: 'white',
blackBG: 'bgBlack',
redBG: 'bgRed',
greenBG: 'bgGreen',
yellowBG: 'bgYellow',
blueBG: 'bgBlue',
magentaBG: 'bgMagenta',
cyanBG: 'bgCyan',
whiteBG: 'bgWhite',
bold: 'bold',
dim: 'dim',
italic: 'italic',
underline: 'underline',
inverse: 'inverse',
hidden: 'hidden',
strikethrough: 'strikethrough',
// Chalk colors
gray: 'gray',
grey: 'grey',
bgBlack: 'bgBlack',
bgRed: 'bgRed',
bgGreen: 'bgGreen',
bgYellow: 'bgYellow',
bgBlue: 'bgBlue',
bgMagenta: 'bgMagenta',
bgCyan: 'bgCyan',
bgWhite: 'bgWhite',
bgGray: 'bgGray',
bgGrey: 'bgGrey',
blackBright: 'blackBright',
redBright: 'redBright',
greenBright: 'greenBright',
yellowBright: 'yellowBright',
blueBright: 'blueBright',
magentaBright: 'magentaBright',
cyanBright: 'cyanBright',
whiteBright: 'whiteBright',
bgBlackBright: 'bgBlackBright',
bgRedBright: 'bgRedBright',
bgGreenBright: 'bgGreenBright',
bgYellowBright: 'bgYellowBright',
bgBlueBright: 'bgBlueBright',
bgMagentaBright: 'bgMagentaBright',
bgCyanBright: 'bgCyanBright',
bgWhiteBright: 'bgWhiteBright',
// These are from the "colors" library, which we used to use.
brightRed: 'redBright',
brightGreen: 'greenBright',
brightYellow: 'yellowBright',
brightBlue: 'blueBright',
brightMagenta: 'magentaBright',
brightCyan: 'cyanBright',
brightWhite: 'whiteBright',
bgBrightRed: 'bgRedBright',
bgBrightGreen: 'bgGreenBright',
bgBrightYellow: 'bgYellowBright',
bgBrightBlue: 'bgBlueBright',
bgBrightMagenta: 'bgMagentaBright',
bgBrightCyan: 'bgCyanBright',
bgBrightWhite: 'bgWhiteBright',
rainbow: 'whiteBright',
zebra: 'whiteBright',
america: 'whiteBright',
trap: 'whiteBright',
};
//# sourceMappingURL=utils.js.map