@livy/util
Version:
Common utilities for the Livy logger
193 lines (192 loc) • 5.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConsoleAdapter = void 0;
const helpers_1 = require("./helpers");
const timer_1 = require("./timer");
/**
* @inheritdoc
*/
class ConsoleAdapter {
constructor(logger) {
this.counters = {};
this.timers = {};
this.indentation = 0;
this.logger = logger;
}
get indentationString() {
return ' '.repeat(this.indentation);
}
/**
* @inheritdoc
*/
count(label) {
if (typeof label === 'undefined') {
label = 'default';
}
this.counters[label] = (this.counters[label] || 0) + 1;
this.logger.debug(`${this.indentationString}console.count`, {
label,
count: this.counters[label]
});
}
/**
* @inheritdoc
*/
countReset(label) {
if (typeof label === 'undefined') {
label = 'default';
}
delete this.counters[label];
}
/**
* @inheritdoc
*/
debug(message, ...optionalParameters) {
this.logger.debug(`${this.indentationString}console.debug`, {
parameters: [message, ...optionalParameters]
});
}
/**
* @inheritdoc
*/
dir(object) {
this.logger.debug(`${this.indentationString}console.dir`, { dir: object });
}
/**
* @inheritdoc
*/
dirxml(...data) {
this.logger.debug(`${this.indentationString}console.dirxml`, {
objects: data
});
}
/**
* @inheritdoc
*/
error(message, ...optionalParameters) {
this.logger.error(`${this.indentationString}console.error`, {
parameters: [message, ...optionalParameters]
});
}
/**
* @inheritdoc
*/
group(label) {
this.logger.debug(`${this.indentationString}console.group`, { label });
this.indentation++;
}
/**
* The `console.groupCollapsed()` function is an alias for `console.group()`
*/
groupCollapsed(label) {
this.logger.debug(`${this.indentationString}console.groupCollapsed`, {
label
});
this.indentation++;
}
/**
* @inheritdoc
*/
groupEnd() {
if (this.indentation > 0) {
this.indentation--;
}
this.logger.debug(`${this.indentationString}console.groupEnd`);
}
/**
* @inheritdoc
*/
info(message, ...optionalParameters) {
this.logger.info(`${this.indentationString}console.info`, {
parameters: [message, ...optionalParameters]
});
}
/**
* @inheritdoc
*/
log(message, ...optionalParameters) {
this.logger.debug(`${this.indentationString}console.log`, {
parameters: [message, ...optionalParameters]
});
}
/**
* @inheritdoc
*/
table(tabularData, properties) {
try {
if (Array.isArray(tabularData) && typeof properties !== 'undefined') {
const propertiesArray = Array.isArray(properties)
? properties
: [properties];
tabularData = tabularData.map(entry => {
return (0, helpers_1.fromEntries)(Object.entries(entry).filter(([property]) => propertiesArray.includes(property)));
});
}
}
catch (_a) {
// Ignore invalid data
}
this.logger.debug(`${this.indentationString}console.table`, {
data: tabularData
});
}
/**
* @inheritdoc
*/
time(label) {
if (typeof label === 'undefined') {
label = 'default';
}
this.timers[label] = this.timers[label] || new timer_1.Timer();
if (this.timers[label].running()) {
return;
}
this.timers[label].start();
}
/**
* @inheritdoc
*/
timeEnd(label) {
if (typeof label === 'undefined') {
label = 'default';
}
const elapsed = label in this.timers ? this.timers[label].reset() : null;
this.logger.debug(`${this.indentationString}console.timeEnd`, {
label,
elapsed
});
delete this.timers[label];
}
/**
* @inheritdoc
*/
timeLog(label, ...data) {
if (typeof label === 'undefined') {
label = 'default';
}
const elapsed = label in this.timers ? this.timers[label].get() : null;
this.logger.debug(`${this.indentationString}console.timeLog`, {
label,
elapsed,
data
});
}
/**
* @inheritdoc
*/
trace(...data) {
this.logger.debug(`${this.indentationString}console.trace`, {
trace: new Error().stack,
data
});
}
/**
* @inheritdoc
*/
warn(message, ...optionalParameters) {
this.logger.warning(`${this.indentationString}console.warn`, {
parameters: [message, ...optionalParameters]
});
}
}
exports.ConsoleAdapter = ConsoleAdapter;