Compare commits
No commits in common. "0c155c365ed1a4c3a1cef081f03d0b610dba33af" and "28a888ce7b2f9ff09987bab51bb6f954a0288926" have entirely different histories.
0c155c365e
...
28a888ce7b
@ -76,10 +76,10 @@
|
||||
],
|
||||
"lines-around-comment": "warn",
|
||||
"lines-around-directive": "warn",
|
||||
// "lines-between-class-members": [
|
||||
// "warn",
|
||||
// "always"
|
||||
// ],
|
||||
"lines-between-class-members": [
|
||||
"warn",
|
||||
"always"
|
||||
],
|
||||
"max-classes-per-file": "warn",
|
||||
"max-nested-callbacks": "warn",
|
||||
"new-parens": "warn",
|
||||
|
@ -20,7 +20,7 @@ The child processes are expected to be attached with the attach() method found i
|
||||
{
|
||||
//////// SHARED BETWEEN CLIENT & MASTER
|
||||
guard: '_logger', // Message guard, e.g this property has to be true in the IPC message for the logger to read it
|
||||
loglevel: false, // On the master logger this determines whether the output is written to console (i.e. will always be written to file), on the client this determines whether it is sent to the master at all
|
||||
debug: false, // On the master logger this determines whether the output is written to console (i.e. will always be written to file), on the client this determines whether it is sent to the master at all
|
||||
customTypes: [], // Log types, defaults are 'error', 'warn', 'info', 'debug', 'status'. Each one of these has an associated shorthand function, the custom ones will receive one too, e.g. adding 'access' to the custom types will add a logger.access() function
|
||||
/////// MASTER EXCLUSIVE
|
||||
customStreams: [], // File streams, by default there are streams for error and default
|
||||
@ -30,10 +30,3 @@ The child processes are expected to be attached with the attach() method found i
|
||||
directory: './logs', // Directory in which to write log files
|
||||
}
|
||||
```
|
||||
|
||||
**Log levels**
|
||||
0 - Debug
|
||||
1 - Info
|
||||
2 - Status
|
||||
3 - Warning
|
||||
4 - Error
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@navy.gif/logger",
|
||||
"version": "1.2.4",
|
||||
"version": "1.2.3",
|
||||
"description": "Logging thing",
|
||||
"main": "index.js",
|
||||
"author": "Navy.gif",
|
||||
|
@ -30,26 +30,12 @@ module.exports = {
|
||||
customStreams: [],
|
||||
customTypeMapping: {},
|
||||
customColors: {},
|
||||
guard: '_logger',
|
||||
logLevel: 1,
|
||||
logLevelMapping: {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
status: 2,
|
||||
warn: 3,
|
||||
error: 4
|
||||
}
|
||||
debug: false,
|
||||
guard: '_logger'
|
||||
},
|
||||
ClientOptions: {
|
||||
debug: true,
|
||||
guard: '_logger',
|
||||
customStreams: [],
|
||||
logLevel: 1,
|
||||
logLevelMapping: {
|
||||
debug: 0,
|
||||
info: 1,
|
||||
status: 2,
|
||||
warn: 3,
|
||||
error: 4
|
||||
}
|
||||
customStreams: []
|
||||
}
|
||||
};
|
@ -4,10 +4,6 @@ class LoggerClient {
|
||||
|
||||
static MaxChars = 0;
|
||||
|
||||
#_guard = null;
|
||||
#_logLevel = null;
|
||||
#_logLevelMapping = null;
|
||||
|
||||
constructor (opts = Defaults.ClientOptions) {
|
||||
|
||||
this.name = opts.name || opts.constructor.name;
|
||||
@ -26,28 +22,14 @@ class LoggerClient {
|
||||
});
|
||||
}
|
||||
|
||||
this.#_guard = opts.guard || Defaults.ClientOptions.guard;
|
||||
this.#_logLevel = opts.logLevel || Defaults.ClientOptions.logLevel;
|
||||
this.#_logLevelMapping = { ...Defaults.ClientOptions.logLevelMapping, ...opts.logLevelMapping };
|
||||
this.guard = opts.guard || Defaults.ClientOptions.guard;
|
||||
this._debug = opts.debug || Defaults.ClientOptions.debug;
|
||||
|
||||
}
|
||||
|
||||
get logLevel () {
|
||||
return this.#_logLevel;
|
||||
}
|
||||
|
||||
setLogLevel (level = 'info') {
|
||||
if (typeof level === 'number')
|
||||
this.#_logLevel = level;
|
||||
else if (typeof level === 'string')
|
||||
this.#_logLevel = this.#_logLevelMapping[level.toLowerCase()];
|
||||
else
|
||||
throw new Error(`Invalid log level type, expected string or number, got ${typeof level}`);
|
||||
}
|
||||
|
||||
transport (message, opts) {
|
||||
|
||||
if (this.#_logLevelMapping[opts.type] < this.#_logLevel)
|
||||
if (opts.type === 'debug' && !this._debug)
|
||||
return;
|
||||
|
||||
if (typeof message !== 'string')
|
||||
@ -60,7 +42,7 @@ class LoggerClient {
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(`${header} ${message}`);
|
||||
else
|
||||
process.send({ [this.#_guard]: true, header, message, ...opts });
|
||||
process.send({ [this.guard]: true, header, message, ...opts });
|
||||
|
||||
}
|
||||
|
||||
|
@ -10,27 +10,36 @@ const Defaults = require('./Defaults');
|
||||
|
||||
const DAY = 1000 * 60 * 60 * 24;
|
||||
|
||||
const DefaultOpts = {
|
||||
fileRotationFreq: 1,
|
||||
directory: './logs',
|
||||
customTypes: [],
|
||||
customStreams: [],
|
||||
customTypeMapping: {},
|
||||
customColors: {},
|
||||
debug: false,
|
||||
guard: '_logger'
|
||||
};
|
||||
|
||||
// TODO:
|
||||
// - 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
|
||||
class MasterLogger {
|
||||
|
||||
#_guard = null;
|
||||
#_logLevel = null;
|
||||
#_logLevelMapping = null;
|
||||
|
||||
constructor (config = Defaults.MasterOptions) {
|
||||
|
||||
const {
|
||||
directory, customTypes, customStreams, customTypeMapping,
|
||||
customColors, guard, fileRotationFreq, logLevel, logLevelMapping
|
||||
} = { ...Defaults.MasterOptions, ...config };
|
||||
constructor ({
|
||||
directory = './logs',
|
||||
customTypes = [],
|
||||
customStreams = [],
|
||||
customTypeMapping = {},
|
||||
customColors = {},
|
||||
debug = false,
|
||||
guard = '_logger',
|
||||
fileRotationFreq = 1
|
||||
} = DefaultOpts) {
|
||||
|
||||
this.directory = path.resolve(directory);
|
||||
if (!fs.existsSync(this.directory))
|
||||
fs.mkdirSync(this.directory, { recursive: true });
|
||||
this.#_logLevel = logLevel;
|
||||
this.#_logLevelMapping = { ...Defaults.MasterOptions.logLevelMapping, ...logLevelMapping };
|
||||
this.#_guard = guard;
|
||||
this._debug = debug;
|
||||
|
||||
this.types = [ ...customTypes, ...Defaults.Types ];
|
||||
for (const type of this.types) {
|
||||
@ -52,11 +61,13 @@ class MasterLogger {
|
||||
this.startDay = Math.floor(Date.now() / this.rotationFreq) * this.rotationFreq;
|
||||
this.rotateTO = setTimeout(this.rotateLogFiles.bind(this), this.startDay + this.rotationFreq - Date.now());
|
||||
|
||||
this.guard = guard;
|
||||
|
||||
}
|
||||
|
||||
attach (shard) {
|
||||
shard.on('message', (msg) => {
|
||||
if (!msg[this.#_guard])
|
||||
if (!msg[this.guard])
|
||||
return;
|
||||
const { message, type, header, broadcast } = msg;
|
||||
this[type](message, { subheader: header, shard, broadcast });
|
||||
@ -77,8 +88,7 @@ class MasterLogger {
|
||||
const maxChars = Math.max(...this.types.map(t => t.length));
|
||||
const spacer = ' '.repeat(maxChars - type.length);
|
||||
|
||||
// if (type === 'debug' && this._debug || type !== 'debug')
|
||||
if (this.#_logLevelMapping[type] >= this.#_logLevel)
|
||||
if (type === 'debug' && this._debug || type !== 'debug')
|
||||
console.log(`${chalk[colour](type)}${spacer} ${chalk[colour](header)}: ${chalk.bold(subheader)}${text}`); // eslint-disable-line no-console
|
||||
|
||||
if (broadcast) {
|
||||
|
@ -1,17 +1,17 @@
|
||||
const { LoggerClient } = require('../');
|
||||
|
||||
const logger = new LoggerClient({
|
||||
debug: true,
|
||||
customTypes: [ 'access' ],
|
||||
customStreams: [ 'access' ],
|
||||
customTypeMapping: { access: 'access', warn: 'error' },
|
||||
customColors: { access: 'green' },
|
||||
logLevelMapping: { access: 2 }
|
||||
customColors: { access: 'green' }
|
||||
});
|
||||
|
||||
const main = async () => {
|
||||
for (let i = 0; i < 5; i++) {
|
||||
// if (i % 500 === 0)
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
for (let i = 0; i < 10000; i++) {
|
||||
if (i % 500 === 0)
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
logger.info('Info test');
|
||||
logger.status('Status test');
|
||||
logger.debug('Debug test');
|
||||
|
@ -16,8 +16,7 @@ const main = async () => {
|
||||
customStreams: [ 'access' ],
|
||||
customTypeMapping: { access: 'access', warn: 'error' },
|
||||
customColors: { access: 'green' },
|
||||
fileRotationFreq: 0.0001,
|
||||
logLevelMapping: { access: 2 }
|
||||
fileRotationFreq: 0.0001
|
||||
});
|
||||
const child = ChildProcess.fork('./test/otherProcess.js');
|
||||
logger.attach(child);
|
||||
|
Loading…
Reference in New Issue
Block a user