@spot-meetings/backend-logger
Version:
Spot's backend logger module.
111 lines (110 loc) âĒ 3.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLogger = exports.logIcons = exports.LogOutput = exports.LogLevel = void 0;
/* eslint-disable no-bitwise */
const date_fns_1 = require("date-fns");
const json_colorizer_1 = __importDefault(require("json-colorizer"));
const winston_1 = __importDefault(require("winston"));
const chalk_1 = __importDefault(require("chalk"));
const { timestamp, splat, json, errors, combine } = winston_1.default.format;
/**
* @see https://github.com/winstonjs/winston#logging
*/
var LogLevel;
(function (LogLevel) {
LogLevel["Error"] = "error";
LogLevel["Warn"] = "warn";
LogLevel["Info"] = "info";
LogLevel["Http"] = "http";
LogLevel["Verbose"] = "verbose";
LogLevel["Debug"] = "debug";
LogLevel["Silly"] = "silly";
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
var LogOutput;
(function (LogOutput) {
LogOutput["Summary"] = "summary";
LogOutput["Details"] = "details";
LogOutput["Silent"] = "silent";
LogOutput["Raw"] = "raw";
})(LogOutput = exports.LogOutput || (exports.LogOutput = {}));
exports.logIcons = new Map([
[LogLevel.Error, 'ðĨ'],
[LogLevel.Warn, 'ð'],
[LogLevel.Info, 'ð'],
[LogLevel.Http, 'ð'],
[LogLevel.Verbose, 'ðŽ'],
[LogLevel.Debug, 'ð'],
[LogLevel.Silly, 'ðĪŠ'],
]);
const colors = {
STRING_LITERAL: 'white',
BOOLEAN_LITERAL: 'blue',
NUMBER_LITERAL: 'cyan',
NULL_LITERAL: 'magenta',
STRING_KEY: 'white.dim',
};
const getLogOutputType = (value = LogOutput.Raw) => {
if (!Object.values(LogOutput).includes(value)) {
// eslint-disable-next-line no-console
console.warn(`The provided LOG_OUTPUT "${value}" is not supported. Defaulting to "${LogOutput.Raw}".`);
return LogOutput.Raw;
}
return value;
};
const getLogLevel = (value = LogLevel.Info) => {
if (!Object.values(LogLevel).includes(value)) {
// eslint-disable-next-line no-console
console.warn(`The provided LOG_LEVEL "${value}" is not supported. Defaulting to "${LogLevel.Info}".`);
return LogLevel.Info;
}
return value;
};
const getReadableFormatter = (details = false) => winston_1.default.format.printf((info) => {
const { level, message, timestamp: ts } = info;
const label = exports.logIcons.get(level) || 'â';
const time = chalk_1.default.yellow((0, date_fns_1.format)(new Date(ts), '[HH:mm:ss.SSS]'));
const header = chalk_1.default.bold(`${time} ${label} ${message}`);
const parts = [header];
if (details) {
parts.push((0, json_colorizer_1.default)(JSON.stringify(info), {
pretty: true,
colors,
}).replace(/\\n/g, '\n'));
}
return parts.join('\n');
});
/**
* SpotLogger Class
*/
const createLogger = (id, ip, version) => {
const { LOG_OUTPUT, LOG_LEVEL, RUNTIME_ENV, NODE_ENV } = process.env;
const outputType = getLogOutputType(LOG_OUTPUT);
const logLevel = getLogLevel(LOG_LEVEL);
const formats = [errors({ stack: true }), timestamp(), splat(), json()];
// If any of the formatted output flags are set we pretty print.
if ([LogOutput.Details, LogOutput.Summary].includes(outputType)) {
formats.push(getReadableFormatter(outputType === LogOutput.Details));
}
return winston_1.default.createLogger({
format: combine(...formats),
level: logLevel,
transports: [
new winston_1.default.transports.Console({
silent: outputType === LogOutput.Silent,
}),
],
defaultMeta: {
environment: RUNTIME_ENV || NODE_ENV,
type: 'log',
reporter: {
version,
id,
ip,
},
},
});
};
exports.createLogger = createLogger;