UNPKG

class-logger

Version:

Boilerplate-free decorator-based class logging

118 lines (117 loc) 3.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fast_safe_stringify_1 = require("fast-safe-stringify"); class ClassLoggerFormatterService { constructor() { this.placeholderNotAvailable = 'N/A'; this.placeholderUndefined = 'undefined'; } start(data) { let message = this.base(data); if (this.includeComplex(data.include.args, 'start')) { message += this.args(data); } if (this.includeComplex(data.include.classInstance, 'start')) { message += this.classInstance(data); } message += this.final(); return message; } end(data) { let message = this.base(data); message += this.operation(data); if (this.includeComplex(data.include.args, 'end')) { message += this.args(data); } if (this.includeComplex(data.include.classInstance, 'end')) { message += this.classInstance(data); } if (data.include.result) { message += this.result(data); } message += this.final(); return message; } base({ className, propertyName }) { return `${className}.${propertyName.toString()}`; } operation({ error }) { return error ? ' -> error' : ' -> done'; } args({ args }) { return `. Args: ${this.valueToString(args)}`; } classInstance({ classInstance }) { return `. Class instance: ${this.complexObjectToString(classInstance)}`; } result({ result }) { return `. Res: ${this.valueToString(result)}`; } final() { return '.'; } complexObjectToString(obj) { if (typeof obj !== 'object') { return this.placeholderNotAvailable; } if (obj === null) { return fast_safe_stringify_1.default(obj); } const classInstanceFiltered = {}; let keys = Object.keys(obj); if (obj instanceof Map || obj instanceof Set) { keys = [...obj.keys()]; } keys.forEach((key) => { const value = obj[key]; if (typeof value === 'function') { return; } classInstanceFiltered[key] = typeof value === 'object' && !this.isPlainObjectOrArray(value) ? this.complexObjectToString(value) : value; }); return `${obj.constructor.name} ${fast_safe_stringify_1.default(classInstanceFiltered)}`; } valueToString(val) { if (val === undefined) { return this.placeholderUndefined; } if (typeof val !== 'object') { return val.toString(); } if (val instanceof Error) { return this.errorToString(val); } if (!this.isPlainObjectOrArray(val)) { return this.complexObjectToString(val); } if (Array.isArray(val)) { const arrayWithStringifiedElements = val.map(this.valueToString.bind(this)); return `[${arrayWithStringifiedElements.join(', ')}]`; } return fast_safe_stringify_1.default(val); } includeComplex(includeComplex, type) { if (typeof includeComplex === 'boolean') { return includeComplex; } return includeComplex[type]; } isPlainObjectOrArray(obj) { if (!obj) { return false; } const proto = Object.getPrototypeOf(obj); return proto === Object.prototype || proto === Array.prototype; } errorToString(error) { const data = { code: error.code, message: error.message, name: error.name, stack: error.stack, }; return `${error.constructor.name} ${fast_safe_stringify_1.default(data)}`; } } exports.ClassLoggerFormatterService = ClassLoggerFormatterService;