{"library":"logging","title":"Python Standard Library logging module","description":"The `logging` module is a robust and flexible event logging system for Python applications and libraries, integrated into the Python Standard Library since version 2.3. It enables all Python modules to participate in logging, facilitating the integration of messages from applications and third-party modules into a unified log. The module provides extensive functionality to produce structured log messages and direct them to various destinations such as the console, files, or network sockets.","language":"python","status":"active","last_verified":"Sat May 16","install":{"commands":[],"cli":null},"imports":["import logging","from logging import getLogger","from logging import basicConfig"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import logging\nimport os\n\n# Basic configuration for quick scripts (logs to console, INFO level and above)\nlogging.basicConfig(\n    level=logging.INFO,\n    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'\n)\n\n# Recommended for applications: get a named logger\nlogger = logging.getLogger(__name__)\n\n# Configure a file handler for a more complex setup\nlog_file_path = os.environ.get('LOG_FILE', 'application.log')\nfile_handler = logging.FileHandler(log_file_path)\nfile_handler.setLevel(logging.DEBUG)\nformatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)')\nfile_handler.setFormatter(formatter)\nlogger.addHandler(file_handler)\n\n# Prevent messages from propagating to the root logger handlers (often console) if file is primary\nlogger.propagate = False\n\n# Example log messages\nlogger.debug('This is a debug message - will only go to file handler.')\nlogger.info('This is an info message.')\nlogger.warning('This is a warning message!')\nlogger.error('This is an error message!')\nlogger.critical('This is a critical message!', exc_info=True)\n\nprint(f\"\\nCheck '{log_file_path}' for detailed logs.\")","lang":"python","description":"This quickstart demonstrates both basic `basicConfig` usage for simple scripts and a more robust application-level setup using a named logger with a `FileHandler` and custom formatter. It also shows how to prevent propagation to avoid duplicate messages.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}