Skip to content

Logger

logger

Logging configuration and utilities for py_ballisticcalc library.

This module provides a centralized logging system for the py_ballisticcalc library, including both console and optional file logging capabilities. The logger is configured with appropriate formatters and can be dynamically adjusted for different logging needs.

The module exposes a pre-configured logger instance and utility functions for managing file-based logging. By default, only console logging is enabled with INFO level, but file logging can be enabled as needed for debugging or detailed analysis.

Global Variables
  • logger: Pre-configured logger instance for the library.
  • file_handler: Global file handler reference (None when file logging disabled).

Functions:

Name Description
enable_file_logging

Enable logging to a file with DEBUG level.

disable_file_logging

Disable file logging and clean up resources.

Examples:

Basic logging usage:

from py_ballisticcalc.logger import logger

logger.info("Ballistic calculation started")
logger.warning("Trajectory calculation ended before requested distance")
logger.error("Unable to find angle to hit target")

Enable file logging for debugging:

from py_ballisticcalc.logger import enable_file_logging, disable_file_logging

# Enable detailed logging to file
enable_file_logging("ballistics_debug.log")

# Perform calculations with detailed logging
# ... ballistic calculations ...

# Clean up file logging
disable_file_logging()

Note

The logger name 'py_balcalc' is used for historical compatibility. All log messages from the library components will be routed through this logger.

Classes:

Name Description
ANSIColorCodes
ColoredFormatter

Attributes:

Name Type Description
__all__
COLOR_MAP
CLI_LOG_FORMAT
FILE_LOG_FORMAT
cpnsole_formatter
console_handler
logger Logger
file_handler Optional[FileHandler]

Attributes

__all__ module-attribute
__all__ = (
    "logger",
    "enable_file_logging",
    "disable_file_logging",
)
COLOR_MAP module-attribute
COLOR_MAP = {
    CRITICAL: BOLD_RED,
    ERROR: RED,
    WARNING: YELLOW,
    INFO: CYAN,
    DEBUG: BLUE,
}
CLI_LOG_FORMAT module-attribute
CLI_LOG_FORMAT = '%(levelname)s:%(name)s:%(message)s'
FILE_LOG_FORMAT module-attribute
FILE_LOG_FORMAT = (
    "%(asctime)s:%(levelname)s:%(name)s:%(message)s"
)
cpnsole_formatter module-attribute
cpnsole_formatter = ColoredFormatter(CLI_LOG_FORMAT)
console_handler module-attribute
console_handler = StreamHandler()
logger module-attribute
logger: Logger = getLogger('py_balcalc')
file_handler module-attribute
file_handler: Optional[FileHandler] = None

Classes

ANSIColorCodes
ColoredFormatter
ColoredFormatter(fmt, datefmt=None, style='%')

Bases: Formatter

Source code in py_ballisticcalc/logger.py
83
84
85
86
87
def __init__(self, fmt, datefmt=None, style="%"):
    super().__init__(fmt, datefmt, style)
    self.fmt = fmt
    if "(levelname)" not in fmt:
        raise ValueError("Formatter must contain '(levelname)' placeholder.")

Functions

enable_file_logging
enable_file_logging(filename: str = 'debug.log') -> None

Enable logging to a file with DEBUG level output.

This function configures file-based logging, replacing any existing file handler. File logging captures all DEBUG level messages and above with timestamp information, providing detailed logging for debugging and analysis purposes.

Parameters:

Name Type Description Default
filename str

Name of the log file to create. Defaults to "debug.log". The file will be created in the current working directory unless an absolute path is provided.

'debug.log'
Note

If file logging is already enabled, the existing file handler will be removed and replaced with a new one using the specified filename. The file will be opened in append mode, so existing content is preserved.

Examples:

from py_ballisticcalc.logger import enable_file_logging, logger

# Enable detailed file logging
enable_file_logging("trajectory_analysis.log")

# All subsequent log messages will be written to file
logger.debug("Detailed calculation step information")
logger.info("Calculation completed successfully")
Source code in py_ballisticcalc/logger.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def enable_file_logging(filename: str = "debug.log") -> None:
    """Enable logging to a file with DEBUG level output.

    This function configures file-based logging, replacing any existing file handler.
    File logging captures all DEBUG level messages and above with timestamp information,
    providing detailed logging for debugging and analysis purposes.

    Args:
        filename: Name of the log file to create. Defaults to "debug.log".
                 The file will be created in the current working directory
                 unless an absolute path is provided.

    Note:
        If file logging is already enabled, the existing file handler will be
        removed and replaced with a new one using the specified filename.
        The file will be opened in append mode, so existing content is preserved.

    Examples:
        ```python
        from py_ballisticcalc.logger import enable_file_logging, logger

        # Enable detailed file logging
        enable_file_logging("trajectory_analysis.log")

        # All subsequent log messages will be written to file
        logger.debug("Detailed calculation step information")
        logger.info("Calculation completed successfully")
        ```
    """
    global file_handler
    # Remove the existing file handler if it exists
    if file_handler is not None:
        disable_file_logging()

    # Add a new file handler
    file_handler = logging.FileHandler(filename)
    file_handler.setLevel(logging.DEBUG)  # Log everything to the file
    file_formatter = logging.Formatter(FILE_LOG_FORMAT)
    file_handler.setFormatter(file_formatter)
    logger.addHandler(file_handler)
disable_file_logging
disable_file_logging() -> None

Disable file logging and clean up resources.

This function removes the file handler from the logger and properly closes the file handle, ensuring no resource leaks. After calling this function, only console logging will remain active.

Note

If no file logging is currently enabled, this function has no effect. It's safe to call this function multiple times or when file logging is already disabled.

Examples:

from py_ballisticcalc.logger import disable_file_logging

# Clean up file logging when done with detailed analysis
disable_file_logging()

# Only console logging remains active
Source code in py_ballisticcalc/logger.py
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def disable_file_logging() -> None:
    """Disable file logging and clean up resources.

    This function removes the file handler from the logger and properly closes the file handle,
    ensuring no resource leaks. After calling this function, only console logging will remain active.

    Note:
        If no file logging is currently enabled, this function has no effect.
        It's safe to call this function multiple times or when file logging is already disabled.

    Examples:
        ```python
        from py_ballisticcalc.logger import disable_file_logging

        # Clean up file logging when done with detailed analysis
        disable_file_logging()

        # Only console logging remains active
        ```
    """
    global file_handler
    if file_handler is not None:
        logger.removeHandler(file_handler)
        file_handler.close()
        file_handler = None