Electron Log is a simple, powerful logging module specifically designed for Electron applications, but also compatible with Node.js and NW.js environments. It provides a comprehensive logging solution with multiple transport methods, automatic file system integration, and built-in IPC communication for seamless logging across main and renderer processes.
npm install electron-logContext-aware import (auto-detects environment):
const log = require('electron-log');import log from 'electron-log';Environment-specific imports:
// Main process
const log = require('electron-log/main');
// Renderer process
const log = require('electron-log/renderer');
// Node.js
const log = require('electron-log/node');
// Preload script
require('electron-log/preload');// Main process
import log from 'electron-log/main';
// Renderer process
import log from 'electron-log/renderer';
// Node.js
import log from 'electron-log/node';import log from 'electron-log';
// Initialize for renderer process (main process only)
log.initialize();
// Basic logging
log.error('Something went wrong');
log.warn('This is a warning');
log.info('Application started');
log.verbose('Verbose information');
log.debug('Debug information');
log.silly('Very detailed debug information');
// Custom scoped logging
const scopedLog = log.scope('auth');
scopedLog.info('User logged in');
// Configure transports
log.transports.file.level = 'debug';
log.transports.console.format = '{h}:{i}:{s} {text}';
// Error handling
log.errorHandler.startCatching();Electron Log is built around several key components:
Essential logging functions with multiple levels and flexible message formatting. Supports both simple string messages and complex object serialization.
interface LogFunctions {
error(...params: any[]): void;
warn(...params: any[]): void;
info(...params: any[]): void;
verbose(...params: any[]): void;
debug(...params: any[]): void;
silly(...params: any[]): void;
log(...params: any[]): void; // Alias for info
}Multiple output destinations for log messages including console, files, IPC communication, and remote HTTP endpoints. Each transport is independently configurable.
interface Transport {
(message: LogMessage): void;
level: LogLevel | false;
transforms: TransformFn[];
}
interface MainTransports {
console: ConsoleTransport;
file: FileTransport;
ipc: Transport;
remote: RemoteTransport;
}
interface RendererTransports {
console: ConsoleTransport;
ipc: Transport;
}Logger instance creation, configuration, and lifecycle management. Supports multiple logger instances with independent configurations and message buffering for batch processing.
interface Logger extends LogFunctions {
logId: string;
levels: string[];
transports: { [key: string]: Transport | null };
variables: Variables;
hooks: Hook[];
addLevel(level: string, index?: number): void;
create(options: { logId: string }): Logger;
processMessage(message: LogMessage, options?: { transports?: Transport[] | string[] }): void;
}Automatic catching and logging of unhandled exceptions and promise rejections with customizable error processing and dialog display.
interface ErrorHandler {
handle(error: Error, options?: ErrorHandlerOptions): void;
setOptions(options: ErrorHandlerOptions): void;
startCatching(options?: ErrorHandlerOptions): void;
stopCatching(): void;
}Automatic logging of Electron application and webContents events with customizable formatting and filtering.
interface EventLogger {
format: string | ((input: EventFormatterInput) => unknown[]);
level: LogLevel;
scope: string;
events: Record<EventSource, Record<string, boolean>>;
formatters: Record<EventSource, Record<string, (input: EventFormatterInput) => object | unknown[]>>;
setOptions(options: EventLoggerOptions): void;
startLogging(options?: EventLoggerOptions): void;
stopLogging(): void;
}Scoped logging for message categorization and filtering with automatic label padding and customizable formatting.
interface Scope {
(label: string): LogFunctions;
defaultLabel: string | false;
labelPadding: boolean | number;
}type LogLevel = 'error' | 'warn' | 'info' | 'verbose' | 'debug' | 'silly';
type LevelOption = LogLevel | false;
interface LogMessage {
data: any[];
date: Date;
level: LogLevel;
logId?: string;
scope?: string;
variables?: Variables;
}
interface Variables {
processType: string;
[name: string]: any;
}
type Hook = (
message: LogMessage,
transport?: Transport,
transportName?: string,
) => LogMessage | false;
type TransformFn = (options: {
data: any[];
message: LogMessage;
transport: Transport;
logger: Logger;
}) => any;