Spaces:
Building
Building
Update logger.py
Browse files
logger.py
CHANGED
@@ -6,9 +6,10 @@ import logging
|
|
6 |
import json
|
7 |
import os
|
8 |
import threading
|
|
|
9 |
from datetime import datetime
|
10 |
from enum import Enum
|
11 |
-
from typing import Optional, Dict, Any
|
12 |
from pathlib import Path
|
13 |
|
14 |
class LogLevel(Enum):
|
@@ -143,8 +144,44 @@ def log_warning(message: str, **kwargs):
|
|
143 |
"""Log warning message"""
|
144 |
logger.warning(message, **kwargs)
|
145 |
|
146 |
-
def log_error(message: str, **kwargs):
|
147 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
148 |
logger.error(message, **kwargs)
|
149 |
|
150 |
def log_critical(message: str, **kwargs):
|
|
|
6 |
import json
|
7 |
import os
|
8 |
import threading
|
9 |
+
import traceback
|
10 |
from datetime import datetime
|
11 |
from enum import Enum
|
12 |
+
from typing import Optional, Dict, Any, Union
|
13 |
from pathlib import Path
|
14 |
|
15 |
class LogLevel(Enum):
|
|
|
144 |
"""Log warning message"""
|
145 |
logger.warning(message, **kwargs)
|
146 |
|
147 |
+
def log_error(message: str, exception: Optional[Exception] = None, **kwargs):
|
148 |
+
"""
|
149 |
+
Log error message with optional exception
|
150 |
+
|
151 |
+
Usage:
|
152 |
+
log_error("Error occurred")
|
153 |
+
log_error("Error occurred", e) # Otomatik olarak str(e) ve traceback ekler
|
154 |
+
log_error("Error occurred", error="custom error")
|
155 |
+
log_error("Error occurred", e, extra_field="value")
|
156 |
+
"""
|
157 |
+
import traceback
|
158 |
+
|
159 |
+
# Eğer exception parametresi verilmişse, otomatik olarak error ve traceback ekle
|
160 |
+
if exception is not None:
|
161 |
+
# Eğer kwargs'da error yoksa, exception'dan al
|
162 |
+
if 'error' not in kwargs:
|
163 |
+
kwargs['error'] = str(exception)
|
164 |
+
|
165 |
+
# Exception tipini ekle
|
166 |
+
if 'error_type' not in kwargs:
|
167 |
+
kwargs['error_type'] = type(exception).__name__
|
168 |
+
|
169 |
+
# Eğer kwargs'da traceback yoksa ve bu bir Exception ise, traceback ekle
|
170 |
+
if 'traceback' not in kwargs and isinstance(exception, Exception):
|
171 |
+
kwargs['traceback'] = traceback.format_exc()
|
172 |
+
|
173 |
+
# Özel exception tipleri için ekstra bilgi
|
174 |
+
if hasattr(exception, '__dict__'):
|
175 |
+
# Custom exception'ların attribute'larını ekle
|
176 |
+
for attr, value in exception.__dict__.items():
|
177 |
+
if not attr.startswith('_') and attr not in kwargs:
|
178 |
+
kwargs[f'exc_{attr}'] = value
|
179 |
+
|
180 |
+
# HTTP status code varsa ekle
|
181 |
+
if hasattr(exception, 'status_code') and 'status_code' not in kwargs:
|
182 |
+
kwargs['status_code'] = exception.status_code
|
183 |
+
|
184 |
+
# Orijinal logger'a gönder
|
185 |
logger.error(message, **kwargs)
|
186 |
|
187 |
def log_critical(message: str, **kwargs):
|