UNPKG

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

174 lines (173 loc) 6.61 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Logger = void 0; const rfc_log_levels_1 = __importStar(require("rfc-log-levels")); const get_current_line_1 = __importDefault(require("get-current-line")); const 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() * ``` */ class Logger extends transform_js_1.Transform { /** Set the default level info via a level number or name. */ set defaultLevel(value) { const levelInfo = this.getLogLevel(value); if (levelInfo == null) { throw new Error(`caterpillar: the intended value of ${value} for the default log level not found in the configured levels`); } this.defaultLevelInfo = levelInfo; } /** Create our instance and apply our configuraiton options. */ constructor(opts) { var _a; super(); /** * 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); } /** Alias for {@link getLogLevel} using the configured logger levels as reference. */ getLogLevel(value) { return (0, rfc_log_levels_1.default)(value, this.levels); } /** Takes an arguments array and tranforms it into a log entry. */ format(args) { // fetch the level const level = args.shift(); let 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 const date = new Date().toISOString(); // fetch the line information const 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, 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" * } * ``` */ log(...args) { this.write(args); } /** Alias for log which prefixes the error log level */ error(...args) { this.write(['error', ...args]); } /** Alias for log which prefixes the warn log level */ warn(...args) { this.write(['warn', ...args]); } /** Alias for log which prefixes the info log level */ info(...args) { this.write(['info', ...args]); } /** Alias for log which prefixes the debug log level */ debug(...args) { this.write(['debug', ...args]); } } exports.Logger = Logger; exports.default = Logger;