Update utils/logging.py
Browse files- utils/logging.py +39 -38
utils/logging.py
CHANGED
@@ -1,55 +1,56 @@
|
|
1 |
import logging
|
2 |
import sys
|
3 |
-
import traceback
|
4 |
from datetime import datetime
|
5 |
-
import
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
logging.basicConfig(
|
14 |
-
level=
|
15 |
-
format=
|
16 |
handlers=[
|
17 |
logging.StreamHandler(sys.stdout),
|
18 |
-
logging.FileHandler(f'logs/app_{datetime.now().strftime("%Y%m%d")}.log', mode='a')
|
19 |
]
|
20 |
)
|
21 |
-
|
22 |
-
logger = logging.getLogger('mona_app')
|
23 |
-
logger.info("Logging setup completed successfully")
|
24 |
-
return logger
|
25 |
|
26 |
-
def get_logger(name
|
27 |
"""Get a logger instance"""
|
28 |
-
if name is None:
|
29 |
-
name = 'mona_app'
|
30 |
return logging.getLogger(name)
|
31 |
|
32 |
-
def log_error(
|
33 |
-
"""Log error with
|
34 |
-
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
logger.error(f"{message}: {str(error)}")
|
38 |
-
logger.error(f"Traceback: {traceback.format_exc()}")
|
39 |
-
else:
|
40 |
-
logger.error(message)
|
41 |
|
42 |
-
def log_info(
|
43 |
-
"""Log info
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
def log_warning(
|
48 |
-
"""Log warning
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
51 |
|
52 |
-
def log_debug(
|
53 |
-
"""Log debug
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
1 |
import logging
|
2 |
import sys
|
|
|
3 |
from datetime import datetime
|
4 |
+
from pathlib import Path
|
5 |
|
6 |
+
# Global logger configuration
|
7 |
+
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
|
8 |
+
LOG_LEVEL = logging.INFO
|
9 |
+
|
10 |
+
def setup_logging(log_level=LOG_LEVEL):
|
11 |
+
"""Setup logging configuration"""
|
12 |
logging.basicConfig(
|
13 |
+
level=log_level,
|
14 |
+
format=LOG_FORMAT,
|
15 |
handlers=[
|
16 |
logging.StreamHandler(sys.stdout),
|
|
|
17 |
]
|
18 |
)
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def get_logger(name):
|
21 |
"""Get a logger instance"""
|
|
|
|
|
22 |
return logging.getLogger(name)
|
23 |
|
24 |
+
def log_error(logger, error_msg, exception=None, context=None):
|
25 |
+
"""Log error with additional context"""
|
26 |
+
full_msg = f"{error_msg}"
|
27 |
+
if context:
|
28 |
+
full_msg += f" | Context: {context}"
|
29 |
+
if exception:
|
30 |
+
full_msg += f" | Exception: {str(exception)}"
|
31 |
|
32 |
+
logger.error(full_msg)
|
|
|
|
|
|
|
|
|
33 |
|
34 |
+
def log_info(logger, msg, context=None):
|
35 |
+
"""Log info with additional context"""
|
36 |
+
full_msg = f"{msg}"
|
37 |
+
if context:
|
38 |
+
full_msg += f" | Context: {context}"
|
39 |
+
|
40 |
+
logger.info(full_msg)
|
41 |
|
42 |
+
def log_warning(logger, msg, context=None):
|
43 |
+
"""Log warning with additional context"""
|
44 |
+
full_msg = f"{msg}"
|
45 |
+
if context:
|
46 |
+
full_msg += f" | Context: {context}"
|
47 |
+
|
48 |
+
logger.warning(full_msg)
|
49 |
|
50 |
+
def log_debug(logger, msg, context=None):
|
51 |
+
"""Log debug with additional context"""
|
52 |
+
full_msg = f"{msg}"
|
53 |
+
if context:
|
54 |
+
full_msg += f" | Context: {context}"
|
55 |
+
|
56 |
+
logger.debug(full_msg)
|