16 - Centralized Logging
Overview
OpenAlgo implements centralized Python logging with configurable levels, colored console output, and optional retained files. General application logs are not stored in logs.db: logs.db is the traffic/security store, while order and analyzer audit rows live in the main database.
Architecture Diagram
┌───────────────────────────────────────────────────────────────────────────────┐
│ Centralized Logging Architecture │
└───────────────────────────────────────────────────────────────────────────────┘
┌───────────────────────────────────────────────────────────────────────────────┐
│ Application Components │
│ │
│ ┌────────────┐ ┌────────────┐ ┌────────────┐ ┌────────────┐ │
│ │ Flask │ │ REST API │ │ WebSocket │ │ Services │ │
│ │ Routes │ │ Endpoints │ │ Proxy │ │ │ │
│ └──────┬─────┘ └──────┬─────┘ └──────┬─────┘ └──────┬─────┘ │
│ │ │ │ │ │
│ └─────────────────┴────────┬────────┴─────────────────┘ │
│ │ │
│ ▼ │
│ ┌──────────────────────────────┐ │
│ │ setup_logging(): root logger │ │
│ └──────────────────────────────┘ │
└────────────────────────────────────┼──────────────────────────────────────────┘
│
┌────────────────┴──────────────────────┐
│ │
▼ ▼
┌────────────────────────────────┐ ┌────────────────────────────────┐
│ Console handler │ │ TimedRotatingFileHandler │
│ ColoredFormatter, LOG_COLORS │ │ when="midnight", interval=1 │
│ SensitiveDataFilter │ │ backupCount=LOG_RETENTION │
│ stdout, always on │ │ only if LOG_TO_FILE=True │
└────────────────────────────────┘ └────────────────────────────────┘
│ │
▼ ▼
┌────────────────────────────────┐ ┌────────────────────────────────┐
│ log/errors.jsonl │ │ log/openalgo_YYYY-MM-DD.log │
│ ERROR and above, JSON lines │ │ one file per day, kept for │
│ always on, trimmed at boot │ │ LOG_RETENTION days │
└────────────────────────────────┘ └────────────────────────────────┘Configuration
Environment Variables
Defaults below are the values utils/logging.py falls back to when the variable is unset.
Usage
Getting a Logger
Log Levels
DEBUG
10
Detailed debugging information
INFO
20
General operational messages
WARNING
30
Something unexpected happened
ERROR
40
Error occurred, operation failed
CRITICAL
50
System is unusable
Implementation
Location: utils/logging.py
Handlers are attached once to the root logger by setup_logging(), which runs at module import. get_logger(name) is only a thin wrapper around logging.getLogger(name), so per-module loggers inherit the root configuration.
Filters And Noise Suppression
SensitiveDataFilter
Redacts credentials and tokens from every handler
ColoredFormatter
Level-based console colors, controlled by LOG_COLORS
JSONErrorFormatter
Structured ERROR+ records for log/errors.jsonl
WerkzeugErrorFilter
Drops known development-server noise
WebSocketHandshakeFilter
Drops short-lived WebSocket handshake errors
setup_logging() also raises the level of werkzeug, urllib3, requests, httpx, httpcore, hpack, apscheduler, websockets and telegram loggers so third-party chatter stays out of the console.
Error Log
log/errors.jsonl is written unconditionally, independent of LOG_TO_FILE. It captures ERROR and above as one JSON object per line and is truncated to the last 1000 entries at startup so it cannot grow without bound.
Log Categories
Application Logs
Auth
blueprints.auth
Login/logout events
Orders
restx_api.place_order
Order placement
WebSocket
websocket_proxy
WS connections
Strategy
blueprints.strategy
Strategy execution
Example Log Output
Startup Banner
The banner is three logged lines between two separator lines of separator_char, not a drawn box. Colors come from colorama and are suppressed when LOG_COLORS is false unless FORCE_COLOR is set. app.py imports the helper but does not currently call it.
Sample output at the default separator_char="=" and width=60:
File Rotation
Rotation is time based, not size based. TimedRotatingFileHandler rolls at midnight and keeps LOG_RETENTION days of history.
Rotation Settings
Handler
TimedRotatingFileHandler
Time based, not size based
When
midnight, interval=1
One rotation per day
Backup Count
LOG_RETENTION (default 14)
Number of rotated files to keep
Encoding
utf-8
File handler encoding
Startup cleanup
cleanup_old_logs(log_dir, retention_days)
Deletes files older than the retention window
Compression
None
Rotated files are not compressed
Viewing Logs
File Logs
UI Log Viewer
blueprints/log.py serves the order-log viewer at /logs (with /logs/export), and blueprints/logging.py serves the consolidated dashboard at /logging, which links live logs, analyzer logs, traffic, latency and security views.
Key Files Reference
utils/logging.py
setup_logging(), get_logger(), formatters and filters
blueprints/log.py
Order log viewer at /logs and /logs/export
blueprints/logging.py
Consolidated logging dashboard at /logging
database/apilog_db.py
Order API audit rows in the main database
database/analyzer_db.py
Analyzer audit rows in the main database
database/traffic_db.py
Request traffic rows in logs.db
log/
Dated log files plus errors.jsonl
Last updated