🔍 Code Extractor

function get_logger

Maturity: 58

Factory function that returns a configured logger instance, automatically initializing the logging system if not already set up.

File:
/tf/active/vicechatdev/invoice_extraction/utils/logging_utils.py
Lines:
270 - 288
Complexity:
simple

Purpose

This function serves as the primary entry point for obtaining logger instances throughout an invoice extraction application. It ensures that the logging system is properly initialized before returning a named logger, providing a consistent logging interface across all modules. The function checks if the root logger has handlers configured, and if not, initializes the logging system using the InvoiceExtractionLogger class with optional configuration parameters.

Source Code

def get_logger(name: str, config: Optional[Dict[str, Any]] = None) -> logging.Logger:
    """
    Get a configured logger instance.
    
    This is the main function for other modules to get properly configured loggers.
    
    Args:
        name: Logger name (typically __name__)
        config: Optional configuration dictionary
        
    Returns:
        Configured logger instance
    """
    # Set up logging if not already done
    if not logging.getLogger().handlers:
        InvoiceExtractionLogger(config)
    
    # Return the named logger
    return logging.getLogger(name)

Parameters

Name Type Default Kind
name str - positional_or_keyword
config Optional[Dict[str, Any]] None positional_or_keyword

Parameter Details

name: The name for the logger instance, typically set to __name__ to identify the module requesting the logger. This allows for hierarchical logger organization and filtering based on module names.

config: Optional dictionary containing logging configuration settings. Can include settings like log level, output format, file paths, etc. If None, default configuration from InvoiceExtractionLogger will be used. The structure depends on InvoiceExtractionLogger's expected configuration format.

Return Value

Type: logging.Logger

Returns a logging.Logger instance configured with the specified name. This logger inherits configuration from the root logger setup by InvoiceExtractionLogger and can be used to emit log messages at various levels (debug, info, warning, error, critical). The logger is ready to use immediately upon return.

Dependencies

  • logging

Required Imports

import logging
from typing import Dict, Any, Optional

Usage Example

import logging
from typing import Dict, Any, Optional

# Assuming InvoiceExtractionLogger is defined elsewhere
# Simple usage with default configuration
logger = get_logger(__name__)
logger.info('Application started')

# Usage with custom configuration
config = {
    'level': 'DEBUG',
    'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    'output_file': 'app.log'
}
logger = get_logger(__name__, config)
logger.debug('Debug message with custom config')
logger.error('Error occurred during processing')

Best Practices

  • Always use __name__ as the logger name parameter to maintain proper module hierarchy
  • Call this function once per module at the module level to get a logger instance
  • Pass configuration only on the first call or when you need to override default settings
  • The function is idempotent - multiple calls with the same name return the same logger instance
  • Ensure InvoiceExtractionLogger class is properly defined and imported before using this function
  • The function checks for existing handlers on the root logger, so subsequent calls won't reinitialize the logging system
  • Consider defining a standard configuration dictionary structure for consistency across the application

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_logger_v1 66.6% similar

    A wrapper function that retrieves a logger instance from Python's logging module with the specified name.

    From: /tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
  • class InvoiceExtractionLogger 59.7% similar

    A comprehensive logging configuration class for invoice extraction systems that provides console and file logging with optional JSON formatting, request tracking via correlation IDs, and configurable log levels.

    From: /tf/active/vicechatdev/invoice_extraction/utils/logging_utils.py
  • function setup_logging 58.1% similar

    Configures and initializes a Python logging system with both console and rotating file handlers, supporting customizable log levels, formats, and file rotation policies.

    From: /tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
  • class TestLoggingUtils 57.1% similar

    Unit test class for testing logging utilities including InvoiceExtractionLogger, PerformanceLogger, and get_logger function.

    From: /tf/active/vicechatdev/invoice_extraction/tests/test_utils.py
  • function setup_logging_v1 50.1% similar

    Configures and initializes Python's logging system with customizable log level and output destination (console or file).

    From: /tf/active/vicechatdev/QA_updater/utils/logging_utils.py
← Back to Browse