Profile
Back to NewsBack
GitHub Trending 6 min
Reader Mode
sindresorhus/electron-timber: Pretty logger for Electron apps

sindresorhus/electron-timber: Pretty logger for Electron apps

15 hours ago

electron-timber

Pretty logger for Electron apps

By default, logs from the renderer process don't show up in the terminal. Now they do.

You can use this module directly in both the main and renderer process.

Install

npm install electron-timber

Requires Electron 44 or later.

Usage

Main process:

import {app, BrowserWindow} from 'electron';
import logger from 'electron-timber';

let mainWindow;

(async () => { await app.whenReady();

mainWindow = new BrowserWindow(); await mainWindow.loadURL(…);

logger.log('Main log'); logger.error('Main error');

const customLogger = logger.create({name: 'custom'}); customLogger.log('Custom log'); })();

Renderer process:

import logger from 'electron-timber';

logger.log('Renderer log'); logger.error('Renderer error');

No preload setup is needed. The module registers its own preload script via session.registerPreloadScript() to share defaults with renderers.

Works with bundlers like Vite (including electron-vite). The renderer entry is browser-only and never bundles Node.js or Electron main-process APIs, so nodeIntegration is not required. If your bundler needs it to be explicit, import electron-timber/renderer in the renderer and electron-timber/main in the main process.

If you bundle the main process, webpack and rspack copy the preload script into the output automatically. Vite/Rollup and esbuild cannot do this, so mark electron-timber as external in the main process build. electron-vite already does this by default.

API

logger

Logging will be prefixed with either main or renderer depending on where it comes from.

Logs from the renderer process only show up if you have imported electron-timber in the main process.

The methods are bound to the class instance, so you can do: const log = logger.log; log('Foo');.

log(…values)

Like console.log.

warn(…values)

Like console.warn.

error(…values)

Like console.error.

time(label?)

Like console.time.

label

Type: string\ Default: 'default'

timeEnd(label?)

Like console.timeEnd. Does nothing when no timer with the label is running.

label

Type: string\ Default: 'default'

streamLog(stream)

Log each line in a stream.Readable. For example, child_process.spawn(…).stdout.

streamWarn(stream)

Same as streamLog, but logs using console.warn instead.

streamError(stream)

Same as streamLog, but logs using console.error instead.

create(options?)

Create a custom logger instance.

You should initialize this on module load so prefix padding is consistent with the other loggers.

options

Type: object

##### name

Type: string

Name of the logger. Used to prefix the log output. Don't use main or renderer.

##### ignore

Type: RegExp

Ignore lines matching the given regex.

##### logLevel

Type: string\ Default: 'info' when NODE_ENV is 'development', otherwise 'warn'

Can be info (log everything), warn (log warnings and errors), or error (log errors only).

##### timestamp

Type: boolean\ Default: false

Prefix the output with the local time, for example 22:10:34 main › Log.

Only applies to the terminal output. Renderer logs are printed in the terminal by the main process, so use setDefaults() in the main process to add timestamps to them. In DevTools, use the “Show timestamps” setting instead.

logger.setDefaults({timestamp: true});

##### file

Type: boolean | string\ Default: false

Also write the output to a file.

Use true to write to main.log in app.getPath('logs') (for example, ~/Library/Logs//main.log on macOS), or a string with the absolute path to the file. The directory is created if it does not exist.

The logLevel, ignore, and TIMBER_LOGGERS filters also apply to the file. Lines are written without colors and with a timestamp, for example 2026-10-07T20:10:34.123Z [warn] main › Something. Lines are written synchronously, so the last lines before a crash are not lost.

Only applies in the main process. Renderer logs are written by the main process, so use setDefaults() in the main process to also write them to the file.

logger.setDefaults({file: true});

##### maxFileSize

Type: number\ Default: 1048576 (1 MB)

The maximum size of the log file in bytes.

When the file reaches this size, it is renamed with .old added to the name (for example, main.old.log), replacing the previous one, and a new file is started, so at most about twice this size is kept on disk. Set it to 0 to never rotate the file.

logger.setDefaults({
	file: true,
	maxFileSize: 5  1024  1024
});

getDefaults()

Get the default options (across main and renderer processes).

Note: logLevel is returned in its internal numeric form.

setDefaults(options?) Main process only

Set the default options (across main and renderer processes). Renderer windows are notified automatically.

The name option is ignored.

It throws when called from a renderer.

options

Type: object

Same as the options for create() (except name).

hookConsole(options?)

Hook console methods (console.log, console.warn, etc.) to use electron-timber instead.

When called with no arguments, hooks the console in the current process. From the main process, pass {renderer: true} to also hook all current and future renderer consoles.

Electron security warnings are not sent to the terminal, as they are already visible in the DevTools console.

Returns a function to unhook the console methods.

options

Type: object

##### main

Type: boolean\ Default: true when called with no arguments from the main process, otherwise false

Hook the console in the main process. Only applies in the main process.

##### renderer

Type: boolean\ Default: true when called with no arguments from a renderer process, otherwise false

Hook the console in renderer processes. Can be set from the main process to hook all current and future renderers, or from a renderer to hook itself.

const unhook = logger.hookConsole({
	main: true,
	renderer: true
});

// Later... unhook();

Note: Custom loggers created with create() do not have access to this method.

Toggle loggers

You can show the output of only a subset of the loggers using the environment variable TIMBER_LOGGERS. It must be set before the module is imported. Here we show the output of the default renderer logger and a custom unicorn logger, but not the default main logger:

TIMBER_LOGGERS=renderer,unicorn electron .

Colors

Colors are disabled when chalk detects that the output is not a TTY, which happens in some terminals and set-ups even though the output ends up somewhere that renders colors fine (Cmder/ConEmu, mintty, the VS Code debugger, Electron on Windows). Set FORCE_COLOR=1 to force colors in that case:

FORCE_COLOR=1 electron .

Related

Chat with me