├── .npmignore ├── index.js ├── .gitignore ├── src ├── index.ts ├── Loggr.js.old └── Loggr.ts ├── ts.d.ts ├── ts.ts ├── ts.js.map ├── ts.js ├── tsconfig.json ├── package.json ├── license.md ├── readme.md ├── test └── test.js └── yarn.lock /.npmignore: -------------------------------------------------------------------------------- 1 | src/ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./dist/Loggr'); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | package-lock.json 3 | dist/ -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import CatLoggr from './Loggr'; 2 | 3 | module.exports = CatLoggr; -------------------------------------------------------------------------------- /ts.d.ts: -------------------------------------------------------------------------------- 1 | import CatLoggr from './dist/Loggr'; 2 | export default CatLoggr; 3 | export * from './dist/Loggr'; -------------------------------------------------------------------------------- /ts.ts: -------------------------------------------------------------------------------- 1 | import CatLoggr from './dist/Loggr'; 2 | 3 | export default CatLoggr; 4 | export * from './dist/Loggr'; -------------------------------------------------------------------------------- /ts.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"ts.js","sourceRoot":"","sources":["../ts.ts"],"names":[],"mappings":";;AAAA,mCAA+B;AAE/B,kBAAe,eAAQ,CAAC"} -------------------------------------------------------------------------------- /ts.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | function __export(m) { 3 | for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; 4 | } 5 | exports.__esModule = true; 6 | var Loggr_1 = require("./dist/Loggr"); 7 | exports["default"] = Loggr_1["default"]; 8 | __export(require("./dist/Loggr")); 9 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "declaration": true, 5 | "moduleResolution": "node", 6 | "module": "commonjs", 7 | "outDir": "./dist", 8 | "lib": [ 9 | "es2017" 10 | ], 11 | "typeRoots": [ 12 | "./lib/@types" 13 | ], 14 | "types": [ 15 | "node" 16 | ], 17 | "forceConsistentCasingInFileNames": true, 18 | "pretty": false, 19 | "target": "ES2017", 20 | "noImplicitAny": true, 21 | "sourceMap": true, 22 | "skipLibCheck": true, 23 | "disableSizeLimit": true, 24 | "allowJs": false, 25 | "experimentalDecorators": true, 26 | "resolveJsonModule": false 27 | }, 28 | "include": [ 29 | "./src" 30 | ], 31 | "exclude": [ 32 | "./node_modules" 33 | ] 34 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cat-loggr", 3 | "version": "1.2.0", 4 | "description": "A utility to make beautiful console logs easily.", 5 | "main": "dist/index.js", 6 | "types": "dist/index.d.ts", 7 | "scripts": { 8 | "test": "npm run build && mocha", 9 | "build": "rm -rf build/ && tsc" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/Ratismal/cat-loggr.git" 14 | }, 15 | "keywords": [ 16 | "logging" 17 | ], 18 | "author": "stupid cat", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/Ratismal/cat-loggr/issues" 22 | }, 23 | "homepage": "https://github.com/Ratismal/cat-loggr#readme", 24 | "dependencies": { 25 | "chalk": "^2.4.1", 26 | "dayjs": "^1.10.5" 27 | }, 28 | "devDependencies": { 29 | "@types/node": "^15.12.2", 30 | "mocha": "^5.2.0", 31 | "tslint": "^5.12.1", 32 | "typescript": "^3.2.4" 33 | } 34 | } -------------------------------------------------------------------------------- /license.md: -------------------------------------------------------------------------------- 1 | Copyright 2018 stupid cat 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Cat Loggr 2 | 3 | [![npm](https://img.shields.io/npm/v/cat-loggr.svg)](https://www.npmjs.com/package/cat-loggr) 4 | 5 | A simple, lightweight utility for making beautiful logs. 6 | 7 | ## Goals 8 | 9 | 1. Be lightweight 10 | 2. Be a drop-in replacement for `console.log` 11 | 3. Generate pretty, colourful logs 12 | 4. Support sharded projects (via a shard ID) 13 | 5. Support parsing of multiple arguments, including the inspecting of objects 14 | 6. Support defining display options at execution 15 | 16 | ## Screenshots 17 | 18 | ![blargbot logs](https://cat.needs-to-s.top/87a975.png) 19 | 20 | ## Installation 21 | 22 | ``` 23 | npm i --save cat-loggr 24 | ``` 25 | 26 | ## Basic Usage 27 | 28 | ```js 29 | // JavaScript: 30 | const CatLoggr = require('cat-loggr'); 31 | // TypeScript: 32 | import CatLoggr from 'cat-loggr/ts'; 33 | 34 | const loggr = new CatLoggr(); 35 | 36 | loggr.log('Hello, world!'); 37 | ``` 38 | 39 | That's it! 40 | 41 | ## Default Log Levels 42 | - fatal 43 | - error 44 | - warn 45 | - trace 46 | - init 47 | - info 48 | - verbose 49 | - debug (aliases: log, dir) 50 | 51 | If defined, the `info` level is default. If not, the lowest priority level is default. 52 | 53 | ## More Advanced Usage 54 | 55 | ### Custom levels 56 | 57 | ```js 58 | const loggr = new CatLoggr({ 59 | levels: [ 60 | { name: 'catnip', color: CatLoggr._chalk.red.bgBlack }, 61 | { name: 'fish', color: CatLoggr._chalk.black.bgRed } 62 | ] 63 | }); 64 | // OR 65 | const loggr = new CatLoggr() 66 | .setLevels([ 67 | { name: 'catnip', color: CatLoggr._chalk.red.bgBlack }, 68 | { name: 'fish', color: CatLoggr._chalk.black.bgRed } 69 | ]); 70 | 71 | loggr.fish('Delicious!'); 72 | ``` 73 | 74 | ### Level threshold 75 | 76 | ```js 77 | const loggr = new CatLoggr({ 78 | level: 'warn' 79 | }); 80 | // OR 81 | const loggr = new CatLoggr() 82 | .setLevel('warn'); 83 | 84 | loggr.info('This will not display,'); 85 | loggr.warn('but this will!'); 86 | ``` 87 | 88 | ### Meta 89 | 90 | - depth - how far into an object to inspect 91 | - color - whether to render colors 92 | - trace - whether to generate a stacktrace 93 | 94 | ```js 95 | const loggr = new CatLoggr(); 96 | 97 | let obj = { 98 | with: { 99 | depth: { 100 | fun: '!' 101 | } 102 | } 103 | }; 104 | 105 | loggr.log(obj); 106 | // { with: { depth: [Object] } } 107 | loggr.meta({depth: 2}).log(obj); 108 | // { with: { depth: { fun: '!' } } } 109 | ``` 110 | 111 | ### Hooks 112 | 113 | You can define hook for extra argument processing. 114 | 115 | If the hook callback returns `null` or `undefined`, the processing for that argument will continue. Otherwise, it will cease if an actual value is returned. 116 | 117 | Returned values will be added to the log's output. If an array is returned, each element of the array will be added individually. 118 | 119 | ```js 120 | class Cat {} 121 | const cat = new Cat(); 122 | 123 | const loggr = new CatLoggr() 124 | .addArgHook(function({ arg, date }) { 125 | if (arg instanceof Cat) 126 | return 'kitty!'; 127 | }); 128 | 129 | loggr.log(cat); 130 | // kitty! 131 | ``` 132 | 133 | You can also define hooks for post processing. 134 | ```js 135 | const loggr = new CatLoggr() 136 | .addPostHook(function({ text }) { 137 | return 'Hello, ' + text; 138 | }); 139 | 140 | loggr.log('world!'); 141 | // Hello, world! 142 | ``` 143 | 144 | ### Global 145 | 146 | If you want to go full meme, you can make CatLoggr overwrite the global `console` object! Use responsibly. 147 | 148 | ```js 149 | const loggr = new CatLoggr() 150 | .setGlobal(); 151 | 152 | console.log('Hello, world!'); 153 | ``` 154 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | const CatLoggr = require('../dist/index.js'); 3 | 4 | describe('CatLoggr', function () { 5 | describe('should instantiate', function () { 6 | it('with no params', function () { 7 | let loggr = new CatLoggr(); 8 | }); 9 | it('with a shard id', function () { 10 | let loggr = new CatLoggr({ shardId: 1 }); 11 | assert.equal(loggr._shard, 1); 12 | }); 13 | it('with a default level', function () { 14 | let loggr = new CatLoggr({ level: 'info' }); 15 | assert.equal(loggr._levelName, 'info'); 16 | }); 17 | it('with default level definitions', function () { 18 | let loggr = new CatLoggr({ 19 | levels: [ 20 | { name: 'catnip', color: CatLoggr._chalk.red.bgBlack }, 21 | { name: 'fish', color: CatLoggr._chalk.black.bgRed } 22 | ] 23 | }); 24 | assert.equal(Object.values(loggr._levels).length, 2); 25 | assert.equal(loggr._levelName, 'fish'); 26 | }); 27 | it('with a default meta definition', function () { 28 | let loggr = new CatLoggr({ 29 | meta: { 30 | depth: 5, color: false, trace: true 31 | } 32 | }); 33 | assert.equal(loggr._defaultMeta.depth, 5); 34 | assert.equal(loggr._defaultMeta.color, false); 35 | assert.equal(loggr._defaultMeta.trace, true); 36 | }); 37 | }); 38 | 39 | let loggr = new CatLoggr(); 40 | 41 | describe('#setLevel', function () { 42 | it('should throw an error if the level doesn\'t exist', function () { 43 | assert.throws(function () { 44 | loggr.setLevel('catnip'); 45 | }, Error); 46 | }); 47 | it('should throw a TypeError if a string is not passed', function () { 48 | assert.throws(function () { 49 | loggr.setLevel(42); 50 | }, TypeError); 51 | }); 52 | }); 53 | 54 | describe('#setLevels', function () { 55 | it('should throw a TypeError it wasn\'t provided an array', function () { 56 | assert.throws(function () { 57 | loggr.setLevels('catnip'); 58 | }, TypeError); 59 | }); 60 | }); 61 | 62 | it('should chain properly', function () { 63 | let a = new CatLoggr(); 64 | let b = a 65 | .setDefaultMeta({ depth: 2 }) 66 | .setLevels([ 67 | { name: 'catnip', color: CatLoggr._chalk.red.bgBlack }, 68 | { name: 'fish', color: CatLoggr._chalk.black.bgRed } 69 | ]) 70 | .setLevel('catnip'); 71 | assert.equal(a, b); 72 | }); 73 | 74 | it('should execute pre-hooks', function() { 75 | let a = new CatLoggr(); 76 | let b; 77 | a.addPreHook(params => b = params.args); 78 | 79 | a.log('a', 'b', 'c') 80 | assert.deepStrictEqual(b, ['a', 'b', 'c']); 81 | }); 82 | 83 | it('should execute arg-hooks', function() { 84 | let a = new CatLoggr(); 85 | let b = []; 86 | a.addArgHook(params => b.push(params.arg)); 87 | 88 | a.log('a', 'b', 'c'); 89 | assert.deepStrictEqual(b, ['a', 'b', 'c']); 90 | }); 91 | 92 | it('should execute post-hooks', function() { 93 | let a = new CatLoggr(); 94 | let b; 95 | a.addPostHook(params => b = params.text); 96 | 97 | a.log('a', 'b', 'c'); 98 | assert.strictEqual(b, '\u001b[35ma\u001b[39m \u001b[35mb\u001b[39m \u001b[35mc\u001b[39m'); 99 | }); 100 | 101 | it('should be cute', function () { 102 | return true; 103 | }); 104 | }); -------------------------------------------------------------------------------- /src/Loggr.js.old: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const moment = require('moment'); 3 | const util = require('util'); 4 | 5 | /** 6 | * Class containing logging functionality 7 | */ 8 | module.exports = class CatLoggr { 9 | static get DefaultLevels() { 10 | return [ 11 | { name: 'fatal', color: chalk.red.bgBlack, err: true }, 12 | { name: 'error', color: chalk.black.bgRed, err: true }, 13 | { name: 'warn', color: chalk.black.bgYellow, err: true }, 14 | { name: 'trace', color: chalk.green.bgBlack, trace: true }, 15 | { name: 'init', color: chalk.black.bgBlue }, 16 | { name: 'info', color: chalk.black.bgGreen }, 17 | { name: 'verbose', color: chalk.black.bgCyan }, 18 | { name: 'debug', color: chalk.magenta.bgBlack, aliases: ['log', 'dir'] } 19 | ]; 20 | } 21 | 22 | /** 23 | * Creates an instance of the logger. 24 | * @param {Object} [options] Configuration options 25 | * @param {string|number} [options.shardId] The shard ID that the logger is on 26 | * @param {string|number} [options.shardLength=4] The maximum number of characters that a shard can be 27 | * @param {string} [options.level=info] The default log threshold 28 | * @param {level[]} [options.levels] Custom level definitions 29 | * @param {metaObject} [options.meta] The default meta configuration 30 | * @param {WriteStream} [options.stdout] The output stream to use for general logs 31 | * @param {WriteStream} [options.stderr] The output stream to use for error logs 32 | */ 33 | constructor({ shardId, shardLength = 4, level, levels, meta, stdout, stderr } = {}) { 34 | if (shardId) { 35 | this._shard = shardId; 36 | this._shardLength = shardLength; 37 | // if (typeof this._shard === 'number' && this._shard < 10) this._shard = '0' + this._shard; 38 | } 39 | 40 | this._stdout = stdout || process.stdout; 41 | this._stderr = stderr || process.stderr; 42 | 43 | this.setLevels(levels || CatLoggr.DefaultLevels); 44 | 45 | 46 | this.setLevel(level || this.__levels[this.__levels.length - 1].name); 47 | 48 | this.setDefaultMeta(meta || {}); 49 | 50 | this._meta = {}; 51 | 52 | this._hooks = { 53 | arg: [], 54 | post: [] 55 | }; 56 | } 57 | 58 | /** 59 | * A helper reference to the chalk library 60 | */ 61 | static get _chalk() { 62 | return chalk; 63 | } 64 | 65 | /** 66 | * An argument hook callback function 67 | * 68 | * @callback argHookCallback 69 | * @param {Object} params The params that are sent by the hook 70 | * @param {*} [params.arg] The provided argument 71 | * @param {Date} params.date The timestamp of execution 72 | * @returns {string|null} The processed argument result, or `null` to continue executing 73 | */ 74 | 75 | /** 76 | * Adds na arg-hook 77 | * @param {preHookCallback} func The hook callback 78 | * @returns {CatLoggr} Self for chaining 79 | */ 80 | addArgHook(func) { 81 | this._hooks.arg.push(func); 82 | 83 | return this; 84 | } 85 | 86 | /** 87 | * A post hook callback function 88 | * 89 | * @callback postHookCallback 90 | * @param {Object} params The params that are sent by the hook 91 | * @param {string} params.level The level of the log 92 | * @param {boolean} params.error A flag signifying that the log is an error 93 | * @param {string} params.text The final formatted text 94 | * @param {Date} params.date The timestamp of execution 95 | * @param {string} params.timestamp The formatted timestamp 96 | * @param {string} [params.shard] The shard ID 97 | * @returns {string|null} The processed result, or `null` to continue executing 98 | */ 99 | 100 | /** 101 | * 102 | * @param {postHookCallback} func 103 | * @returns {CatLoggr} Self for chaining 104 | */ 105 | addPostHook(func) { 106 | this._hooks.post.push(func); 107 | 108 | return this; 109 | } 110 | 111 | /** 112 | * @typedef {Object} metaObject 113 | * @property {number} [metaObject.depth=1] The depth of objects to inspect 114 | * @property {boolean} [metaObject.color=true] Whether to display colors 115 | * @property {boolean} [metaObject.trace=false] Whether to generate a stacktrace 116 | */ 117 | 118 | /** 119 | * Sets the default meta object to use for all logs. 120 | * @param {metaObject?} meta The default meta object to use 121 | * @returns {CatLoggr?} Self for chaining 122 | */ 123 | setDefaultMeta(meta) { 124 | if (typeof meta !== 'object') 125 | throw new TypeError('meta must be an object'); 126 | 127 | let temp = { depth: 1, color: true, trace: false }; 128 | Object.assign(temp, meta); 129 | 130 | this._defaultMeta = temp; 131 | 132 | return this; 133 | } 134 | 135 | /** 136 | * Sets the level threshold. Only logs on and above the threshold will be output. 137 | * @param {string} level The level threshold 138 | * @returns {CatLoggr} Self for chaining 139 | */ 140 | setLevel(level) { 141 | if (typeof level !== 'string') 142 | throw new TypeError('level must be a string'); 143 | 144 | if (!this._levels[level]) 145 | throw new Error(`the level '${level}' does not exist`); 146 | 147 | this.__level = level; 148 | return this; 149 | } 150 | 151 | /** 152 | * @typedef level 153 | * @property {string} level.name The name of the level 154 | * @property {Object} level.color The color of the level (using chalk) 155 | * @property {string[]} level.aliases The alternate names that can be used to invoke the level 156 | * @property {boolean} level.err A flag signifying that the level writes to stderr 157 | * @property {boolean} level.trace A flag signifying that the level should generate a stacktrace 158 | */ 159 | 160 | /** 161 | * Overwrites the currently set levels with a custom set. 162 | * @param {level[]} levels An array of levels, in order from high priority to low priority 163 | * @returns {CatLoggr} Self for chaining 164 | */ 165 | setLevels(levels) { 166 | if (!Array.isArray(levels)) 167 | throw new TypeError('levels must be an array.'); 168 | 169 | this._levels = {}; 170 | let max = 0; 171 | this.__levels = levels; 172 | this.__levels = this.__levels.map(l => { 173 | l.position = this.__levels.indexOf(l); 174 | this._levels[l.name] = l; 175 | let func = function (...args) { 176 | return this._format(l, ...args); 177 | }.bind(this); 178 | this[l.name] = func; 179 | if (l.aliases && Array.isArray(l.aliases)) 180 | for (const alias of l.aliases) this[alias] = func; 181 | max = l.name.length > max ? l.name.length : max; 182 | return l; 183 | }); 184 | 185 | if (!this._levels[this.__level]) 186 | this.__level = this.__levels[this.__levels.length - 1].name; 187 | 188 | this._maxLength = max + 2; 189 | 190 | return this; 191 | } 192 | 193 | /** 194 | * Registers CatLoggr as the global `console` property. 195 | * @returns {CatLoggr} Self for chaining 196 | */ 197 | setGlobal() { 198 | Object.defineProperty.bind(this)(global, 'console', { 199 | get: () => { 200 | return this; 201 | } 202 | }); 203 | return this; 204 | } 205 | 206 | get _level() { 207 | return this._levels[this.__level]; 208 | } 209 | 210 | get _timestamp() { 211 | let ts = moment(); 212 | let formatted = ts.format('MM/DD HH:mm:ss'); 213 | return { 214 | raw: ts.toDate(), 215 | formatted: chalk.black.bgWhite(` ${formatted} `), 216 | formattedRaw: formatted 217 | }; 218 | } 219 | 220 | /** 221 | * Center aligns text. 222 | * @param {string} text The text to align 223 | * @param {number} length The length that it should be padded to 224 | * @returns {string} The padded text 225 | */ 226 | _centrePad(text, length) { 227 | if (text.length < length) 228 | return ' '.repeat(Math.floor((length - text.length) / 2)) 229 | + text + ' '.repeat(Math.ceil((length - text.length) / 2)); 230 | else return text; 231 | } 232 | 233 | /** 234 | * Writes the log to the proper stream. 235 | * @param {Level} level The level of the log 236 | * @param {string} text The text to write 237 | * @param {boolean} err A flag signifying whether to write to stderr 238 | * @param {Object} [timestamp] An optional timestamp to use 239 | * @param {string} timestamp.formatted The formatted timestamp 240 | * @param {Date} timestamp.raw The raw timestamp 241 | * @returns {CatLoggr} Self for chaining 242 | */ 243 | _write(level, text, err = false, timestamp) { 244 | if (!timestamp) timestamp = this._timestamp; 245 | let levelStr = level.color(this._centrePad(level.name, this._maxLength)); 246 | let stream = err ? this._stderr : this._stdout; 247 | let shardText = ''; 248 | if (this._shard) 249 | shardText = chalk.black.bold.bgYellow(this._centrePad(this._meta.shard ? this._meta.shard.toString() : this._shard.toString(), this._shardLength, false)); 250 | 251 | for (const hook of this._hooks.post) { 252 | if (typeof hook == 'function') { 253 | let res = hook({ 254 | text, 255 | date: timestamp.raw, 256 | timestamp: timestamp.formattedRaw, 257 | shard: this._shard ? this._shard.toString() : undefined, 258 | level: level.name, 259 | error: level.err 260 | }); 261 | if (res === undefined || res === null) continue; 262 | else { 263 | text = res.toString(); 264 | } 265 | break; 266 | } 267 | } 268 | 269 | stream.write(`${shardText}${timestamp.formatted}${levelStr} ${text}\n`); 270 | 271 | this._meta = {}; 272 | return this; 273 | } 274 | 275 | /** 276 | * Sets the meta for the next log. 277 | * @param {metaObject} meta - The meta object to set 278 | * @returns {CatLoggr} Self for chaining 279 | */ 280 | meta(meta = {}) { 281 | let temp = {}; 282 | Object.assign(temp, this._defaultMeta, meta); 283 | this._meta = temp; 284 | return this; 285 | } 286 | 287 | /** 288 | * Formats logs in preparation for writing. 289 | * @param {*} level The level of the log 290 | * @param {*} args The args that were directly passed to the function 291 | * @returns {CatLoggr} Self for chaining 292 | */ 293 | _format(level, ...args) { 294 | let timestamp = this._timestamp; 295 | if (level.position > this._level.position) return; 296 | let output = ''; 297 | let text = []; 298 | if (typeof args[0] === 'string') { 299 | let formats = args[0].match(/%[sdifjoO]/g); 300 | if (formats) { 301 | let a = args.splice(1, formats.length); 302 | args.unshift(util.format(args.shift(), ...a)); 303 | } 304 | } 305 | for (const arg of args) { 306 | let finished = false; 307 | for (const hook of this._hooks.arg) { 308 | if (typeof hook == 'function') { 309 | let res = hook({ arg, date: timestamp.raw }); 310 | if (res === undefined || res === null) continue; 311 | else if (Array.isArray(res)) { 312 | text.push(...res); 313 | } else { 314 | text.push(res); 315 | } 316 | finished = true; 317 | break; 318 | } 319 | } 320 | if (finished) continue; 321 | 322 | if (typeof arg === 'string') { 323 | text.push(chalk.magenta(this._meta.quote ? `'${arg}'` : arg)); 324 | } else if (typeof arg === 'number') { 325 | text.push(chalk.cyan(arg)); 326 | } else if (typeof arg === 'object') { 327 | text.push('\n'); 328 | 329 | if (arg instanceof Error) { 330 | text.push(chalk.red(arg.stack)); 331 | } else { 332 | text.push(util.inspect(arg, this._meta)); 333 | } 334 | } else text.push(arg); 335 | } 336 | 337 | output += text.join(' '); 338 | if (level.trace || this._meta.trace) { 339 | output += '\n' + new Error().stack.split('\n').slice(1).join('\n'); 340 | } 341 | if (level.err) output = chalk.red(output); 342 | return this._write(level, output, level.err).meta(); 343 | } 344 | }; -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | "@babel/code-frame@^7.0.0": 6 | version "7.14.5" 7 | resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.14.5.tgz#23b08d740e83f49c5e59945fbf1b43e80bbf4edb" 8 | integrity sha512-9pzDqyc6OLDaqe+zbACgFkb6fKMNG6CObKpnYXChRsvYGyEdc7CA2BaqeOM+vOtCS5ndmJicPJhKAwYRI6UfFw== 9 | dependencies: 10 | "@babel/highlight" "^7.14.5" 11 | 12 | "@babel/helper-validator-identifier@^7.14.5": 13 | version "7.14.5" 14 | resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.5.tgz#d0f0e277c512e0c938277faa85a3968c9a44c0e8" 15 | integrity sha512-5lsetuxCLilmVGyiLEfoHBRX8UCFD+1m2x3Rj97WrW3V7H3u4RWRXA4evMjImCsin2J2YT0QaVDGf+z8ondbAg== 16 | 17 | "@babel/highlight@^7.14.5": 18 | version "7.14.5" 19 | resolved "https://registry.yarnpkg.com/@babel/highlight/-/highlight-7.14.5.tgz#6861a52f03966405001f6aa534a01a24d99e8cd9" 20 | integrity sha512-qf9u2WFWVV0MppaL877j2dBtQIDgmidgjGk5VIMw3OadXvYaXn66U1BFlH2t4+t3i+8PhedppRv+i40ABzd+gg== 21 | dependencies: 22 | "@babel/helper-validator-identifier" "^7.14.5" 23 | chalk "^2.0.0" 24 | js-tokens "^4.0.0" 25 | 26 | "@types/node@^15.12.2": 27 | version "15.12.2" 28 | resolved "https://registry.yarnpkg.com/@types/node/-/node-15.12.2.tgz#1f2b42c4be7156ff4a6f914b2fb03d05fa84e38d" 29 | integrity sha512-zjQ69G564OCIWIOHSXyQEEDpdpGl+G348RAKY0XXy9Z5kU9Vzv1GMNnkar/ZJ8dzXB3COzD9Mo9NtRZ4xfgUww== 30 | 31 | ansi-styles@^3.2.1: 32 | version "3.2.1" 33 | resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.1.tgz#41fbb20243e50b12be0f04b8dedbf07520ce841d" 34 | integrity sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA== 35 | dependencies: 36 | color-convert "^1.9.0" 37 | 38 | argparse@^1.0.7: 39 | version "1.0.10" 40 | resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911" 41 | integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg== 42 | dependencies: 43 | sprintf-js "~1.0.2" 44 | 45 | balanced-match@^1.0.0: 46 | version "1.0.2" 47 | resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" 48 | integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== 49 | 50 | brace-expansion@^1.1.7: 51 | version "1.1.11" 52 | resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd" 53 | integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA== 54 | dependencies: 55 | balanced-match "^1.0.0" 56 | concat-map "0.0.1" 57 | 58 | browser-stdout@1.3.1: 59 | version "1.3.1" 60 | resolved "https://registry.yarnpkg.com/browser-stdout/-/browser-stdout-1.3.1.tgz#baa559ee14ced73452229bad7326467c61fabd60" 61 | integrity sha512-qhAVI1+Av2X7qelOfAIYwXONood6XlZE/fXaBSmW/T5SzLAmCgzi+eiWE7fUvbHaeNBQH13UftjpXxsfLkMpgw== 62 | 63 | builtin-modules@^1.1.1: 64 | version "1.1.1" 65 | resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f" 66 | integrity sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8= 67 | 68 | chalk@^2.0.0, chalk@^2.3.0, chalk@^2.4.1: 69 | version "2.4.2" 70 | resolved "https://registry.yarnpkg.com/chalk/-/chalk-2.4.2.tgz#cd42541677a54333cf541a49108c1432b44c9424" 71 | integrity sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ== 72 | dependencies: 73 | ansi-styles "^3.2.1" 74 | escape-string-regexp "^1.0.5" 75 | supports-color "^5.3.0" 76 | 77 | color-convert@^1.9.0: 78 | version "1.9.3" 79 | resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.3.tgz#bb71850690e1f136567de629d2d5471deda4c1e8" 80 | integrity sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg== 81 | dependencies: 82 | color-name "1.1.3" 83 | 84 | color-name@1.1.3: 85 | version "1.1.3" 86 | resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.3.tgz#a7d0558bd89c42f795dd42328f740831ca53bc25" 87 | integrity sha1-p9BVi9icQveV3UIyj3QIMcpTvCU= 88 | 89 | commander@2.15.1: 90 | version "2.15.1" 91 | resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" 92 | integrity sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag== 93 | 94 | commander@^2.12.1: 95 | version "2.20.3" 96 | resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" 97 | integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== 98 | 99 | concat-map@0.0.1: 100 | version "0.0.1" 101 | resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" 102 | integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= 103 | 104 | dayjs@^1.10.5: 105 | version "1.10.5" 106 | resolved "https://registry.yarnpkg.com/dayjs/-/dayjs-1.10.5.tgz#5600df4548fc2453b3f163ebb2abbe965ccfb986" 107 | integrity sha512-BUFis41ikLz+65iH6LHQCDm4YPMj5r1YFLdupPIyM4SGcXMmtiLQ7U37i+hGS8urIuqe7I/ou3IS1jVc4nbN4g== 108 | 109 | debug@3.1.0: 110 | version "3.1.0" 111 | resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261" 112 | integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g== 113 | dependencies: 114 | ms "2.0.0" 115 | 116 | diff@3.5.0: 117 | version "3.5.0" 118 | resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12" 119 | integrity sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA== 120 | 121 | diff@^4.0.1: 122 | version "4.0.2" 123 | resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" 124 | integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== 125 | 126 | escape-string-regexp@1.0.5, escape-string-regexp@^1.0.5: 127 | version "1.0.5" 128 | resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" 129 | integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= 130 | 131 | esprima@^4.0.0: 132 | version "4.0.1" 133 | resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71" 134 | integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A== 135 | 136 | fs.realpath@^1.0.0: 137 | version "1.0.0" 138 | resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" 139 | integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8= 140 | 141 | function-bind@^1.1.1: 142 | version "1.1.1" 143 | resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" 144 | integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A== 145 | 146 | glob@7.1.2: 147 | version "7.1.2" 148 | resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" 149 | integrity sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ== 150 | dependencies: 151 | fs.realpath "^1.0.0" 152 | inflight "^1.0.4" 153 | inherits "2" 154 | minimatch "^3.0.4" 155 | once "^1.3.0" 156 | path-is-absolute "^1.0.0" 157 | 158 | glob@^7.1.1: 159 | version "7.1.7" 160 | resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.7.tgz#3b193e9233f01d42d0b3f78294bbeeb418f94a90" 161 | integrity sha512-OvD9ENzPLbegENnYP5UUfJIirTg4+XwMWGaQfQTY0JenxNvvIKP3U3/tAQSPIu/lHxXYSZmpXlUHeqAIdKzBLQ== 162 | dependencies: 163 | fs.realpath "^1.0.0" 164 | inflight "^1.0.4" 165 | inherits "2" 166 | minimatch "^3.0.4" 167 | once "^1.3.0" 168 | path-is-absolute "^1.0.0" 169 | 170 | growl@1.10.5: 171 | version "1.10.5" 172 | resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e" 173 | integrity sha512-qBr4OuELkhPenW6goKVXiv47US3clb3/IbuWF9KNKEijAy9oeHxU9IgzjvJhHkUzhaj7rOUD7+YGWqUjLp5oSA== 174 | 175 | has-flag@^3.0.0: 176 | version "3.0.0" 177 | resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-3.0.0.tgz#b5d454dc2199ae225699f3467e5a07f3b955bafd" 178 | integrity sha1-tdRU3CGZriJWmfNGfloH87lVuv0= 179 | 180 | has@^1.0.3: 181 | version "1.0.3" 182 | resolved "https://registry.yarnpkg.com/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796" 183 | integrity sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw== 184 | dependencies: 185 | function-bind "^1.1.1" 186 | 187 | he@1.1.1: 188 | version "1.1.1" 189 | resolved "https://registry.yarnpkg.com/he/-/he-1.1.1.tgz#93410fd21b009735151f8868c2f271f3427e23fd" 190 | integrity sha1-k0EP0hsAlzUVH4howvJx80J+I/0= 191 | 192 | inflight@^1.0.4: 193 | version "1.0.6" 194 | resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9" 195 | integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk= 196 | dependencies: 197 | once "^1.3.0" 198 | wrappy "1" 199 | 200 | inherits@2: 201 | version "2.0.3" 202 | resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de" 203 | integrity sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4= 204 | 205 | is-core-module@^2.2.0: 206 | version "2.4.0" 207 | resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.4.0.tgz#8e9fc8e15027b011418026e98f0e6f4d86305cc1" 208 | integrity sha512-6A2fkfq1rfeQZjxrZJGerpLCTHRNEBiSgnu0+obeJpEPZRUooHgsizvzv0ZjJwOz3iWIHdJtVWJ/tmPr3D21/A== 209 | dependencies: 210 | has "^1.0.3" 211 | 212 | js-tokens@^4.0.0: 213 | version "4.0.0" 214 | resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" 215 | integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ== 216 | 217 | js-yaml@^3.13.1: 218 | version "3.14.1" 219 | resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537" 220 | integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g== 221 | dependencies: 222 | argparse "^1.0.7" 223 | esprima "^4.0.0" 224 | 225 | minimatch@3.0.4, minimatch@^3.0.4: 226 | version "3.0.4" 227 | resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" 228 | integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== 229 | dependencies: 230 | brace-expansion "^1.1.7" 231 | 232 | minimist@0.0.8: 233 | version "0.0.8" 234 | resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d" 235 | integrity sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0= 236 | 237 | minimist@^1.2.5: 238 | version "1.2.5" 239 | resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" 240 | integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== 241 | 242 | mkdirp@0.5.1: 243 | version "0.5.1" 244 | resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903" 245 | integrity sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM= 246 | dependencies: 247 | minimist "0.0.8" 248 | 249 | mkdirp@^0.5.1: 250 | version "0.5.5" 251 | resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.5.tgz#d91cefd62d1436ca0f41620e251288d420099def" 252 | integrity sha512-NKmAlESf6jMGym1++R0Ra7wvhV+wFW63FaSOFPwRahvea0gMUcGUhVeAg/0BC0wiv9ih5NYPB1Wn1UEI1/L+xQ== 253 | dependencies: 254 | minimist "^1.2.5" 255 | 256 | mocha@^5.2.0: 257 | version "5.2.0" 258 | resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.2.0.tgz#6d8ae508f59167f940f2b5b3c4a612ae50c90ae6" 259 | integrity sha512-2IUgKDhc3J7Uug+FxMXuqIyYzH7gJjXECKe/w43IGgQHTSj3InJi+yAA7T24L9bQMRKiUEHxEX37G5JpVUGLcQ== 260 | dependencies: 261 | browser-stdout "1.3.1" 262 | commander "2.15.1" 263 | debug "3.1.0" 264 | diff "3.5.0" 265 | escape-string-regexp "1.0.5" 266 | glob "7.1.2" 267 | growl "1.10.5" 268 | he "1.1.1" 269 | minimatch "3.0.4" 270 | mkdirp "0.5.1" 271 | supports-color "5.4.0" 272 | 273 | ms@2.0.0: 274 | version "2.0.0" 275 | resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8" 276 | integrity sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g= 277 | 278 | once@^1.3.0: 279 | version "1.4.0" 280 | resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1" 281 | integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E= 282 | dependencies: 283 | wrappy "1" 284 | 285 | path-is-absolute@^1.0.0: 286 | version "1.0.1" 287 | resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f" 288 | integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18= 289 | 290 | path-parse@^1.0.6: 291 | version "1.0.7" 292 | resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735" 293 | integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw== 294 | 295 | resolve@^1.3.2: 296 | version "1.20.0" 297 | resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.20.0.tgz#629a013fb3f70755d6f0b7935cc1c2c5378b1975" 298 | integrity sha512-wENBPt4ySzg4ybFQW2TT1zMQucPK95HSh/nq2CFTZVOGut2+pQvSsgtda4d26YrYcr067wjbmzOG8byDPBX63A== 299 | dependencies: 300 | is-core-module "^2.2.0" 301 | path-parse "^1.0.6" 302 | 303 | semver@^5.3.0: 304 | version "5.7.1" 305 | resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" 306 | integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== 307 | 308 | sprintf-js@~1.0.2: 309 | version "1.0.3" 310 | resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" 311 | integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw= 312 | 313 | supports-color@5.4.0: 314 | version "5.4.0" 315 | resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54" 316 | integrity sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w== 317 | dependencies: 318 | has-flag "^3.0.0" 319 | 320 | supports-color@^5.3.0: 321 | version "5.5.0" 322 | resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f" 323 | integrity sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow== 324 | dependencies: 325 | has-flag "^3.0.0" 326 | 327 | tslib@^1.8.0, tslib@^1.8.1: 328 | version "1.14.1" 329 | resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" 330 | integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== 331 | 332 | tslint@^5.12.1: 333 | version "5.20.1" 334 | resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.20.1.tgz#e401e8aeda0152bc44dd07e614034f3f80c67b7d" 335 | integrity sha512-EcMxhzCFt8k+/UP5r8waCf/lzmeSyVlqxqMEDQE7rWYiQky8KpIBz1JAoYXfROHrPZ1XXd43q8yQnULOLiBRQg== 336 | dependencies: 337 | "@babel/code-frame" "^7.0.0" 338 | builtin-modules "^1.1.1" 339 | chalk "^2.3.0" 340 | commander "^2.12.1" 341 | diff "^4.0.1" 342 | glob "^7.1.1" 343 | js-yaml "^3.13.1" 344 | minimatch "^3.0.4" 345 | mkdirp "^0.5.1" 346 | resolve "^1.3.2" 347 | semver "^5.3.0" 348 | tslib "^1.8.0" 349 | tsutils "^2.29.0" 350 | 351 | tsutils@^2.29.0: 352 | version "2.29.0" 353 | resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.29.0.tgz#32b488501467acbedd4b85498673a0812aca0b99" 354 | integrity sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA== 355 | dependencies: 356 | tslib "^1.8.1" 357 | 358 | typescript@^3.2.4: 359 | version "3.9.9" 360 | resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.9.tgz#e69905c54bc0681d0518bd4d587cc6f2d0b1a674" 361 | integrity sha512-kdMjTiekY+z/ubJCATUPlRDl39vXYiMV9iyeMuEuXZh2we6zz80uovNN2WlAxmmdE/Z/YQe+EbOEXB5RHEED3w== 362 | 363 | wrappy@1: 364 | version "1.0.2" 365 | resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f" 366 | integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8= 367 | -------------------------------------------------------------------------------- /src/Loggr.ts: -------------------------------------------------------------------------------- 1 | import chalk from 'chalk'; 2 | import { Chalk, ColorSupport } from 'chalk'; 3 | import * as dayjs from 'dayjs'; 4 | import { format, inspect } from 'util'; 5 | import { WriteStream } from 'tty'; 6 | import * as util from 'util'; 7 | 8 | export class LoggrConfig { 9 | public shardId?: number | string; 10 | public shardLength?: number; 11 | 12 | public timestampFormat?: string; 13 | 14 | public level?: string; 15 | public levels?: LogLevel[]; 16 | 17 | public meta?: LogMeta; 18 | 19 | public stdout?: WriteStream; 20 | public stderr?: WriteStream; 21 | 22 | constructor({ shardId, shardLength, level, levels, meta, stdout, stderr, timestampFormat }: LoggrConfig) { 23 | this.shardId = shardId; 24 | this.shardLength = shardLength; 25 | this.level = level; 26 | this.levels = levels; 27 | this.meta = meta; 28 | this.stdout = stdout; 29 | this.stderr = stderr; 30 | this.timestampFormat = timestampFormat; 31 | } 32 | } 33 | 34 | 35 | export class LogLevel { 36 | public name: string; 37 | public color: Chalk & { supportsColor: ColorSupport }; 38 | public err: boolean = false; 39 | public trace: boolean = false; 40 | public aliases: string[] = []; 41 | public position?: number; 42 | 43 | constructor(name: string, color: Chalk & { supportsColor: ColorSupport }) { 44 | this.name = name; 45 | this.color = color; 46 | } 47 | 48 | setError(err: boolean = true): LogLevel { 49 | this.err = err; 50 | return this; 51 | } 52 | 53 | setTrace(trace: boolean = true): LogLevel { 54 | this.trace = trace; 55 | return this; 56 | } 57 | 58 | setAliases(...aliases: string[]): LogLevel { 59 | this.aliases = aliases; 60 | return this; 61 | } 62 | } 63 | 64 | /** 65 | * @typedef {Object} LogMeta 66 | * @property {number} [metaObject.depth=1] The depth of objects to inspect 67 | * @property {boolean} [metaObject.color=true] Whether to display colors 68 | * @property {boolean} [metaObject.trace=false] Whether to generate a stacktrace 69 | */ 70 | export class LogMeta { 71 | public depth?: number = 1; 72 | public color?: boolean = true; 73 | public trace?: boolean = false; 74 | public shardId?: string | number = ''; 75 | public quote?: boolean = false; 76 | public context?: object = {}; 77 | 78 | constructor({ depth = 1, color = true, trace = false, shardId = '', quote = false, context = {} }: LogMeta) { 79 | this.depth = depth; 80 | this.color = color; 81 | this.trace = trace; 82 | this.shardId = shardId; 83 | this.quote = quote; 84 | this.context = context; 85 | } 86 | } 87 | 88 | /** 89 | * An argument hook callback function 90 | * 91 | * @callback ArgHookCallback 92 | * @param {Object} params The params that are sent by the hook 93 | * @param {*} [params.arg] The provided argument 94 | * @param {Date} params.date The timestamp of execution 95 | * @returns {string|null} The processed argument result, or `null` to continue executing 96 | */ 97 | export type ArgHookCallback = (params: { arg?: any, date: Date }) => string | null; 98 | 99 | /** 100 | * A post hook callback function 101 | * 102 | * @callback PostHookCallback 103 | * @param {Object} params The params that are sent by the hook 104 | * @param {string} params.level The level of the log 105 | * @param {boolean} params.error A flag signifying that the log is an error 106 | * @param {string} params.text The final formatted text 107 | * @param {Date} params.date The timestamp of execution 108 | * @param {string} params.timestamp The formatted timestamp 109 | * @param {string} [params.shard] The shard ID 110 | * @param {object} [params.context] Context passed through meta info 111 | * @param {LogMeta} [params.meta] Raw meta info 112 | * @returns {string|null} The processed result, or `null` to continue executing 113 | */ 114 | export type PostHookCallback = (params: { 115 | level: string, 116 | error: boolean, 117 | text: string, 118 | date: Date, 119 | timestamp: string, 120 | shard?: string, 121 | context?: Object, 122 | meta: LogMeta 123 | }) => string | null; 124 | 125 | /** 126 | * A post hook callback function 127 | * 128 | * @callback PreHookCallback 129 | * @param {Object} params The params that are sent by the hook 130 | * @param {string} params.level The level of the log 131 | * @param {boolean} params.error A flag signifying that the log is an error 132 | * @param {any[]} params.args The args being logged 133 | * @param {Date} params.date The timestamp of execution 134 | * @param {string} params.timestamp The formatted timestamp 135 | * @param {string} [params.shard] The shard ID 136 | * @param {object} [params.context] Context passed through meta info 137 | * @param {LogMeta} [params.meta] Raw meta info 138 | * @returns {string|null} The processed result, or `null` to continue executing 139 | */ 140 | export type PreHookCallback = (params: { 141 | level: string, 142 | error: boolean, 143 | args: any[], 144 | date: Date, 145 | timestamp: string, 146 | shard?: string, 147 | context?: Object, 148 | meta: LogMeta 149 | }) => string | null; 150 | 151 | export class LogHooks { 152 | public pre: PreHookCallback[] = []; 153 | public arg: ArgHookCallback[] = []; 154 | public post: PostHookCallback[] = []; 155 | } 156 | 157 | 158 | /** 159 | * Class containing logging functionality 160 | */ 161 | export default class CatLoggr { 162 | static get DefaultLevels(): LogLevel[] { 163 | return [ 164 | new LogLevel('fatal', chalk.red.bgBlack).setError(), 165 | new LogLevel('error', chalk.black.bgRed).setError(), 166 | new LogLevel('warn', chalk.black.bgYellow).setError(), 167 | new LogLevel('trace', chalk.green.bgBlack).setTrace(), 168 | new LogLevel('init', chalk.black.bgBlue), 169 | new LogLevel('info', chalk.black.bgGreen), 170 | new LogLevel('verbose', chalk.black.bgCyan), 171 | new LogLevel('debug', chalk.magenta.bgBlack).setAliases('log', 'dir') 172 | ]; 173 | } 174 | 175 | private _config: LoggrConfig; 176 | private _shard: number | string; 177 | private _shardLength: number; 178 | private _levelName: string; 179 | private _levels: LogLevel[]; 180 | private _levelMap: { [name: string]: LogLevel }; 181 | private _meta: LogMeta; 182 | private _defaultMeta: LogMeta; 183 | private _stdout: WriteStream | NodeJS.WriteStream; 184 | private _stderr: WriteStream | NodeJS.WriteStream; 185 | private _maxLength: number; 186 | private _hooks: LogHooks; 187 | 188 | [key: string]: any; 189 | 190 | /** 191 | * Creates an instance of the logger. 192 | * @param {LoggrConfig} [options] Configuration options 193 | * @param {string|number} [options.shardId] The shard ID that the logger is on 194 | * @param {string|number} [options.shardLength=4] The maximum number of characters that a shard can be 195 | * @param {string} [options.level=info] The default log threshold 196 | * @param {level[]} [options.levels] Custom level definitions 197 | * @param {metaObject} [options.meta] The default meta configuration 198 | * @param {WriteStream} [options.stdout] The output stream to use for general logs 199 | * @param {WriteStream} [options.stderr] The output stream to use for error logs 200 | */ 201 | constructor(config?: LoggrConfig) { 202 | if (!config) config = new LoggrConfig({}); 203 | this._config = config; 204 | if (config.shardId) { 205 | this._shard = config.shardId; 206 | this._shardLength = config.shardLength; 207 | // if (typeof this._shard === 'number' && this._shard < 10) this._shard = '0' + this._shard; 208 | } 209 | 210 | this._stdout = config.stdout || process.stdout; 211 | this._stderr = config.stderr || process.stderr; 212 | 213 | this.setLevels(config.levels || CatLoggr.DefaultLevels); 214 | 215 | this.setLevel(config.level || this._levels[this._levels.length - 1].name); 216 | 217 | this.setDefaultMeta(config.meta || {}); 218 | 219 | this._meta = {}; 220 | 221 | this._hooks = new LogHooks(); 222 | } 223 | 224 | /** 225 | * A helper reference to the chalk library 226 | */ 227 | static get _chalk(): Chalk { 228 | return chalk; 229 | } 230 | 231 | /** 232 | * Adds a pre-hook 233 | * @param {ArgHookCallback} func The hook callback 234 | * @returns {CatLoggr} Self for chaining 235 | */ 236 | addPreHook(func: ArgHookCallback) { 237 | this._hooks.pre.push(func); 238 | 239 | return this; 240 | } 241 | 242 | /** 243 | * Adds an arg-hook 244 | * @param {ArgHookCallback} func The hook callback 245 | * @returns {CatLoggr} Self for chaining 246 | */ 247 | addArgHook(func: ArgHookCallback) { 248 | this._hooks.arg.push(func); 249 | 250 | return this; 251 | } 252 | 253 | /** 254 | * Adds a post-hook 255 | * @param {PostHookCallback} func 256 | * @returns {CatLoggr} Self for chaining 257 | */ 258 | addPostHook(func: PostHookCallback) { 259 | this._hooks.post.push(func); 260 | 261 | return this; 262 | } 263 | 264 | 265 | 266 | /** 267 | * Sets the default meta object to use for all logs. 268 | * @param {metaObject?} meta The default meta object to use 269 | * @returns {CatLoggr?} Self for chaining 270 | */ 271 | setDefaultMeta(meta: LogMeta) { 272 | if (typeof meta !== 'object') 273 | throw new TypeError('meta must be an object'); 274 | 275 | this._defaultMeta = new LogMeta(meta); 276 | 277 | return this; 278 | } 279 | 280 | /** 281 | * Sets the level threshold. Only logs on and above the threshold will be output. 282 | * @param {string} level The level threshold 283 | * @returns {CatLoggr} Self for chaining 284 | */ 285 | setLevel(level: string) { 286 | if (typeof level !== 'string') 287 | throw new TypeError('level must be a string'); 288 | 289 | if (!this._levelMap[level]) 290 | throw new Error(`the level '${level}' does not exist`); 291 | 292 | this._levelName = level; 293 | return this; 294 | } 295 | 296 | /** 297 | * @typedef level 298 | * @property {string} level.name The name of the level 299 | * @property {Object} level.color The color of the level (using chalk) 300 | * @property {string[]} level.aliases The alternate names that can be used to invoke the level 301 | * @property {boolean} level.err A flag signifying that the level writes to stderr 302 | * @property {boolean} level.trace A flag signifying that the level should generate a stacktrace 303 | */ 304 | 305 | /** 306 | * Overwrites the currently set levels with a custom set. 307 | * @param {level[]} levels An array of levels, in order from high priority to low priority 308 | * @returns {CatLoggr} Self for chaining 309 | */ 310 | setLevels(levels: LogLevel[]) { 311 | if (!Array.isArray(levels)) 312 | throw new TypeError('levels must be an array.'); 313 | 314 | this._levelMap = {}; 315 | this._levels = levels; 316 | let max = 0; 317 | this._levels = this._levels.map(l => { 318 | l.position = this._levels.indexOf(l); 319 | this._levelMap[l.name] = l; 320 | let func = function (...args: any[]) { 321 | return this._format(l, ...args); 322 | }.bind(this); 323 | this[l.name] = func; 324 | if (l.aliases && Array.isArray(l.aliases)) 325 | for (const alias of l.aliases) this[alias] = func; 326 | max = l.name.length > max ? l.name.length : max; 327 | return l; 328 | }); 329 | 330 | if (!this._levelMap[this._levelName]) 331 | this._levelName = this._levels[this._levels.length - 1].name; 332 | 333 | this._maxLength = max + 2; 334 | 335 | return this; 336 | } 337 | 338 | /** 339 | * Registers CatLoggr as the global `console` property. 340 | * @returns {CatLoggr} Self for chaining 341 | */ 342 | setGlobal() { 343 | Object.defineProperty.bind(this)(global, 'console', { 344 | get: () => { 345 | return this; 346 | } 347 | }); 348 | return this; 349 | } 350 | 351 | get _level() { 352 | return this._levelMap[this._levelName]; 353 | } 354 | 355 | get _timestamp() { 356 | let ts = dayjs(); 357 | let formatted = ts.format(this._config.timestampFormat || 'MM/DD HH:mm:ss'); 358 | return { 359 | raw: ts.toDate(), 360 | formatted: chalk.black.bgWhite(` ${formatted} `), 361 | formattedRaw: formatted 362 | }; 363 | } 364 | 365 | /** 366 | * Center aligns text. 367 | * @param {string} text The text to align 368 | * @param {number} length The length that it should be padded to 369 | * @returns {string} The padded text 370 | */ 371 | _centrePad(text: string, length: number) { 372 | if (text.length < length) 373 | return ' '.repeat(Math.floor((length - text.length) / 2)) 374 | + text + ' '.repeat(Math.ceil((length - text.length) / 2)); 375 | else return text; 376 | } 377 | 378 | /** 379 | * Writes the log to the proper stream. 380 | * @param {Level} level The level of the log 381 | * @param {string} text The text to write 382 | * @param {boolean} err A flag signifying whether to write to stderr 383 | * @param {Object} [timestamp] An optional timestamp to use 384 | * @param {string} timestamp.formatted The formatted timestamp 385 | * @param {Date} timestamp.raw The raw timestamp 386 | * @returns {CatLoggr} Self for chaining 387 | */ 388 | _write(level: LogLevel, text: string, err: boolean = false, 389 | timestamp?: { formatted: string, raw: Date, formattedRaw: string }) { 390 | if (!timestamp) timestamp = this._timestamp; 391 | let levelStr = level.color(this._centrePad(level.name, this._maxLength)); 392 | let stream = err ? this._stderr : this._stdout; 393 | let shardText = ''; 394 | if (this._shard) 395 | shardText = chalk.black.bold.bgYellow( 396 | this._centrePad(this._meta.shardId ? this._meta.shardId.toString() : this._shard.toString(), 397 | this._shardLength) 398 | ); 399 | 400 | for (const hook of this._hooks.post) { 401 | if (typeof hook == 'function') { 402 | let res = hook({ 403 | text, 404 | date: timestamp.raw, 405 | timestamp: timestamp.formattedRaw, 406 | shard: this._shard ? this._shard.toString() : undefined, 407 | level: level.name, 408 | error: level.err, 409 | context: this._meta.context, 410 | meta: this._meta 411 | }); 412 | if (res === undefined || res === null) continue; 413 | else { 414 | text = res.toString(); 415 | } 416 | break; 417 | } 418 | } 419 | 420 | stream.write(`${shardText}${timestamp.formatted}${levelStr} ${text}\n`); 421 | 422 | this._meta = {}; 423 | return this; 424 | } 425 | 426 | /** 427 | * Sets the meta for the next log. 428 | * @param {metaObject} meta - The meta object to set 429 | * @returns {CatLoggr} Self for chaining 430 | */ 431 | meta(meta = {}) { 432 | let temp = {}; 433 | Object.assign(temp, this._defaultMeta, meta); 434 | this._meta = temp; 435 | return this; 436 | } 437 | 438 | /** 439 | * Formats logs in preparation for writing. 440 | * @param {string} level The level of the log 441 | * @param {*} args The args that were directly passed to the function 442 | * @returns {CatLoggr} Self for chaining 443 | */ 444 | _format(level: LogLevel, ...args: any[]) { 445 | let timestamp = this._timestamp; 446 | if (level.position > this._level.position) return; 447 | let output = ''; 448 | let text = []; 449 | if (typeof args[0] === 'string') { 450 | let formats = args[0].match(/%[sdifjoO]/g); 451 | if (formats) { 452 | let a = args.splice(1, formats.length); 453 | args.unshift(util.format(args.shift(), ...a)); 454 | } 455 | } 456 | 457 | for (const hook of this._hooks.pre) { 458 | let res = hook({ 459 | args, 460 | date: timestamp.raw, 461 | timestamp: timestamp.formattedRaw, 462 | shard: this._shard ? this._shard.toString() : undefined, 463 | level: level.name, 464 | error: level.err, 465 | context: this._meta.context, 466 | meta: this._meta 467 | }); 468 | } 469 | 470 | for (const arg of args) { 471 | let finished = false; 472 | for (const hook of this._hooks.arg) { 473 | if (typeof hook == 'function') { 474 | let res = hook({ arg, date: timestamp.raw }); 475 | if (res === undefined || res === null) continue; 476 | else if (Array.isArray(res)) { 477 | text.push(...res); 478 | } else { 479 | text.push(res); 480 | } 481 | finished = true; 482 | break; 483 | } 484 | } 485 | if (finished) continue; 486 | 487 | if (typeof arg === 'string') { 488 | text.push(chalk.magenta(this._meta.quote ? `'${arg}'` : arg)); 489 | } else if (typeof arg === 'number') { 490 | text.push(chalk.cyan(arg.toString())); 491 | } else if (typeof arg === 'object') { 492 | text.push('\n'); 493 | 494 | if (arg instanceof Error) { 495 | text.push(chalk.red(arg.stack)); 496 | } else { 497 | text.push(util.inspect(arg, this._meta)); 498 | } 499 | } else text.push(arg); 500 | } 501 | 502 | output += text.join(' '); 503 | if (level.trace || this._meta.trace) { 504 | output += '\n' + new Error().stack.split('\n').slice(1).join('\n'); 505 | } 506 | if (level.err) output = chalk.red(output); 507 | return this._write(level, output, level.err).meta(); 508 | } 509 | }; --------------------------------------------------------------------------------