How Alacritty Debug Logging Captures and Persists Runtime Information
Alacritty initializes a global Logger at startup that filters records by level and target, formats them with elapsed timestamps, and persists runtime information to both stdout and an on-demand log file while routing errors to the UI message bar.
Alacritty's debug logging system provides comprehensive visibility into terminal runtime behavior by capturing internal events, errors, and warnings. The implementation in the alacritty/alacritty repository demonstrates how a Rust application can persist runtime information to multiple destinations while maintaining performance. This article examines how debug logging captures and persists runtime information through its global Logger architecture.
Architectural Overview of Debug Logging
Logger Initialization
The logging subsystem starts in src/main.rs where the entry point calls logging::initialize exactly once, passing parsed CLI options and an EventLoopProxy that allows the logger to push messages to the UI thread.
// src/main.rs – startup (excerpt)
let options = Options::new();
let event_proxy = event_loop.create_proxy();
let log_path = logging::initialize(&options, event_proxy)?; // ← creates the global Logger
The initialize function in src/logging.rs (lines 60-71) performs three critical operations:
- Sets the maximum log level via
log::set_max_levelbased on CLI verbosity flags (Options::log_level) - Instantiates a
Loggerstruct that holds a mutex-protectedOnDemandLogFile, a stdout writer, and the event proxy - Registers the
Loggeras the global logger withlog::set_boxed_logger
The Logger Type Structure
The Logger struct defined in src/logging.rs (lines 74-81) maintains the state necessary to capture and persist runtime information:
| Field | Purpose |
|---|---|
logfile: Mutex<OnDemandLogFile> |
Lazily creates and writes to $TMPDIR/Alacritty-<pid>.log |
stdout: Mutex<LineWriter<Stdout>> |
Mirrors every message to the user's terminal |
event_proxy: Mutex<EventLoopProxy<Event>> |
Sends error/warning messages to the UI message bar |
start: Instant |
Reference point for elapsed timestamps |
Log Record Processing Pipeline
When any code calls a log macro (e.g., info!, error!), the log crate forwards the Record to Logger::log in src/logging.rs (lines 122-158). The method executes a five-step pipeline to capture and persist runtime information:
-
Target Filtering: The record's target is trimmed and compared against
ALLOWED_TARGETSandALACRITTY_EXTRA_LOG_TARGETS, ensuring only Alacritty-specific crates are logged unless the level isTrace. -
Message Formatting: The
create_log_messagefunction (lines 162-181) builds a formatted string with elapsed time since startup:[seconds.nanoseconds] [LEVEL] [target] <payload> -
Dual Output Writing: The formatted string writes simultaneously to the on-demand file via
logfile.write_alland to the buffered stdout writer. -
Message Bar Routing: For
ErrororWarnlevels,message_bar_log(lines 94-124) creates aMessageand sends it through theevent_proxyto display at the top of the terminal.
On-Demand Log File Persistence
The OnDemandLogFile struct in src/logging.rs (lines 92-138) handles lazy file creation. It generates a path at $TMPDIR/Alacritty-<pid>.log and stores this path in the ALACRITTY_LOG environment variable for external discovery. If the file is deleted while Alacritty is running, the next write operation automatically recreates it, ensuring continuous capture of runtime information.
Controlling Debug Logging Verbosity
CLI options in src/cli.rs (lines 12-30) expose -v/--verbose and -q/--quiet flags. The Options::log_level method maps these flags to a log::LevelFilter. Users can also set the ALACRITTY_LOG environment variable to a custom path, or export additional log targets via ALACRITTY_EXTRA_LOG_TARGETS using a semicolon-separated list.
Message Bar Integration
When the logger forwards an Error or Warn to the UI, MessageBar (implemented in src/message_bar.rs lines 14-38) receives a Message containing the formatted text and its target. The bar renders the text with a close button. The logger also removes stale messages tied to specific targets (e.g., LOG_TARGET_IPC_CONFIG) via MessageBuffer::remove_target in src/window_context.rs (lines 345-357).
Implementing Custom Debug Logging
To capture runtime information from new features, import the logging macros and use Alacritty's predefined targets or custom ones.
use log::{debug, trace, info, warn, error};
use crate::logging::LOG_TARGET_WINIT;
pub fn create_pane(pane_id: u64, geometry: Rect) {
// Appears in stdout and log file
debug!(target: LOG_TARGET_WINIT, "Creating pane id={}", pane_id);
// Only visible with -vvv (Trace level)
trace!(target: LOG_TARGET_WINIT, "Pane geometry: {:?}", geometry);
}
Because LOG_TARGET_WINIT is part of ALLOWED_TARGETS, the record passes the filter. The logger prepends the runtime timestamp, writes the line to $TMPDIR/Alacritty-<pid>.log, prints it to stdout, and flashes a message bar if the level is Error or Warn.
Enabling Extra Targets
To extend debug logging to custom components without recompiling, set the environment variable:
export ALACRITTY_EXTRA_LOG_TARGETS="my_pane_debug;custom_component"
alacritty -vv
Now any log! call with target: "my_pane_debug" will be captured and persisted alongside standard runtime information.
Key Source Files for Debug Logging
| File | Role | Location |
|---|---|---|
alacritty/src/logging.rs |
Core logger implementation, file creation, formatting, message-bar dispatch | logging.rs |
alacritty/src/cli.rs |
Parses -v/--quiet flags, builds log_level() |
cli.rs |
alacritty/src/message_bar.rs |
UI component displaying error/warning messages from the logger | message_bar.rs |
alacritty/src/window_context.rs |
Removes stale messages tied to specific targets | window_context.rs |
alacritty/src/event.rs |
Emits info! events with LOG_TARGET_WINIT for the event loop |
event.rs |
alacritty/src/config/mod.rs |
Uses LOG_TARGET_CONFIG for configuration-related logs |
config/mod.rs |
Environment Variables for Debug Logging
| Variable | Purpose |
|---|---|
ALACRITTY_LOG |
Path to the active log file (set automatically by the logger) |
ALACRITTY_EXTRA_LOG_TARGETS |
Semicolon-separated list of additional log targets to capture |
Summary
- Alacritty's debug logging captures and persists runtime information through a global Logger initialized at startup in
src/logging.rs. - The system filters records by level and target, formats them with elapsed timestamps, and writes to both stdout and an on-demand log file at
$TMPDIR/Alacritty-<pid>.log. - Error and warning levels trigger message bar notifications via an
EventLoopProxyfor immediate user visibility. - Developers can extend logging to custom targets using the
ALACRITTY_EXTRA_LOG_TARGETSenvironment variable without modifying source code. - Verbosity is controlled via CLI flags (
-v,-q) parsed insrc/cli.rs, with automatic file recreation if the log file is deleted during runtime.
Frequently Asked Questions
Where does Alacritty store its debug log files?
Alacritty creates on-demand log files in the system temporary directory at $TMPDIR/Alacritty-<pid>.log. The exact path is automatically exported to the ALACRITTY_LOG environment variable, allowing external tools to locate the file while the terminal is running. If the file is deleted while Alacritty is running, the next write operation automatically recreates it, ensuring continuous capture of runtime information.
How do I enable debug logging for specific Alacritty components?
You can extend debug logging to specific components by setting the ALACRITTY_EXTRA_LOG_TARGETS environment variable to a semicolon-separated list of target names. For example, export ALACRITTY_EXTRA_LOG_TARGETS="my_custom_target;winit" enables logging for those targets without modifying the source code's ALLOWED_TARGETS list. Then run Alacritty with -v or -vv flags to set the appropriate verbosity level for capturing the runtime information.
What is the difference between the log file and the message bar?
The log file and stdout capture all debug logging levels according to the filter settings, providing a complete historical record of runtime information in $TMPDIR/Alacritty-<pid>.log. In contrast, the message bar only displays Error and Warning level records for immediate user visibility. The logger routes these critical levels through an EventLoopProxy to the UI thread, while the full log persists to the on-demand file in the temporary directory regardless of message bar state.
How does Alacritty handle log rotation or file deletion?
Alacritty implements an on-demand log file strategy rather than traditional rotation. The OnDemandLogFile struct in src/logging.rs lazily creates the file on first write and stores the file handle. If the file is deleted while Alacritty is running, the next logging operation automatically recreates it at the same path. This ensures continuous debug logging captures runtime information without requiring process restarts or manual intervention.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →