TasmotaManager/debug_format_changes_summary.md
Mike Geppert 126cd39555 Major code improvements and bug fixes:
1. Restructured configuration: Moved config_other and console to top level
2. Added common _match_pattern function for regex pattern matching
3. Implemented Unifi Hostname bug fix in is_hostname_unknown
4. Created common get_device_hostname function to eliminate code duplication
5. Added comprehensive test scripts for all new functionality
6. Added detailed documentation for all changes
2025-08-08 19:04:33 -05:00

3.4 KiB

Debug Format Changes Summary

Issue Description

The issue was to modify all debug prints in the TasmotaManager code to include the file name and line number when debug mode is enabled. This enhancement improves debugging by providing more context about where each log message originates from.

Changes Made

Two locations in the code were modified to include file name and line number in debug logs:

  1. TasmotaDiscovery.init method (lines 78-86):

    def __init__(self, debug: bool = False):
        """Initialize the TasmotaDiscovery with optional debug mode."""
        log_level = logging.DEBUG if debug else logging.INFO
        log_format = '%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s' if debug else '%(asctime)s - %(levelname)s - %(message)s'
        logging.basicConfig(
            level=log_level,
            format=log_format,
            datefmt='%Y-%m-%d %H:%M:%S'
        )
    
  2. main function (lines 1733-1739):

    # Set up logging
    log_level = logging.DEBUG if args.debug else logging.INFO
    log_format = '%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s' if args.debug else '%(asctime)s - %(levelname)s - %(message)s'
    logging.basicConfig(level=log_level,
                       format=log_format,
                       datefmt='%Y-%m-%d %H:%M:%S')
    

In both locations, a conditional format string was added that includes file name and line number (%(filename)s:%(lineno)d) only when debug mode is enabled. This ensures that:

  1. When debug mode is ON, logs include file name and line number:

    2025-08-07 07:25:16 - DEBUG - TasmotaManager.py:96 - Loading configuration from: network_configuration.json
    
  2. When debug mode is OFF, logs maintain the original format without file name and line number:

    2025-08-07 07:25:16 - INFO - Loading configuration from: network_configuration.json
    

Testing

The changes were tested by running the code in debug mode with the command:

python3 TasmotaManager.py --debug --Device UtilFan-5469

The output confirmed that debug logs now include file name and line number information as expected. For example:

2025-08-07 07:25:16 - DEBUG - TasmotaManager.py:96 - Loading configuration from: network_configuration.json
2025-08-07 07:25:16 - DEBUG - TasmotaManager.py:100 - Configuration loaded successfully from network_configuration.json
2025-08-07 07:25:16 - INFO - TasmotaManager.py:1306 - Processing single device: UtilFan-5469

Benefits

These changes provide several benefits:

  1. Improved Debugging: Developers can now quickly identify the exact file and line number where each debug message originates, making it easier to locate and fix issues.

  2. Contextual Information: The file name and line number provide important context about the code's execution flow, especially in a large codebase.

  3. Selective Enhancement: The enhanced format is only applied when debug mode is enabled, maintaining the cleaner, more concise format for normal operation.

  4. Consistent Implementation: The same approach is used in both logging configuration locations, ensuring consistent behavior throughout the application.

Conclusion

The implemented changes successfully fulfill the requirement to include file name and line number in debug logs when debug mode is enabled. This enhancement will make debugging more efficient by providing additional context for each log message.