UNPKG

watchlog

Version:

Fast logger with multiple outputs and verbosity settings

72 lines (56 loc) 2.26 kB
/* Copyright (c) 2016 Wouter Klijn, https://wuhkuh.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 'use strict' /** * The Watchlog object */ function Watchlog (opts) { if (!(this instanceof Watchlog)) return new Watchlog(opts) opts = opts || {} this._output = (((opts.output !== undefined) && (opts.output !== null)) ? opts.output : console.log) this._verbosity = (((opts.verbosity !== undefined) && (opts.verbosity !== null)) ? opts.verbosity : 1) } /** * Log and error */ Watchlog.prototype.error = function (subject, vMin, vMax) { this.log(subject, vMin, vMax) throw new Error(subject) } /** * Log to the Watchlog output */ Watchlog.prototype.log = function (subject, vMin, vMax) { if (vMin === undefined || vMin === null) vMin = 1 if (vMax === undefined || vMax === null) vMax = Infinity if ((vMin <= this._verbosity) && (this._verbosity <= vMax)) { this._putout(subject) } } /** * Execute output function */ Watchlog.prototype._putout = function (subject) { if (this._output.constructor === Array) { for (let i = 0, len = this._output.length; i < len; i++) { this._output[i](subject) } } else this._output(subject) } module.exports = Watchlog