From 06b27ed2f23e19f72cb927c04accd94ca00dcc75 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Sun, 16 Apr 2023 16:40:57 +0300 Subject: [PATCH] v2.3.0 --- package.json | 2 +- src/Defaults.ts | 22 +++++++++++----------- src/LoggerClient.ts | 8 +++++--- src/MasterLogger.ts | 12 +++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index e03e6b9..6c4addd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@navy.gif/logger", - "version": "2.2.2", + "version": "2.3.0", "description": "Logging thing", "author": "Navy.gif", "license": "MIT", diff --git a/src/Defaults.ts b/src/Defaults.ts index ad697f3..f651c93 100644 --- a/src/Defaults.ts +++ b/src/Defaults.ts @@ -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 = { diff --git a/src/LoggerClient.ts b/src/LoggerClient.ts index 860d05f..2b852cd 100644 --- a/src/LoggerClient.ts +++ b/src/LoggerClient.ts @@ -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 }; } diff --git a/src/MasterLogger.ts b/src/MasterLogger.ts index 0f69aa8..e5fcafe 100644 --- a/src/MasterLogger.ts +++ b/src/MasterLogger.ts @@ -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());