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

clrsole

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clrsole - npm Package Compare versions

Comparing version 2.0.0 to 3.0.0-beta.0

lib/BasicLayout.js

17

clrsole.js

@@ -5,13 +5,14 @@ 'use strict';

const { inspect } = require('util');
const { isObject, each } = require('celia');
const { colors } = require('./lib/util');
const { isObjectLike } = require('lodash');
const { COLORS } = require('./lib/constants');
colors.forEach((color) => {
COLORS.forEach((color) => {
exports[color] = function () {
const args = [];
each(arguments, (arg, i) => {
args[i] = isObject(arg) ? inspect(arg, { depth: Infinity }) : arg;
});
console.log(chalk[color](args.join(' ')));
let str = '';
for (let i = 0, len = arguments.length; i < len; i++) {
const arg = arguments[i];
str += isObjectLike(arg) ? inspect(arg, { depth: Infinity }) : arg;
}
console.log(chalk[color](str));
};
});
'use strict';
const assert = require('assert');
const { isNil, isObject, isString } = require('celia');
const { addAppender, getAppender } = require('./lib/appender');
const { addLayout, getLayout } = require('./lib/layout');
const { normalizeLevel, isLevelEnabled, setLevels } = require('./lib/util');
const { isNil, isObject, isString, isNumber } = require('lodash');
const BasicLayout = require('./lib/BasicLayout');
const ConsoleAppender = require('./lib/ConsoleAppender');
const { addAppender, getAppender, addLayout, getLayout } = require('./lib/utils');
const defaultLevels = require('./lib/levels');
const { COLORS } = require('./lib/constants');

@@ -16,11 +17,2 @@ const { isArray } = Array;

/**
* 创建logger实例
* @param {String} loggerName
* @param {Object} options
*/
static getLogger(loggerName, options) {
return new Logger(loggerName, options);
}
/**
* 日志操作类

@@ -32,4 +24,5 @@ * @param {String} loggerName 日志分类

assert(!isNil(loggerName), `Invalid log name ${loggerName}`);
options = options || {};
this.name = loggerName;
options = options || {};
this.setLevels(defaultLevels, options.level || 'INFO');

@@ -40,3 +33,3 @@ this.appenders = [options.defaultAppender || { type: 'console' }];

set level(level) {
const nlevel = normalizeLevel(level);
const nlevel = this.normalizeLevel(level);
assert(nlevel !== null, `Invalid log level ${level}`);

@@ -46,6 +39,2 @@ this._level = nlevel;

get level() {
return this._level;
}
set appenders(appenders) {

@@ -63,22 +52,19 @@ assert(isArray(appenders), `Invalid appenders config ${stringify(appenders)}`);

get level() {
return this._level;
}
get levels() {
return this._levels;
}
/**
* 设置新的日志级别
* @param {Array<object>} levels
* @param {String} defaultLevel
* @param {String} currentLevel 当前日志级别
*/
setLevels(levels, defaultLevel) {
const lastLevels = this._levels;
if (lastLevels) {
lastLevels.forEach(({ level }) => {
delete this[level.toLocaleLowerCase()];
});
}
setLevels(levels, currentLevel) {
assert(isArray(levels), `Invalid log levels ${levels}`);
setLevels(levels, ({ level }) => {
this[level.toLocaleLowerCase()] = function (...args) {
return this._log(level, args);
};
});
this._levels = levels;
this.level = defaultLevel;
this._setLevels(levels, currentLevel, true);
}

@@ -99,9 +85,22 @@

* 判断当前级别是否启用
* @param {String} l
* @param {String} l 待比较的日志级别
*/
isLevelEnabled(l) {
return isLevelEnabled(this.level, l);
const { level: currentLevel, _levelValueMapping } = this;
return currentLevel === 'ALL' ||
(currentLevel !== 'OFF' && _levelValueMapping[currentLevel] >= _levelValueMapping[l]);
}
/**
* 序列化level级别
* @param {String} level
*/
normalizeLevel(level) {
const nlevel = isString(level) ? level.toLocaleUpperCase() : null;
return nlevel && this._levelValueMapping[nlevel] !== undefined
? nlevel
: null;
}
/**
* 记录日志

@@ -112,3 +111,3 @@ * @param {String} level 级别

log(level, ...args) {
const nlevel = normalizeLevel(level);
const nlevel = this.normalizeLevel(level);
assert(nlevel !== null, `Invalid log level ${level}`);

@@ -138,3 +137,8 @@ return this._log(nlevel, args);

if (this.isLevelEnabled(level)) {
const opts = { level, logger: this.name, timestamp: Date.now() };
const opts = {
level,
color: this._levelColorMapping[level],
loggerName: this.name,
timestamp: Date.now()
};
this._appenders.forEach((appender) => {

@@ -147,10 +151,73 @@ appender.append(args, opts);

_assertLevels(levels, checking, callback) {
levels.forEach(checking ? (n) => {
const { level, value, color } = n;
assert(isString(level), `Invalid log level ${level} in config ${stringify(n)}`);
assert(COLORS.indexOf(color) > -1, `Invalid color string ${color} in config ${stringify(n)}, valid color in ${stringify(COLORS)}`);
assert(isNumber(value), `Invalid log level value ${value} in config ${stringify(n)}`);
n = { level: level.toLocaleUpperCase(), value, color };
callback(n);
} : callback);
}
_setLevels(levels, currentLevel, checking) {
const lastLevels = this.levels;
if (lastLevels) {
lastLevels.forEach((l) => {
delete this[l.toLocaleLowerCase()];
});
}
const levelMethods = [];
const levelValueMapping = Object.create(null);
const levelColorMapping = Object.create(null);
this._assertLevels(levels, checking, ({ level, value, color }) => {
levelValueMapping[level] = value;
levelColorMapping[level] = color;
levelMethods[levelMethods.length] = level;
this[level.toLocaleLowerCase()] = function (...args) {
return this._log(level, args);
};
});
levelValueMapping.ALL = Number.MAX_VALUE;
levelColorMapping.ALL = 'whiteBright';
levelValueMapping.OFF = -Number.MAX_VALUE;
this._levelValueMapping = levelValueMapping;
this._levelColorMapping = levelColorMapping;
this._levels = levelMethods;
this.level = currentLevel;
}
}
Logger.clrsole = require('./clrsole');
Logger.addAppender = addAppender;
Logger.getAppender = getAppender;
Logger.addLayout = addLayout;
Logger.getLayout = getLayout;
addAppender('console', ConsoleAppender);
addLayout('basic', BasicLayout);
Object.assign(Logger, {
/**
* 创建logger实例
* @param {String} loggerName
* @param {Object} options
*/
getLogger(loggerName, options) {
return new Logger(loggerName, options);
},
clrsole: require('./clrsole'),
BasicLayout: BasicLayout,
ConsoleAppender: ConsoleAppender,
addAppender: addAppender,
getAppender: getAppender,
addLayout: addLayout,
getLayout: getLayout
});
module.exports = Logger;
{
"name": "clrsole",
"version": "2.0.0",
"description": "colorful logger with console.log or custom appender",
"main": "index.js",
"directories": {
"example": "examples",
"lib": "lib",
"test": "test"
},
"scripts": {
"test": "jest --config jest.config.js",
"example:console": "node ./examples/console.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/fengxinming/clrsole.git"
},
"keywords": [
"corie",
"clrsole",
"debug",
"console",
"logger",
"logging",
"log"
],
"author": "Jesse Feng",
"license": "MIT",
"bugs": {
"url": "https://github.com/fengxinming/clrsole/issues"
},
"homepage": "https://github.com/fengxinming/clrsole#readme",
"dependencies": {
"celia": "^6.1.4",
"chalk": "^2.4.2",
"date-manip": "^1.0.0"
},
"devDependencies": {
"eslint": "^5.15.3",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0",
"graylog-sender": "^1.0.4",
"jest": "^24.5.0"
}
}
"name": "clrsole",
"version": "3.0.0-beta.0",
"description": "colorful console.log with custom appender",
"main": "index.js",
"directories": {
"lib": "lib",
"test": "test"
},
"repository": "https://github.com/fengxinming/logger/tree/master/packages/clrsole",
"keywords": [
"clrsole",
"console",
"logger",
"logging",
"log"
],
"author": "Jesse Feng",
"license": "MIT",
"bugs": {
"url": "https://github.com/fengxinming/logger/issues/issues"
},
"homepage": "https://github.com/fengxinming/logger/tree/master/packages/clrsole#readme",
"publishConfig": {
"main": "index.js"
},
"dependencies": {
"chalk": "^3.0.0",
"lodash": "^4.17.15",
"moment": "^2.24.0"
}
}
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