@onesy/log
Version:
Log utils, very readable and clean logs
412 lines (337 loc) • 13.8 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import is from '@onesy/utils/is';
import isEnvironment from '@onesy/utils/isEnvironment';
import merge from '@onesy/utils/merge';
import copy from '@onesy/utils/copy';
import stringify from '@onesy/utils/stringify';
import OnesyDate from '@onesy/date/OnesyDate';
import format from '@onesy/date/format';
import OnesySubscription from '@onesy/subscription';
export const variantNames = ['log', 'debug', 'info', 'warn', 'error', 'important'];
const optionsDefault = {
minimal: true,
log: {
enabled: true,
native: false,
variants: variantNames,
padding: {
top: true,
bottom: true
}
},
arguments: {
pre: [],
post: []
},
variants: {},
date: {
add: true,
method: () => format(OnesyDate.utc, "MM-DD-YYYY HH:mm:ss.SSS")
},
stringify: {
method: stringify
}
};
const amalogOptionsDefault = {
log: {
enabled: true,
native: false,
padding: {
top: true,
bottom: true
}
}
};
class OnesyLog {
// Able to control some behavior globally
static archiveClear() {
this.debug('OnesyLog archive cleared');
this.archive = [];
}
static get onesylog() {
return new OnesyLog();
}
static log() {
let variant_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'debug';
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return new OnesyLog().log(variant_, ...args);
}
static debug() {
return new OnesyLog().debug(...arguments);
}
static info() {
return new OnesyLog().info(...arguments);
}
static warn() {
return new OnesyLog().warn(...arguments);
}
static error() {
return new OnesyLog().error(...arguments);
}
static important() {
return new OnesyLog().important(...arguments);
}
static color(value) {
let color = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'green';
let options = arguments.length > 2 ? arguments[2] : undefined;
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 isEnvironment('browser') ? ["%c".concat(value), "color: ".concat(colorValues.browser)] : ["\x1B".concat(colorValues.server, "m").concat(value, "\x1B[0m")];
}
static get variantNames() {
return 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'
}
}];
}
constructor() {
let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : optionsDefault;
this.options = options;
_defineProperty(this, "variants", {});
_defineProperty(this, "archive", []);
this.options = merge(options, optionsDefault); // Set all variants
Object.keys(this.options.variants).forEach(key => {
const variant = this.options.variants[key];
this.variants[variant.name] = _objectSpread(_objectSpread({}, variant), {}, {
pre: {
subscription: new OnesySubscription()
},
post: {
subscription: new OnesySubscription()
}
});
});
}
log() {
var _OnesyLog$options$log;
let variant_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'debug';
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
const variantLog = OnesyLog.variants.log || this.variants.log;
const variant = this.variants[variant_] || OnesyLog.variants[variant_] || variantLog;
const logVariants = ((_OnesyLog$options$log = OnesyLog.options.log) === null || _OnesyLog$options$log === void 0 ? void 0 : _OnesyLog$options$log.variants) || this.options.log.variants;
if (variant && logVariants.indexOf(variant_) > -1) {
var _OnesyLog$options$log2, _OnesyLog$options$log3, _this$options$log, _this$options$log$pad, _OnesyLog$options$log6, _OnesyLog$options$log7, _this$options$log3, _this$options$log3$pa;
// Pre methods run
if (variantLog !== variant) variantLog.pre.subscription.emit(...args);
variant.pre.subscription.emit(...args);
const dateMethod = is('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 ((_OnesyLog$options$log2 = OnesyLog.options.log) !== null && _OnesyLog$options$log2 !== void 0 && (_OnesyLog$options$log3 = _OnesyLog$options$log2.padding) !== null && _OnesyLog$options$log3 !== void 0 && _OnesyLog$options$log3.top && (_this$options$log = this.options.log) !== null && _this$options$log !== void 0 && (_this$options$log$pad = _this$options$log.padding) !== null && _this$options$log$pad !== void 0 && _this$options$log$pad.top) {
var _OnesyLog$options$log4, _OnesyLog$options$log5, _this$options$log2, _this$options$log2$pa;
logPaddingTop = (_OnesyLog$options$log4 = OnesyLog.options.log) !== null && _OnesyLog$options$log4 !== void 0 && (_OnesyLog$options$log5 = _OnesyLog$options$log4.padding) !== null && _OnesyLog$options$log5 !== void 0 && _OnesyLog$options$log5.top && (_this$options$log2 = this.options.log) !== null && _this$options$log2 !== void 0 && (_this$options$log2$pa = _this$options$log2.padding) !== null && _this$options$log2$pa !== void 0 && _this$options$log2$pa.top ? space : '';
if (isEnvironment('browser') && logNative && ['error', 'warn'].indexOf(variant_) > -1) logPaddingTop = '';
}
if (date) {
firstArgument.push("".concat(logPaddingTop).concat(date, " "));
}
if (!this.options.minimal && !date && logPaddingTop) {
firstArgument.push(logPaddingTop);
}
firstArgument[0] += isEnvironment('browser') ? "%c".concat(variant.prefix).concat(variant.name).concat(variant.sufix, ":") : "\x1B[".concat(variant.color.server, "m").concat(variant.prefix).concat(variant.name).concat(variant.sufix, "\x1B[0m:");
if (isEnvironment('browser')) firstArgument.push("color: ".concat(variant.color.browser));
const args_ = [...(this.options.arguments.pre || []), ...args, ...(this.options.arguments.post || [])];
if (args_.length === 1 && is('simple', args_[0])) arguments_.push(args_[0]);else {
arguments_.push(args_.map((argument, index) => {
const isSimple = is('simple', argument); // Nice enter space/s above logged line
let item = index === 0 ? this.options.minimal ? space : "".concat(space).concat(space) : '';
const previous = index - 1 >= 0 && args[index - 1];
if (index !== 0 && previous && is('simple', previous) && !isSimple) item += space;
const stringifyMethod = is('function', this.options.stringify.method) && this.options.stringify.method;
if (argument instanceof Error) item += "".concat(argument.message).concat(space).concat(space).concat(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 ((_OnesyLog$options$log6 = OnesyLog.options.log) !== null && _OnesyLog$options$log6 !== void 0 && (_OnesyLog$options$log7 = _OnesyLog$options$log6.padding) !== null && _OnesyLog$options$log7 !== void 0 && _OnesyLog$options$log7.bottom && (_this$options$log3 = this.options.log) !== null && _this$options$log3 !== void 0 && (_this$options$log3$pa = _this$options$log3.padding) !== null && _this$options$log3$pa !== void 0 && _this$options$log3$pa.bottom) {
if (isEnvironment('nodejs')) logPaddingBottom = space;
if (isEnvironment('browser')) {
logPaddingBottom = "".concat(space).concat(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() {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return this.log('debug', ...args);
}
info() {
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
return this.log('info', ...args);
}
warn() {
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
return this.log('warn', ...args);
}
error() {
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
args[_key6] = arguments[_key6];
}
return this.log('error', ...args);
}
important() {
for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
args[_key7] = arguments[_key7];
}
return this.log('important', ...args);
}
archiveClear() {
this.debug('Archive cleared');
this.archive = [];
}
static reset() {
OnesyLog.options = copy(amalogOptionsDefault); // Add OnesyLog global variants
OnesyLog.variantsDefault.forEach(variant => OnesyLog.variants[variant.name] = _objectSpread(_objectSpread({}, variant), {}, {
pre: {
subscription: new OnesySubscription()
},
post: {
subscription: new OnesySubscription()
}
}));
}
} // Add OnesyLog global variants
_defineProperty(OnesyLog, "options", copy(amalogOptionsDefault));
_defineProperty(OnesyLog, "variants", {});
_defineProperty(OnesyLog, "archive", []);
OnesyLog.variantsDefault.forEach(variant => OnesyLog.variants[variant.name] = _objectSpread(_objectSpread({}, variant), {}, {
pre: {
subscription: new OnesySubscription()
},
post: {
subscription: new OnesySubscription()
}
}));
export default OnesyLog;