From a54d8bdced02f804a9e2234f6743bcd9e3b3882b Mon Sep 17 00:00:00 2001 From: cerberus Date: Sat, 27 Sep 2025 17:31:58 +0200 Subject: [PATCH] added logging --- tempbox/README.md | 1 + tempbox/{ => src/modules}/tempbox_functions.py | 10 ++++++++++ tempbox/src/modules/tempbox_logger.py | 17 +++++++++++++++++ tempbox/{ => src}/tempbox.py | 7 +++++-- 4 files changed, 33 insertions(+), 2 deletions(-) rename tempbox/{ => src/modules}/tempbox_functions.py (76%) create mode 100644 tempbox/src/modules/tempbox_logger.py rename tempbox/{ => src}/tempbox.py (81%) diff --git a/tempbox/README.md b/tempbox/README.md index 7f8bfb9..5c95793 100644 --- a/tempbox/README.md +++ b/tempbox/README.md @@ -9,6 +9,7 @@ again. ## Changelog +<2025-09-27> - Beta b0.2.1 - added logging in $USER/.local/share/tempbox
<2025-09-27> - Beta b0.1.1 - added the version output
<2025-09-27> - Beta b0.1.1 - fixed verbosity switch for subprocess
<2025-09-27> - *Beta b0.0.1* - first release of tempbox diff --git a/tempbox/tempbox_functions.py b/tempbox/src/modules/tempbox_functions.py similarity index 76% rename from tempbox/tempbox_functions.py rename to tempbox/src/modules/tempbox_functions.py index 22d251f..0df94a4 100644 --- a/tempbox/tempbox_functions.py +++ b/tempbox/src/modules/tempbox_functions.py @@ -1,6 +1,8 @@ import os import subprocess +from .tempbox_logger import logger + def create_folders(number_folders, base_path, verbosity=False): """creates an amount of folders and returning its path""" @@ -25,9 +27,15 @@ def create_folders(number_folders, base_path, verbosity=False): def execute_as_subprocess(command, base_path, verbosity=False): """executes the string given with the '-c, --command' flag.""" + + logger.debug("Entered execute_as_subprocess()") + logger.debug(f"Path:\t{base_path}\nCommand:\t{command}") + if verbosity: + logger.info("Running subprocess with terminal output.") subprocess.run(command, cwd=base_path, shell=True) else: + logger.info("Running with suppressed stdout and stderr") subprocess.run( command, cwd=base_path, @@ -35,3 +43,5 @@ def execute_as_subprocess(command, base_path, verbosity=False): stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, ) + + logger.debug("Exited execute_as_subprocess()") diff --git a/tempbox/src/modules/tempbox_logger.py b/tempbox/src/modules/tempbox_logger.py new file mode 100644 index 0000000..b987ecf --- /dev/null +++ b/tempbox/src/modules/tempbox_logger.py @@ -0,0 +1,17 @@ +import logging +from pathlib import Path + +log_dir = Path.home() / ".local" / "share" / "tempbox" +log_dir.mkdir(parents=True, exist_ok=True) + +log_file = log_dir / "tempbox.log" + +logging.basicConfig( + level=logging.DEBUG, + format="%(asctime)s - %(levelname)s - %(message)s", + filename=log_file, +) + +logger = logging.getLogger() +logger.debug(f"Log path:\t{log_dir}") +logger.debug(f"Log file:\t{log_file}") diff --git a/tempbox/tempbox.py b/tempbox/src/tempbox.py similarity index 81% rename from tempbox/tempbox.py rename to tempbox/src/tempbox.py index 3cb8797..063b977 100644 --- a/tempbox/tempbox.py +++ b/tempbox/src/tempbox.py @@ -1,9 +1,10 @@ import tempfile import argparse -from tempbox_functions import execute_as_subprocess +from modules.tempbox_functions import execute_as_subprocess +from modules.tempbox_logger import logger -tempbox_version = "Tempbox Beta b0.1.1" +tempbox_version = "Tempbox Beta b0.2.1" # Argument parsing parser = argparse.ArgumentParser( prog="Tempbox", @@ -31,6 +32,7 @@ args = parser.parse_args() if args.command is not None: with tempfile.TemporaryDirectory() as temp_dir: + logger.debug(f"'{temp_dir}' was created") if args.command is not None: execute_as_subprocess( args.command, @@ -39,6 +41,7 @@ if args.command is not None: ) else: parser.print_help() + logger.info("Printed Version") # Creates a temporary directory and executes the command in it.