bugfix, test & readme
This commit is contained in:
parent
803096ce3e
commit
6d090b8414
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
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",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
@ -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 () {
|
||||
|
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