Small tweaks, more type definitions
This commit is contained in:
parent
e2dcb7578b
commit
996844dc60
16
README.md
16
README.md
@ -19,13 +19,6 @@ The child processes are expected to be attached with the attach() method found i
|
|||||||
guard: '_logger', // Message guard, e.g this property has to be true in the IPC message for the logger to read it
|
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
|
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
|
||||||
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
|
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
|
|
||||||
customTypeMapping: {}, // This maps a type to a stream, e.g. adding "warn": "error" will pipe any warnings to the error log file
|
|
||||||
customColors: {}, // Supports any colours chalk.js supports, e.g. "warn": "green" will turn warning outputs green
|
|
||||||
fileRotationFreq: 1, // How frequently to roate files in days - will not work with anything less than 1 day currently
|
|
||||||
directory: './logs', // Directory in which to write log files
|
|
||||||
logLevelMapping: {
|
logLevelMapping: {
|
||||||
debug: 0,
|
debug: 0,
|
||||||
info: 1,
|
info: 1,
|
||||||
@ -33,6 +26,13 @@ The child processes are expected to be attached with the attach() method found i
|
|||||||
warn: 3,
|
warn: 3,
|
||||||
error: 4
|
error: 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/////// MASTER EXCLUSIVE
|
||||||
|
customStreams: [], // File streams, by default there are streams for error and default
|
||||||
|
customTypeMapping: {}, // This maps a type to a stream, e.g. adding "warn": "error" will pipe any warnings to the error log file
|
||||||
|
customColours: {}, // Supports any colours chalk.js supports, e.g. "warn": "green" will turn warning outputs green
|
||||||
|
fileRotationFreq: 1, // How frequently to roate files in days - will not work with anything less than 1 day currently
|
||||||
|
directory: './logs', // Directory in which to write log files
|
||||||
broadcastLevel: 4, // Level at which to broadcast to webhook if supplied
|
broadcastLevel: 4, // Level at which to broadcast to webhook if supplied
|
||||||
webhook: {
|
webhook: {
|
||||||
url: string
|
url: string
|
||||||
@ -40,7 +40,7 @@ The child processes are expected to be attached with the attach() method found i
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**Log levels**
|
**Built-in log levels**
|
||||||
0 - Debug
|
0 - Debug
|
||||||
1 - Info
|
1 - Info
|
||||||
2 - Status
|
2 - Status
|
||||||
|
13
index.ts
13
index.ts
@ -1,4 +1,15 @@
|
|||||||
import MasterLogger from "./src/MasterLogger.js";
|
import MasterLogger from "./src/MasterLogger.js";
|
||||||
import LoggerClient from './src/LoggerClient.js';
|
import LoggerClient from './src/LoggerClient.js';
|
||||||
|
import Defaults, { LogLevel } from "./src/Defaults.js";
|
||||||
|
|
||||||
export { MasterLogger, LoggerClient };
|
const addLogLevel = (name: string, level: number) => {
|
||||||
|
if (typeof name !== 'string')
|
||||||
|
throw new Error('Name must be a string');
|
||||||
|
if (typeof level !== 'number')
|
||||||
|
throw new Error('Level must be a number');
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore
|
||||||
|
LogLevel[LogLevel[name] = level] = name;
|
||||||
|
};
|
||||||
|
|
||||||
|
export { MasterLogger, LoggerClient, Defaults, LogLevel, addLogLevel };
|
@ -1,3 +1,12 @@
|
|||||||
|
enum LogLevel {
|
||||||
|
debug = 0,
|
||||||
|
info = 1,
|
||||||
|
status = 2,
|
||||||
|
warn = 3,
|
||||||
|
error = 4
|
||||||
|
}
|
||||||
|
export { LogLevel };
|
||||||
|
|
||||||
const ColourCodes: { [key: string]: number } = {
|
const ColourCodes: { [key: string]: number } = {
|
||||||
error: 0xe88388,
|
error: 0xe88388,
|
||||||
warn: 0xf9d472,
|
warn: 0xf9d472,
|
||||||
@ -6,56 +15,68 @@ const ColourCodes: {[key: string]: number} = {
|
|||||||
status: 0x72d4d7
|
status: 0x72d4d7
|
||||||
};
|
};
|
||||||
|
|
||||||
export default {
|
const Types = [
|
||||||
Types: [
|
|
||||||
'error',
|
'error',
|
||||||
'warn',
|
'warn',
|
||||||
'info',
|
'info',
|
||||||
'debug',
|
'debug',
|
||||||
'status'
|
'status'
|
||||||
],
|
];
|
||||||
TypeStream: { // If none defined they are sent to default
|
|
||||||
|
const TypeStream = { // If none defined they are sent to default
|
||||||
error: 'error'
|
error: 'error'
|
||||||
},
|
};
|
||||||
Colours: {
|
|
||||||
|
const Colours = {
|
||||||
error: 'red',
|
error: 'red',
|
||||||
warn: 'yellow',
|
warn: 'yellow',
|
||||||
info: 'blue',
|
info: 'blue',
|
||||||
debug: 'magenta',
|
debug: 'magenta',
|
||||||
status: 'cyanBright'
|
status: 'cyanBright'
|
||||||
},
|
};
|
||||||
ColourCodes,
|
|
||||||
MasterOptions: {
|
const customColours: { [key: string]: number | string } = {};
|
||||||
|
const customTypeMapping: { [key: string]: string } = {};
|
||||||
|
const customStreams: string[] = [];
|
||||||
|
const customTypes: string[] = [];
|
||||||
|
const guard = '_logger';
|
||||||
|
const webhook: { url?: string, id?: string, token?: string } = {};
|
||||||
|
|
||||||
|
const logLevelMapping = {
|
||||||
|
debug: LogLevel.debug,
|
||||||
|
info: LogLevel.info,
|
||||||
|
status: LogLevel.status,
|
||||||
|
warn: LogLevel.warn,
|
||||||
|
error: LogLevel.error
|
||||||
|
};
|
||||||
|
|
||||||
|
const SharedOptions = {
|
||||||
|
guard,
|
||||||
|
customStreams,
|
||||||
|
logLevel: LogLevel.info,
|
||||||
|
logLevelMapping,
|
||||||
|
};
|
||||||
|
|
||||||
|
const MasterOptions = {
|
||||||
|
...SharedOptions,
|
||||||
fileRotationFreq: 1,
|
fileRotationFreq: 1,
|
||||||
directory: './logs',
|
directory: './logs',
|
||||||
customTypes: [],
|
customTypes,
|
||||||
customStreams: [],
|
customTypeMapping,
|
||||||
customTypeMapping: {},
|
customColours,
|
||||||
customColors: {},
|
|
||||||
guard: '_logger',
|
|
||||||
broadcastLevel: 4,
|
broadcastLevel: 4,
|
||||||
logLevel: 1,
|
webhook,
|
||||||
logLevelMapping: {
|
};
|
||||||
debug: 0,
|
|
||||||
info: 1,
|
const ClientOptions = {
|
||||||
status: 2,
|
...SharedOptions
|
||||||
warn: 3,
|
};
|
||||||
error: 4
|
|
||||||
},
|
export default {
|
||||||
webhook: {
|
Types,
|
||||||
url: null
|
TypeStream,
|
||||||
},
|
Colours,
|
||||||
},
|
ColourCodes,
|
||||||
ClientOptions: {
|
MasterOptions,
|
||||||
guard: '_logger',
|
ClientOptions
|
||||||
customStreams: [],
|
|
||||||
logLevel: 1,
|
|
||||||
logLevelMapping: {
|
|
||||||
debug: 0,
|
|
||||||
info: 1,
|
|
||||||
status: 2,
|
|
||||||
warn: 3,
|
|
||||||
error: 4
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
@ -9,8 +9,9 @@ import { inspect } from 'node:util';
|
|||||||
|
|
||||||
// Own
|
// Own
|
||||||
import DiscordWebhook from '@navy.gif/discord-webhook';
|
import DiscordWebhook from '@navy.gif/discord-webhook';
|
||||||
import Defaults from './Defaults.js';
|
import Defaults, { LogLevel } from './Defaults.js';
|
||||||
import { Shard, WriteOptions } from './Types.js';
|
import { Shard, WriteOptions } from './Types.js';
|
||||||
|
import { addLogLevel } from '../index.js';
|
||||||
|
|
||||||
const DAY = 1000 * 60 * 60 * 24;
|
const DAY = 1000 * 60 * 60 * 24;
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ type WriteStreams = {
|
|||||||
class MasterLogger {
|
class MasterLogger {
|
||||||
|
|
||||||
#_guard: string;
|
#_guard: string;
|
||||||
#_logLevel: number;
|
#_logLevel: LogLevel;
|
||||||
#_logLevelMapping: {[key: string]: number};
|
#_logLevelMapping: {[key: string]: number};
|
||||||
#_broadcastLevel: number;
|
#_broadcastLevel: number;
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ class MasterLogger {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
directory, customTypes, customStreams, customTypeMapping,
|
directory, customTypes, customStreams, customTypeMapping,
|
||||||
customColors, guard, fileRotationFreq, logLevel, logLevelMapping,
|
customColours, guard, fileRotationFreq, logLevel, logLevelMapping,
|
||||||
webhook, broadcastLevel
|
webhook, broadcastLevel
|
||||||
} = { ...Defaults.MasterOptions, ...config };
|
} = { ...Defaults.MasterOptions, ...config };
|
||||||
|
|
||||||
@ -64,6 +65,10 @@ class MasterLogger {
|
|||||||
this.#_broadcastLevel = broadcastLevel;
|
this.#_broadcastLevel = broadcastLevel;
|
||||||
this.#_logLevel = logLevel;
|
this.#_logLevel = logLevel;
|
||||||
this.#_logLevelMapping = { ...Defaults.MasterOptions.logLevelMapping, ...logLevelMapping };
|
this.#_logLevelMapping = { ...Defaults.MasterOptions.logLevelMapping, ...logLevelMapping };
|
||||||
|
if (logLevelMapping)
|
||||||
|
Object.entries(logLevelMapping).forEach(([ name, level ]) => {
|
||||||
|
addLogLevel(name, level);
|
||||||
|
});
|
||||||
this.#_guard = guard;
|
this.#_guard = guard;
|
||||||
|
|
||||||
this.#types = [ ...customTypes, ...Defaults.Types ];
|
this.#types = [ ...customTypes, ...Defaults.Types ];
|
||||||
@ -72,7 +77,7 @@ class MasterLogger {
|
|||||||
value: (msg: string, opts: WriteOptions) => this.write(type, msg, opts)
|
value: (msg: string, opts: WriteOptions) => this.write(type, msg, opts)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
this.#colours = { ...Defaults.Colours, ...customColors };
|
this.#colours = { ...Defaults.Colours, ...customColours };
|
||||||
|
|
||||||
this.#colourFuncs = Object.entries(this.#colours).reduce((prev: FuncsType, [ type, colour ]: (string | number)[]) => {
|
this.#colourFuncs = Object.entries(this.#colours).reduce((prev: FuncsType, [ type, colour ]: (string | number)[]) => {
|
||||||
if (typeof colour === 'number')
|
if (typeof colour === 'number')
|
||||||
@ -103,7 +108,7 @@ class MasterLogger {
|
|||||||
this.#rotateTO = setTimeout(this.rotateLogFiles.bind(this), this.#startDay + this.#rotationFreq - Date.now());
|
this.#rotateTO = setTimeout(this.rotateLogFiles.bind(this), this.#startDay + this.#rotationFreq - Date.now());
|
||||||
|
|
||||||
if (webhook && webhook.url) {
|
if (webhook && webhook.url) {
|
||||||
this.#webhook = new DiscordWebhook({ url: webhook.url });
|
this.#webhook = new DiscordWebhook(webhook);
|
||||||
this.#webhook.fetch();
|
this.#webhook.fetch();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,22 +126,18 @@ class MasterLogger {
|
|||||||
return Object.keys(this.#_logLevelMapping);
|
return Object.keys(this.#_logLevelMapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
setLogLevel (level: string | number = 'info') {
|
setLogLevel (level: number) {
|
||||||
if (typeof level === 'number')
|
if (LogLevel[level] in this.#_logLevelMapping)
|
||||||
this.#_logLevel = level;
|
this.#_logLevel = level;
|
||||||
else if (typeof level === 'string')
|
|
||||||
this.#_logLevel = this.#_logLevelMapping[level.toLowerCase()];
|
|
||||||
else
|
else
|
||||||
throw new Error(`Invalid log level type, expected string or number, got ${typeof level}`);
|
throw new Error(`Not a valid log level`);
|
||||||
}
|
}
|
||||||
|
|
||||||
setBroadcastLevel (level: string | number) {
|
setBroadcastLevel (level: number) {
|
||||||
if (!level)
|
if (LogLevel[level] in this.#_logLevelMapping)
|
||||||
throw new Error('Missing level');
|
|
||||||
if (typeof level === 'number')
|
|
||||||
this.#_broadcastLevel = level;
|
this.#_broadcastLevel = level;
|
||||||
else if (typeof level === 'string')
|
else
|
||||||
this.#_broadcastLevel = this.#_logLevelMapping[level.toLowerCase()];
|
throw new Error('Not a valid log level');
|
||||||
}
|
}
|
||||||
|
|
||||||
attach (shard: Shard) {
|
attach (shard: Shard) {
|
||||||
|
@ -10,5 +10,4 @@ type WriteOptions = {
|
|||||||
broadcast?: boolean
|
broadcast?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export { WriteOptions, Shard };
|
export { WriteOptions, Shard };
|
@ -5,7 +5,7 @@ const logger = new LoggerClient({
|
|||||||
customTypes: [ 'access' ],
|
customTypes: [ 'access' ],
|
||||||
customStreams: [ 'access' ],
|
customStreams: [ 'access' ],
|
||||||
customTypeMapping: { access: 'access', warn: 'error' },
|
customTypeMapping: { access: 'access', warn: 'error' },
|
||||||
customColors: { access: 'green' },
|
customColours: { access: 'green' },
|
||||||
logLevelMapping: { access: 2 }
|
logLevelMapping: { access: 2 }
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -1,11 +1,17 @@
|
|||||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||||
const { MasterLogger } = require('../build/cjs');
|
const { MasterLogger, LogLevel, addLogLevel } = require('../build/cjs');
|
||||||
|
console.log(LogLevel.debug === LogLevel[0], LogLevel.debug === LogLevel[LogLevel[0]], LogLevel[0]);
|
||||||
|
// LogLevel[LogLevel.access = 6] = 'access';
|
||||||
|
addLogLevel('access', 6);
|
||||||
|
console.log(LogLevel);
|
||||||
|
|
||||||
|
|
||||||
const logger = new MasterLogger({
|
const logger = new MasterLogger({
|
||||||
debug: true,
|
debug: true,
|
||||||
customTypes: [ 'access' ],
|
customTypes: [ 'access' ],
|
||||||
customStreams: [ 'access' ],
|
customStreams: [ 'access' ],
|
||||||
customTypeMapping: { access: 'access', warn: 'error' },
|
customTypeMapping: { access: 'access', warn: 'error' },
|
||||||
customColors: {
|
customColours: {
|
||||||
access: 'green',
|
access: 'green',
|
||||||
// error: '#FF0000'
|
// error: '#FF0000'
|
||||||
},
|
},
|
||||||
@ -17,6 +23,9 @@ const logger = new MasterLogger({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(logger);
|
console.log(logger);
|
||||||
|
logger.setLogLevel(LogLevel.access);
|
||||||
|
console.log(logger.logLevel);
|
||||||
|
process.exit();
|
||||||
logger.info('Test');
|
logger.info('Test');
|
||||||
|
|
||||||
const spawn = (child) => {
|
const spawn = (child) => {
|
||||||
|
@ -17,7 +17,7 @@ const main = async () => {
|
|||||||
customTypes: [ 'access' ],
|
customTypes: [ 'access' ],
|
||||||
customStreams: [ 'access' ],
|
customStreams: [ 'access' ],
|
||||||
customTypeMapping: { access: 'access', warn: 'error' },
|
customTypeMapping: { access: 'access', warn: 'error' },
|
||||||
customColors: {
|
customColours: {
|
||||||
access: 'green',
|
access: 'green',
|
||||||
// error: '#FF0000'
|
// error: '#FF0000'
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user