Comparing version 2.0.0 to 3.0.0-beta.0
@@ -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)); | ||
}; | ||
}); |
153
index.js
'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" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
481
17226
11
1
2
5
+ Addedlodash@^4.17.15
+ Addedmoment@^2.24.0
+ Addedansi-styles@4.3.0(transitive)
+ Addedchalk@3.0.0(transitive)
+ Addedcolor-convert@2.0.1(transitive)
+ Addedcolor-name@1.1.4(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedsupports-color@7.2.0(transitive)
- Removedcelia@^6.1.4
- Removeddate-manip@^1.0.0
- Removedansi-styles@3.2.1(transitive)
- Removedcelia@6.1.46.1.5-1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removeddate-manip@1.0.1(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedsupports-color@5.5.0(transitive)
Updatedchalk@^3.0.0