|
| 1 | +# Logging Systems |
| 2 | + |
| 3 | +BalatroBot implements three distinct logging systems to support different aspects of development, debugging, and analysis: |
| 4 | + |
| 5 | +1. [**JSONL Run Logging**](#jsonl-run-logging) - Records complete game runs for replay and analysis |
| 6 | +2. [**Python SDK Logging**](#python-sdk-logging) - Future logging capabilities for the Python framework |
| 7 | +3. [**Mod Logging**](#mod-logging) - Traditional streamodded logging for mod development and debugging |
| 8 | + |
| 9 | +## JSONL Run Logging |
| 10 | + |
| 11 | +The run logging system records complete game runs as JSONL (JSON Lines) files. Each line represents a single game action with its parameters, timestamp, and game state **before** the action. |
| 12 | + |
| 13 | +The system hooks into these game functions: |
| 14 | + |
| 15 | +- `start_run`: begins a new game run |
| 16 | +- `skip_or_select_blind`: blind selection actions |
| 17 | +- `play_hand_or_discard`: card play actions |
| 18 | +- `cash_out`: end blind and collect rewards |
| 19 | +- `shop`: shop interactions |
| 20 | +- `go_to_menu`: return to main menu |
| 21 | + |
| 22 | +The JSONL files are automatically created when: |
| 23 | + |
| 24 | +- **Playing manually**: Starting a new run through the game interface |
| 25 | +- **Using the API**: Interacting with the game through the TCP API |
| 26 | + |
| 27 | +Files are saved as: `{mod_path}/runs/YYYYMMDDTHHMMSS.jsonl` |
| 28 | + |
| 29 | +!!! tip "Replay runs" |
| 30 | + |
| 31 | + The JSONL logs enable complete run replay for testing and analysis. |
| 32 | + |
| 33 | + ```python |
| 34 | + state = load_jsonl_run("20250714T145700.jsonl") |
| 35 | + for step in state: |
| 36 | + send_and_receive_api_message( |
| 37 | + tcp_client, |
| 38 | + step["function"]["name"], |
| 39 | + step["function"]["arguments"] |
| 40 | + ) |
| 41 | + ``` |
| 42 | + |
| 43 | +Examples for runs can be found in the [test suite](https://github.com/S1M0N38/balatrobot/tree/main/tests/runs). |
| 44 | + |
| 45 | +### Format Specification |
| 46 | + |
| 47 | +Each log entry follows this structure: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "timestamp_ms": int, |
| 52 | + "function": { |
| 53 | + "name": "...", |
| 54 | + "arguments": {...} |
| 55 | + }, |
| 56 | + "game_state": { ... } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +- **`timestamp_ms`**: Unix timestamp in milliseconds when the action occurred |
| 61 | +- **`function`**: The game function that was called |
| 62 | + - `name`: Function name (e.g., "start_run", "play_hand_or_discard", "cash_out") |
| 63 | + - `arguments`: Arguments passed to the function |
| 64 | +- **`game_state`**: Complete game state **before** the function execution |
| 65 | + |
| 66 | +## Python SDK Logging |
| 67 | + |
| 68 | +The Python SDK (`src/balatrobot/`) implements structured logging for bot development and debugging. The logging system provides visibility into client operations, API communications, and error handling. |
| 69 | + |
| 70 | +### What Gets Logged |
| 71 | + |
| 72 | +The `BalatroClient` logs the following operations: |
| 73 | + |
| 74 | +- **Connection events**: When connecting to and disconnecting from the game API |
| 75 | +- **API requests**: Function names being called and their completion status |
| 76 | +- **Errors**: Connection failures, socket errors, and invalid API responses |
| 77 | + |
| 78 | +### Configuration Example |
| 79 | + |
| 80 | +The SDK uses Python's built-in `logging` module. Configure it in your bot code before using the client: |
| 81 | + |
| 82 | +```python |
| 83 | +import logging |
| 84 | +from balatrobot import BalatroClient |
| 85 | + |
| 86 | +# Configure logging |
| 87 | +log_format = '%(asctime)s [%(levelname)s] %(name)s: %(message)s' |
| 88 | +console_handler = logging.StreamHandler() |
| 89 | +console_handler.setLevel(logging.INFO) |
| 90 | +file_handler = logging.FileHandler('balatrobot.log') |
| 91 | +file_handler.setLevel(logging.DEBUG) |
| 92 | + |
| 93 | +logging.basicConfig( |
| 94 | + level=logging.DEBUG, |
| 95 | + format=log_format, |
| 96 | + handlers=[console_handler, file_handler] |
| 97 | +) |
| 98 | + |
| 99 | +# Use the client |
| 100 | +with BalatroClient() as client: |
| 101 | + state = client.get_game_state() |
| 102 | + client.start_run(deck="Red Deck", stake=1) |
| 103 | +``` |
| 104 | + |
| 105 | +## Mod Logging |
| 106 | + |
| 107 | +BalatroBot uses Steamodded's built-in logging system for mod development and debugging. |
| 108 | + |
| 109 | +- **Traditional logging**: Standard log levels (DEBUG, INFO, WARNING, ERROR) |
| 110 | +- **Development focus**: Primarily for debugging mod functionality |
| 111 | +- **Console output**: Displays in game console and log files |
| 112 | + |
| 113 | +```lua |
| 114 | +-- Available through Steamodded |
| 115 | +sendDebugMessage("This is a debug message") |
| 116 | +sendInfoMessage("This is an info message") |
| 117 | +sendWarningMessage("This is a warning message") |
| 118 | +sendErrorMessage("This is an error message") |
| 119 | +``` |
0 commit comments