Skip to content

Commit

Permalink
feat: ensure loggers get initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
KatoakDR committed Sep 15, 2024
1 parent a9e8ede commit a034894
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
14 changes: 4 additions & 10 deletions electron/common/logger/create-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, ElectronLogFunctions> = {};

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.
Expand All @@ -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) {
Expand Down
38 changes: 36 additions & 2 deletions electron/common/logger/initialize-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<LogFunction>;
Expand All @@ -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;
};
2 changes: 2 additions & 0 deletions electron/main/logger/initialize-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions electron/renderer/lib/logger/initialize-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

0 comments on commit a034894

Please sign in to comment.