Created
July 30, 2025 07:37
-
-
Save ishaquehassan/075e43d6286e3c4f1635a0f9d3d104a3 to your computer and use it in GitHub Desktop.
Logging best configs for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| import logging.config | |
| # Configuration with separate console handlers for different formatters | |
| LOGGING_CONFIG = { | |
| 'version': 1, | |
| 'formatters': { | |
| 'detailed': { | |
| 'format': '%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s' | |
| }, | |
| 'simple': { | |
| 'format': '%(levelname)s - %(message)s' | |
| }, | |
| 'api_console': { | |
| 'format': '[API] %(levelname)s - %(message)s' | |
| }, | |
| 'db_console': { | |
| 'format': '[DATABASE] %(levelname)s - %(funcName)s:%(lineno)d - %(message)s' | |
| }, | |
| 'api_access': { | |
| 'format': '%(asctime)s - API - %(levelname)s - %(message)s' | |
| }, | |
| 'database': { | |
| 'format': '%(asctime)s - DB - %(levelname)s - [%(funcName)s:%(lineno)d] - %(message)s' | |
| } | |
| }, | |
| 'handlers': { | |
| 'api_console': { | |
| 'class': 'logging.StreamHandler', | |
| 'formatter': 'api_console', | |
| 'level': 'INFO' | |
| }, | |
| 'db_console': { | |
| 'class': 'logging.StreamHandler', | |
| 'formatter': 'db_console', | |
| 'level': 'INFO' | |
| }, | |
| 'debug_file': { | |
| 'class': 'logging.FileHandler', | |
| 'filename': 'debug.log', | |
| 'formatter': 'detailed', | |
| 'level': 'DEBUG' | |
| }, | |
| 'api_log': { | |
| 'class': 'logging.FileHandler', | |
| 'filename': 'api_access.log', | |
| 'formatter': 'api_access', | |
| 'level': 'INFO' | |
| }, | |
| 'db_log': { | |
| 'class': 'logging.FileHandler', | |
| 'filename': 'database.log', | |
| 'formatter': 'database', | |
| 'level': 'DEBUG' | |
| } | |
| }, | |
| 'loggers': { | |
| 'api': { | |
| 'handlers': ['api_console', 'api_log'], | |
| 'level': 'INFO', | |
| 'propagate': False | |
| }, | |
| 'database': { | |
| 'handlers': ['db_console', 'db_log'], | |
| 'level': 'DEBUG', | |
| 'propagate': False | |
| }, | |
| 'debug': { | |
| 'handlers': ['debug_file'], | |
| 'level': 'DEBUG', | |
| 'propagate': False | |
| } | |
| } | |
| } | |
| logging.config.dictConfig(LOGGING_CONFIG) | |
| # Different loggers for different purposes | |
| api_logger = logging.getLogger('api') | |
| db_logger = logging.getLogger('database') | |
| debug_logger = logging.getLogger('debug') | |
| def api_endpoint(user_id, action): | |
| api_logger.info(f"User {user_id} performed {action}") | |
| db_logger.debug(f"Querying database for user {user_id}") | |
| # Database operation | |
| db_logger.info(f"Database query completed for user {user_id}") | |
| debug_logger.debug(f"Internal state: processing {action} for {user_id}") | |
| # Test different loggers | |
| print("Testing different logger outputs:") | |
| api_endpoint("user123", "login") | |
| api_endpoint("user456", "logout") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment