Skip to content

Commit b514531

Browse files
committed
Feature: message logging
1 parent 0c95567 commit b514531

File tree

3 files changed

+508
-4
lines changed

3 files changed

+508
-4
lines changed

MESSAGE_LOGGING.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
# Message Logging System
2+
3+
## Overview
4+
5+
Your chatbot now has complete message monitoring capabilities. The system automatically records all messages sent by users and AI responses, including timestamps, user identification, IP addresses, and other relevant information.
6+
7+
## Features
8+
9+
### 🔍 Automatic Recording
10+
- **User Messages**: Records all questions sent by users
11+
- **AI Responses**: Records all AI response content
12+
- **Error Information**: Records any system errors or API failures
13+
- **User Identification**: Generates unique ID for each session
14+
- **IP Address**: Records visitor IP addresses
15+
- **Timestamp**: Precisely records the time of each message
16+
17+
### 📊 Data Storage
18+
- **JSON Format**: Stored in date-separated files for easy querying
19+
- **Local Storage**: All logs saved in `backend/logs/` directory
20+
- **Auto-organization**: Automatically organizes log files by date
21+
22+
## Usage
23+
24+
### 1. View Log Files
25+
26+
Log files are saved in the `backend/logs/` directory with the format:
27+
```
28+
chat_logs_YYYY-MM-DD.json
29+
```
30+
31+
### 2. Web Interface
32+
33+
After starting the backend server, visit:
34+
```
35+
http://localhost:5001/logs
36+
```
37+
38+
This interface provides:
39+
- 📅 Date selector
40+
- 📊 Daily statistics
41+
- 📝 Complete message log table
42+
- 🎨 Color coding (user messages/AI responses/errors)
43+
44+
### 3. API Endpoints
45+
46+
#### Get Logs
47+
```bash
48+
# Get today's logs
49+
curl http://localhost:5001/api/logs
50+
51+
# Get logs for a specific date
52+
curl http://localhost:5001/api/logs?date=2024-01-15
53+
54+
# Get logs for a specific user
55+
curl http://localhost:5001/api/logs?user_id=abc123
56+
57+
# Limit the number of results
58+
curl http://localhost:5001/api/logs?limit=50
59+
```
60+
61+
#### Get Statistics
62+
```bash
63+
# Get statistics for all logs
64+
curl http://localhost:5001/api/logs/stats
65+
```
66+
67+
## Log Format
68+
69+
Each log entry contains the following fields:
70+
71+
```json
72+
{
73+
"timestamp": "2024-01-15T10:30:45.123456",
74+
"user_id": "550e8400-e29b-41d4-a716-446655440000",
75+
"message_type": "user", // "user" or "ai"
76+
"message": "User's message content",
77+
"response": "AI's response content",
78+
"error": "Error message (if any)",
79+
"ip_address": "192.168.1.100",
80+
"user_agent": "Mozilla/5.0..."
81+
}
82+
```
83+
84+
## Privacy and Security
85+
86+
### 🔒 Data Protection
87+
- Log files are added to `.gitignore` and won't be committed to the code repository
88+
- Only necessary technical information is recorded, no personal sensitive data is collected
89+
- IP addresses are only used for basic access statistics
90+
91+
### 🗑️ Data Cleanup
92+
You can delete log files at any time:
93+
```bash
94+
# Delete all logs
95+
rm -rf backend/logs/
96+
97+
# Delete logs for a specific date
98+
rm backend/logs/chat_logs_2024-01-15.json
99+
```
100+
101+
## Example Use Cases
102+
103+
### 1. View Today's Activity
104+
```bash
105+
curl http://localhost:5001/api/logs/stats
106+
```
107+
108+
### 2. Analyze User Question Types
109+
Review log files to identify the most common types of questions
110+
111+
### 3. Monitor System Performance
112+
Track AI service stability through error logs
113+
114+
### 4. User Behavior Analysis
115+
Analyze session length, question complexity, etc.
116+
117+
## Important Notes
118+
119+
1. **Storage Space**: Log files will grow over time, recommend regular cleanup
120+
2. **Performance Impact**: Logging has minimal impact on system performance
121+
3. **Data Retention**: Decide log retention period based on your needs
122+
4. **Compliance**: Ensure compliance with local data protection regulations
123+
124+
## Troubleshooting
125+
126+
### Log Files Don't Exist
127+
- Check if `backend/logs/` directory exists
128+
- Confirm backend server is running
129+
- Check file permissions
130+
131+
### Can't Access Web Interface
132+
- Confirm backend server is running on port 5001
133+
- Check firewall settings
134+
- Review backend console error messages
135+
136+
### API Endpoints Not Working
137+
- Verify server is running
138+
- Check if endpoints are accessible
139+
- Review server logs for errors
140+
141+
## API Reference
142+
143+
### GET /api/logs
144+
Retrieve chat logs with optional filtering.
145+
146+
**Query Parameters:**
147+
- `date` (optional): Date in YYYY-MM-DD format
148+
- `user_id` (optional): Filter by specific user ID
149+
- `limit` (optional): Maximum number of entries to return (default: 100)
150+
151+
**Response:**
152+
```json
153+
{
154+
"logs": [...],
155+
"total_entries": 50,
156+
"date": "2024-01-15",
157+
"success": true
158+
}
159+
```
160+
161+
### GET /api/logs/stats
162+
Get statistics about chat usage.
163+
164+
**Response:**
165+
```json
166+
{
167+
"total_messages": 150,
168+
"unique_users": 25,
169+
"daily_stats": {
170+
"2024-01-15": {
171+
"total_messages": 50,
172+
"user_messages": 25,
173+
"ai_responses": 25,
174+
"unique_users": 10
175+
}
176+
},
177+
"success": true
178+
}
179+
```
180+
181+
### GET /logs
182+
Web interface for viewing logs in a browser.
183+
184+
**Features:**
185+
- Interactive date selector
186+
- Real-time statistics
187+
- Color-coded message types
188+
- Responsive table layout
189+
190+
## Development
191+
192+
### Adding New Log Fields
193+
To add new fields to the log entries, modify the `log_message()` function in `backend/app.py`:
194+
195+
```python
196+
def log_message(user_id, message, is_user=True, response=None, error=None):
197+
log_entry = {
198+
'timestamp': datetime.now().isoformat(),
199+
'user_id': user_id,
200+
'message_type': 'user' if is_user else 'ai',
201+
'message': message,
202+
'response': response,
203+
'error': error,
204+
'ip_address': request.remote_addr,
205+
'user_agent': request.headers.get('User-Agent', 'Unknown'),
206+
# Add your new field here
207+
'new_field': 'new_value'
208+
}
209+
# ... rest of the function
210+
```
211+
212+
### Custom Log Formats
213+
You can modify the log format by changing the JSON structure in the `log_message()` function.
214+
215+
---
216+
217+
This logging system gives you complete visibility into who is using your chatbot and what questions they're asking. This is extremely helpful for improving AI response quality and understanding user needs!

backend/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@ chroma_db/
4747
Thumbs.db
4848

4949
# Logs
50-
*.log
50+
*.log
51+
logs/
52+
chat_logs_*.json

0 commit comments

Comments
 (0)