From 764061b4beb9b34034f976ebef947dfed970c20d Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Wed, 26 Feb 2025 00:28:05 +0100 Subject: [PATCH] fix: :loud_sound: Automatically create logs folder if it doesn't exist --- utils/loggers.ts | 49 ++++++------------------------------------------ 1 file changed, 6 insertions(+), 43 deletions(-) diff --git a/utils/loggers.ts b/utils/loggers.ts index 9039193c..0274b052 100644 --- a/utils/loggers.ts +++ b/utils/loggers.ts @@ -1,17 +1,6 @@ -import { - type Stats, - appendFileSync, - closeSync, - existsSync, - mkdirSync, - openSync, - renameSync, - statSync, -} from "node:fs"; -import { - type RotatingFileSinkDriver, - getRotatingFileSink, -} from "@logtape/file"; +import { mkdir } from "node:fs/promises"; +import { dirname } from "node:path"; +import { getRotatingFileSink } from "@logtape/file"; import { type LogLevel, type LogRecord, @@ -22,6 +11,9 @@ import { import chalk from "chalk"; import { config } from "~/config.ts"; +// config.logging.log_file_path is a path to a file, create the directory if it doesn't exist +await mkdir(dirname(config.logging.log_file_path), { recursive: true }); + const levelAbbreviations: Record = { debug: "DBG", info: "INF", @@ -74,35 +66,6 @@ export function defaultConsoleFormatter(record: LogRecord): string[] { ]; } -export const nodeDriver: RotatingFileSinkDriver = { - openSync(path: string): number { - return openSync(path, "a"); - }, - writeSync(fd, chunk): void { - appendFileSync(fd, chunk, { - flush: true, - }); - }, - flushSync(): void { - // ... - }, - closeSync(fd): void { - closeSync(fd); - }, - statSync(path): Stats { - // If file does not exist, create it - if (!existsSync(path)) { - // Mkdir all directories in path - const dirs = path.split("/"); - dirs.pop(); - mkdirSync(dirs.join("/"), { recursive: true }); - appendFileSync(path, ""); - } - return statSync(path); - }, - renameSync, -}; - export const configureLoggers = (silent = false): Promise => configure({ reset: true,