@rsksmart/rif-storage-pinning
Version:
Application for providing your storage space to other to use in exchange of RIF Tokens
181 lines (180 loc) • 5.83 kB
JavaScript
;
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.loggingFactory = exports.formatLogMessage = void 0;
const winston_1 = require("winston");
const safe_1 = __importDefault(require("colors/safe"));
const supports_colors_1 = require("colors/lib/system/supports-colors");
const config_1 = __importDefault(require("config"));
const util_1 = require("util");
const COLORS_ENABLED = supports_colors_1.supportsColor() && !process.env.LOG_NO_COLORS;
// Inspired from https://github.com/visionmedia/debug
const names = [];
const skips = [];
/**
* From given namespaces string parse RegExes which
* will be used for determining if given service should be
* ignored in logging or not.
*
* @param namespaces
*/
function loadFilter(namespaces) {
const splits = namespaces.split(/[\s,]+/);
for (const split of splits) {
if (!split) {
continue;
}
const namespace = split.replace(/\*/g, '.*?');
if (namespace[0] === '-') {
skips.push(new RegExp(`^${namespace.substr(1)}$`));
}
else {
names.push(new RegExp(`^${namespace}$`));
}
}
}
/**
* Returns true if the given mode name is enabled, false otherwise.
*
* @param {String} name
* @return {Boolean}
* @api public
*/
function enabled(name) {
if (name[name.length - 1] === '*') {
return true;
}
function isMatch(set) {
for (const regex of set) {
if (regex.test(name)) {
return true;
}
}
return false;
}
if (isMatch(skips)) {
return false;
}
if (names.length > 0) {
return isMatch(names);
}
return true;
}
const filterServices = winston_1.format(info => {
if (info.metadata.service) {
return enabled(info.metadata.service) ? info : false;
}
else {
return info;
}
});
/**
* Format utility which will uppercase logging level.
*/
const upperCaseLevel = winston_1.format(info => {
if (info.level) {
info.level = info.level.toUpperCase();
}
return info;
});
const supportedLevels = {
critical: 0,
error: 1,
warn: 2,
info: 3,
verbose: 4,
debug: 5
};
winston_1.addColors({
debug: 'grey',
info: 'blue',
critical: 'white redBG'
});
let mainLogger;
const loggers = {};
function initLogging() {
loadFilter(config_1.default.get('log.filter') || '*');
const transportsSet = [new winston_1.transports.Console()];
if (config_1.default.get('log.path')) {
transportsSet.push(new winston_1.transports.File({
filename: config_1.default.get('log.path'),
maxsize: 5000000,
maxFiles: 5,
tailable: true,
format: winston_1.format.uncolorize()
}));
}
mainLogger = winston_1.createLogger({
// To see more detailed errors, change this to 'debug'
level: config_1.default.get('log.level') || 'info',
levels: supportedLevels,
format: winston_1.format.combine(winston_1.format.errors({ stack: true }), winston_1.format.metadata(), filterServices(), upperCaseLevel(),
// format.padLevels(),
winston_1.format.timestamp({ format: 'DD/MM hh:mm:ss' }), winston_1.format.colorize(), winston_1.format.printf(formatLogMessage), COLORS_ENABLED ? winston_1.format(i => i)() : winston_1.format.uncolorize()),
transports: transportsSet
});
}
function formatLogMessage(info) {
let message;
const _a = info.metadata, { service } = _a, rest = __rest(_a, ["service"]);
const sanitizedMessage = info.message.replace(/\n/g, '\\n');
if (service) {
message = `[${info.level}] ${safe_1.default.grey(info.timestamp)} (${service}): ${sanitizedMessage}`;
}
else {
message = `[${info.level}] ${safe_1.default.grey(info.timestamp)}: ${sanitizedMessage}`;
}
if (Object.keys(rest).length > 0) {
message += '\n' + util_1.inspect(rest, false, 5, true);
}
return message;
}
exports.formatLogMessage = formatLogMessage;
function delayedLoggingMethod(level, name) {
return function (message, ...meta) {
// First logging call, lets setup logging
if (!mainLogger) {
initLogging();
}
if (name) {
if (!loggers[name]) {
loggers[name] = mainLogger.child({ service: name });
}
loggers[name].log(level, message, ...meta);
}
else {
mainLogger.log(level, message, ...meta);
}
};
}
function exitAfterProcessingClosure(fn) {
return (...args) => {
fn(...args);
process.exit(1);
};
}
function loggingFactory(name) {
return {
critical: exitAfterProcessingClosure(delayedLoggingMethod('critical', name)),
error: delayedLoggingMethod('error', name),
warn: delayedLoggingMethod('warn', name),
info: delayedLoggingMethod('info', name),
verbose: delayedLoggingMethod('verbose', name),
debug: delayedLoggingMethod('debug', name),
extend: (extension) => loggingFactory(`${name}:${extension}`)
};
}
exports.loggingFactory = loggingFactory;