FloatWM includes a comprehensive structured logging system built on top of the log and fern crates. The system provides flexible configuration options, multiple output formats, and environment variable-based configuration.
- Multiple Log Levels: trace, debug, info, warn, error, off
- Multiple Output Formats: Terminal (colored), JSON (structured), Compact
- Flexible Output Targets: stdout, stderr, file, or both
- Environment Variable Configuration: Easy configuration without code changes
- Module-Level Filtering: Control logging verbosity per module
- Timestamps and Metadata: Optional timestamps, module paths, and file/line information
The logging system can be configured using the following environment variables:
| Variable | Description | Values | Default |
|---|---|---|---|
FLOATWM_LOG_LEVEL |
Minimum log level to display | trace, debug, info, warn, error, off |
info |
FLOATWM_LOG_FORMAT |
Output format for logs | terminal, json, compact |
terminal |
FLOATWM_LOG_TARGET |
Where to send log output | stdout, stderr, file, both |
stderr |
FLOATWM_LOG_FILE |
Path to log file (required for file and both targets) |
Any valid file path | None |
FLOATWM_LOG_TIMESTAMP |
Include timestamps in logs | true, false, 1, 0 |
true |
FLOATWM_LOG_MODULE |
Include module paths in logs | true, false, 1, 0 |
true |
RUST_LOG |
Alternative log level setting (also supports module-specific levels) | See RUST_LOG documentation | info |
# Uses all defaults: info level, terminal format, stderr output
floatwm# Enable debug level logging
FLOATWM_LOG_LEVEL=debug floatwm# Output JSON logs to a file
FLOATWM_LOG_FORMAT=json \
FLOATWM_LOG_TARGET=file \
FLOATWM_LOG_FILE=/var/log/floatwm.log \
floatwm# Output to both console and file
FLOATWM_LOG_TARGET=both \
FLOATWM_LOG_FILE=/var/log/floatwm.log \
floatwm# Enable debug for specific modules only
RUST_LOG=floatwm::x11=debug,floatwm::sensor=trace,floatwm=info floatwm# Enable all trace logs
FLOATWM_LOG_LEVEL=trace floatwmColored, human-readable output suitable for console viewing:
2024-01-13 10:30:45.123 [INFO ] floatwm::main FloatWM starting with sensor support enabled
2024-01-13 10:30:45.124 [DEBUG] floatwm::main Logging system initialized successfully
2024-01-13 10:30:45.125 [INFO ] floatwm::lib Connected to X11 display: :0
Structured JSON output for machine parsing and log aggregation:
{"timestamp":"2024-01-13T10:30:45.123+00:00","level":"INFO","message":"FloatWM starting","module":"floatwm::main"}
{"timestamp":"2024-01-13T10:30:45.124+00:00","level":"DEBUG","message":"Logging initialized","module":"floatwm::main"}Simplified format with minimal metadata:
2024-01-13 10:30:45.123 [INFO] FloatWM starting
2024-01-13 10:30:45.124 [DEBUG] Logging initialized
use floatwm::logging;
fn main() {
// Initialize with environment variables
logging::init_logging().expect("Failed to initialize logging");
log::info!("Application started");
}use floatwm::logging::{LoggingConfig, LogFormat, LogTarget};
use log::LevelFilter;
fn main() {
let config = LoggingConfig::new()
.with_level(LevelFilter::Debug)
.with_format(LogFormat::Json)
.with_target(LogTarget::Both)
.with_log_file("/var/log/floatwm.log")
.with_timestamps(true)
.with_module_path(true);
logging::init_logging_with_config(config)
.expect("Failed to initialize logging");
log::info!("Application started");
}use log::{trace, debug, info, warn, error};
fn example_function() {
trace!("Detailed trace information");
debug!("Debugging information");
info!("General information");
warn!("Warning condition");
error!("Error occurred");
}| Level | Description | Use Case |
|---|---|---|
trace |
Extremely detailed debugging | Tracing function execution, variable states |
debug |
Detailed information for debugging | Development and troubleshooting |
info |
General informational messages | Normal operation, significant events |
warn |
Warning conditions | Unexpected but recoverable situations |
error |
Error conditions | Failures that don't stop execution |
off |
Disable logging | Performance-critical sections |
# Enable detailed logging
FLOATWM_LOG_LEVEL=debug floatwm# Log warnings and errors to file
FLOATWM_LOG_LEVEL=warn \
FLOATWM_LOG_TARGET=file \
FLOATWM_LOG_FILE=/var/log/floatwm.log \
floatwm# Enable trace logging for specific module
RUST_LOG=floatwm::x11=trace,floatwm=info floatwm# JSON format for easy parsing
FLOATWM_LOG_FORMAT=json \
FLOATWM_LOG_TARGET=file \
FLOATWM_LOG_FILE=/var/log/floatwm.log \
floatwmThe logging system writes to files in append mode but does not implement automatic log rotation. For production use, consider using external tools like logrotate:
/var/log/floatwm.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0640 floatwm adm
}
- Logging at
traceanddebuglevels has minimal overhead when disabled - File I/O is buffered for better performance
- JSON formatting has slightly more overhead than terminal format
- For high-frequency logging, consider using
warnor higher levels in production
Check that the log level is set appropriately:
# Check current RUST_LOG setting
echo $RUST_LOG
# Try explicitly setting the level
RUST_LOG=debug floatwmEnsure FloatWM has write permissions for the log file location:
# Create log directory with proper permissions
sudo mkdir -p /var/log/floatwm
sudo chown $USER:$USER /var/log/floatwm
chmod 755 /var/log/floatwmVerify the module path matches the actual Rust module structure:
// In floatwm/src/x11.rs
log::debug!("This message is from module: floatwm::x11");Then use:
RUST_LOG=floatwm::x11=debug floatwm[Service]
ExecStart=/usr/bin/floatwm
Environment="FLOATWM_LOG_LEVEL=info"
Environment="FLOATWM_LOG_TARGET=file"
Environment="FLOATWM_LOG_FILE=/var/log/floatwm.log"
StandardOutput=null
StandardError=journalENV FLOATWM_LOG_LEVEL=info
ENV FLOATWM_LOG_FORMAT=json
ENV FLOATWM_LOG_TARGET=stdoutSee the floatwm::logging module documentation for complete API details:
LoggingConfig: Main configuration structLogFormat: Output format enumLogTarget: Output target enuminit_logging(): Initialize with environment variablesinit_logging_with_config(): Initialize with custom config