From ee7d656f2aaa6228695e3fef416b02c15425b015 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Sun, 10 Nov 2019 20:47:59 -0700 Subject: [PATCH] Delete log atexit if empty --- scripts/wk/log.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/scripts/wk/log.py b/scripts/wk/log.py index 6ac43f39..8fb105a8 100644 --- a/scripts/wk/log.py +++ b/scripts/wk/log.py @@ -76,6 +76,23 @@ def get_root_logger_path(): return log_path +def remove_empty_log(): + """Remove log if empty.""" + is_empty = False + + # Check if log is empty + log_path = get_root_logger_path() + try: + is_empty = log_path and log_path.exists() and log_path.stat().st_size == 0 + except (FileNotFoundError, AttributeError): + # File doesn't exist or couldn't verify it's empty + pass + + # Delete log + if is_empty: + log_path.unlink() + + def start(config=None): """Configure and start logging using safe defaults.""" log_path = format_log_path(timestamp=os.name != 'nt') @@ -94,6 +111,7 @@ def start(config=None): logging.basicConfig(filename=log_path, **config) # Register shutdown to run atexit + atexit.register(remove_empty_log) atexit.register(logging.shutdown)