bugfix, test & readme
This commit is contained in:
parent
803096ce3e
commit
6d090b8414
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
node_modules
|
node_modules
|
||||||
|
logs
|
11
README.md
Normal file
11
README.md
Normal 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.
|
@ -1,16 +1,20 @@
|
|||||||
{
|
{
|
||||||
"name": "logger",
|
"name": "@navy.gif/logger",
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"description": "Logging thing",
|
"description": "Logging thing",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Navy.gif",
|
"author": "Navy.gif",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"private": false,
|
"private": false,
|
||||||
|
"files": ["src"],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.26.0"
|
"eslint": "^8.26.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"chalk": "^4.1.2",
|
"chalk": "^4.1.2",
|
||||||
"moment": "^2.29.4"
|
"moment": "^2.29.4"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "node test/test.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,17 +41,20 @@ class MasterLogger {
|
|||||||
constructor (options = {}) {
|
constructor (options = {}) {
|
||||||
|
|
||||||
this.directory = path.resolve(options.directory || './logs');
|
this.directory = path.resolve(options.directory || './logs');
|
||||||
if (!fs.stat(this.directory)) fs.mkdirSync(this.directory, { recursive: true });
|
if (!fs.existsSync(this.directory)) fs.mkdirSync(this.directory, { recursive: true });
|
||||||
const { customTypes = [], customStreams = [], typeMappings = {}, customColors = {}, debug = false } = options;
|
const { customTypes = [], customStreams = [], customTypeMapping = {}, customColors = {}, debug = false } = options;
|
||||||
this._debug = debug;
|
this._debug = debug;
|
||||||
|
|
||||||
this.types = [ ...customTypes, ...Defaults.Types ];
|
this.types = [ ...customTypes, ...Defaults.Types ];
|
||||||
this.colours = { ...Defaults.colours, ...customColors };
|
this.colours = { ...Defaults.Colours, ...customColors };
|
||||||
|
|
||||||
this.streamTypes = [ ...customStreams, 'error', 'default' ];
|
this.streamTypes = [ ...customStreams, 'error', 'default' ];
|
||||||
this.streamTypeMapping = { ...Defaults.TypeStream, ...typeMappings };
|
this.streamTypeMapping = { ...Defaults.TypeStream, ...customTypeMapping };
|
||||||
// eslint-disable-next-line no-return-assign
|
// 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';
|
const streamType = this.streamTypeMapping[type] || 'default';
|
||||||
if (this.writeStreams[streamType]) this.writeStreams[streamType].write(`\n${type}${spacer} ${header}: ${text}`);
|
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) {
|
_shard (shard) {
|
||||||
|
if (!shard) return 'controller';
|
||||||
const id = `${shard.id < 10 ? `0${shard.id}` : shard.id}`;
|
const id = `${shard.id < 10 ? `0${shard.id}` : shard.id}`;
|
||||||
return shard ? `shard${id}` : 'manager';
|
return `shard${id}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
get date () {
|
get date () {
|
||||||
|
13
test/test.js
Normal file
13
test/test.js
Normal 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');
|
Loading…
Reference in New Issue
Block a user