File size: 9,034 Bytes
eb17e9f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# Logging Guide for IMO Math Problem Solver

Comprehensive logging has been added to help you debug and monitor the application, especially in containerized environments like Hugging Face Spaces.

## πŸ“‹ Logging Overview

The Gradio app now includes detailed logging at every step:
- API key detection
- Request validation
- Subprocess execution
- File operations
- Solution checking
- Error handling

## πŸ” What Gets Logged

### Startup Logging
```
2025-11-26 10:30:00 [INFO] __main__: Starting IMO Math Problem Solver - Gradio Interface
2025-11-26 10:30:00 [INFO] __main__: Python version: 3.10.12
2025-11-26 10:30:00 [INFO] __main__: Gradio version: 3.50.0
2025-11-26 10:30:00 [INFO] __main__: Working directory: /app
2025-11-26 10:30:00 [INFO] __main__: ============================================================
2025-11-26 10:30:00 [INFO] __main__: Checking API keys availability...
2025-11-26 10:30:00 [INFO] __main__: βœ“ GOOGLE_API_KEY found (length: 39)
2025-11-26 10:30:00 [INFO] __main__: Available providers: ['Google Gemini 2.5 Pro']
```

### Request Logging
```
2025-11-26 10:31:15 [INFO] __main__: ============================================================
2025-11-26 10:31:15 [INFO] __main__: NEW SOLVE REQUEST
2025-11-26 10:31:15 [INFO] __main__: ============================================================
2025-11-26 10:31:15 [INFO] __main__: Provider: Google Gemini 2.5 Pro
2025-11-26 10:31:15 [INFO] __main__: Max runs: 10
2025-11-26 10:31:15 [INFO] __main__: Num agents: 1
2025-11-26 10:31:15 [INFO] __main__: Other prompts: None
2025-11-26 10:31:15 [INFO] __main__: Problem length: 245 characters
```

### Subprocess Logging
```
2025-11-26 10:31:15 [INFO] __main__: Starting SINGLE AGENT mode
2025-11-26 10:31:15 [INFO] __main__: Agent file: code/agent.py
2025-11-26 10:31:15 [INFO] __main__: Solution log file: logs/session_1732630275/solution.log
2025-11-26 10:31:15 [INFO] __main__: Command: python code/agent.py /tmp/tmpxyz.txt --log logs/session_1732630275/solution.log --max_runs 10
2025-11-26 10:31:15 [INFO] __main__: Starting subprocess...
2025-11-26 10:31:16 [INFO] __main__: Subprocess started with PID: 12345
```

### Progress Logging
```
2025-11-26 10:31:20 [INFO] __main__: Processed 50 lines from subprocess
2025-11-26 10:31:25 [INFO] __main__: Processed 100 lines from subprocess
2025-11-26 10:31:30 [INFO] __main__: Processed 150 lines from subprocess
```

### Completion Logging
```
2025-11-26 10:35:42 [INFO] __main__: Subprocess completed with return code: 0
2025-11-26 10:35:42 [INFO] __main__: Total output lines: 523
2025-11-26 10:35:42 [INFO] __main__: Checking for solution in log file...
2025-11-26 10:35:42 [INFO] __main__: Log file size: 45678 bytes
2025-11-26 10:35:42 [INFO] __main__: βœ“ SOLUTION FOUND in log file!
2025-11-26 10:35:42 [INFO] __main__: Returning SUCCESS result to user
2025-11-26 10:35:42 [INFO] __main__: Cleaned up temporary problem file: /tmp/tmpxyz.txt
2025-11-26 10:35:42 [INFO] __main__: ============================================================
2025-11-26 10:35:42 [INFO] __main__: SOLVE REQUEST COMPLETED
2025-11-26 10:35:42 [INFO] __main__: ============================================================
```

### Error Logging
```
2025-11-26 10:31:15 [ERROR] __main__: API key not found for OpenAI GPT-5
2025-11-26 10:31:15 [ERROR] __main__: Available providers: ['Google Gemini 2.5 Pro']
```

## πŸ“ Viewing Logs

### On Hugging Face Spaces

1. **Go to your Space**
2. **Click "Logs" tab** (top navigation)
3. **View real-time logs**

The logs will show all the information logged by the application.

### Locally

When running locally, logs appear in two places:

#### 1. Console Output
All logs print to stdout/stderr:
```bash
python gradio_app.py
```

#### 2. Redirect to File (Optional)
To save logs to a file:
```bash
python gradio_app.py 2>&1 | tee app.log
```

This will display logs in console AND save to `app.log`.

## πŸ”§ Log Levels

The app uses standard Python logging levels:

| Level | When Used | Example |
|-------|-----------|---------|
| **INFO** | Normal operations | "Subprocess started with PID: 12345" |
| **WARNING** | Potential issues | "No API keys found at startup!" |
| **ERROR** | Errors that can be handled | "API key not found for OpenAI GPT-5" |
| **EXCEPTION** | Unhandled exceptions | Full stack trace with `logger.exception()` |

## πŸ› Debugging with Logs

### Problem: App won't start

