Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@agile-ts/logger

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@agile-ts/logger - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

dist/index.js.map

18

CHANGELOG.md
# @agile-ts/logger
## 0.0.10
### Patch Changes
- e6ef3a7: #### :bug: Bug Fix
- `core`, `multieditor`, `react`
- [#204](https://github.com/agile-ts/agile/pull/204) fix multieditor ([@bennodev19](https://github.com/bennodev19))
- `api`, `core`, `cra-template-agile-typescript`, `cra-template-agile`, `event`, `logger`, `multieditor`, `proxytree`, `react`, `utils`, `vue`
- [#201](https://github.com/agile-ts/agile/pull/201) fix commonjs bundle ([@bennodev19](https://github.com/bennodev19))
#### Committers: 1
- BennoDev ([@bennodev19](https://github.com/bennodev19))
- Updated dependencies [e6ef3a7]
- @agile-ts/utils@0.0.10
## 0.0.9

@@ -4,0 +22,0 @@

26

dist/esm/logger.js

@@ -119,19 +119,13 @@ import { defineConfig, includesArray, isValidObject, generateId, isFunction } from '@agile-ts/utils';

}
const doNothing = () => {
};
return {
log: () => {
},
debug: () => {
},
info: () => {
},
success: () => {
},
warn: () => {
},
error: () => {
},
trace: () => {
},
table: () => {
}
log: doNothing,
debug: doNothing,
info: doNothing,
success: doNothing,
warn: doNothing,
error: doNothing,
trace: doNothing,
table: doNothing
};

@@ -138,0 +132,0 @@ }

@@ -7,357 +7,397 @@ 'use strict';

class Logger$1 {
key;
isActive;
config;
allowedTags = [];
loggerCategories = {}; // Holds all registered Logger Categories
watchers = {};
/**
* @public
* Logger - Handy Class for handling console.logs
*/
constructor(config = {}) {
let _config = typeof config === 'function' ? config(this) : config;
_config = utils.defineConfig(_config, {
prefix: '',
allowedTags: [],
canUseCustomStyles: true,
active: true,
level: 0,
timestamp: false,
});
this.isActive = _config.active;
this.allowedTags = _config.allowedTags;
this.config = {
timestamp: _config.timestamp,
prefix: _config.prefix,
canUseCustomStyles: _config.canUseCustomStyles,
level: _config.level,
};
this.addDefaultLoggerCategories();
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
/**
* @public
* Adds Conditions to Logs
*/
get if() {
return {
tag: (tags) => this.tag(tags),
};
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
/**
* @public
* Default Levels of Logger
*/
static get level() {
return {
TRACE: 1,
DEBUG: 2,
LOG: 5,
TABLE: 5,
INFO: 10,
SUCCESS: 15,
WARN: 20,
ERROR: 50,
};
}
return target;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Logger$1 {
constructor() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_defineProperty(this, "key", void 0);
_defineProperty(this, "isActive", void 0);
_defineProperty(this, "config", void 0);
_defineProperty(this, "allowedTags", []);
_defineProperty(this, "loggerCategories", {});
_defineProperty(this, "watchers", {});
var _config = typeof config === 'function' ? config(this) : config;
_config = utils.defineConfig(_config, {
prefix: '',
allowedTags: [],
canUseCustomStyles: true,
active: true,
level: 0,
timestamp: false
});
this.isActive = _config.active;
this.allowedTags = _config.allowedTags;
this.config = {
timestamp: _config.timestamp,
prefix: _config.prefix,
canUseCustomStyles: _config.canUseCustomStyles,
level: _config.level
};
this.addDefaultLoggerCategories();
}
get if() {
return {
tag: tags => this.tag(tags)
};
}
static get level() {
return {
TRACE: 1,
DEBUG: 2,
LOG: 5,
TABLE: 5,
INFO: 10,
SUCCESS: 15,
WARN: 20,
ERROR: 50
};
}
addDefaultLoggerCategories() {
this.createLoggerCategory({
key: 'log',
level: Logger$1.level.LOG
});
this.createLoggerCategory({
key: 'debug',
customStyle: 'color: #656565;',
prefix: 'Debug',
level: Logger$1.level.DEBUG
});
this.createLoggerCategory({
key: 'info',
customStyle: 'color: #6c69a0;',
prefix: 'Info',
level: Logger$1.level.INFO
});
this.createLoggerCategory({
key: 'success',
customStyle: 'color: #00b300;',
prefix: 'Success',
level: Logger$1.level.SUCCESS
});
this.createLoggerCategory({
key: 'warn',
prefix: 'Warn',
level: Logger$1.level.WARN
});
this.createLoggerCategory({
key: 'error',
prefix: 'Error',
level: Logger$1.level.ERROR
});
this.createLoggerCategory({
key: 'trace',
prefix: 'Trace',
level: Logger$1.level.TRACE
});
this.createLoggerCategory({
key: 'table',
level: Logger$1.level.TABLE
});
}
tag(tags) {
var _this = this;
if (utils.includesArray(this.allowedTags, tags)) {
return {
log: function log() {
return _this.log(...arguments);
},
debug: function debug() {
return _this.debug(...arguments);
},
info: function info() {
return _this.info(...arguments);
},
success: function success() {
return _this.success(...arguments);
},
warn: function warn() {
return _this.warn(...arguments);
},
error: function error() {
return _this.error(...arguments);
},
trace: function trace() {
return _this.trace(...arguments);
},
table: function table() {
return _this.table(...arguments);
}
};
}
//=========================================================================================================
// Add Default Logger Categories
//=========================================================================================================
/**
* @internal
* Adds Default Logger Categories
*/
addDefaultLoggerCategories() {
this.createLoggerCategory({
key: 'log',
level: Logger$1.level.LOG,
});
this.createLoggerCategory({
key: 'debug',
customStyle: 'color: #656565;',
prefix: 'Debug',
level: Logger$1.level.DEBUG,
});
this.createLoggerCategory({
key: 'info',
customStyle: 'color: #6c69a0;',
prefix: 'Info',
level: Logger$1.level.INFO,
});
this.createLoggerCategory({
key: 'success',
customStyle: 'color: #00b300;',
prefix: 'Success',
level: Logger$1.level.SUCCESS,
});
this.createLoggerCategory({
key: 'warn',
prefix: 'Warn',
level: Logger$1.level.WARN,
});
this.createLoggerCategory({
key: 'error',
prefix: 'Error',
level: Logger$1.level.ERROR,
});
this.createLoggerCategory({
key: 'trace',
prefix: 'Trace',
level: Logger$1.level.TRACE,
});
this.createLoggerCategory({
key: 'table',
level: Logger$1.level.TABLE,
});
var doNothing = () => {};
return {
log: doNothing,
debug: doNothing,
info: doNothing,
success: doNothing,
warn: doNothing,
error: doNothing,
trace: doNothing,
table: doNothing
};
}
log() {
for (var _len = arguments.length, data = new Array(_len), _key = 0; _key < _len; _key++) {
data[_key] = arguments[_key];
}
//=========================================================================================================
// Tag
//=========================================================================================================
/**
* @internal
* Only executes following 'command' if all given tags are included in allowedTags
* @param tags - Tags
*/
tag(tags) {
if (utils.includesArray(this.allowedTags, tags)) {
return {
log: (...data) => this.log(...data),
debug: (...data) => this.debug(...data),
info: (...data) => this.info(...data),
success: (...data) => this.success(...data),
warn: (...data) => this.warn(...data),
error: (...data) => this.error(...data),
trace: (...data) => this.trace(...data),
table: (...data) => this.table(...data),
};
}
return {
log: () => {
/* do nothing */
},
debug: () => {
/* do nothing */
},
info: () => {
/* do nothing */
},
success: () => {
/* do nothing */
},
warn: () => {
/* do nothing */
},
error: () => {
/* do nothing */
},
trace: () => {
/* do nothing */
},
table: () => {
/* do nothing */
},
};
this.invokeConsole(data, 'log', 'log');
}
debug() {
for (var _len2 = arguments.length, data = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
data[_key2] = arguments[_key2];
}
log(...data) {
this.invokeConsole(data, 'log', 'log');
this.invokeConsole(data, 'debug', typeof console.debug !== 'undefined' ? 'debug' : 'log');
}
info() {
for (var _len3 = arguments.length, data = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
data[_key3] = arguments[_key3];
}
debug(...data) {
this.invokeConsole(data, 'debug', typeof console.debug !== 'undefined' ? 'debug' : 'log');
this.invokeConsole(data, 'info', typeof console.info !== 'undefined' ? 'info' : 'log');
}
success() {
for (var _len4 = arguments.length, data = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
data[_key4] = arguments[_key4];
}
info(...data) {
this.invokeConsole(data, 'info', typeof console.info !== 'undefined' ? 'info' : 'log');
this.invokeConsole(data, 'success', 'log');
}
warn() {
for (var _len5 = arguments.length, data = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
data[_key5] = arguments[_key5];
}
success(...data) {
this.invokeConsole(data, 'success', 'log');
this.invokeConsole(data, 'warn', typeof console.warn !== 'undefined' ? 'warn' : 'log');
}
error() {
for (var _len6 = arguments.length, data = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
data[_key6] = arguments[_key6];
}
warn(...data) {
this.invokeConsole(data, 'warn', typeof console.warn !== 'undefined' ? 'warn' : 'log');
this.invokeConsole(data, 'error', typeof console.error !== 'undefined' ? 'error' : 'log');
}
trace() {
for (var _len7 = arguments.length, data = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
data[_key7] = arguments[_key7];
}
error(...data) {
this.invokeConsole(data, 'error', typeof console.error !== 'undefined' ? 'error' : 'log');
this.invokeConsole(data, 'trace', typeof console.trace !== 'undefined' ? 'trace' : 'log');
}
table() {
for (var _len8 = arguments.length, data = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
data[_key8] = arguments[_key8];
}
trace(...data) {
this.invokeConsole(data, 'trace', typeof console.trace !== 'undefined' ? 'trace' : 'log');
this.invokeConsole(data, 'table', typeof console.table !== 'undefined' ? 'table' : 'log');
}
custom(loggerCategory) {
for (var _len9 = arguments.length, data = new Array(_len9 > 1 ? _len9 - 1 : 0), _key9 = 1; _key9 < _len9; _key9++) {
data[_key9 - 1] = arguments[_key9];
}
table(...data) {
this.invokeConsole(data, 'table', typeof console.table !== 'undefined' ? 'table' : 'log');
this.invokeConsole(data, loggerCategory, 'log');
}
invokeConsole(data, loggerCategoryKey, consoleLogType) {
var loggerCategory = this.getLoggerCategory(loggerCategoryKey);
if (!this.isActive || loggerCategory.level < this.config.level) return;
var buildPrefix = () => {
var prefix = '';
if (this.config.timestamp) prefix = prefix.concat("[".concat(Date.now().toString(), "] "));
if (this.config.prefix) prefix = prefix.concat(this.config.prefix);
if (loggerCategory.prefix) prefix = prefix.concat(' ' + loggerCategory.prefix);
if (this.config.prefix || loggerCategory.prefix) prefix = prefix.concat(':');
return prefix;
};
if (typeof data[0] === 'string') data[0] = buildPrefix().concat(' ').concat(data[0]);else data.unshift(buildPrefix());
for (var key in this.watchers) {
var watcher = this.watchers[key];
if (loggerCategory.level >= (watcher.level || 0)) {
watcher.callback(loggerCategory, data);
}
}
custom(loggerCategory, ...data) {
this.invokeConsole(data, loggerCategory, 'log');
}
//=========================================================================================================
// Invoke Console
//=========================================================================================================
/**
* @internal
* Logs data in Console
* @param data - Data
* @param loggerCategoryKey - Key/Name of Logger Category
* @param consoleLogType - console[consoleLogProperty]
*/
invokeConsole(data, loggerCategoryKey, consoleLogType) {
const loggerCategory = this.getLoggerCategory(loggerCategoryKey);
// Check if Logger Category is allowed
if (!this.isActive || loggerCategory.level < this.config.level)
return;
// Build Prefix of Log
const buildPrefix = () => {
let prefix = '';
if (this.config.timestamp)
prefix = prefix.concat(`[${Date.now().toString()}] `);
if (this.config.prefix)
prefix = prefix.concat(this.config.prefix);
if (loggerCategory.prefix)
prefix = prefix.concat(' ' + loggerCategory.prefix);
if (this.config.prefix || loggerCategory.prefix)
prefix = prefix.concat(':');
return prefix;
};
// Add built Prefix
if (typeof data[0] === 'string')
data[0] = buildPrefix().concat(' ').concat(data[0]);
else
data.unshift(buildPrefix());
// Call Watcher Callbacks
for (const key in this.watchers) {
const watcher = this.watchers[key];
if (loggerCategory.level >= (watcher.level || 0)) {
watcher.callback(loggerCategory, data);
}
if (this.config.canUseCustomStyles && loggerCategory.customStyle) {
var newLogs = [];
var hasStyledString = false;
for (var log of data) {
if (!hasStyledString && typeof log === 'string') {
newLogs.push("%c".concat(log));
newLogs.push(loggerCategory.customStyle);
hasStyledString = true;
} else {
newLogs.push(log);
}
// Init Custom Style
if (this.config.canUseCustomStyles && loggerCategory.customStyle) {
const newLogs = [];
let hasStyledString = false; // NOTE: Only one style can be used for one String block!
for (const log of data) {
if (!hasStyledString && typeof log === 'string') {
newLogs.push(`%c${log}`);
newLogs.push(loggerCategory.customStyle);
hasStyledString = true;
}
else {
newLogs.push(log);
}
}
data = newLogs;
}
// Handle Console Table Log
if (consoleLogType === 'table') {
if (typeof data[0] === 'string') {
console.log(data[0]);
console.table(data.filter((d) => typeof d !== 'string' && 'number'));
}
return;
}
// Normal Log
console[consoleLogType](...data);
}
data = newLogs;
}
//=========================================================================================================
// Create Logger Category
//=========================================================================================================
/**
* @public
* Creates new Logger Category
* @param loggerCategory - Logger Category
*/
createLoggerCategory(loggerCategory) {
loggerCategory = {
prefix: '',
// @ts-ignore
level: 0,
...loggerCategory,
};
this.loggerCategories[loggerCategory.key] = loggerCategory;
if (consoleLogType === 'table') {
if (typeof data[0] === 'string') {
console.log(data[0]);
console.table(data.filter(d => typeof d !== 'string' && 'number'));
}
return;
}
//=========================================================================================================
// Get Logger Category
//=========================================================================================================
/**
* @public
* Get Logger Category
* @param key - Key/Name of Logger Category
*/
getLoggerCategory(key) {
return this.loggerCategories[key];
console[consoleLogType](...data);
}
createLoggerCategory(loggerCategory) {
loggerCategory = _objectSpread2({
prefix: '',
level: 0
}, loggerCategory);
this.loggerCategories[loggerCategory.key] = loggerCategory;
}
getLoggerCategory(key) {
return this.loggerCategories[key];
}
watch(keyOrConfig, config) {
var generateKey = utils.isValidObject(keyOrConfig);
var _config;
var key;
if (generateKey) {
key = utils.generateId();
_config = keyOrConfig;
} else {
key = keyOrConfig;
_config = config;
}
watch(keyOrConfig, config) {
const generateKey = utils.isValidObject(keyOrConfig);
let _config;
let key;
if (generateKey) {
key = utils.generateId();
_config = keyOrConfig;
}
else {
key = keyOrConfig;
_config = config;
}
_config = utils.defineConfig(_config, {
level: 0,
});
// Check if Callback is a Function
if (!utils.isFunction(_config.callback)) {
console.error('Agile: A Watcher Callback Function has to be an function!');
return this;
}
// Check if Callback Function already exists
if (this.watchers[key] != null) {
console.error(`Agile: Watcher Callback Function with the key/name ${key} already exists!`);
return this;
}
this.watchers[key] = _config;
return generateKey ? key : this;
_config = utils.defineConfig(_config, {
level: 0
});
if (!utils.isFunction(_config.callback)) {
console.error('Agile: A Watcher Callback Function has to be an function!');
return this;
}
//=========================================================================================================
// Remove Watcher
//=========================================================================================================
/**
* @public
* Removes Watcher at given Key
* @param key - Key of Watcher that gets removed
*/
removeWatcher(key) {
delete this.watchers[key];
return this;
if (this.watchers[key] != null) {
console.error("Agile: Watcher Callback Function with the key/name ".concat(key, " already exists!"));
return this;
}
//=========================================================================================================
// Set Level
//=========================================================================================================
/**
* @public
* Assigns new Level to Logger
* NOTE: Default Levels can be found in 'Logger.level.x'
* @param level - Level
*/
setLevel(level) {
this.config.level = level;
return this;
}
this.watchers[key] = _config;
return generateKey ? key : this;
}
removeWatcher(key) {
delete this.watchers[key];
return this;
}
setLevel(level) {
this.config.level = level;
return this;
}
}
const defaultSharedLoggerConfig = {
prefix: 'Agile',
active: true,
level: Logger$1.level.WARN,
canUseCustomStyles: true,
allowedTags: ['runtime', 'storage', 'subscription', 'multieditor'],
var defaultSharedLoggerConfig = {
prefix: 'Agile',
active: true,
level: Logger$1.level.WARN,
canUseCustomStyles: true,
allowedTags: ['runtime', 'storage', 'subscription', 'multieditor']
};
exports.sharedLogger = new Logger$1(defaultSharedLoggerConfig);
/**
* Assigns the specified configuration object to the shared Agile Logger.
*
* @param logger - Configuration object
*/
function assignSharedLogger(logger) {
exports.sharedLogger = logger;
exports.sharedLogger = logger;
}
/**
* Returns a newly created Logger.
*
* @param config - Configuration object
*/
function createLogger(config = {}) {
config = utils.defineConfig(config, defaultSharedLoggerConfig);
return new Logger$1(config);
function createLogger() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
config = utils.defineConfig(config, defaultSharedLoggerConfig);
return new Logger$1(config);
}

@@ -372,1 +412,2 @@

exports.defaultSharedLoggerConfig = defaultSharedLoggerConfig;
//# sourceMappingURL=index.js.map
{
"name": "@agile-ts/logger",
"version": "0.0.9",
"version": "0.0.10",
"author": "BennoDev",

@@ -44,3 +44,3 @@ "license": "MIT",

"dependencies": {
"@agile-ts/utils": "^0.0.9"
"@agile-ts/utils": "^0.0.10"
},

@@ -47,0 +47,0 @@ "publishConfig": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc