How sindresorhus/electron-timber works
Project type and purpose
• This is an open-source utility library for Electron apps, “electron-timber,” that captures and pretty-prints logs from both the main and renderer processes. It acts as a drop-in replacement for console.* calls and routes renderer logs through the main process so they appear in the terminal.
File structure
• Top-level module files:
• index.js – Entry point, detects process type (main or renderer) and exports the logger API.
• preload.mjs – Optional preload script for the renderer to inject or initialize the logger before your web code runs.
• Config & metadata: .editorconfig, .gitignore, .npmrc, package.json, license, README.
• Tests: A “test/fixture” folder with sample main/renderer code, HTML, and test runners demonstrating how the logger behaves.
README insights
• Describes API surface: log, warn, error, time, timeEnd, streamLog/Warn/Error, create, getDefaults, setDefaults.
• Highlights that renderer logs must be “required” in the main process to be forwarded.
• Mentions IPC under the hood (implied by cross-process functionality) and environment-variable filtering (TIMBER_LOGGERS).
Main components
A. Application Main Process
– User’s Electron main script that calls logger.log/error/time/etc.
– Imports and initializes electron-timber (index.js).
B. Electron-Timber Main Module
– Core Logger class and default instance, console output logic, prefix padding, logLevel filtering, environment‐variable filtering.
– IPC listener (via ipcMain) that receives serialized log events from renderer processes.
C. Console/Terminal (stdout/stderr)
– Final sink for formatted log messages.
D. Application Renderer Process
– User’s renderer script or UI code that calls logger.log/error/time/etc.
– May load logger via preload.mjs or bundler.
E. Electron-Timber Renderer Module
– Proxy logger that serializes log calls and sends events over IPC (via ipcRenderer).
F. Node Streams (optional)
– streamLog/Warn/Error helpers wrap a stream.Readable and forward each chunk/line to the logger methods.
Relationships and data flows
1. In Main:
• Calls to logger.log → Electron-Timber Main Module → direct console.log with prefix “main” and optional custom names.
2. In Renderer:
• Calls to logger.log → Electron-Timber Renderer Module → ipcRenderer.send('TIMBER_LOG', payload).
• Electron-Timber Main Module’s ipcMain.on('TIMBER_LOG') → dispatches to core logger.log with prefix “renderer”.
3. streamLog:
• Attaches to a stream → on data event splits into lines → for each line calls appropriate logger method.
4. Environment filtering:
• Main Module checks TIMBER_LOGGERS env var → filters out loggers not in the list.
Architectural patterns and design principles
• Facade over console and Electron IPC to unify logging API.
• Singleton/default instance pattern + factory (create) for custom named loggers.
• Separation of concerns: main logic vs. renderer proxy vs. stream adapters.
• Extensibility via create(options) and defaults override.