-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
362 lines (303 loc) · 13.3 KB
/
logger.py
File metadata and controls
362 lines (303 loc) · 13.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import logging
import json
import os
import hashlib
from datetime import datetime
from typing import Dict, Any, Optional
import streamlit as st
# Optional GCP imports - will fallback gracefully if not available
try:
from google.cloud import logging as gcp_logging
from google.auth import default
GCP_LOGGING_AVAILABLE = True
except ImportError:
GCP_LOGGING_AVAILABLE = False
class UserActionLogger:
"""
User Action Logger for WorshipFlow
用户操作日志记录器
This class logs user actions with session tracking for analytics and debugging.
该类记录用户操作并进行会话跟踪,用于分析和调试。
"""
def __init__(self, log_dir: str = "logs"):
"""
Initialize the user action logger
初始化用户操作日志记录器
Args:
log_dir: Directory to store log files / 存储日志文件的目录
"""
self.log_dir = log_dir
self.use_cloud_logging = self._should_use_cloud_logging()
self.cloud_logger = None
self.setup_logging()
def _should_use_cloud_logging(self) -> bool:
"""
Determine if Cloud Logging should be used
判断是否应该使用Cloud Logging
Returns:
bool: True if should use Cloud Logging / 是否使用Cloud Logging
"""
# Check if running in GCP environment
is_gcp = os.getenv('GOOGLE_CLOUD_PROJECT') is not None
# Check if force local storage (development mode)
force_local = os.getenv('FORCE_LOCAL_STORAGE', 'false').lower() == 'true'
# Check if Cloud Logging is explicitly disabled
cloud_logging_disabled = os.getenv('DISABLE_CLOUD_LOGGING', 'false').lower() == 'true'
return (GCP_LOGGING_AVAILABLE and is_gcp and
not force_local and not cloud_logging_disabled)
def setup_logging(self):
"""Setup logging configuration / 设置日志配置"""
# Setup local logging
os.makedirs(self.log_dir, exist_ok=True)
# Create logger
self.logger = logging.getLogger('worship_flow_user_actions')
self.logger.setLevel(logging.INFO)
# Clear existing handlers to avoid duplicates
self.logger.handlers.clear()
# Setup Cloud Logging if enabled
if self.use_cloud_logging:
self._setup_cloud_logging()
# Always setup local file logging as backup
self._setup_file_logging()
def _setup_cloud_logging(self):
"""Setup Google Cloud Logging / 设置Google Cloud Logging"""
try:
# Initialize Cloud Logging client
client = gcp_logging.Client()
# Create a Cloud Logging handler
cloud_handler = client.get_default_handler()
cloud_handler.setLevel(logging.INFO)
# Add structured logging format
cloud_formatter = logging.Formatter('%(message)s')
cloud_handler.setFormatter(cloud_formatter)
self.logger.addHandler(cloud_handler)
self.cloud_logger = client.logger('worship-flow-user-actions')
print("✅ Cloud Logging initialized successfully")
except Exception as e:
print(f"❌ Cloud Logging setup failed: {e}")
print("Falling back to local file logging only")
self.use_cloud_logging = False
def _setup_file_logging(self):
"""Setup local file logging / 设置本地文件日志"""
# File handler for daily logs
log_filename = os.path.join(
self.log_dir,
f"user_actions_{datetime.now().strftime('%Y-%m-%d')}.log"
)
file_handler = logging.FileHandler(log_filename, encoding='utf-8')
file_handler.setLevel(logging.INFO)
# JSON formatter for structured logs
formatter = logging.Formatter(
'%(asctime)s - %(levelname)s - %(message)s'
)
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
def get_user_id(self) -> str:
"""
Generate or retrieve user ID for session tracking
生成或获取用户ID用于会话跟踪
Returns:
str: Unique user session ID / 唯一用户会话ID
"""
if 'user_session_id' not in st.session_state:
try:
# Try to get session ID from Streamlit runtime
runtime = st.runtime.get_instance()
session_id = getattr(runtime, 'session_id', None)
if session_id is None:
# Try alternative methods to get session info
try:
from streamlit.runtime.scriptrunner import get_script_run_ctx
ctx = get_script_run_ctx()
session_id = ctx.session_id if ctx else None
except (ImportError, AttributeError):
session_id = None
# Fallback: generate based on timestamp and random info
if session_id is None:
import uuid
session_id = str(uuid.uuid4())[:8]
# Generate unique session ID based on session info and timestamp
session_info = f"{session_id}_{datetime.now().isoformat()}"
user_id = hashlib.md5(session_info.encode()).hexdigest()[:12]
st.session_state.user_session_id = user_id
except Exception:
# Final fallback: use timestamp-based ID
import uuid
fallback_id = str(uuid.uuid4())[:12]
st.session_state.user_session_id = fallback_id
return st.session_state.user_session_id
def get_user_info(self) -> Dict[str, Any]:
"""
Get user context information
获取用户上下文信息
Returns:
Dict: User context data / 用户上下文数据
"""
# Get basic info
user_id = self.get_user_id()
timestamp = datetime.now().isoformat()
page = st.session_state.get('page', 'unknown')
# Try to get session and client info
session_id = "unknown"
client_ip = "unknown"
user_agent = "unknown"
try:
# Try to get session ID using different methods
runtime = st.runtime.get_instance()
session_id = getattr(runtime, 'session_id', None)
if session_id is None:
try:
from streamlit.runtime.scriptrunner import get_script_run_ctx
ctx = get_script_run_ctx()
session_id = getattr(ctx, 'session_id', 'unknown') if ctx else 'unknown'
except (ImportError, AttributeError):
pass
# Try to get session context for client info
try:
session_ctx = getattr(runtime, 'get_current_session', lambda: None)()
if session_ctx:
client_ip = getattr(session_ctx, 'client_ip', 'unknown')
user_agent = getattr(session_ctx, 'user_agent', 'unknown')
else:
# Try alternative method
from streamlit.runtime.scriptrunner import get_script_run_ctx
script_ctx = get_script_run_ctx()
if script_ctx and hasattr(script_ctx, 'session_state'):
# Extract from request headers if available
pass
except (ImportError, AttributeError, Exception):
pass
except Exception:
# Use fallback values
pass
return {
"user_id": user_id,
"session_id": str(session_id) if session_id else "unknown",
"client_ip": client_ip,
"user_agent": user_agent,
"timestamp": timestamp,
"page": page
}
def log_action(self, action: str, details: Optional[Dict[str, Any]] = None,
category: str = "general"):
"""
Log user action with context
记录用户操作及上下文
Args:
action: Action description / 操作描述
details: Additional action details / 额外操作详情
category: Action category / 操作类别
"""
# Check if logging is enabled
if os.getenv('ENABLE_USER_LOGGING', 'true').lower() != 'true':
return
try:
log_entry = {
"category": category,
"action": action,
"details": details or {},
**self.get_user_info()
}
# Log to local file (JSON format for compatibility)
self.logger.info(json.dumps(log_entry, ensure_ascii=False))
# Also log to Cloud Logging with structured data
if self.use_cloud_logging and self.cloud_logger:
self._log_to_cloud(log_entry)
except Exception as e:
# Fallback logging in case of errors
self.logger.error(f"Failed to log action '{action}': {str(e)}")
def _log_to_cloud(self, log_entry: Dict[str, Any]):
"""
Log structured data to Google Cloud Logging
将结构化数据记录到Google Cloud Logging
Args:
log_entry: Log entry data / 日志条目数据
"""
try:
# Add severity and labels for better Cloud Logging integration
severity = 'INFO'
if log_entry.get('category') == 'error':
severity = 'ERROR'
elif log_entry.get('category') == 'ai_generation' and not log_entry.get('details', {}).get('success', True):
severity = 'WARNING'
# Structure the log for Cloud Logging
structured_log = {
**log_entry,
'severity': severity,
'service': 'worship-flow',
'version': '1.0'
}
# Send to Cloud Logging
self.cloud_logger.log_struct(
structured_log,
severity=severity,
labels={
'service': 'worship-flow',
'category': log_entry.get('category', 'general'),
'user_id': log_entry.get('user_id', 'unknown')
}
)
except Exception as e:
# Don't fail the main logging if Cloud Logging fails
print(f"Cloud Logging failed: {e}")
def log_page_visit(self, page_name: str):
"""Log page visit / 记录页面访问"""
self.log_action(
action="page_visit",
details={"page_name": page_name},
category="navigation"
)
def log_song_action(self, action: str, song_data: Dict[str, Any]):
"""Log song-related actions / 记录诗歌相关操作"""
self.log_action(
action=action,
details={
"song_title": song_data.get("title", "unknown"),
"song_author": song_data.get("author", "unknown"),
"song_tags": song_data.get("tags", []),
"song_key": song_data.get("key", "unknown")
},
category="song_management"
)
def log_flow_action(self, action: str, flow_data: Dict[str, Any]):
"""Log worship flow actions / 记录敬拜流程操作"""
self.log_action(
action=action,
details={
"sermon_title": flow_data.get("sermon_title", "unknown"),
"key_scripture": flow_data.get("key_scripture", "unknown"),
"date": flow_data.get("date", "unknown"),
"song_count": len(flow_data.get("worship_flow", [])),
"has_transitions": any(
item.get("type") == "transition_text"
for item in flow_data.get("worship_flow", [])
)
},
category="flow_design"
)
def log_ai_action(self, action: str, model_name: str, prompt_type: str,
success: bool, details: Optional[Dict[str, Any]] = None):
"""Log AI-related actions / 记录AI相关操作"""
self.log_action(
action=action,
details={
"model_name": model_name,
"prompt_type": prompt_type,
"success": success,
"additional_details": details or {}
},
category="ai_generation"
)
def log_error(self, error_type: str, error_message: str, context: Optional[Dict[str, Any]] = None):
"""Log application errors / 记录应用错误"""
self.log_action(
action="error_occurred",
details={
"error_type": error_type,
"error_message": error_message,
"context": context or {}
},
category="error"
)
# Global logger instance
user_logger = UserActionLogger()