Managing Log Files with RotatingFileHandler | Toss Dev #1-4

📚 Toss Auto-Trading Dev Journal (Phase 1-4 / 6 posts so far)

Next: Phase 2 (strategy modularization) — link coming after publish.

Quick context for anyone new here: this bot runs against the Toss Securities API, a Korean brokerage, and trades in KRW (Korean won). This post wraps up the last item on the Phase 1 checklist.

The bot’s watch loop runs every 5 seconds, logging on every pass. That’s fine for a few hours, but leave it running unattended for days and toss_trader.log just keeps growing. Nothing was capping it. Time to fix that before it became an actual disk-space problem.

Sizing the rotation

Working backward from the 5-second loop interval, I estimated roughly how much log volume a day of continuous running would produce, and landed on:

  • Max size per file: 5MB — about 1.5–2 days of logs. Frequent enough to keep things manageable, not so frequent that rotation itself becomes noise.
  • Backup count: 5 — toss_trader.log plus .1 through .5, capping total disk usage around 30MB.

Both values live in config.py as LOG_MAX_BYTES and LOG_BACKUP_COUNT rather than hardcoded, so they’re easy to retune later.

The implementation

Swapped the plain logging.FileHandler in toss_common.py for logging.handlers.RotatingFileHandler:

from logging.handlers import RotatingFileHandler
import config

handler = RotatingFileHandler(
    "toss_trader.log",
    maxBytes=config.LOG_MAX_BYTES,
    backupCount=config.LOG_BACKUP_COUNT,
    encoding="utf-8",
)

Standard rotation behavior: once the file hits the size limit, it shifts to .log.1, then .2, and so on, dropping the oldest backup once the count is exceeded.

Verifying it actually rotates

Waiting around for a real 5MB file to fill up just to watch rotation happen would’ve been a waste of time, so I temporarily set up a handler with a tiny 1KB limit and hammered it with dummy log lines.

Confirmed .log, .log.1, .log.2, and .log.3 all got created exactly as configured, with older files getting cleaned up past the backup count. The point wasn’t to validate the 5MB/5-file numbers themselves — it was to confirm the rotation mechanism actually works as expected. Scaling the threshold down for testing saved a lot of time.

Also added a regression test in test_config.py to check the log rotation settings stay within a sane range going forward.

Where things landed

The pytest suite went from 33 to 34 tests, all passing. Re-ran the actual get_price.py script to confirm logging still worked correctly to both console and file. Local environment and the repo are in sync.

Phase 1, in hindsight

ItemStatus
Config extraction (config.py)
Shared retry/backoff logic
Log rotation (RotatingFileHandler)
pytest test suite

Config extraction, then shared logic, then a real regression scare, then tests, then this rotation fix — it reads like one continuous arc now that it’s done. Next up is Phase 2: modularizing the trading strategy, building out a strategy.py for indicator-based strategies (moving averages, RSI), and supporting multiple tickers under watch at once.

Note: all figures and file structures here are reconstructed placeholders per the project’s masking rules and don’t reflect actual account data.

1 thought on “Managing Log Files with RotatingFileHandler | Toss Dev #1-4”

  1. Pingback: [Toss Auto-Trading Dev Journal #7] Extracting the Buy Condition into a Strategy Class (Phase 2 Kickoff) - Orbit - Space & ETF Investing

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top