caterpillar
Version:
Caterpillar is the ultimate logging system for Deno, Node.js, and Web Browsers. Log levels are implemented to the RFC standard. Log entries can be filtered and piped to various streams, including coloured output to the terminal, the browser's console, and
241 lines (240 loc) • 9.47 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Logger = void 0;
var rfc_log_levels_1 = __importStar(require("rfc-log-levels"));
var get_current_line_1 = __importDefault(require("get-current-line"));
var transform_js_1 = require("./transform.js");
/**
* Logger.
* This is what we write to.
* @example Creation
* ``` javascript
* // Via class
* import { Logger } from 'caterpillar'
* const logger = new Logger()
* ```
*/
var Logger = /** @class */ (function (_super) {
__extends(Logger, _super);
/** Create our instance and apply our configuraiton options. */
function Logger(opts) {
var _a;
var _this = _super.call(this) || this;
/**
* The configuration to use for the line offset.
* This defaults to any file path that includes `logger`, and any method that includes the word `log`.
*/
_this.lineOffset = {
file: /logger/i,
method: /log/i,
};
/**
* The mapping of log level names to log level numbers.
* Defaults to the RFC Log Level configuration.
*/
_this.levels = rfc_log_levels_1.rfcLogLevels;
/**
* Only fetch line information for entries that have a log level equal to, or below this number.
* You should only specify this if you need it, as fFetching line information for thousands of log entries, which is typical in large applications, will slow your application down dramatically.
* If not specified, defaults to `-Infinity` which effect is to ignore gathering line information for all log levels.
*/
_this.lineLevel = -Infinity;
// options
if ((opts === null || opts === void 0 ? void 0 : opts.lineOffset) != null)
_this.lineOffset = opts.lineOffset;
if ((opts === null || opts === void 0 ? void 0 : opts.levels) != null)
_this.levels = opts.levels;
if ((opts === null || opts === void 0 ? void 0 : opts.lineLevel) != null)
_this.lineLevel = opts.lineLevel;
// options: default level
_this.defaultLevel = (_a = opts === null || opts === void 0 ? void 0 : opts.defaultLevel) !== null && _a !== void 0 ? _a : 'info';
// dereference
_this.levels = Object.assign({}, _this.levels);
return _this;
}
Object.defineProperty(Logger.prototype, "defaultLevel", {
/** Set the default level info via a level number or name. */
set: function (value) {
var levelInfo = this.getLogLevel(value);
if (levelInfo == null) {
throw new Error("caterpillar: the intended value of ".concat(value, " for the default log level not found in the configured levels"));
}
this.defaultLevelInfo = levelInfo;
},
enumerable: false,
configurable: true
});
/** Alias for {@link getLogLevel} using the configured logger levels as reference. */
Logger.prototype.getLogLevel = function (value) {
return (0, rfc_log_levels_1.default)(value, this.levels);
};
/** Takes an arguments array and tranforms it into a log entry. */
Logger.prototype.format = function (args) {
// fetch the level
var level = args.shift();
var levelInfo = level === 'default' ? this.defaultLevelInfo : this.getLogLevel(level);
if (levelInfo == null) {
// fallback to the default log level
levelInfo = this.defaultLevelInfo;
// as the level (first param) was not actually a level, put it back
args.unshift(level);
}
// fetch the date
var date = new Date().toISOString();
// fetch the line information
var lineInfo = levelInfo.levelNumber <= this.lineLevel
? (0, get_current_line_1.default)(this.lineOffset)
: {
line: -1,
char: -1,
method: '',
file: '',
};
// put it all together
return Object.assign({ date: date, args: args }, levelInfo, lineInfo);
};
/**
* Log the arguments into the logger stream as formatted data with debugging information.
* Such that our transformers can deal with it intelligently.
*
* @example Inputs
* ``` javascript
* logger.log('note', 'this is working swell')
* ```
* ``` javascript
* logger.log('this', 'worked', 'swell')
* ```
*
* @example Results
* ``` json
* {
* "args": ["this is working swell"],
* "date": "2013-04-25T10:18:25.722Z",
* "levelNumber": 5,
* "levelName": "notice",
* "line": "59",
* "method": "Object.<anonymous>",
* "file": "/Users/balupton/some-project/calling-file.js"
* }
* ```
* ``` json
* {
* "args": ["this", "worked", "well"],
* "date": "2013-04-25T10:18:26.539Z",
* "levelNumber": 6,
* "levelName": "info",
* "line": "60",
* "method": "Object.<anonymous>",
* "file": "/Users/balupton/some-project/calling-file.js"
* }
* ```
*/
Logger.prototype.log = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.write(args);
};
/** Alias for log which prefixes the error log level */
Logger.prototype.error = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.write(__spreadArray(['error'], __read(args), false));
};
/** Alias for log which prefixes the warn log level */
Logger.prototype.warn = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.write(__spreadArray(['warn'], __read(args), false));
};
/** Alias for log which prefixes the info log level */
Logger.prototype.info = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.write(__spreadArray(['info'], __read(args), false));
};
/** Alias for log which prefixes the debug log level */
Logger.prototype.debug = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this.write(__spreadArray(['debug'], __read(args), false));
};
return Logger;
}(transform_js_1.Transform));
exports.Logger = Logger;
exports.default = Logger;
;