| 'use strict' | ||
| const Time = require('./time.js') | ||
| const LOGGER_LEVEL = [ | ||
| 'ALL', | ||
| 'TRACE', | ||
| 'DEBUG', | ||
| 'INFO', | ||
| 'WARN', | ||
| 'ERROR', | ||
| 'FATAL', | ||
| 'MARK', | ||
| 'OFF', | ||
| ] | ||
| const levelIndex = Symbol('Level-Index') | ||
| module.exports = class Logger { | ||
| constructor() { | ||
| // ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < MARK < OFF | ||
| this[levelIndex] = 0 | ||
| } | ||
| get level() { | ||
| const index = this[levelIndex] | ||
| return LOGGER_LEVEL[index] | ||
| } | ||
| set level(val) { | ||
| const index = LOGGER_LEVEL.indexOf(val.toUpperCase()) | ||
| const SMALLEST_INDEX = 0 | ||
| if ( index >= SMALLEST_INDEX) { | ||
| this[levelIndex] = index | ||
| } else { | ||
| throw new Error(`日志等级 level 错误,只能设置为以下值:${LOGGER_LEVEL.toString()}`) | ||
| } | ||
| } | ||
| static getTime() { | ||
| const now = new Time() | ||
| return now.getFormattedTime() | ||
| } | ||
| isActive(currentLevel) { | ||
| const currentIndex = LOGGER_LEVEL.indexOf(currentLevel) | ||
| if (currentIndex >= this[levelIndex]) { | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
| debug(content) { | ||
| const currentLevel = 'DEBUG' | ||
| if (this.isActive(currentLevel)) { | ||
| console.debug(`[${Logger.getTime()}] [${currentLevel}] ${content}`) | ||
| } | ||
| } | ||
| info(content) { | ||
| const currentLevel = 'INFO' | ||
| if (this.isActive(currentLevel)) { | ||
| console.info(`[${Logger.getTime()}] [${currentLevel}] ${content}`) | ||
| } | ||
| } | ||
| warn(content) { | ||
| const currentLevel = 'WARN' | ||
| if (this.isActive(currentLevel)) { | ||
| console.warn(`[${Logger.getTime()}] [${currentLevel}] ${content}`) | ||
| } | ||
| } | ||
| error(content) { | ||
| const currentLevel = 'ERROR' | ||
| if (this.isActive(currentLevel)) { | ||
| console.error(`[${Logger.getTime()}] [${currentLevel}] ${content}`) | ||
| } | ||
| } | ||
| } |
| 'use strict' | ||
| const Logger = require('./logger.js') | ||
| module.exports.getLogger = function getLogger() { | ||
| const logger = new Logger() | ||
| return logger | ||
| } |
+56
| 'use strict' | ||
| /** | ||
| * 将 0 至 9 的数字前补零,输出为字符串,例如 00,01,...,09 | ||
| * @param {number} n | ||
| */ | ||
| function zerofill(n) { | ||
| if (typeof n !== 'number') { | ||
| throw new Error('参数 n 应该是一个数字') | ||
| } | ||
| const SMALLEST_SINGLE_DIGIT = 0 | ||
| const LARGEST_SINGLE_DIGIT = 9 | ||
| if (n >= SMALLEST_SINGLE_DIGIT && n <= LARGEST_SINGLE_DIGIT) { | ||
| return `0${n.toString()}` | ||
| } | ||
| return n.toString() | ||
| } | ||
| module.exports = class Time extends Date { | ||
| constructor() { | ||
| super() | ||
| } | ||
| getFullYear() { | ||
| return super.getFullYear().toString() | ||
| } | ||
| getFullMonth() { | ||
| return zerofill(this.getMonth()) | ||
| } | ||
| getFullDay() { | ||
| return zerofill(this.getUTCDay()) | ||
| } | ||
| getFullHour() { | ||
| return zerofill(this.getHours()) | ||
| } | ||
| getFullMinute() { | ||
| return zerofill(this.getMinutes()) | ||
| } | ||
| getFullSecond() { | ||
| return zerofill(this.getSeconds()) | ||
| } | ||
| getFormattedTime() { | ||
| const formatted = `${this.getFullYear()}-${this.getFullMonth()}-${this.getFullDay()} ${this.getFullHour()}:${this.getFullMinute()}:${this.getFullSecond()}` | ||
| return formatted | ||
| } | ||
| } |
+1
-1
| 'use strict' | ||
| module.exports = require('./lib/loghere') | ||
| module.exports = require('./lib/loghere.js') |
+2
-2
| { | ||
| "name": "loghere", | ||
| "version": "0.0.1", | ||
| "description": "PM2 中使用的一款简易日志工具", | ||
| "version": "1.0.0", | ||
| "description": "可以在 PM2 和 Serverless 中使用的一款简易日志工具", | ||
| "main": "index.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
+1
-1
| # loghere | ||
| PM2 中使用的一款简易日志工具 | ||
| 可以在 PM2 和 Serverless 中使用的一款简易日志工具 |
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
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
10295
38.45%10
42.86%119
5850%1
-50%0
-100%