**Look for:**
```
[ERROR] __main__: Failed to start subprocess
[EXCEPTION] __main__: EXCEPTION occurred during solve_problem
```

**Check:**
- API keys are set correctly
- `code/` directory exists
- Agent files are present

### Problem: No solution found

**Look for:**
```
[INFO] __main__: Subprocess completed with return code: 1
[WARNING] __main__: No solution found in log file
```

**Check:**
- The subprocess return code (0 = success, non-zero = error)
- Review agent-specific logs in `logs/session_*/solution.log`

### Problem: API key issues

**Look for:**
```
[WARNING] βœ— GOOGLE_API_KEY not found
[ERROR] __main__: API key not found for Google Gemini 2.5 Pro
```

**Fix:**
- Set API key in Hugging Face Spaces: Settings β†’ Repository Secrets
- Restart the Space (Factory reboot)

### Problem: Subprocess fails

**Look for:**
```
[ERROR] __main__: Failed to start subprocess: <error message>
[INFO] __main__: Command: python code/agent.py ...
```

**Check:**
- The exact command that was run
- Whether agent file exists
- Python interpreter is available

## πŸ“Š Log Analysis Examples

### Example 1: Successful Run

```
2025-11-26 10:30:00 [INFO] __main__: NEW SOLVE REQUEST
2025-11-26 10:30:00 [INFO] __main__: Provider: Google Gemini 2.5 Pro
2025-11-26 10:30:00 [INFO] __main__: Created temporary problem file: /tmp/tmpxyz.txt
2025-11-26 10:30:00 [INFO] __main__: Starting SINGLE AGENT mode
2025-11-26 10:30:00 [INFO] __main__: Subprocess started with PID: 12345
2025-11-26 10:30:50 [INFO] __main__: Subprocess completed with return code: 0
2025-11-26 10:30:50 [INFO] __main__: βœ“ SOLUTION FOUND in log file!
2025-11-26 10:30:50 [INFO] __main__: SOLVE REQUEST COMPLETED
```
βœ… **Result:** Success! Everything worked.

### Example 2: Missing API Key

```
2025-11-26 10:30:00 [INFO] __main__: βœ— GOOGLE_API_KEY not found
2025-11-26 10:30:05 [ERROR] __main__: API key not found for Google Gemini 2.5 Pro
2025-11-26 10:30:05 [ERROR] __main__: Available providers: []
```
❌ **Problem:** No API key set. **Fix:** Add GOOGLE_API_KEY to secrets.

### Example 3: Agent Subprocess Error

```
2025-11-26 10:30:00 [INFO] __main__: Command: python code/agent.py /tmp/tmpxyz.txt ...
2025-11-26 10:30:00 [INFO] __main__: Subprocess started with PID: 12345
2025-11-26 10:30:10 [INFO] __main__: Subprocess completed with return code: 1
2025-11-26 10:30:10 [WARNING] __main__: No solution found in log file
```
⚠️ **Problem:** Agent ran but failed. **Check:** Look at `logs/session_*/solution.log` for agent-specific errors.

## 🎯 Key Metrics to Monitor

Track these in your logs:

1. **Return codes**: `return code: 0` = success
2. **Processing time**: Time between "Subprocess started" and "completed"
3. **Output volume**: "Total output lines" indicates activity
4. **Success rate**: Ratio of "SOLUTION FOUND" to total requests

## πŸ“ Custom Logging (Advanced)

If you want more detailed logging, edit `gradio_app.py`:

### Change Log Level

```python
# More verbose logging
logging.basicConfig(
    level=logging.DEBUG,  # Changed from INFO
    format='%(asctime)s [%(levelname)s] %(name)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S'
)
```

### Add Custom Logs

```python
logger.debug("Detailed debug information")
logger.info("General information")
logger.warning("Warning message")
logger.error("Error message")
logger.exception("Exception with full stack trace")
```

## πŸ” Security Note

**⚠️ Never log API keys!**

The current logging shows:
```
βœ“ GOOGLE_API_KEY found (length: 39)  # βœ… Safe - only shows length
```

NOT:
```
βœ“ GOOGLE_API_KEY = AIza...xyz  # ❌ NEVER DO THIS
```

## πŸ“¦ Log Retention

### Hugging Face Spaces
- Logs are available in the "Logs" tab
- Older logs scroll out of view
- Not permanently stored (restart clears logs)

### Local Development
- Console logs disappear when app stops
- Use `tee` to save to file if needed
- Agent logs saved in `logs/session_*/*.log` directories

## πŸš€ Production Tips

1. **Monitor startup**: Check logs immediately after deployment
2. **Watch for patterns**: Recurring errors indicate systematic issues
3. **Check timestamps**: Long gaps between log entries = slow operations
4. **Review after changes**: Always check logs after updating code
5. **Save critical runs**: Copy important logs before they're lost

## πŸ“– Related Files

- **[gradio_app.py](gradio_app.py)** - Main file with logging
- **[app.py](app.py)** - Copy for Hugging Face (same logging)
- **logs/session_*/*.log** - Agent-specific logs

---

With comprehensive logging, you can now see exactly what's happening in your containerized environment! πŸŽ‰