@livy/ansi-line-formatter
Version:
Formats Livy log records as single lines with terminal highlighting
98 lines (97 loc) • 2.96 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnsiLineFormatter = void 0;
const line_formatter_1 = require("@livy/line-formatter");
const helpers_1 = require("@livy/util/lib/helpers");
const chalk_1 = __importDefault(require("chalk"));
const emphasize = require('emphasize');
/**
* Formats log records as single lines with terminal highlighting
*/
class AnsiLineFormatter extends line_formatter_1.LineFormatter {
constructor({ decorated, ...options } = {}) {
super(options);
this.decorated = decorated;
}
/**
* @inheritdoc
*/
formatDatetime(datetime) {
const timestamp = super.formatDatetime(datetime);
if (this.shouldDecorate()) {
return chalk_1.default.dim(timestamp);
}
else {
return timestamp;
}
}
/**
* @inheritdoc
*/
formatLevel(level) {
const formatted = super.formatLevel(level);
if (!this.shouldDecorate()) {
return formatted;
}
else {
let color;
switch (level) {
case 'emergency':
color = 'red';
break;
case 'alert':
color = 'red';
break;
case 'critical':
color = 'red';
break;
case 'error':
color = 'red';
break;
case 'warning':
color = 'yellow';
break;
case 'notice':
color = 'blue';
break;
case 'info':
color = 'blue';
break;
case 'debug':
color = 'blue.dim';
break;
// istanbul ignore next: This should never happen, but is included for type safety
default:
return formatted;
}
return chalk_1.default `{${color} ${formatted}}`;
}
}
/**
* @inheritdoc
*/
formatData(object, ignoreEmpty) {
if (helpers_1.isEmpty(object) && ignoreEmpty) {
return '';
}
const stringified = super.formatData(object);
if (this.shouldDecorate()) {
return emphasize.highlight('json', stringified).value;
}
else {
return stringified;
}
}
/**
* Check whether the formatter should use ANSI codes to decorate log records
*/
shouldDecorate() {
return !!(typeof this.decorated !== 'undefined'
? this.decorated
: chalk_1.default.supportsColor);
}
}
exports.AnsiLineFormatter = AnsiLineFormatter;