function get_logger
A wrapper function that retrieves a logger instance from Python's logging module with the specified name.
/tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
68 - 78
simple
Purpose
This function provides a simple interface to obtain a logger instance from Python's standard logging module. It acts as a convenience wrapper around logging.getLogger(), allowing for consistent logger retrieval throughout an application. The function is typically used as part of a larger logging infrastructure where multiple modules need to create or access named loggers for organized log output.
Source Code
def get_logger(name: str) -> logging.Logger:
"""
Get a logger instance with the specified name.
Args:
name: Logger name
Returns:
Logger instance
"""
return logging.getLogger(name)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
name |
str | - | positional_or_keyword |
Parameter Details
name: A string identifier for the logger. This name is used to organize loggers in a hierarchy (e.g., 'myapp.module1'). Using dot-separated names creates a logger hierarchy where parent loggers can control child logger behavior. Common patterns include using __name__ to automatically name loggers after their module.
Return Value
Type: logging.Logger
Returns a logging.Logger instance associated with the specified name. If a logger with this name already exists, the same instance is returned (loggers are singletons per name). The returned Logger object can be used to emit log messages at various levels (debug, info, warning, error, critical) and can be configured with handlers, formatters, and filters.
Dependencies
logging
Required Imports
import logging
Usage Example
import logging
# Configure logging (optional but recommended)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Get a logger instance
logger = get_logger('my_application')
# Use the logger
logger.info('Application started')
logger.warning('This is a warning')
logger.error('An error occurred')
# Common pattern: use module name
logger = get_logger(__name__)
logger.debug('Debug message from current module')
Best Practices
- Use __name__ as the logger name to automatically name loggers after their module for better log organization
- Configure the logging system (using logging.basicConfig or custom configuration) before creating loggers to ensure proper output
- Create loggers at module level (outside functions) rather than repeatedly calling get_logger in functions to avoid overhead
- Use hierarchical logger names (e.g., 'app.module.submodule') to enable granular control over logging levels
- Avoid creating loggers with hardcoded names unless you have a specific reason; prefer __name__ for automatic naming
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function log_performance 49.9% similar
-
function get_model_class 46.7% similar
-
function log_info 46.6% similar
-
function log_debug 43.3% similar
-
function log_error 42.6% similar