-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
logging_setup.py
84 lines (74 loc) · 2.41 KB
/
logging_setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Logging setup for UglyFeed
"""
import logging
import logging.config
# Define a logging configuration using a dictionary.
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
},
},
'handlers': {
'console_info': {
'class': 'logging.StreamHandler',
'level': 'INFO',
'formatter': 'standard',
'stream': 'ext://sys.stdout',
},
'console_error': {
'class': 'logging.StreamHandler',
'level': 'WARNING',
'formatter': 'standard',
'stream': 'ext://sys.stderr',
},
# Optional: Add a file handler if you want to log to a file
'file': {
'class': 'logging.FileHandler',
'level': 'DEBUG',
'formatter': 'standard',
'filename': 'uglyfeed.log',
'mode': 'a',
},
},
'loggers': {
'__main__': {
'handlers': ['console_info', 'console_error'], # Add 'file' if you want file logging
'level': 'DEBUG',
'propagate': False,
},
},
'root': {
'handlers': ['console_info', 'console_error'], # Add 'file' if you want file logging
'level': 'DEBUG',
},
}
def setup_logging() -> logging.Logger:
"""Set up logging configuration and return the root logger."""
try:
logging.config.dictConfig(LOGGING_CONFIG)
# Create the logger
logger = logging.getLogger(__name__)
# Log a diagnostics message to confirm successful setup
logger.debug("Logging setup complete and operational.")
return logger
except Exception as e:
print(f"Failed to configure logging: {e}")
raise
def get_logger(name: str) -> logging.Logger:
"""Get a logger with the specified name."""
return logging.getLogger(name)
# Usage example:
if __name__ == "__main__":
logger = setup_logging()
logger.debug("This is a debug message")
logger.info("This is an info message")
logger.warning("This is a warning message")
logger.error("This is an error message")
logger.critical("This is a critical message")
# Additional logger for a different module or context
another_logger = get_logger('another_module')
another_logger.info("Logging from another module")