UNPKG

@valuabletouch/winston-seq

Version:
278 lines 9.39 kB
'use strict'; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Transport = void 0; var seq_logging_1 = require("seq-logging"); var winston_transport_1 = __importDefault(require("winston-transport")); function defaultLevelMapper(level) { switch (level === null || level === void 0 ? void 0 : level.toLowerCase()) { case 'verbose': case 'silly': return 'Verbose'; case 'debug': return 'Debug'; case 'info': return 'Information'; case 'warn': return 'Warning'; case 'error': return 'Error'; /** Non standart */ case 'fatal': return 'Fatal'; default: return 'Information'; } } function isError(obj) { var _a; if (!obj) { return false; } if (obj instanceof Error) { return true; } if (((_a = obj.constructor) === null || _a === void 0 ? void 0 : _a.name) === 'Error') { return true; } // quack-quack if (typeof obj.name === 'string' && typeof obj.message === 'string' && typeof obj.stack === 'string') { return true; } return false; } function isPrimitive(val) { if (val === null) { return true; } switch (typeof val) { case 'undefined': case 'string': case 'number': case 'bigint': case 'boolean': return true; default: return false; } } function isComplex(val) { return val && (typeof val === 'object' || typeof val === 'function'); } function formatMeta(options, meta) { var errors = []; var allValues = []; return { properties: format(meta, '$root', errors, allValues, options), errors: errors }; } function getErrorStack(err, id) { if (!err) { return "[" + id + "]: <No stack>"; } var stack = typeof err.stack !== 'undefined' ? err.stack : err.toString(); return "[" + id + "]: " + stack; } function format(val, path, errors, allValues, options) { if (val == null) { // tslint:disable-line return null; } var circularResult = checkCircular(val, path, allValues); if (circularResult) { return circularResult; } if (isError(val)) { var id = errors.length; errors.push({ error: val, id: id }); return formatError(val, id); } if (isPrimitive(val)) { return val; } if (val instanceof Date) { return formatDate(val); } if (val instanceof Buffer) { return formatBuffer(val, options.maxBufferLength); } if (typeof val === 'symbol') { return formatSymbol(val); } if (typeof val === 'function') { return formatFunction(val, options.maxFunctionSourceLength); } if (Array.isArray(val)) { return formatArray(val, path, errors, allValues, options); } if (typeof val !== 'object') { return formatNonObject(val); } return formatProperties(val, path, errors, allValues, options); } function checkCircular(val, path, allValues) { if (isComplex(val)) { if (allValues.some(function (v) { return v.value === val; })) { var existingValue = allValues.find(function (v) { return v.value === val; }); return { $circular: existingValue.path }; } else { allValues.push({ value: val, path: path }); } } } function formatError(error, id) { var result = {}; Object.getOwnPropertyNames(error) .filter(function (key) { return key !== 'stack'; }) .forEach(function (key) { return result[key] = error[key]; }); result.stack = "*[" + id + "]"; return { $error: result }; } function formatDate(date) { return { $date: date.toISOString() }; } function formatBuffer(buffer, maxLength) { if (maxLength != null && maxLength <= 0) { // tslint:disable-line return '$buffer'; } var length = buffer.length; return { $buffer: maxLength && length > maxLength ? buffer.slice(0, maxLength) : buffer.slice(0) }; } function formatSymbol(symbol) { return { $symbol: Symbol.prototype.toString.call(symbol) }; } function formatFunction(fn, maxLength) { if (maxLength != null && maxLength <= 0) { // tslint:disable-line return '$function'; } var src = fn.toString(); if (maxLength && src.length > maxLength) { src = src.substring(0, maxLength - 8) + '\n// ...'; } return { $function: { name: fn.name, src: src } }; } function formatArray(val, path, errors, allValues, options) { return val.map(function (v, i) { return format(v, path + "[" + i + "]", errors, allValues, options); }); } function formatNonObject(val) { if (typeof val.toString === 'function') { return val.toString(); } return null; } function formatProperties(val, path, errors, allValues, options) { var properties = {}; for (var key in val) { var value = val[key]; properties[key] = format(value, path + "." + key, errors, allValues, options); } return properties; } var Transport = /** @class */ (function (_super) { __extends(Transport, _super); function Transport(options) { if (options === void 0) { options = {}; } var _this = _super.call(this, options) || this; _this.name = 'seq'; if (typeof options !== 'object' || options === null) { options = {}; } if (typeof options.onError !== 'function') { options.onError = function (err) { return _this.emit('error', err); }; } _this.seqLoggerConfig = options; _this.seqLogger = new seq_logging_1.Logger(_this.seqLoggerConfig); _this.levelMapper = typeof options.levelMapper === 'function' ? options.levelMapper : defaultLevelMapper; _this.options = options; if (!_this.options.applicationName) { _this.options.applicationName = process.title + "-" + process.pid; } return _this; } Transport.prototype.log = function (info, next) { var _this = this; setImmediate(function () { try { var level = info.level, message = info.message, timestamp = info.timestamp, meta = __rest(info, ["level", "message", "timestamp"]); var _a = formatMeta(_this.options, meta), properties = _a.properties, errors = _a.errors; var ts = Date.parse(timestamp); var seqEvent = { timestamp: timestamp && !Number.isNaN(ts) ? new Date(ts) : new Date(), level: _this.levelMapper(level), messageTemplate: message, properties: { applicationName: _this.options.applicationName, data: properties }, exception: errors.length > 0 ? errors .map(function (_a) { var error = _a.error, id = _a.id; return getErrorStack(error, id); }) .join('\n\n') : void 0 }; _this.seqLogger.emit(seqEvent); } catch (err) { console.error('[@valuabletouch/winston-seq]', err); } }); this.emit('logged', info); if (typeof next === 'function') { next(); } }; Transport.prototype.close = function () { return this.seqLogger.close(); }; Transport.prototype.flush = function () { return this.seqLogger.flush(); }; return Transport; }(winston_transport_1.default)); exports.Transport = Transport; //# sourceMappingURL=index.js.map