option to not write to file at all

This commit is contained in:
Erik 2023-08-10 11:23:19 +03:00
parent 4c68f237ff
commit 115e832109
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
2 changed files with 18 additions and 8 deletions

View File

@ -44,6 +44,7 @@ const customTypes: string[] = [];
const guard = '_logger';
const webhook: { url?: string, id?: string, token?: string } = {};
const pruneDays = 3;
const skipFileWrite = false;
type LogLevelType = {
debug: LogLevel,
@ -83,7 +84,8 @@ export type LoggerMasterOptions = SharedOptionsType & {
customColours?: { [key: string]: number | string },
broadcastLevel?: number,
webhook?: WebhookClientOptions,
pruneDays?: number
pruneDays?: number,
skipFileWrite?: boolean
}
const MasterOptions: LoggerMasterOptions = {
@ -95,7 +97,8 @@ const MasterOptions: LoggerMasterOptions = {
customColours,
broadcastLevel: 4,
webhook,
pruneDays
pruneDays,
skipFileWrite
};
export type LoggerClientOptions = SharedOptionsType;

View File

@ -51,6 +51,7 @@ class MasterLogger implements Logger
#rotateTO: NodeJS.Timeout;
#pruneInterval: NodeJS.Timer;
#pruneDays: number;
#skipFileWrite: boolean;
constructor (config = Defaults.MasterOptions)
{
@ -58,7 +59,7 @@ class MasterLogger implements Logger
const {
directory, customTypes = [], customStreams = [], customTypeMapping,
customColours, guard, fileRotationFreq, logLevel, logLevelMapping,
webhook, broadcastLevel, pruneDays
webhook, broadcastLevel, pruneDays, skipFileWrite
} = { ...Defaults.MasterOptions, ...config };
if (!directory)
@ -76,6 +77,7 @@ class MasterLogger implements Logger
addLogLevel(name, level);
});
this.#_guard = guard as string;
this.#skipFileWrite = skipFileWrite as boolean;
this.#types = [ ...customTypes, ...Defaults.Types ];
for (const type of this.#types)
@ -105,11 +107,14 @@ class MasterLogger implements Logger
this.#streamTypes = [ ...customStreams, 'error', 'default' ];
this.#streamTypeMapping = { ...Defaults.TypeStream, ...customTypeMapping };
this.#writeStreams = this.#streamTypes.reduce((acc, type) =>
{
acc[type] = this.loadFile(type);
return acc;
}, {} as WriteStreams);
if (this.#skipFileWrite)
this.#writeStreams = {};
else
this.#writeStreams = this.#streamTypes.reduce((acc, type) =>
{
acc[type] = this.loadFile(type);
return acc;
}, {} as WriteStreams);
// Startup day, used to keep track of file rotation
this.#rotationFreq = fileRotationFreq as number * DAY;
@ -232,6 +237,8 @@ class MasterLogger implements Logger
});
}
if (this.#skipFileWrite)
return;
const streamType = this.#streamTypeMapping[type] || 'default';
if (this.#writeStreams[streamType])
this.#writeStreams[streamType].write(`\n${type}${spacer} ${header}: ${subheader}${text}`);