From f83134f622ff99207eb7dd1e5b43a47a1be9cc99 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Tue, 8 Nov 2022 22:57:03 +0200 Subject: [PATCH] fix a thing + update readme --- README.md | 6 ++++++ package.json | 2 +- src/LoggerClient.js | 10 ++++------ src/MasterLogger.js | 12 ++++++------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 056e18e..08b39e0 100644 --- a/README.md +++ b/README.md @@ -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. 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 ``` { diff --git a/package.json b/package.json index b5366fd..913e984 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@navy.gif/logger", - "version": "1.1.5", + "version": "1.1.6", "description": "Logging thing", "main": "index.js", "author": "Navy.gif", diff --git a/src/LoggerClient.js b/src/LoggerClient.js index bf3e20b..558a760 100644 --- a/src/LoggerClient.js +++ b/src/LoggerClient.js @@ -1,6 +1,4 @@ const { inspect } = require('node:util'); -const chalk = require('chalk'); - const Defaults = require('./Defaults'); class LoggerClient { @@ -28,11 +26,11 @@ class LoggerClient { if (typeof message !== 'string') message = inspect(message); 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 - if (!process.send || !process.connected) console.log(message); - else process.send({ _logger: true, message, ...opts }); + if (!process.send || !process.connected) console.log(`${header} ${message}`); + else process.send({ _logger: true, header, message, ...opts }); } diff --git a/src/MasterLogger.js b/src/MasterLogger.js index d3b3e89..96f3a70 100644 --- a/src/MasterLogger.js +++ b/src/MasterLogger.js @@ -24,7 +24,7 @@ class MasterLogger { for (const type of this.types) { Object.defineProperty(this, type, { // 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 }); } @@ -43,12 +43,12 @@ class MasterLogger { attach (child) { child.on('message', (msg) => { if (!msg._logger) return; - const { message, type } = msg; - this[type](message, child); + const { message, type, header } = msg; + this[type](message, header, child); }); } - write (type = 'info', text, shard = null) { + write (type = 'info', text, subheader = '', shard = null) { type = type.toLowerCase(); let colour = this.colours[type]; @@ -60,10 +60,10 @@ class MasterLogger { const maxChars = Math.max(...this.types.map(t => t.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'; - 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 }