mrradix commited on
Commit
53ccced
·
verified ·
1 Parent(s): acc0243

Update utils/logging.py

Browse files
Files changed (1) hide show
  1. 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 os
6
 
7
- def setup_logging():
8
- """Setup logging configuration for the application"""
9
- # Create logs directory if it doesn't exist
10
- os.makedirs('logs', exist_ok=True)
11
-
12
- # Configure logging
13
  logging.basicConfig(
14
- level=logging.INFO,
15
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
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=None):
27
  """Get a logger instance"""
28
- if name is None:
29
- name = 'mona_app'
30
  return logging.getLogger(name)
31
 
32
- def log_error(message, error=None, logger_name=None):
33
- """Log error with optional traceback"""
34
- logger = get_logger(logger_name)
 
 
 
 
35
 
36
- if error:
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(message, logger_name=None):
43
- """Log info message"""
44
- logger = get_logger(logger_name)
45
- logger.info(message)
 
 
 
46
 
47
- def log_warning(message, logger_name=None):
48
- """Log warning message"""
49
- logger = get_logger(logger_name)
50
- logger.warning(message)
 
 
 
51
 
52
- def log_debug(message, logger_name=None):
53
- """Log debug message"""
54
- logger = get_logger(logger_name)
55
- logger.debug(message)
 
 
 
 
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)