bugfix, test & readme

This commit is contained in:
Erik 2022-11-06 17:21:10 +02:00
parent 803096ce3e
commit 6d090b8414
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
5 changed files with 44 additions and 11 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules
logs

11
README.md Normal file
View File

@ -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.

View File

@ -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"
}
}

View File

@ -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;
}, {});
}
@ -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 () {

13
test/test.js Normal file
View File

@ -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');