@onesy/log
Version:
Log utils, very readable and clean logs
254 lines (253 loc) • 12 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.variantNames = void 0;
const is_1 = __importDefault(require("@onesy/utils/is"));
const isEnvironment_1 = __importDefault(require("@onesy/utils/isEnvironment"));
const merge_1 = __importDefault(require("@onesy/utils/merge"));
const copy_1 = __importDefault(require("@onesy/utils/copy"));
const stringify_1 = __importDefault(require("@onesy/utils/stringify"));
const OnesyDate_1 = __importDefault(require("@onesy/date/OnesyDate"));
const format_1 = __importDefault(require("@onesy/date/format"));
const subscription_1 = __importDefault(require("@onesy/subscription"));
exports.variantNames = ['log', 'debug', 'info', 'warn', 'error', 'important'];
const optionsDefault = {
minimal: true,
log: {
enabled: true,
native: false,
variants: exports.variantNames,
padding: {
top: true,
bottom: true
}
},
arguments: {
pre: [],
post: []
},
variants: {},
date: {
add: true,
method: () => (0, format_1.default)(OnesyDate_1.default.utc, `MM-DD-YYYY HH:mm:ss.SSS`)
},
stringify: {
method: stringify_1.default
}
};
const amalogOptionsDefault = {
log: {
enabled: true,
native: false,
padding: {
top: true,
bottom: true
}
}
};
class OnesyLog {
constructor(options = optionsDefault) {
this.options = options;
this.variants = {};
this.archive = [];
this.options = (0, merge_1.default)(options, optionsDefault);
// Set all local variants optionally
Object.keys(this.options.variants).forEach(key => {
const variant = this.options.variants[key];
this.variants[variant.name] = Object.assign(Object.assign({}, variant), { pre: { subscription: new subscription_1.default() }, post: { subscription: new subscription_1.default() } });
});
}
static archiveClear() {
this.debug('OnesyLog archive cleared');
this.archive = [];
}
static get onesylog() { return new OnesyLog(); }
static log(variant_ = 'debug', ...args) { return new OnesyLog().log(variant_, ...args); }
static debug(...args) { return new OnesyLog().debug(...args); }
static info(...args) { return new OnesyLog().info(...args); }
static warn(...args) { return new OnesyLog().warn(...args); }
static error(...args) { return new OnesyLog().error(...args); }
static important(...args) { return new OnesyLog().important(...args); }
static color(value, color = 'green', options) {
let colorValues;
switch (color) {
case 'blue':
colorValues = { browser: '#0b9fc3', server: '36' };
break;
case 'green':
colorValues = { browser: '#1fc926', server: '32' };
break;
case 'orange':
colorValues = { browser: '#a9b030', server: '33' };
break;
case 'red':
colorValues = { browser: '#d74644', server: '31' };
break;
case 'magenta':
colorValues = { browser: '#ca00c5', server: '35' };
break;
default:
colorValues = { browser: '#1fc926', server: '32' };
break;
}
if (options)
colorValues = options;
return (0, isEnvironment_1.default)('browser') ? [`%c${value}`, `color: ${colorValues.browser}`] : [`\x1b${colorValues.server}m${value}\x1b[0m`];
}
static get variantNames() {
return exports.variantNames;
}
static get variantsDefault() {
return [
{ name: 'log', prefix: '(', sufix: ')', color: { browser: '#0b9fc3', server: '36' } },
{ name: 'debug', prefix: '(', sufix: ')', color: { browser: '#0b9fc3', server: '36' } },
{ name: 'info', prefix: '(', sufix: ')', color: { browser: '#1fc926', server: '32' } },
{ name: 'warn', prefix: '(', sufix: ')', color: { browser: '#a9b030', server: '33' } },
{ name: 'error', prefix: '(', sufix: ')', color: { browser: '#d74644', server: '31' } },
{ name: 'important', prefix: '(', sufix: ')', color: { browser: '#ca00c5', server: '35' } },
];
}
log(variant_ = 'debug', ...args) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
const variantLog = OnesyLog.variants.log || this.variants.log;
const variant = this.variants[variant_] || OnesyLog.variants[variant_] || variantLog;
const logVariants = ((_a = OnesyLog.options.log) === null || _a === void 0 ? void 0 : _a.variants) || this.options.log.variants;
if (variant &&
logVariants.indexOf(variant_) > -1) {
// Pre methods run
if (variantLog !== variant)
variantLog.pre.subscription.emit(...args);
variant.pre.subscription.emit(...args);
const dateMethod = (0, is_1.default)('function', this.options.date.method) && this.options.date.method;
const date = this.options.date.add && dateMethod && dateMethod();
const arguments_ = [];
// Log
const logNative = OnesyLog.options.log.native && this.options.log.native;
const method = (logNative && console[variant_]) || console.log;
const space = this.options.minimal ? ' ' : '\n';
const firstArgument = [];
// Log padding top
let logPaddingTop = '';
if (((_c = (_b = OnesyLog.options.log) === null || _b === void 0 ? void 0 : _b.padding) === null || _c === void 0 ? void 0 : _c.top) && ((_e = (_d = this.options.log) === null || _d === void 0 ? void 0 : _d.padding) === null || _e === void 0 ? void 0 : _e.top)) {
logPaddingTop = (((_g = (_f = OnesyLog.options.log) === null || _f === void 0 ? void 0 : _f.padding) === null || _g === void 0 ? void 0 : _g.top) && ((_j = (_h = this.options.log) === null || _h === void 0 ? void 0 : _h.padding) === null || _j === void 0 ? void 0 : _j.top)) ? space : '';
if ((0, isEnvironment_1.default)('browser') && (logNative && ['error', 'warn'].indexOf(variant_) > -1))
logPaddingTop = '';
}
if (date) {
firstArgument.push(`${logPaddingTop}${date} `);
}
if (!this.options.minimal && !date && logPaddingTop) {
firstArgument.push(logPaddingTop);
}
firstArgument[0] += (0, isEnvironment_1.default)('browser') ? `%c${variant.prefix}${variant.name}${variant.sufix}:` : `\x1b[${variant.color.server}m${variant.prefix}${variant.name}${variant.sufix}\x1b[0m:`;
if ((0, isEnvironment_1.default)('browser'))
firstArgument.push(`color: ${variant.color.browser}`);
const args_ = [
...(this.options.arguments.pre || []),
...args,
...(this.options.arguments.post || []),
];
if (args_.length === 1 && (0, is_1.default)('simple', args_[0]))
arguments_.push(args_[0]);
else {
arguments_.push(args_.map((argument, index) => {
const isSimple = (0, is_1.default)('simple', argument);
// Nice enter space/s above logged line
let item = index === 0 ? this.options.minimal ? space : `${space}${space}` : '';
const previous = index - 1 >= 0 && args[index - 1];
if (index !== 0 && previous && (0, is_1.default)('simple', previous) && !isSimple)
item += space;
const stringifyMethod = (0, is_1.default)('function', this.options.stringify.method) && this.options.stringify.method;
if (argument instanceof Error)
item += `${argument.message}${space}${space}${argument.stack}`;
else
item += isSimple ? argument : (stringifyMethod ? stringifyMethod(argument) : argument);
// Nice enter space below logged line
if (index !== args_.length - 1) {
item += space;
if (!isSimple)
item += space;
}
return item;
}));
}
// Log padding bottom
let logPaddingBottom = '';
if (((_l = (_k = OnesyLog.options.log) === null || _k === void 0 ? void 0 : _k.padding) === null || _l === void 0 ? void 0 : _l.bottom) && ((_o = (_m = this.options.log) === null || _m === void 0 ? void 0 : _m.padding) === null || _o === void 0 ? void 0 : _o.bottom)) {
if ((0, isEnvironment_1.default)('nodejs'))
logPaddingBottom = space;
if ((0, isEnvironment_1.default)('browser')) {
logPaddingBottom = `${space}${space}`;
if ((logNative && ['error', 'warn'].indexOf(variant_) > -1) && args_.length <= 1)
logPaddingBottom = '';
}
}
if (!this.options.minimal && logPaddingBottom)
arguments_.push(logPaddingBottom);
const allArguments = [...firstArgument, ([].concat(...arguments_)).join('')].map((item, index) => {
if (!index) {
if (this.options.minimal)
return item.trimStart();
}
return item;
});
// More for testing purposes
const output = {
method,
arguments: allArguments,
logged: false,
};
if (OnesyLog.options.log.enabled && this.options.log.enabled) {
method(...allArguments);
output.logged = true;
}
// Post methods run
if (variantLog !== variant)
variantLog.post.subscription.emit(...args);
variant.post.subscription.emit(...args);
// Archive
if (OnesyLog.options.log.archive)
OnesyLog.archive.push(output);
if (this.options.log.archive)
this.archive.push(output);
return output;
}
// More for testing purposes
return {
logged: false,
};
}
debug(...args) {
return this.log('debug', ...args);
}
info(...args) {
return this.log('info', ...args);
}
warn(...args) {
return this.log('warn', ...args);
}
error(...args) {
return this.log('error', ...args);
}
important(...args) {
return this.log('important', ...args);
}
archiveClear() {
this.debug('Archive cleared');
this.archive = [];
}
static reset() {
OnesyLog.options = (0, copy_1.default)(amalogOptionsDefault);
// Add OnesyLog global variants
OnesyLog.variantsDefault.forEach(variant => OnesyLog.variants[variant.name] = Object.assign(Object.assign({}, variant), { pre: { subscription: new subscription_1.default() }, post: { subscription: new subscription_1.default() } }));
}
}
// Able to control some behavior globally
OnesyLog.options = (0, copy_1.default)(amalogOptionsDefault);
OnesyLog.variants = {};
OnesyLog.archive = [];
// Add OnesyLog global variants
OnesyLog.variantsDefault.forEach(variant => OnesyLog.variants[variant.name] = Object.assign(Object.assign({}, variant), { pre: { subscription: new subscription_1.default() }, post: { subscription: new subscription_1.default() } }));
exports.default = OnesyLog;