@brainbits/node-logger
Version:
Logger for node projects
141 lines (133 loc) • 3.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _config = _interopRequireDefault(require("./config"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/**
* @description Logger class
* @class Logger
*/
class Logger {
/**
* @description Creates an instance of Logger.
*/
constructor(config = {}) {
/**
* @description Map for the timer
*/
_defineProperty(this, "timeMap", new Map());
this.config = (0, _config.default)(config);
const {
levels,
channel
} = this.config;
// Creates for all level methods
levels.forEach(level => {
this[level] = (message, meta) => {
const event = {
channel,
level,
message,
meta
};
this.logToPlugins(event);
this.write(event);
};
});
}
/**
* @description Start the timer with a message as identifier
* @param {*} message
*/
start(message) {
this.timeMap.set(message, process.hrtime());
}
/**
* @description Stops the timer with an existing message
* @param {*} message
* @param {*} meta
*/
stop(message, meta = {}) {
let timeMs = null;
if (this.timeMap.has(message)) {
const [s, ns] = process.hrtime(this.timeMap.get(message));
timeMs = Math.round((s * 1e9 + ns) * 1e-6);
}
this[this.config.timerLevel](message, {
...meta,
timeMs
});
}
/**
* @description logs the plugins with the feed object
* @memberof Logger
* @private
*/
logToPlugins(event) {
const {
plugins
} = this.config;
if (!plugins && !Array.isArray(plugins)) {
return;
}
plugins.forEach(plugin => {
if ('log' in plugin) {
plugin.log(event);
}
});
}
/**
* @description Returns the output for a corresponding level
* @param {string} level
* @returns {Stream|null} output
* @memberof Logger
* @private
*/
getOutputForLevel(level) {
const outputLevel = this.config.levels.slice(0, this.config.levels.indexOf(level) + 1).reverse().find(levelName => levelName in this.config.outputs);
const output = this.config.outputs[outputLevel];
if (!output) {
return null;
}
if (!['stderr', 'stdout'].includes(output)) {
throw new Error(`Output ${output} not supported`);
}
return process[output];
}
/**
* @description Checks if the level is allowed to write to our targets
* @param {*} level
* @returns {boolean}
* @memberof Logger
* @private
*/
isLevelSilent(level) {
const {
levels,
maxLevel
} = this.config;
return levels.indexOf(level) > levels.indexOf(maxLevel);
}
/**
* @description Writes the content of an event to the output
* @param {object} event
* @memberof Logger
* @private
*/
write(event) {
if (this.isLevelSilent(event.level)) {
return;
}
const message = this.config.formatter(event);
const target = this.getOutputForLevel(event.level);
if (target && target.writable) {
target.write(`${message}\n`);
}
}
}
exports.default = Logger;