fix a thing + update readme
This commit is contained in:
parent
6560923704
commit
f83134f622
@ -10,6 +10,12 @@ Simple logger I wrote to have a unified system for logging throughout my project
|
|||||||
Split into Master and Client for logging between processes, where master resides on the master process and the clients on the spawned processes.
|
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.
|
Should be fairly trivial to modify it to work across nodes with websockets.
|
||||||
|
|
||||||
|
**Note**
|
||||||
|
When logging from a child process, the master logger expects the child process to be be in a wrapper containing at the very least an ID property to work properly (used for identifying which child the message came from).
|
||||||
|
Notably the logger will work with just a raw child process object though, it will lack the identifier.
|
||||||
|
|
||||||
|
The child processes are expected to be attached with the attach() method found in the master logger. This will attatch a listener for the 'message' event.
|
||||||
|
|
||||||
## Logger Options
|
## Logger Options
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@navy.gif/logger",
|
"name": "@navy.gif/logger",
|
||||||
"version": "1.1.5",
|
"version": "1.1.6",
|
||||||
"description": "Logging thing",
|
"description": "Logging thing",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"author": "Navy.gif",
|
"author": "Navy.gif",
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
const { inspect } = require('node:util');
|
const { inspect } = require('node:util');
|
||||||
const chalk = require('chalk');
|
|
||||||
|
|
||||||
const Defaults = require('./Defaults');
|
const Defaults = require('./Defaults');
|
||||||
class LoggerClient {
|
class LoggerClient {
|
||||||
|
|
||||||
@ -28,11 +26,11 @@ class LoggerClient {
|
|||||||
|
|
||||||
if (typeof message !== 'string') message = inspect(message);
|
if (typeof message !== 'string') message = inspect(message);
|
||||||
const spacer = ' '.repeat(LoggerClient.MaxChars - this.name.length);
|
const spacer = ' '.repeat(LoggerClient.MaxChars - this.name.length);
|
||||||
message = `${chalk.bold(`[${this.name.substring(0, LoggerClient.MaxChars)}]${spacer}`)} ${message}`;
|
const header = `${`[${this.name.substring(0, LoggerClient.MaxChars)}]${spacer}`} `;
|
||||||
|
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
if (!process.send || !process.connected) console.log(message);
|
if (!process.send || !process.connected) console.log(`${header} ${message}`);
|
||||||
else process.send({ _logger: true, message, ...opts });
|
else process.send({ _logger: true, header, message, ...opts });
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ class MasterLogger {
|
|||||||
for (const type of this.types) {
|
for (const type of this.types) {
|
||||||
Object.defineProperty(this, type, {
|
Object.defineProperty(this, type, {
|
||||||
// Only write debug outputs if debug mode is enabled
|
// Only write debug outputs if debug mode is enabled
|
||||||
value: (msg, child) => msg.type === 'debug' && debug || msg.type !== 'debug'
|
value: (msg, header, subheader, child) => msg.type === 'debug' && debug || msg.type !== 'debug'
|
||||||
? this.write(type, msg, child) : null
|
? this.write(type, msg, child) : null
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -43,12 +43,12 @@ class MasterLogger {
|
|||||||
attach (child) {
|
attach (child) {
|
||||||
child.on('message', (msg) => {
|
child.on('message', (msg) => {
|
||||||
if (!msg._logger) return;
|
if (!msg._logger) return;
|
||||||
const { message, type } = msg;
|
const { message, type, header } = msg;
|
||||||
this[type](message, child);
|
this[type](message, header, child);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
write (type = 'info', text, shard = null) {
|
write (type = 'info', text, subheader = '', shard = null) {
|
||||||
|
|
||||||
type = type.toLowerCase();
|
type = type.toLowerCase();
|
||||||
let colour = this.colours[type];
|
let colour = this.colours[type];
|
||||||
@ -60,10 +60,10 @@ class MasterLogger {
|
|||||||
const maxChars = Math.max(...this.types.map(t => t.length));
|
const maxChars = Math.max(...this.types.map(t => t.length));
|
||||||
const spacer = ' '.repeat(maxChars - type.length);
|
const spacer = ' '.repeat(maxChars - type.length);
|
||||||
|
|
||||||
console.log(`${chalk[colour](type)}${spacer} ${chalk[colour](header)}: ${text}`); // eslint-disable-line no-console
|
console.log(`${chalk[colour](type)}${spacer} ${chalk[colour](header)}: ${chalk.bold(subheader)}${text}`); // eslint-disable-line no-console
|
||||||
|
|
||||||
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}: ${subheader}${text}`);
|
||||||
else console.log(`${chalk.red(`[LOGGER] Missing file stream for ${streamType}`)}`); // eslint-disable-line no-console
|
else console.log(`${chalk.red(`[LOGGER] Missing file stream for ${streamType}`)}`); // eslint-disable-line no-console
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user