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
186 lines (185 loc) • 7.08 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Human = void 0;
var transform_js_1 = require("../transform.js");
var util_1 = require("util");
var ansi = __importStar(require("@bevry/ansi"));
/**
* Return the given argument.
* Used for when there is no formatter.
*/
function ansiNoop(a) {
return a;
}
/**
* Convert Logger entries into human readable format.
* @extends Transform
* @example
* ``` javascript
* import { Logger, Human } from 'caterpillar'
* const logger = new Logger()
* const human = new Human()
* logger.pipe(human).pipe(process.stdout)
* logger.log('info', 'some', {data: 'oh yeah'}, 42)
* ```
*/
var Human = /** @class */ (function (_super) {
__extends(Human, _super);
/** Create our instance and apply our configuration options. */
function Human(opts) {
var _this = _super.call(this) || this;
/** Whether or not to use colors? */
_this.color = true;
/** Mapping of which log level numbers correspond to which colours */
_this.colors = {
'0': 'red',
'1': 'red',
'2': 'red',
'3': 'red',
'4': 'yellow',
'5': 'yellow',
'6': 'green',
'7': 'green',
};
// options
if ((opts === null || opts === void 0 ? void 0 : opts.color) != null)
_this.color = opts.color;
if ((opts === null || opts === void 0 ? void 0 : opts.colors) != null)
_this.colors = opts.colors;
return _this;
}
/** Get the color for the log level */
Human.prototype.getColor = function (levelNumber) {
// Determine
var color = this.colors[levelNumber] || false;
// Return
return color;
};
/** Pad the left of some content if need be with the specified padding to make the content reach a certain size */
Human.prototype.padLeft = function (padding, size, content) {
// Prepare
padding = String(padding);
content = String(content);
// Handle
if (content.length < size) {
for (var i = 0, n = size - content.length; i < n; ++i) {
content = padding + content;
}
}
// Return
return content;
};
/** Convert logger entry arguments into a human readable string */
Human.prototype.formatArguments = function (args) {
var _this = this;
return args
.map(function (value) {
return typeof value === 'string'
? value
: (0, util_1.inspect)(value, {
showHidden: false,
depth: 10,
colors: _this.color,
});
})
.join(' ');
};
/** Convert a datetime into a human readable format */
Human.prototype.formatDate = function (datetime) {
// Prepare
var now = new Date(datetime);
var year = now.getFullYear();
var month = this.padLeft('0', 2, now.getMonth() + 1);
var date = this.padLeft('0', 2, now.getDate());
var hours = this.padLeft('0', 2, now.getHours());
var minutes = this.padLeft('0', 2, now.getMinutes());
var seconds = this.padLeft('0', 2, now.getSeconds());
var ms = this.padLeft('0', 3, now.getMilliseconds());
// Apply
var result = "".concat(year, "-").concat(month, "-").concat(date, " ").concat(hours, ":").concat(minutes, ":").concat(seconds, ".").concat(ms);
// Return
return result;
};
/** Convert a logger entry into a human readable format */
Human.prototype.format = function (entry) {
// Prepare
var color = this.color;
var useLine = entry.line !== -1;
var result;
// Format
var format = {
color: this.getColor(entry.levelNumber),
timestamp: this.formatDate(entry.date),
text: this.formatArguments(entry.args),
};
// Check
if (format.text) {
// Formatters
var levelFormatter = (color && format.color && ansi[format.color]) || ansiNoop;
var lineFormatter = (useLine && color && ansi.dim) || ansiNoop;
// Message
// @ts-ignore
var levelString = levelFormatter("".concat(entry.levelName, ":"));
var entryString = format.text;
var messageString = "".concat(levelString, " ").concat(entryString);
// Format
if (useLine) {
// Line Information
var seperator = '\n ';
var debugString = lineFormatter("\u2192 [".concat(format.timestamp, "] [").concat(entry.file, ":").concat(entry.line, ":").concat(entry.char, "] [").concat(entry.method, "]"));
// Result
result = "".concat(messageString).concat(seperator).concat(debugString, "\n");
}
else {
// Result
result = "".concat(messageString, "\n");
}
}
else {
result = format.text;
}
// Return
return result;
};
return Human;
}(transform_js_1.Transform));
exports.Human = Human;
exports.default = Human;
;