From 6d090b8414fb595b750ba1a18a14559712560d6e Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 6 Nov 2022 17:21:10 +0200 Subject: [PATCH] bugfix, test & readme --- .gitignore | 3 ++- README.md | 11 +++++++++++ package.json | 8 ++++++-- src/MasterLogger.js | 20 ++++++++++++-------- test/test.js | 13 +++++++++++++ 5 files changed, 44 insertions(+), 11 deletions(-) create mode 100644 README.md create mode 100644 test/test.js diff --git a/.gitignore b/.gitignore index b512c09..4363a92 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +logs \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ba5c52 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Navy's logger + +Simple logger I wrote to have a unified system for logging throughout my projects. + +## TODO +- Automatic file rotation, currently only rotates files on startup +- Discord webhook, need to write a separate package for that, don't want to add the entirety of d.js as a dep just for a webhook + +## Features +Split into Master and Client for logging between processes, where master resides on the master process and the clients on the spawned processes. +Should be fairly trivial to modify it to work across nodes with websockets. \ No newline at end of file diff --git a/package.json b/package.json index 98c5a28..5b704bf 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,20 @@ { - "name": "logger", - "version": "0.0.1", + "name": "@navy.gif/logger", + "version": "0.0.2", "description": "Logging thing", "main": "index.js", "author": "Navy.gif", "license": "MIT", "private": false, + "files": ["src"], "devDependencies": { "eslint": "^8.26.0" }, "dependencies": { "chalk": "^4.1.2", "moment": "^2.29.4" + }, + "scripts": { + "test": "node test/test.js" } } diff --git a/src/MasterLogger.js b/src/MasterLogger.js index e4ed738..ac445d1 100644 --- a/src/MasterLogger.js +++ b/src/MasterLogger.js @@ -41,17 +41,20 @@ class MasterLogger { constructor (options = {}) { this.directory = path.resolve(options.directory || './logs'); - if (!fs.stat(this.directory)) fs.mkdirSync(this.directory, { recursive: true }); - const { customTypes = [], customStreams = [], typeMappings = {}, customColors = {}, debug = false } = options; + if (!fs.existsSync(this.directory)) fs.mkdirSync(this.directory, { recursive: true }); + const { customTypes = [], customStreams = [], customTypeMapping = {}, customColors = {}, debug = false } = options; this._debug = debug; this.types = [ ...customTypes, ...Defaults.Types ]; - this.colours = { ...Defaults.colours, ...customColors }; + this.colours = { ...Defaults.Colours, ...customColors }; this.streamTypes = [ ...customStreams, 'error', 'default' ]; - this.streamTypeMapping = { ...Defaults.TypeStream, ...typeMappings }; + this.streamTypeMapping = { ...Defaults.TypeStream, ...customTypeMapping }; // eslint-disable-next-line no-return-assign - this.writeStreams = this.streamTypes.reduce((acc, type) => acc[type] = this.loadFile(type), {}); + this.writeStreams = this.streamTypes.reduce((acc, type) => { + acc[type] = this.loadFile(type); + return acc; + }, {}); } @@ -63,7 +66,7 @@ class MasterLogger { if (typeof text !== 'string') text = inspect(text); - const header = `[${this.date}][${this._shard(shard)}]`; + const header = `[${this.date}] [${this._shard(shard)}]`; const maxChars = Math.max(...this.types.map(t => t.length)); const spacer = ' '.repeat(maxChars - type.length); @@ -71,7 +74,7 @@ class MasterLogger { const streamType = this.streamTypeMapping[type] || 'default'; if (this.writeStreams[streamType]) this.writeStreams[streamType].write(`\n${type}${spacer} ${header}: ${text}`); - else console.log(`${chalk.red(`[LOGGER] Missing file stream for ${type}`)}`); // eslint-disable-line no-console + else console.log(`${chalk.red(`[LOGGER] Missing file stream for ${streamType}`)}`); // eslint-disable-line no-console } @@ -108,8 +111,9 @@ class MasterLogger { } _shard (shard) { + if (!shard) return 'controller'; const id = `${shard.id < 10 ? `0${shard.id}` : shard.id}`; - return shard ? `shard${id}` : 'manager'; + return `shard${id}`; } get date () { diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..48b8c43 --- /dev/null +++ b/test/test.js @@ -0,0 +1,13 @@ +/* eslint-disable no-console */ +const { MasterLogger } = require('../'); + +const logger = new MasterLogger({ debug: true, customStreams: [ 'debug' ], customTypeMapping: { debug: 'debug' } }); +const { types, colours, streamTypes, streamTypeMapping } = logger; // , writeStreams + +console.log(types, colours, streamTypes, streamTypeMapping); // , writeStreams + +logger.info('Info test'); +logger.status('Status test'); +logger.debug('Debug test'); +logger.warn('Warn test'); +logger.error('Error test'); \ No newline at end of file