Linter pass + fix logger sometimes not pruning old files

This commit is contained in:
Erik 2024-10-08 22:32:09 +03:00
parent 8c0ae9170f
commit 9e6188d7c5
5 changed files with 102 additions and 101 deletions

View File

@ -10,10 +10,10 @@ enum LogLevel {
export { LogLevel };
const ColourCodes: { [key: string]: number } = {
error: 0xe88388,
warn: 0xf9d472,
info: 0x76a9d8,
debug: 0xd8abd7,
error: 0xe88388,
warn: 0xf9d472,
info: 0x76a9d8,
debug: 0xd8abd7,
status: 0x72d4d7
};
@ -30,10 +30,10 @@ const TypeStream = { // If none defined they are sent to default
};
const Colours = {
error: 'red',
warn: 'yellow',
info: 'blue',
debug: 'magenta',
error: 'red',
warn: 'yellow',
info: 'blue',
debug: 'magenta',
status: 'cyanBright'
};
@ -55,11 +55,11 @@ type LogLevelType = {
}
const logLevelMapping = {
debug: LogLevel.debug,
info: LogLevel.info,
debug: LogLevel.debug,
info: LogLevel.info,
status: LogLevel.status,
warn: LogLevel.warn,
error: LogLevel.error
warn: LogLevel.warn,
error: LogLevel.error
};
type SharedOptionsType = {
@ -74,10 +74,10 @@ type SharedOptionsType = {
const SharedOptions: SharedOptionsType = {
guard,
customStreams,
logLevel: LogLevel.info,
logLevel: LogLevel.info,
logLevelMapping,
customTypes: [],
labels: []
labels: []
};
export type LoggerMasterOptions = SharedOptionsType & {
@ -94,11 +94,11 @@ export type LoggerMasterOptions = SharedOptionsType & {
const MasterOptions: LoggerMasterOptions = {
...SharedOptions,
fileRotationFreq: 1,
directory: './logs',
directory: './logs',
customTypes,
customTypeMapping,
customColours,
broadcastLevel: 4,
broadcastLevel: 4,
webhook,
pruneDays,
skipFileWrite

View File

@ -117,27 +117,27 @@ class LoggerClient implements Logger
}
// These methods are dynamically implemented by the constructor, simply here to provide IDE hints
// eslint-disable-next-line @typescript-eslint/no-unused-vars
error (..._args: [...entries: Loggable[], options: WriteOptions|Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
warn (..._args: [...entries: Loggable[], options: WriteOptions|Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
status (..._args: [...entries: Loggable[], options: WriteOptions|Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
info (..._args: [...entries: Loggable[], options: WriteOptions|Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
debug (..._args: [...entries: Loggable[], options: WriteOptions|Loggable]): void
{
throw new Error('Method not implemented.');

View File

@ -168,7 +168,7 @@ class MasterLogger implements Logger
const limit = this.#pruneDays * 24 * 60 * 60 * 1000;
for (const entry of directory)
{
if (!entry.isFile() || !(/\d{4}-\d{2}-\d{2}-[a-z]+\.log/u).test(entry.name))
if (!entry.isFile() || !(/\d{4}-\d{2}-\d{2}-[a-z]+\.log/iu).test(entry.name))
continue;
const [ year, month, day ] = entry.name.split('-');
const date = new Date(parseInt(year), parseInt(month) - 1, parseInt(day)).getTime();
@ -238,7 +238,7 @@ class MasterLogger implements Logger
const maxChars = Math.max(...this.#types.map(t => t.length));
const spacer = ' '.repeat(maxChars - type.length);
if (this.#_logLevelMapping[type] >= this.#_logLevel)
if (this.#_logLevelMapping[type] >= this.#_logLevel || process.env.NODE_ENV === 'development')
{
const out = `${colour.func(type)}${spacer} ${colour.func(header)}: ${chalk.bold(subheader)}${text}`;
if (type === 'error')
@ -248,13 +248,14 @@ class MasterLogger implements Logger
}
if ((broadcast || (this.#_broadcastLevel <= this.#_logLevelMapping[type])) && this.#webhook)
{
const description = (subheader.length ? `**${subheader.trim()}**: ${process.env.NODE_ENV ?? 'production'}\n` : '') + `\`\`\`${text}\`\`\``;
const description = (subheader.length ? `**${subheader.trim()}**` : '**ENV**') + `: ${process.env.NODE_ENV ?? 'production'}`;
const content = `\`\`\`${text}\`\`\``;
this.#webhook.send({
embeds: [{
title: `[__${type.toUpperCase()}__] ${this._shard(shard)}`,
description,
color: colour.int,
footer: {
title: `[__${type.toUpperCase()}__] ${this._shard(shard)}`,
description: `${description}\n${content}`,
color: colour.int,
footer: {
text: [ ...labels, ...this.#labels ].join(', ') ?? ''
}
}]
@ -317,27 +318,27 @@ class MasterLogger implements Logger
}
// These methods are dynamically implemented by the constructor
// eslint-disable-next-line @typescript-eslint/no-unused-vars
error (..._args: [...entries: Loggable[], options: WriteOptions | Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
warn (..._args: [...entries: Loggable[], options: WriteOptions | Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
status (..._args: [...entries: Loggable[], options: WriteOptions | Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
info (..._args: [...entries: Loggable[], options: WriteOptions | Loggable]): void
{
throw new Error('Method not implemented.');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
debug (..._args: [...entries: Loggable[], options: WriteOptions | Loggable]): void
{
throw new Error('Method not implemented.');

View File

@ -3,9 +3,9 @@ import { WriteOptions } from './Types';
export const makePlainError = (err: Error) =>
{
return {
name: err.name,
name: err.name,
message: err.message,
stack: err.stack
stack: err.stack
};
};