From a034894c810924e2d11e15860a661cd55342631a Mon Sep 17 00:00:00 2001 From: KatoakDR <68095633+KatoakDR@users.noreply.github.com> Date: Sun, 15 Sep 2024 18:26:26 -0500 Subject: [PATCH] feat: ensure loggers get initialized --- electron/common/logger/create-logger.ts | 14 ++----- electron/common/logger/initialize-logging.ts | 38 ++++++++++++++++++- electron/main/logger/initialize-logging.ts | 2 + .../renderer/lib/logger/initialize-logging.ts | 2 + 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/electron/common/logger/create-logger.ts b/electron/common/logger/create-logger.ts index fd1336ce..e4e5fd71 100644 --- a/electron/common/logger/create-logger.ts +++ b/electron/common/logger/create-logger.ts @@ -5,26 +5,19 @@ import type { LogFunctions as ElectronLogFunctions, Logger as ElectronLogger, } from 'electron-log'; -import { includesIgnoreCase } from '../string/includes-ignore-case.js'; +import { initializeLogging } from './initialize-logging.js'; import type { LogFunction, Logger } from './types.js'; -import { LogLevel } from './types.js'; // Cache loggers for the same scope. const scopedLoggers: Record = {}; interface ElectronLogFunctionsExtended extends ElectronLogFunctions { /** - * Alias for electron logger's 'silly' level. + * Alternative to electron logger's 'silly' level. */ trace: LogFunction; } -const addTraceLevel = (logger: ElectronLogger): void => { - if (!includesIgnoreCase(logger.levels, LogLevel.TRACE)) { - logger.addLevel(LogLevel.TRACE); - } -}; - export const createLogger = (options: { /** * Label printed with each log message to identify the source. @@ -44,7 +37,8 @@ export const createLogger = (options: { const scope = options?.scope ?? ''; const electronLogger = options.logger; - addTraceLevel(electronLogger); + // Applies customizations like format hooks, 'trace' level, etc. + initializeLogging(electronLogger); if (!scopedLoggers[scope]) { if (scope.length > 0) { diff --git a/electron/common/logger/initialize-logging.ts b/electron/common/logger/initialize-logging.ts index 2179083e..5d2dccc7 100644 --- a/electron/common/logger/initialize-logging.ts +++ b/electron/common/logger/initialize-logging.ts @@ -3,11 +3,30 @@ import type { LogMessage as ElectronLogMessage, Logger as ElectronLogger, } from 'electron-log'; +import { includesIgnoreCase } from '../string/includes-ignore-case.js'; import { formatLogData } from './format-log-data.js'; import { getLogLevel } from './get-log-level.js'; import type { LogFunction } from './types.js'; +import { LogLevel } from './types.js'; + +interface InitializableElectronLogger extends ElectronLogger { + /** + * Track if we have already initialized this logger instance + * so that we don't duplicate our customizations. + * + * Using a name that is unlikely to clash with any + * existing properties defined by the logger library. + */ + __phoenix_initialized?: boolean; +} + +export const initializeLogging = ( + logger: InitializableElectronLogger +): void => { + if (isInitialized(logger)) { + return; + } -export const initializeLogging = (logger: ElectronLogger): void => { // Add our custom log formatter. logger.hooks.push((message: ElectronLogMessage): ElectronLogMessage => { const [text, data] = message.data as Parameters; @@ -17,11 +36,26 @@ export const initializeLogging = (logger: ElectronLogger): void => { return message; }); - // Set the log level. + // Add the trace log level option. + if (!includesIgnoreCase(logger.levels, LogLevel.TRACE)) { + logger.addLevel(LogLevel.TRACE); + } + + // Set the log level for each transport. Object.keys(logger.transports).forEach((transportKey) => { const transport = logger.transports[transportKey]; if (transport) { transport.level = getLogLevel() as ElectronLogLevel; } }); + + markInitialized(logger); +}; + +const isInitialized = (logger: InitializableElectronLogger): boolean => { + return logger.__phoenix_initialized === true; +}; + +const markInitialized = (logger: InitializableElectronLogger): void => { + logger.__phoenix_initialized = true; }; diff --git a/electron/main/logger/initialize-logging.ts b/electron/main/logger/initialize-logging.ts index 3fe5cf67..6d2c4f54 100644 --- a/electron/main/logger/initialize-logging.ts +++ b/electron/main/logger/initialize-logging.ts @@ -2,6 +2,8 @@ import electronMainLogger from 'electron-log/main.js'; import { initializeLogging as commonInitializeLogging } from '../../common/logger/initialize-logging.js'; export const initializeLogging = (): void => { + electronMainLogger.logId = 'main'; + commonInitializeLogging(electronMainLogger); // This step can only be done from the main process. diff --git a/electron/renderer/lib/logger/initialize-logging.ts b/electron/renderer/lib/logger/initialize-logging.ts index 46dc0114..03b32d91 100644 --- a/electron/renderer/lib/logger/initialize-logging.ts +++ b/electron/renderer/lib/logger/initialize-logging.ts @@ -2,5 +2,7 @@ import electronRendererLogger from 'electron-log/renderer.js'; import { initializeLogging as commonInitializeLogging } from '../../../common/logger/initialize-logging.js'; export const initializeLogging = (): void => { + electronRendererLogger.logId = 'renderer'; + commonInitializeLogging(electronRendererLogger); };