winston-format-debug
Version:
Debug formatter for Winston
158 lines • 6.69 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Format = exports.DebugFormat = void 0;
const exceptionUtils_1 = require("./exceptionUtils");
const path = __importStar(require("path"));
const triple_beam_1 = require("triple-beam");
const utils_1 = require("./utils");
const SKIP_VALUES = ['level', 'message', '@timestamp', 'name'];
/**
* Debug formatter intended for logging to console. This is roughly a port
* of bunyan-debug-stream.
*/
class DebugFormat {
constructor(options = {}) {
var _a;
this.options = options;
const scriptName = process.argv[1] || process.argv[0];
this._processName = scriptName ? path.basename(scriptName, path.extname(scriptName)) : '';
const levels = options.levels || triple_beam_1.configs.npm.levels;
const levelNames = Object.keys(levels);
this._maxLevelLength = Math.max.apply(Math, levelNames.map((s) => s.length));
this._colors = options.colors === false ? false : options.colors || triple_beam_1.configs.npm.colors;
this._basepath = process.cwd();
const indentSize = (_a = options.indent) !== null && _a !== void 0 ? _a : 4;
this._indent = ''.padEnd(indentSize, ' ');
}
_skipKey(key, value) {
if (this.options.skip !== this._skip) {
const skip = this.options.skip;
this._skip = skip;
if (Array.isArray(skip)) {
this._skipFn = (key) => skip.includes(key);
}
else {
this._skipFn = skip;
}
}
if (this._skipFn) {
return this._skipFn(key, value);
}
return false;
}
_getPrefix(info, options) {
var _a, _b;
let name = (_a = options.processName) !== null && _a !== void 0 ? _a : this._processName;
if (info.name) {
if (name) {
name += ':';
}
name += `${info.name}`;
}
const date = (0, utils_1.dateToString)(new Date());
const level = info[triple_beam_1.LEVEL] || info.level || 'info';
const pid = ((_b = options.showPID) !== null && _b !== void 0 ? _b : true) ? `[${process.pid}]` : '';
const space = name || pid ? ' ' : '';
// If `info.level` is defined, use it for the level label - just in
// case some previous formatter has colorized it or done something
// else exciting.
const levelLabel = (0, utils_1.rpad)(`${(info.level || level).toUpperCase()}:`, this._maxLevelLength + 1);
return `${date} ${name}${pid}${space}${levelLabel}`;
}
_getValues(info, options) {
const values = [];
for (const key of Object.keys(info)) {
// Skip fields we don't care about
if (SKIP_VALUES.includes(key) || this._skipKey(key, info[key])) {
continue;
}
const value = info[key];
if (value instanceof Error) {
values.push(`${this._indent}${key}: ${this._formatError(value, options)}`);
}
else {
const valueString = value != null && JSON.stringify(value);
if (valueString) {
// Make sure value isn't too long.
const cols = 'terminalWidth' in this.options
? this.options.terminalWidth
: process.stdout.columns || 80;
let line = `${this._indent}${key}: ${valueString}`;
if (cols && line.length >= cols) {
line = line.slice(0, cols - 3) + '...';
}
values.push(line);
}
}
}
return values;
}
_formatError(err, options) {
const formatted = (0, exceptionUtils_1.formatException)(err, {
color: !!options.colors,
basePath: options.basePath || this._basepath,
maxLines: options.maxExceptionLines,
});
return formatted.split('\n').join(`\n${this._indent}`);
}
transform(info, options) {
const level = info[triple_beam_1.LEVEL] || info.level || 'info';
let prefix = this._getPrefix(info, options);
let message = info.message;
let valuesString = this._getValues(info, options).join('\n');
const colors = options.colors === false ? false : options.colors || this._colors;
if (colors && colors[level]) {
const colorizePrefix = !!options.colorizePrefix;
const colorizeMessage = !('colorizeMessage' in options) || options.colorizeMessage;
const colorizeValues = !('colorizeValues' in options) || options.colorizeValues;
if (colorizePrefix) {
prefix = (0, utils_1.applyColors)(prefix, colors[level]);
}
if (colorizeMessage) {
message = (0, utils_1.applyColors)(message, colors[level]);
}
if (colorizeValues && valuesString) {
valuesString = (0, utils_1.applyColors)(valuesString, colors[level]);
}
}
let result = `${prefix} ${message}`;
if (valuesString.length) {
result = `${result}\n${valuesString}`;
}
info[triple_beam_1.MESSAGE] = result;
return info;
}
}
exports.DebugFormat = DebugFormat;
function createDebugFormat(options = {}) {
return new DebugFormat(options);
}
exports.default = createDebugFormat;
//
// Attach the Colorizer for registration purposes
//
exports.Format = DebugFormat; // tslint:disable-line
//# sourceMappingURL=index.js.map