This commit is contained in:
Erik 2023-04-16 16:40:57 +03:00
parent 3f8bf97931
commit 06b27ed2f2
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
4 changed files with 24 additions and 20 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@navy.gif/logger",
"version": "2.2.2",
"version": "2.3.0",
"description": "Logging thing",
"author": "Navy.gif",
"license": "MIT",

View File

@ -61,10 +61,10 @@ const logLevelMapping = {
};
type SharedOptionsType = {
guard: string,
customStreams: string[]
logLevel: LogLevel,
logLevelMapping: LogLevelType
guard?: string,
customStreams?: string[]
logLevel?: LogLevel,
logLevelMapping?: LogLevelType
}
const SharedOptions: SharedOptionsType = {
@ -75,13 +75,13 @@ const SharedOptions: SharedOptionsType = {
};
export type LoggerMasterOptions = SharedOptionsType & {
fileRotationFreq: number,
directory: string,
customTypes: string[],
customTypeMapping: { [key: string]: string },
customColours: { [key: string]: number | string },
broadcastLevel: number,
webhook: WebhookClientOptions,
fileRotationFreq?: number,
directory?: string,
customTypes?: string[],
customTypeMapping?: { [key: string]: string },
customColours?: { [key: string]: number | string },
broadcastLevel?: number,
webhook?: WebhookClientOptions,
}
const MasterOptions: LoggerMasterOptions = {

View File

@ -2,7 +2,7 @@
// const Defaults = require('./Defaults');
import Defaults from "./Defaults.js";
import { inspect } from "node:util";
import { WriteOptions } from "./Types.js";
import { LogFunction, WriteOptions } from "./Types.js";
import { Logger } from "./LoggerInterface.js";
type ClientOptions = {
@ -23,6 +23,8 @@ class LoggerClient implements Logger {
static MaxChars = 0;
[key: string]: LogFunction | unknown;
#_guard: string;
#_logLevel: number;
#_logLevelMapping: {[key: string]: number};
@ -47,8 +49,8 @@ class LoggerClient implements Logger {
});
}
this.#_guard = opts.guard || Defaults.ClientOptions.guard;
this.#_logLevel = opts.logLevel ?? Defaults.ClientOptions.logLevel;
this.#_guard = opts.guard || Defaults.ClientOptions.guard as string;
this.#_logLevel = opts.logLevel ?? Defaults.ClientOptions.logLevel as number;
this.#_logLevelMapping = { ...Defaults.ClientOptions.logLevelMapping, ...opts.logLevelMapping };
}

View File

@ -51,22 +51,24 @@ class MasterLogger implements Logger {
constructor (config = Defaults.MasterOptions) {
const {
directory, customTypes, customStreams, customTypeMapping,
directory, customTypes = [], customStreams = [], customTypeMapping,
customColours, guard, fileRotationFreq, logLevel, logLevelMapping,
webhook, broadcastLevel
} = { ...Defaults.MasterOptions, ...config };
if (!directory)
throw new Error('Missing directory for log files');
this.#directory = path.resolve(directory);
if (!fs.existsSync(this.#directory))
fs.mkdirSync(this.#directory, { recursive: true });
this.#_broadcastLevel = broadcastLevel;
this.#_logLevel = logLevel;
this.#_broadcastLevel = broadcastLevel as number;
this.#_logLevel = logLevel as number;
this.#_logLevelMapping = { ...Defaults.MasterOptions.logLevelMapping, ...logLevelMapping };
if (logLevelMapping)
Object.entries(logLevelMapping).forEach(([ name, level ]) => {
addLogLevel(name, level);
});
this.#_guard = guard;
this.#_guard = guard as string;
this.#types = [ ...customTypes, ...Defaults.Types ];
for (const type of this.#types) {
@ -100,7 +102,7 @@ class MasterLogger implements Logger {
}, {} as WriteStreams);
// Startup day, used to keep track of file rotation
this.#rotationFreq = fileRotationFreq * DAY;
this.#rotationFreq = fileRotationFreq as number * DAY;
this.#startDay = Math.floor(Date.now() / this.#rotationFreq) * this.#rotationFreq;
this.#rotateTO = setTimeout(this.rotateLogFiles.bind(this), this.#startDay + this.#rotationFreq - Date.now());