add logfile to logging when called as main

This commit is contained in:
matt committed 2026-10-17 04:31:24 -07:00
1 parent 5c06fb36a5
commit ff4d688c9a
1 file changed
+17 -4
+17 -4
View File
@@ -43,12 +43,18 @@ DISCOVER_TIMEOUT = SCAN_TIMEOUT + 5.0 # Timeout for discovering device
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #
# Logging # Logging
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #
logging.basicConfig( _logger = logging.getLogger(__name__)
level=logging.INFO, _logger.setLevel(logging.INFO)
format="%(asctime)s [%(levelname)s] %(message)s",
# Stream (stdout) handler – always present
_stdout_handler = logging.StreamHandler(sys.stdout)
_stdout_handler.setFormatter(
logging.Formatter(
fmt="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S", datefmt="%H:%M:%S",
) )
log = logging.getLogger(__name__) )
_logger.addHandler(_stdout_handler)
# --------------------------------------------------------------------------- # # --------------------------------------------------------------------------- #
# Data model # Data model
@@ -305,6 +311,10 @@ def test(test_count = 0) -> None:
if __name__ == "__main__": if __name__ == "__main__":
import json import json
import sys import sys
_file_handler = logging.FileHandler("app.log", mode="a")
_file_handler.setLevel(logging.INFO) # same level (or tweak)
_file_handler.setFormatter(_stdout_handler.formatter) # reuse formatter
_logger.addHandler(_file_handler)
# 1. Grab the argument (string) # 1. Grab the argument (string)
if len(sys.argv) < 2: if len(sys.argv) < 2:
print("Usage: python3 test.py <count>") print("Usage: python3 test.py <count>")
@@ -350,6 +360,9 @@ if __name__ == "__main__":
result_summary = " | ".join(parts) result_summary = " | ".join(parts)
log.info(f" {result_summary}") log.info(f" {result_summary}")
# finally create the log
log = _logger
# This is the structure of test_results # This is the structure of test_results
# [ # [
# { # {