js-tests-results-collector
Version:
Universal test results collector for Jest, Jasmine, Mocha, Cypress, and Playwright that sends results to Buddy Works API
42 lines (35 loc) • 1.16 kB
JavaScript
class Logger {
constructor(prefix = 'JS-Test-Collector') {
this.prefix = prefix;
this.debugEnabled = process.env.BUDDY_LOGGER_DEBUG === '1';
}
// Safe JSON stringify that handles circular references
safeStringify(obj) {
if (obj === null || obj === undefined) return '';
const cache = new Set();
return JSON.stringify(obj, (key, value) => {
if (typeof value === 'object' && value !== null) {
if (cache.has(value)) {
return '[Circular]';
}
cache.add(value);
}
return value;
}, 2);
}
debug(message, data = null) {
if (this.debugEnabled) {
console.log(`[${this.prefix}] DEBUG: ${message}`, data ? this.safeStringify(data) : '');
}
}
info(message, data = null) {
console.log(`[${this.prefix}] INFO: ${message}`, data ? this.safeStringify(data) : '');
}
warn(message, data = null) {
console.warn(`[${this.prefix}] WARN: ${message}`, data ? this.safeStringify(data) : '');
}
error(message, error = null) {
console.error(`[${this.prefix}] ERROR: ${message}`, error ? error.stack || error : '');
}
}
module.exports = Logger;