UNPKG

logger-steps

Version:

Group log messages and send them in one batch to the logger of your choize

93 lines (80 loc) 3.3 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.loggerSteps = factory()); }(this, (function () { 'use strict'; var levels = ["error", "warn", "log", "info", "debug"]; /** * Returns an object with the same interface the Logger has. * The difference is that instead of inmediatelly send the log messages to the * target output, messages are collected into a trace and then otputed all at the * same time when a trace flush is triggered. * You should peform a flush when you finish the trace, but in case you don't do * an automatic flush is peformed after a configurable timeout. * Please note that you should allways call the flush method. * Not doing it and relying to the automatic flush could cause unexpected behaviors and performance problems. * * @param {any} logger the logger to use to flush the trace * @param {number} secs the number of seconds before triggering an automatic flush * @return {Object} tracer - the tracer object containing the usual methods the logger has * @return {function} tracer.flush - sends the complete trace to the provided logger */ var tracer = function tracer(logger) { var tracename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "Steps"; var secs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 30; secs = typeof secs === "number" ? secs : 30; var levelName = "debug"; var level = 4; var steps = [tracename + ": \n"]; var API = {}; var flushTimeout = resetFlushTimeout(); /** * Flushes the current trace to the logger. * If this is an automatic flush, it adds a warning to the trace because this should never happen */ function flush(isAutomaticFlush) { if (isAutomaticFlush) { API.warn("Automatic trace flush", "This usually means an error on the process using the trace or a bad trace-API usage"); } logger[levelName].apply(logger, steps); clearTimeout(flushTimeout); } function resetFlushTimeout() { flushTimeout && clearTimeout(flushTimeout); flushTimeout = setTimeout(flush, secs * 1000, true); return flushTimeout; } /** * Registers a log message in the trace. * If the logging level has more priority than the current one (lower name) * then the whole trace level is set to that logging level */ var handleLogMessage = function handleLogMessage(lvlName, lvl, title) { for (var _len = arguments.length, rest = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { rest[_key - 3] = arguments[_key]; } var step = { title: title }; if (rest.length > 0) { step.details = rest.length === 1 ? rest[0] : rest; } steps.push(step); if (lvl < level) { level = lvl; levelName = lvlName; } resetFlushTimeout(); }; /** * We bind flush to false so whatever is passed is ignored and * we do not erroneous flag the flush as automatic*/ API.flush = flush.bind(API, false); /** * Build the rest of the API interface */ return levels.reduce(function (api, name, lvl) { api[name] = handleLogMessage.bind(api, name, lvl); return api; }, API); }; return tracer; })));