mrradix commited on
Commit
1c0efba
·
verified ·
1 Parent(s): 4470ea4

Update utils/logging.py

Browse files
Files changed (1) hide show
  1. utils/logging.py +5 -233
utils/logging.py CHANGED
@@ -1,234 +1,4 @@
1
  import logging
2
- import os
3
- import sys
4
- from datetime import datetime
5
- from typing import Optional, Dict, Any
6
- import json
7
-
8
- # Configure logging
9
- def setup_logger(name: str = "mona_app", level: str = "INFO") -> logging.Logger:
10
- """
11
- Set up a logger with both file and console handlers
12
- """
13
- logger = logging.getLogger(name)
14
-
15
- # Clear existing handlers to avoid duplicates
16
- if logger.handlers:
17
- logger.handlers.clear()
18
-
19
- # Set log level
20
- log_level = getattr(logging, level.upper(), logging.INFO)
21
- logger.setLevel(log_level)
22
-
23
- # Create formatters
24
- detailed_formatter = logging.Formatter(
25
- '%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s'
26
- )
27
- simple_formatter = logging.Formatter('%(levelname)s - %(message)s')
28
-
29
- # Console handler
30
- console_handler = logging.StreamHandler(sys.stdout)
31
- console_handler.setLevel(logging.INFO)
32
- console_handler.setFormatter(simple_formatter)
33
-
34
- # File handler (if in a writable environment)
35
- try:
36
- log_dir = "/tmp/logs" if os.path.exists("/tmp") else "./logs"
37
- os.makedirs(log_dir, exist_ok=True)
38
-
39
- log_file = os.path.join(log_dir, f"{name}.log")
40
- file_handler = logging.FileHandler(log_file)
41
- file_handler.setLevel(logging.DEBUG)
42
- file_handler.setFormatter(detailed_formatter)
43
- logger.addHandler(file_handler)
44
- except Exception:
45
- # If we can't create file handler, continue with just console
46
- pass
47
-
48
- logger.addHandler(console_handler)
49
-
50
- return logger
51
-
52
- # Global logger instance
53
- _logger = None
54
-
55
- def get_logger() -> logging.Logger:
56
- """Get the global logger instance"""
57
- global _logger
58
- if _logger is None:
59
- _logger = setup_logger()
60
- return _logger
61
-
62
- def log_error(message: str, error: Exception = None) -> None:
63
- """Log error messages with optional exception details"""
64
- logger = get_logger()
65
- if error:
66
- logger.error(f"{message}: {str(error)}", exc_info=True)
67
- else:
68
- logger.error(message)
69
-
70
- def log_warning(message: str) -> None:
71
- """Log warning messages"""
72
- logger = get_logger()
73
- logger.warning(message)
74
-
75
- def log_info(message: str) -> None:
76
- """Log info messages"""
77
- logger = get_logger()
78
- logger.info(message)
79
-
80
- def log_debug(message: str) -> None:
81
- """Log debug messages"""
82
- logger = get_logger()
83
- logger.debug(message)
84
-
85
- def log_performance(operation: str, duration: float, additional_info: Dict[str, Any] = None) -> None:
86
- """Log performance metrics"""
87
- logger = get_logger()
88
- perf_data = {
89
- "operation": operation,
90
- "duration_seconds": round(duration, 4),
91
- "timestamp": datetime.now().isoformat()
92
- }
93
-
94
- if additional_info:
95
- perf_data.update(additional_info)
96
-
97
- logger.info(f"PERFORMANCE: {json.dumps(perf_data)}")
98
-
99
- def log_user_action(action: str, user_id: str = None, details: Dict[str, Any] = None) -> None:
100
- """Log user actions for analytics"""
101
- logger = get_logger()
102
- action_data = {
103
- "action": action,
104
- "user_id": user_id or "anonymous",
105
- "timestamp": datetime.now().isoformat()
106
- }
107
-
108
- if details:
109
- action_data.update(details)
110
-
111
- logger.info(f"USER_ACTION: {json.dumps(action_data)}")
112
-
113
- def log_data_operation(operation: str, table_name: str = None, record_count: int = None, success: bool = True) -> None:
114
- """Log database/data operations"""
115
- logger = get_logger()
116
- data_op = {
117
- "operation": operation,
118
- "table": table_name,
119
- "record_count": record_count,
120
- "success": success,
121
- "timestamp": datetime.now().isoformat()
122
- }
123
-
124
- level = logging.INFO if success else logging.ERROR
125
- logger.log(level, f"DATA_OP: {json.dumps(data_op)}")
126
-
127
- def log_api_call(endpoint: str, method: str = "GET", status_code: int = None, duration: float = None) -> None:
128
- """Log API calls and responses"""
129
- logger = get_logger()
130
- api_data = {
131
- "endpoint": endpoint,
132
- "method": method,
133
- "status_code": status_code,
134
- "duration_seconds": round(duration, 4) if duration else None,
135
- "timestamp": datetime.now().isoformat()
136
- }
137
-
138
- logger.info(f"API_CALL: {json.dumps(api_data)}")
139
-
140
- def log_security_event(event_type: str, details: Dict[str, Any] = None, severity: str = "INFO") -> None:
141
- """Log security-related events"""
142
- logger = get_logger()
143
- security_data = {
144
- "event_type": event_type,
145
- "severity": severity,
146
- "timestamp": datetime.now().isoformat()
147
- }
148
-
149
- if details:
150
- security_data.update(details)
151
-
152
- level = getattr(logging, severity.upper(), logging.INFO)
153
- logger.log(level, f"SECURITY: {json.dumps(security_data)}")
154
-
155
- def log_system_health(component: str, status: str, metrics: Dict[str, Any] = None) -> None:
156
- """Log system health checks"""
157
- logger = get_logger()
158
- health_data = {
159
- "component": component,
160
- "status": status,
161
- "timestamp": datetime.now().isoformat()
162
- }
163
-
164
- if metrics:
165
- health_data.update(metrics)
166
-
167
- level = logging.INFO if status == "healthy" else logging.WARNING
168
- logger.log(level, f"HEALTH: {json.dumps(health_data)}")
169
-
170
- def log_ai_model_usage(model_name: str, operation: str, tokens_used: Optional[int] = None) -> None:
171
- """Log AI model usage for monitoring and billing"""
172
- logger = get_logger()
173
- ai_data = {
174
- "model": model_name,
175
- "operation": operation,
176
- "tokens_used": tokens_used,
177
- "timestamp": datetime.now().isoformat()
178
- }
179
-
180
- logger.info(f"AI_USAGE: {json.dumps(ai_data)}")
181
-
182
- def log_business_metric(metric_name: str, value: float, unit: str = None, tags: Dict[str, str] = None) -> None:
183
- """Log business metrics for analytics"""
184
- logger = get_logger()
185
- metric_data = {
186
- "metric": metric_name,
187
- "value": value,
188
- "unit": unit,
189
- "timestamp": datetime.now().isoformat()
190
- }
191
-
192
- if tags:
193
- metric_data["tags"] = tags
194
-
195
- logger.info(f"METRIC: {json.dumps(metric_data)}")
196
-
197
- # Context manager for performance logging
198
- class PerformanceLogger:
199
- def __init__(self, operation: str, additional_info: Dict[str, Any] = None):
200
- self.operation = operation
201
- self.additional_info = additional_info or {}
202
- self.start_time = None
203
-
204
- def __enter__(self):
205
- self.start_time = datetime.now()
206
- return self
207
-
208
- def __exit__(self, exc_type, exc_val, exc_tb):
209
- if self.start_time:
210
- duration = (datetime.now() - self.start_time).total_seconds()
211
- success = exc_type is None
212
-
213
- perf_info = self.additional_info.copy()
214
- perf_info["success"] = success
215
-
216
- if not success and exc_val:
217
- perf_info["error"] = str(exc_val)
218
-
219
- log_performance(self.operation, duration, perf_info)
220
-
221
- # Decorator for automatic performance logging
222
- def log_performance_decorator(operation_name: str = None):
223
- def decorator(func):
224
- def wrapper(*args, **kwargs):
225
- op_name = operation_name or f"{func.__module__}.{func.__name__}"
226
- with PerformanceLogger(op_name):
227
- return func(*args, **kwargs)
228
- return wrapper
229
- return decorator
230
- # utils/logging.py
231
- import logging
232
  import sys
233
  from datetime import datetime
234
 
@@ -255,7 +25,7 @@ def get_logger(name=None):
255
 
256
  # Create formatter
257
  formatter = logging.Formatter(
258
- '%(levelname)s - %(name)s - %(message)s'
259
  )
260
  handler.setFormatter(formatter)
261
 
@@ -282,5 +52,7 @@ def log_error(logger, error, context=""):
282
  # Initialize logging system
283
  print(f"===== Application Startup at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
284
  print("INFO - Logging system initialized")
285
- # Initialize logger on module import
286
- get_logger().info("Logging system initialized")
 
 
 
1
  import logging
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import sys
3
  from datetime import datetime
4
 
 
25
 
26
  # Create formatter
27
  formatter = logging.Formatter(
28
+ 'INFO - %(name)s - %(message)s'
29
  )
30
  handler.setFormatter(formatter)
31
 
 
52
  # Initialize logging system
53
  print(f"===== Application Startup at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
54
  print("INFO - Logging system initialized")
55
+
56
+ # Create a default logger for the module
57
+ logger = get_logger(__name__)
58
+ logger.info("Logging system initialized")