@comake/skl-js-engine
Version:
Standard Knowledge Language Javascript Engine
119 lines • 4.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
const util_1 = require("util");
class Logger {
constructor(isDebug, metadata, options) {
this.isDebug = true;
this.metadata = metadata;
this.maxStringLength = options?.maxStringLength ?? 2000;
this.maxArrayItems = options?.maxArrayItems ?? 10;
}
/**
* Returns a singleton instance of the logger. The logger honours the DEBUG env variable unless
* explicitly overridden via the `isDebug` parameter.
*/
static getInstance(isDebug, metadata, options) {
if (!Logger.instance) {
// eslint-disable-next-line no-process-env
const debugEnabled = isDebug ?? process.env.DEBUG === 'true';
Logger.instance = new Logger(debugEnabled, metadata ?? {}, options);
}
return Logger.instance;
}
/**
* Merges the given metadata with the existing metadata attached to the logger.
*/
setMetadata(metadata) {
this.metadata = { ...this.metadata, ...metadata };
}
getMetadataString() {
if (!this.metadata || Object.keys(this.metadata).length === 0) {
return undefined;
}
return JSON.stringify(this.metadata);
}
// ---------------------------------------------------------------------------
// Public logging APIs
// ---------------------------------------------------------------------------
log(...args) {
if (this.isDebug) {
const metadataString = this.getMetadataString();
const logArgs = metadataString ? [...this.formatArgs(args), metadataString] : this.formatArgs(args);
// eslint-disable-next-line no-console
console.log(...logArgs);
}
}
error(...args) {
if (this.isDebug) {
const metadataString = this.getMetadataString();
const logArgs = metadataString ? [...this.formatArgs(args), metadataString] : this.formatArgs(args);
// eslint-disable-next-line no-console
console.error(...logArgs);
}
}
debug(...args) {
if (this.isDebug) {
const metadataString = this.getMetadataString();
const logArgs = metadataString ? [...this.formatArgs(args), metadataString] : this.formatArgs(args);
// eslint-disable-next-line no-console
console.debug(...logArgs);
}
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
/**
* Applies safe formatting to every argument before it is passed to the console. Large strings are
* truncated, large arrays/objects are abbreviated and circular references are handled gracefully
* by `util.inspect`.
*/
formatArgs(args) {
return args.map((arg) => this.formatValue(arg));
}
formatValue(value) {
try {
if (typeof value === 'string') {
return this.truncateString(value);
}
if (Array.isArray(value)) {
return JSON.stringify(this.truncateArray(value));
}
if (typeof value === 'object' && value !== null) {
// For objects we rely on util.inspect which gives us fine-grained control over depth,
// array length and string length.
return (0, util_1.inspect)(value, {
depth: 6,
maxArrayLength: this.maxArrayItems,
maxStringLength: this.maxStringLength,
breakLength: 120,
compact: false,
colors: true
});
}
// Primitives, functions, etc. are returned as-is.
return value;
}
catch (err) {
// In the unlikely event that formatting fails, fall back to a best-effort stringify.
// eslint-disable-next-line no-console
console.warn('Logger: failed to format value', err);
return String(value);
}
}
truncateString(str) {
if (str.length <= this.maxStringLength) {
return str;
}
return `${str.slice(0, this.maxStringLength)}...(truncated ${str.length - this.maxStringLength} chars)`;
}
truncateArray(arr) {
if (arr.length <= this.maxArrayItems) {
return arr.map((item) => this.formatValue(item));
}
const displayedItems = arr.slice(0, this.maxArrayItems).map((item) => this.formatValue(item));
return [...displayedItems, `...(truncated ${arr.length - this.maxArrayItems} items)`];
}
}
exports.Logger = Logger;
//# sourceMappingURL=logger.js.map