From e87b89703478697c1fc5bbd2bc01e331588c0b03 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:25:44 +0800 Subject: [PATCH 1/2] Edit logging now will output log file Edit logging now will output log file --- file_automation/local/zip/zip_process.py | 2 +- .../callback/callback_function_executor.py | 11 ++++++++-- .../utils/executor/action_executor.py | 19 ++++++++++++------ file_automation/utils/json/json_file.py | 13 +++++++++--- .../utils/logging/loggin_instance.py | 19 ++++++++++++------ .../utils/scheduler/extend_apscheduler.py | 20 +++++++++++++++++++ pyproject.toml | 4 ++-- dev.toml => stable.toml | 4 ++-- 8 files changed, 70 insertions(+), 22 deletions(-) rename dev.toml => stable.toml (95%) diff --git a/file_automation/local/zip/zip_process.py b/file_automation/local/zip/zip_process.py index 4e03176..038875a 100644 --- a/file_automation/local/zip/zip_process.py +++ b/file_automation/local/zip/zip_process.py @@ -98,5 +98,5 @@ def set_zip_password(zip_file_path: str, password: bytes): file_automation_logger.info( f"Set zip file password, " f"zip file: {zip_file_path}, " - f"zup password: {password}" + f"zip password: {password}" ) diff --git a/file_automation/utils/callback/callback_function_executor.py b/file_automation/utils/callback/callback_function_executor.py index 5aab8e6..2a5a9a7 100644 --- a/file_automation/utils/callback/callback_function_executor.py +++ b/file_automation/utils/callback/callback_function_executor.py @@ -1,5 +1,4 @@ import typing -from sys import stderr from file_automation.local.dir.dir_process import copy_dir, create_dir, remove_dir_tree from file_automation.local.file.file_process import copy_file, remove_file, rename_file, copy_specify_extension_file, \ @@ -18,6 +17,7 @@ upload_dir_to_folder, upload_to_folder, upload_dir_to_drive, upload_to_drive from file_automation.utils.exception.exception_tags import get_bad_trigger_function, get_bad_trigger_method from file_automation.utils.exception.exceptions import CallbackExecutorException +from file_automation.utils.logging.loggin_instance import file_automation_logger class CallbackFunctionExecutor(object): @@ -76,19 +76,26 @@ def callback_function( try: if trigger_function_name not in self.event_dict.keys(): raise CallbackExecutorException(get_bad_trigger_function) + file_automation_logger.info(f"Callback trigger {trigger_function_name} with param {kwargs}") execute_return_value = self.event_dict.get(trigger_function_name)(**kwargs) if callback_function_param is not None: if callback_param_method not in ["kwargs", "args"]: raise CallbackExecutorException(get_bad_trigger_method) if callback_param_method == "kwargs": callback_function(**callback_function_param) + file_automation_logger.info( + f"Callback function {callback_function} with param {callback_function_param}") else: callback_function(*callback_function_param) + file_automation_logger.info( + f"Callback function {callback_function} with param {callback_function_param}") else: callback_function() + file_automation_logger.info(f"Callback function {callback_function}") return execute_return_value except Exception as error: - print(repr(error), file=stderr) + file_automation_logger.error( + f"Callback function failed. {repr(error)}") callback_executor = CallbackFunctionExecutor() diff --git a/file_automation/utils/executor/action_executor.py b/file_automation/utils/executor/action_executor.py index 08689dd..40890da 100644 --- a/file_automation/utils/executor/action_executor.py +++ b/file_automation/utils/executor/action_executor.py @@ -22,6 +22,7 @@ action_is_null_error, cant_execute_action_error from file_automation.utils.exception.exceptions import ExecuteActionException, AddCommandException from file_automation.utils.json.json_file import read_action_json +from file_automation.utils.logging.loggin_instance import file_automation_logger class Executor(object): @@ -93,20 +94,23 @@ def execute_action(self, action_list: [list, dict]) -> dict: else: raise ExecuteActionException(action_is_null_error) except Exception as error: - print(repr(error), file=sys.stderr, flush=True) + file_automation_logger.error( + f"Execute {action_list} failed. {repr(error)}" + ) for action in action_list: try: event_response = self._execute_event(action) execute_record = "execute: " + str(action) + file_automation_logger.info( + f"Execute {action}" + ) execute_record_dict.update({execute_record: event_response}) except Exception as error: - print(repr(error), file=sys.stderr, flush=True) - print(action, file=sys.stderr, flush=True) + file_automation_logger.error( + f"Execute {action} failed. {repr(error)}" + ) execute_record = "execute: " + str(action) execute_record_dict.update({execute_record: repr(error)}) - for key, value in execute_record_dict.items(): - print(key, flush=True) - print(value, flush=True) return execute_record_dict def execute_files(self, execute_files_list: list) -> list: @@ -127,6 +131,9 @@ def add_command_to_executor(command_dict: dict): """ :param command_dict: dict include command we want to add to event_dict """ + file_automation_logger.info( + f"Add command to executor {command_dict}" + ) for command_name, command in command_dict.items(): if isinstance(command, (types.MethodType, types.FunctionType)): executor.event_dict.update({command_name: command}) diff --git a/file_automation/utils/json/json_file.py b/file_automation/utils/json/json_file.py index 00aea46..abb4727 100644 --- a/file_automation/utils/json/json_file.py +++ b/file_automation/utils/json/json_file.py @@ -2,8 +2,9 @@ from pathlib import Path from threading import Lock -from file_automation.utils.exception.exception_tags import cant_find_json_error +from file_automation.utils.exception.exception_tags import cant_find_json_error, cant_save_json_error from file_automation.utils.exception.exceptions import JsonActionException +from file_automation.utils.logging.loggin_instance import file_automation_logger _lock = Lock() @@ -17,6 +18,9 @@ def read_action_json(json_file_path: str) -> list: try: file_path = Path(json_file_path) if file_path.exists() and file_path.is_file(): + file_automation_logger.info( + f"Read json file {json_file_path}" + ) with open(json_file_path) as read_file: return json.loads(read_file.read()) except JsonActionException: @@ -33,9 +37,12 @@ def write_action_json(json_save_path: str, action_json: list) -> None: """ _lock.acquire() try: + file_automation_logger.info( + f"Write {action_json} as file {json_save_path}" + ) with open(json_save_path, "w+") as file_to_write: file_to_write.write(json.dumps(action_json, indent=4)) - except AutoControlJsonActionException: - raise AutoControlJsonActionException(cant_save_json_error) + except JsonActionException: + raise JsonActionException(cant_save_json_error) finally: _lock.release() diff --git a/file_automation/utils/logging/loggin_instance.py b/file_automation/utils/logging/loggin_instance.py index cc8746a..63b5da1 100644 --- a/file_automation/utils/logging/loggin_instance.py +++ b/file_automation/utils/logging/loggin_instance.py @@ -1,9 +1,16 @@ import logging +import sys -logging.getLogger().setLevel(logging.INFO) file_automation_logger = logging.getLogger("File Automation") -handler = logging.StreamHandler() -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -handler.setFormatter(formatter) -file_automation_logger.addHandler(handler) -file_automation_logger.setLevel(logging.DEBUG) +file_automation_logger.setLevel(logging.INFO) +formatter = logging.Formatter('%(asctime)s | %(name)s | %(levelname)s | %(message)s') +# Stream handler +stream_handler = logging.StreamHandler(stream=sys.stderr) +stream_handler.setFormatter(formatter) +stream_handler.setLevel(logging.WARNING) +file_automation_logger.addHandler(stream_handler) +# File handler +file_handler = logging.FileHandler(filename="FileAutomation.log", mode="w+") +file_handler.setFormatter(formatter) +file_automation_logger.addHandler(file_handler) + diff --git a/file_automation/utils/scheduler/extend_apscheduler.py b/file_automation/utils/scheduler/extend_apscheduler.py index c791e51..b650a69 100644 --- a/file_automation/utils/scheduler/extend_apscheduler.py +++ b/file_automation/utils/scheduler/extend_apscheduler.py @@ -4,6 +4,8 @@ from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.util import undefined +from file_automation.utils.logging.loggin_instance import file_automation_logger + class SchedulerManager(object): @@ -38,9 +40,15 @@ def get_nonblocking_scheduler(self): return self._background_schedulers def start_block_scheduler(self, *args, **kwargs): + file_automation_logger.info( + f"Start blocking scheduler with {args}, {kwargs}" + ) self._blocking_schedulers.start(*args, **kwargs) def start_nonblocking_scheduler(self, *args, **kwargs): + file_automation_logger.info( + f"Start background scheduler with {args}, {kwargs}" + ) self._background_schedulers.start(*args, **kwargs) def start_all_scheduler(self, *args, **kwargs): @@ -116,13 +124,25 @@ def add_cron_nonblocking( self.add_nonblocking_job(func=function, id=id, trigger="cron", **trigger_args) def remove_blocking_job(self, id: str, jobstore: str = 'default'): + file_automation_logger.info( + f"Remove blocking job {id}, store on {jobstore}" + ) self._blocking_schedulers.remove_job(job_id=id, jobstore=jobstore) def remove_nonblocking_job(self, id: str, jobstore: str = 'default'): + file_automation_logger.info( + f"Remove nonblocking job {id}, store on {jobstore}" + ) self._background_schedulers.remove_job(job_id=id, jobstore=jobstore) def shutdown_blocking_scheduler(self, wait: bool = False): + file_automation_logger.info( + f"Shutdown blocking scheduler wait = {wait}" + ) self._blocking_schedulers.shutdown(wait=wait) def shutdown_nonblocking_scheduler(self, wait: bool = False): + file_automation_logger.info( + f"Shutdown nonblocking scheduler wait = {wait}" + ) self._background_schedulers.shutdown(wait=wait) diff --git a/pyproject.toml b/pyproject.toml index 5e43e46..5381059 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.6" +name = "automation_file_dev" +version = "0.0.8" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/dev.toml b/stable.toml similarity index 95% rename from dev.toml rename to stable.toml index f399704..5e43e46 100644 --- a/dev.toml +++ b/stable.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.7" +name = "automation_file" +version = "0.0.6" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] From ed869a7bd162a3efc5b6139121c4661f153e2f83 Mon Sep 17 00:00:00 2001 From: JeffreyChen <33644111+JE-Chen@users.noreply.github.com> Date: Mon, 12 Jun 2023 15:40:34 +0800 Subject: [PATCH 2/2] Update stable version Update stable version --- stable.toml => dev.toml | 4 ++-- pyproject.toml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename stable.toml => dev.toml (95%) diff --git a/stable.toml b/dev.toml similarity index 95% rename from stable.toml rename to dev.toml index 5e43e46..5381059 100644 --- a/stable.toml +++ b/dev.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file" -version = "0.0.6" +name = "automation_file_dev" +version = "0.0.8" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ] diff --git a/pyproject.toml b/pyproject.toml index 5381059..cdc3340 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,8 +5,8 @@ requires = ["setuptools>=61.0"] build-backend = "setuptools.build_meta" [project] -name = "automation_file_dev" -version = "0.0.8" +name = "automation_file" +version = "0.0.7" authors = [ { name = "JE-Chen", email = "zenmailman@gmail.com" }, ]