console-log-interceptor
Version:
console.log interceptor for grab your console.logs
82 lines (81 loc) • 3.02 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LogInterceptor = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const dateFormatter_1 = require("./dateFormatter");
class LogInterceptor {
constructor() {
this.origin = console.log;
this.logs = [];
this.datetimeOption = undefined;
}
clear() {
this.logs = [];
this.datetimeOption = undefined;
}
intercept(option) {
this.clear();
this.datetimeOption = option === null || option === void 0 ? void 0 : option.datetime;
// redefine console.log
console.log = (...data) => {
this._appendLog(data);
this.origin(...data);
};
}
stopIntercept(clear) {
// take back to the original console.log
console.log = this.origin;
if (clear)
this.clear();
}
_appendLog(...data) {
const log = data
.map(d => (d + "").replace(/\x1B\[[0-9]*m/gi, "")) // remove console color
.filter(Boolean)
.join(' ');
this.logs.push({
timestamp: new Date().getTime(),
log
});
}
get() {
return this.logs.map(logData => {
var _a;
const { timestamp, log } = logData;
if (this.datetimeOption === null)
return log;
else {
const date = new Date(timestamp);
const dateString = ((_a = this.datetimeOption) === null || _a === void 0 ? void 0 : _a.format) ?
typeof this.datetimeOption.format === "function" ?
this.datetimeOption.format(date)
:
(0, dateFormatter_1.dateFormatter)(date, this.datetimeOption.format)
:
`${date.toLocaleString()}`;
return `${dateString} ${log}`;
}
});
}
save(options) {
const slashIndex = options.path.lastIndexOf('/');
const dir = options.path.slice(0, slashIndex);
if (!fs_extra_1.default.existsSync(dir)) {
fs_extra_1.default.mkdirSync(dir, { recursive: true });
}
const logs = this.get();
const header = options.header ? options.header + '\n' : "";
const footer = options.footer ? '\n' + options.footer + '\n\n' : "";
const data = options.json ?
JSON.stringify(logs, options.json.replacer, options.json.space)
:
header + logs.join('\n') + (footer || (logs.length > 0 ? '\n\n' : ''));
fs_extra_1.default.writeFileSync(options.path, data, { encoding: 'utf-8', flag: options.append !== false ? 'a' : 'w' });
if (options.clear !== false)
this.clear();
}
}
exports.LogInterceptor = LogInterceptor;