Spaces:
Sleeping
Sleeping
Michael Hu
commited on
Commit
·
95687b7
1
Parent(s):
b6a5e98
fix logging issue
Browse files
src/application/error_handling/structured_logger.py
CHANGED
|
@@ -124,21 +124,24 @@ class StructuredLogger:
|
|
| 124 |
"""Log debug message."""
|
| 125 |
if self.logger.isEnabledFor(logging.DEBUG):
|
| 126 |
log_data = self._get_log_data(message, LogLevel.DEBUG.value, context, extra)
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
def info(self, message: str, context: Optional[LogContext] = None,
|
| 130 |
extra: Optional[Dict[str, Any]] = None) -> None:
|
| 131 |
"""Log info message."""
|
| 132 |
if self.logger.isEnabledFor(logging.INFO):
|
| 133 |
log_data = self._get_log_data(message, LogLevel.INFO.value, context, extra)
|
| 134 |
-
|
|
|
|
| 135 |
|
| 136 |
def warning(self, message: str, context: Optional[LogContext] = None,
|
| 137 |
extra: Optional[Dict[str, Any]] = None) -> None:
|
| 138 |
"""Log warning message."""
|
| 139 |
if self.logger.isEnabledFor(logging.WARNING):
|
| 140 |
log_data = self._get_log_data(message, LogLevel.WARNING.value, context, extra)
|
| 141 |
-
|
|
|
|
| 142 |
|
| 143 |
def error(self, message: str, context: Optional[LogContext] = None,
|
| 144 |
extra: Optional[Dict[str, Any]] = None,
|
|
@@ -146,7 +149,8 @@ class StructuredLogger:
|
|
| 146 |
"""Log error message."""
|
| 147 |
if self.logger.isEnabledFor(logging.ERROR):
|
| 148 |
log_data = self._get_log_data(message, LogLevel.ERROR.value, context, extra, exception)
|
| 149 |
-
|
|
|
|
| 150 |
|
| 151 |
def critical(self, message: str, context: Optional[LogContext] = None,
|
| 152 |
extra: Optional[Dict[str, Any]] = None,
|
|
@@ -154,7 +158,8 @@ class StructuredLogger:
|
|
| 154 |
"""Log critical message."""
|
| 155 |
if self.logger.isEnabledFor(logging.CRITICAL):
|
| 156 |
log_data = self._get_log_data(message, LogLevel.CRITICAL.value, context, extra, exception)
|
| 157 |
-
|
|
|
|
| 158 |
|
| 159 |
def log_operation_start(self, operation: str, context: Optional[LogContext] = None,
|
| 160 |
extra: Optional[Dict[str, Any]] = None) -> str:
|
|
@@ -256,7 +261,7 @@ class JsonFormatter(logging.Formatter):
|
|
| 256 |
"""Format log record as JSON."""
|
| 257 |
try:
|
| 258 |
# Get structured data from extra
|
| 259 |
-
log_data = getattr(record, '
|
| 260 |
|
| 261 |
# Ensure basic fields are present
|
| 262 |
if 'timestamp' not in log_data:
|
|
@@ -300,16 +305,16 @@ class ContextFormatter(logging.Formatter):
|
|
| 300 |
# Get correlation ID from context or record
|
| 301 |
correlation_id = correlation_id_context.get()
|
| 302 |
if not correlation_id:
|
| 303 |
-
|
| 304 |
-
correlation_id =
|
| 305 |
|
| 306 |
# Add correlation ID to record
|
| 307 |
record.correlation_id = correlation_id
|
| 308 |
|
| 309 |
# Add context information if available
|
| 310 |
-
|
| 311 |
-
if 'operation' in
|
| 312 |
-
record.message = f"[{
|
| 313 |
|
| 314 |
return super().format(record)
|
| 315 |
|
|
|
|
| 124 |
"""Log debug message."""
|
| 125 |
if self.logger.isEnabledFor(logging.DEBUG):
|
| 126 |
log_data = self._get_log_data(message, LogLevel.DEBUG.value, context, extra)
|
| 127 |
+
# Use 'structured_data' to avoid conflicts with LogRecord attributes
|
| 128 |
+
self.logger.debug(message, extra={'structured_data': log_data})
|
| 129 |
|
| 130 |
def info(self, message: str, context: Optional[LogContext] = None,
|
| 131 |
extra: Optional[Dict[str, Any]] = None) -> None:
|
| 132 |
"""Log info message."""
|
| 133 |
if self.logger.isEnabledFor(logging.INFO):
|
| 134 |
log_data = self._get_log_data(message, LogLevel.INFO.value, context, extra)
|
| 135 |
+
# Use 'structured_data' to avoid conflicts with LogRecord attributes
|
| 136 |
+
self.logger.info(message, extra={'structured_data': log_data})
|
| 137 |
|
| 138 |
def warning(self, message: str, context: Optional[LogContext] = None,
|
| 139 |
extra: Optional[Dict[str, Any]] = None) -> None:
|
| 140 |
"""Log warning message."""
|
| 141 |
if self.logger.isEnabledFor(logging.WARNING):
|
| 142 |
log_data = self._get_log_data(message, LogLevel.WARNING.value, context, extra)
|
| 143 |
+
# Use 'structured_data' to avoid conflicts with LogRecord attributes
|
| 144 |
+
self.logger.warning(message, extra={'structured_data': log_data})
|
| 145 |
|
| 146 |
def error(self, message: str, context: Optional[LogContext] = None,
|
| 147 |
extra: Optional[Dict[str, Any]] = None,
|
|
|
|
| 149 |
"""Log error message."""
|
| 150 |
if self.logger.isEnabledFor(logging.ERROR):
|
| 151 |
log_data = self._get_log_data(message, LogLevel.ERROR.value, context, extra, exception)
|
| 152 |
+
# Use 'structured_data' to avoid conflicts with LogRecord attributes
|
| 153 |
+
self.logger.error(message, extra={'structured_data': log_data})
|
| 154 |
|
| 155 |
def critical(self, message: str, context: Optional[LogContext] = None,
|
| 156 |
extra: Optional[Dict[str, Any]] = None,
|
|
|
|
| 158 |
"""Log critical message."""
|
| 159 |
if self.logger.isEnabledFor(logging.CRITICAL):
|
| 160 |
log_data = self._get_log_data(message, LogLevel.CRITICAL.value, context, extra, exception)
|
| 161 |
+
# Use 'structured_data' to avoid conflicts with LogRecord attributes
|
| 162 |
+
self.logger.critical(message, extra={'structured_data': log_data})
|
| 163 |
|
| 164 |
def log_operation_start(self, operation: str, context: Optional[LogContext] = None,
|
| 165 |
extra: Optional[Dict[str, Any]] = None) -> str:
|
|
|
|
| 261 |
"""Format log record as JSON."""
|
| 262 |
try:
|
| 263 |
# Get structured data from extra
|
| 264 |
+
log_data = getattr(record, 'structured_data', {})
|
| 265 |
|
| 266 |
# Ensure basic fields are present
|
| 267 |
if 'timestamp' not in log_data:
|
|
|
|
| 305 |
# Get correlation ID from context or record
|
| 306 |
correlation_id = correlation_id_context.get()
|
| 307 |
if not correlation_id:
|
| 308 |
+
structured_data = getattr(record, 'structured_data', {})
|
| 309 |
+
correlation_id = structured_data.get('correlation_id', 'unknown')
|
| 310 |
|
| 311 |
# Add correlation ID to record
|
| 312 |
record.correlation_id = correlation_id
|
| 313 |
|
| 314 |
# Add context information if available
|
| 315 |
+
structured_data = getattr(record, 'structured_data', {})
|
| 316 |
+
if 'operation' in structured_data:
|
| 317 |
+
record.message = f"[{structured_data['operation']}] {record.getMessage()}"
|
| 318 |
|
| 319 |
return super().format(record)
|
| 320 |
|