8000 utils: Thread - Fix that thread logs don't end after it completed by SchoolGuy · Pull Request #3264 · cobbler/cobbler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

utils: Thread - Fix that thread logs don't end after it completed #3264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cobbler/utils/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
self.event_id = event_id
self.remote = remote
self.logger = logging.getLogger()
self.__task_log_handler = None
self.__setup_logger()
self._run = run
self.on_done = on_done
Expand All @@ -55,13 +56,13 @@ def __setup_logger(self):
Utility function that will set up the Python logger for the tasks in a special directory.
"""
filename = pathlib.Path("/var/log/cobbler/tasks") / f"{self.event_id}.log"
task_log_handler = logging.FileHandler(str(filename), encoding="utf-8")
self.__task_log_handler = logging.FileHandler(str(filename), encoding="utf-8")
task_log_formatter = logging.Formatter(
"[%(threadName)s] %(asctime)s - %(levelname)s | %(message)s"
)
task_log_handler.setFormatter(task_log_formatter)
self.__task_log_handler.setFormatter(task_log_formatter)
self.logger.setLevel(logging.INFO)
self.logger.addHandler(task_log_handler)
self.logger.addHandler(self.__task_log_handler)

def _set_task_state(self, new_state: enums.EventStatus):
"""
Expand Down Expand Up @@ -113,3 +114,5 @@ def run(self):
utils.log_exc()
self._set_task_state(enums.EventStatus.FAILED)
return False
finally:
self.logger.removeHandler(self.__task_log_handler)
0