🔍 Code Extractor

function get_logger

Maturity: 49

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

File:
/tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
Lines:
68 - 78
Complexity:
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

    A context manager decorator that logs the performance metrics of an operation by wrapping it with a PerformanceLogger instance.

    From: /tf/active/vicechatdev/contract_validity_analyzer/utils/logging_utils.py
  • function get_model_class 46.7% similar

    Retrieves a model class from a registry by its string name, returning the class type or None if not found.

    From: /tf/active/vicechatdev/CDocs/models/__init__.py
  • function log_info 46.6% similar

    Logs an informational message using a logger named 'EmailForwarder'.

    From: /tf/active/vicechatdev/email-forwarder/src/utils/logger.py
  • function log_debug 43.3% similar

    A logging utility function that writes debug-level messages to a logger named 'EmailForwarder'.

    From: /tf/active/vicechatdev/email-forwarder/src/utils/logger.py
  • function log_error 42.6% similar

    Logs an error message using a named logger instance for the EmailForwarder application.

    From: /tf/active/vicechatdev/email-forwarder/src/utils/logger.py
← Back to Browse