Logger
logger
¶
Logging configuration and utilities for pyballistic library.
This module provides a centralized logging system for the pyballistic 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 pyballistic.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 pyballistic.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.
Attributes:
Name | Type | Description |
---|---|---|
__all__ |
|
|
formatter |
|
|
console_handler |
|
|
logger |
Logger
|
|
file_handler |
Optional[FileHandler]
|
|
Attributes¶
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 pyballistic.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 pyballistic/logger.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
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 pyballistic.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 pyballistic/logger.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|