From 3d530c2d11a1f6333e3eec63f6a685f18c154c17 Mon Sep 17 00:00:00 2001 From: pike Date: Sun, 3 Dec 2023 15:05:41 +0100 Subject: [PATCH] feat: Change Logger and Storage to singletons --- README.md | 13 +++--- src/core/Logger.ts | 96 +++++++++++++++++++++++++-------------------- src/core/Storage.ts | 30 ++++++++++---- 3 files changed, 83 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index a099a7b..5edd4da 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ # Fairpost -Fairpost helps you manage your social -feeds from the command line, using Node. +Fairpost helps you manage your social media feeds from a single +entry point, using Node. It supports Facebook, Instagram, +Reddit, Twitter and LinkedIn. Your Feed is just a folder on disk, and all subfolders are Posts, containing at least one text file (the post body) and @@ -14,7 +15,7 @@ how a folder with contents can best be presented as a post on each platform. Commonly, you would call this script every day or week. -Fairpost will then automatically **prepare** the folders, +Fairpost can then automatically **prepare** the folders, **schedule** the next post using a certain interval and **publish** any post when it is due. All you have to do is add folders with content. @@ -45,7 +46,7 @@ cp .env.dist .env && nano .env ## Enable platforms -Read how to enable various media platforms in the [docs](docs). +Read how to enable various social media platforms in the [docs](docs). ## Feed planning @@ -127,10 +128,10 @@ fairpost.js publish-due-posts [--folders=xxx,xxx] [--platforms=xxx,xxx] [--dry-r # Select which config file to use fairpost.js [command] [arguments] --config=.env-test -# Set the cli output format to pure json +# Set the cli report format to pure json fairpost.js [command] [arguments] --report=json -# Output trace logging on the command line (overriding .env) +# Enable trace logging output to the console (overriding .env) fairpost.js [command] [arguments] --verbose ``` diff --git a/src/core/Logger.ts b/src/core/Logger.ts index bc655b5..066083c 100644 --- a/src/core/Logger.ts +++ b/src/core/Logger.ts @@ -1,49 +1,61 @@ import * as log4js from "log4js"; -const configFile = process.env.FAIRPOST_LOGGER_CONFIG || "log4js.json"; -const category = process.env.FAIRPOST_LOGGER_CATEGORY || "default"; -const level = process.env.FAIRPOST_LOGGER_LEVEL || "INFO"; -const console = process.env.FAIRPOST_LOGGER_CONSOLE !== "false"; - -// eslint-disable-next-line @typescript-eslint/no-var-requires -const config = require(__dirname + "/../../" + configFile); -if (!config.categories[category]) { - throw new Error( - "Log4js category " + category + " not found in " + configFile, - ); -} -if (console && !config.categories[category]["appenders"].includes("console")) { - if (!config.appenders["console"]) { - config.appenders["console"] = { type: "console" }; - } - config.categories[category]["appenders"].push("console"); -} - -log4js.configure(config); -const logger = log4js.getLogger(category); -logger.level = level; - -export default logger; - -// (trace, debug, info, warn, error, fatal). -/*export default class Logger { - trace(msg) { - logger.trace(msg); - } - debug(msg) { - logger.trace(msg); +class Logger { + static instance: Logger; + logger: log4js.Logger; + constructor() { + if (Logger.instance) { + throw new Error("Logger: call getInstance() instead"); } - info(msg) { - logger.trace(msg); - } - warn(msg) { - logger.trace(msg); + const configFile = process.env.FAIRPOST_LOGGER_CONFIG || "log4js.json"; + const category = process.env.FAIRPOST_LOGGER_CATEGORY || "default"; + const level = process.env.FAIRPOST_LOGGER_LEVEL || "INFO"; + const console = process.env.FAIRPOST_LOGGER_CONSOLE !== "false"; + + // eslint-disable-next-line @typescript-eslint/no-var-requires + const config = require(__dirname + "/../../" + configFile); + if (!config.categories[category]) { + throw new Error( + "Logger: Log4js category " + category + " not found in " + configFile, + ); } - error(msg) { - logger.trace(msg); + if ( + console && + !config.categories[category]["appenders"].includes("console") + ) { + if (!config.appenders["console"]) { + config.appenders["console"] = { type: "console" }; + } + config.categories[category]["appenders"].push("console"); } - fatal(msg) { - logger.trace(msg); + + log4js.configure(config); + this.logger = log4js.getLogger(category); + this.logger.level = level; + } + static getInstance(): Logger { + if (!Logger.instance) { + Logger.instance = new Logger(); } + return Logger.instance; + } + trace(...args) { + this.logger.trace(args); + } + debug(...args) { + this.logger.trace(args); + } + info(...args) { + this.logger.trace(args); + } + warn(...args) { + this.logger.trace(args); + } + error(...args) { + this.logger.trace(args); + } + fatal(...args) { + this.logger.trace(args); + } } -*/ +export default Logger.getInstance(); diff --git a/src/core/Storage.ts b/src/core/Storage.ts index 549c8b6..5713c34 100644 --- a/src/core/Storage.ts +++ b/src/core/Storage.ts @@ -1,5 +1,5 @@ /** - * Store - minimalist storage manager + * Store - minimalist storage manager - singleton * * - sets and gets key / value pairs. * - uses two 'stores': @@ -8,12 +8,24 @@ * stored and encrypted somewhere else * */ -export default class Storage { - public static get( - store: "settings" | "auth", - key: string, - def?: string, - ): string { + +class Storage { + static instance: Storage; + + constructor() { + if (Storage.instance) { + throw new Error("Storage: call getInstance() instead"); + } + } + + static getInstance(): Storage { + if (!Storage.instance) { + Storage.instance = new Storage(); + } + return Storage.instance; + } + + public get(store: "settings" | "auth", key: string, def?: string): string { let value = "" as string; const storage = store === "settings" @@ -33,7 +45,7 @@ export default class Storage { return value; } - public static set(store: "settings" | "auth", key: string, value: string) { + public set(store: "settings" | "auth", key: string, value: string) { const storage = store === "settings" ? process.env.FAIRPOST_STORAGE_SETTINGS @@ -53,3 +65,5 @@ export default class Storage { } } } + +export default Storage.getInstance();