separate to defs

This commit is contained in:
Maxim Khomutov 2025-03-18 14:43:39 +03:00
parent b493929ce1
commit 448d096f45

View File

@ -24,36 +24,39 @@ def handle_thread_exception(args):
return
logger.error("Unhandled exception in thread:\n" + "".join(traceback.format_exception(exc_type, exc_value, exc_traceback)))
log_dir = Core.DIR / "logs"
log_file = log_dir / "info.log"
os.makedirs(log_dir, exist_ok=True)
os.makedirs(Core.DIR / "cache", exist_ok=True)
try:
if os.path.exists(log_file):
ftime = os.path.getmtime(log_file)
index = 1
while True:
zip_path = log_dir / f"{datetime.fromtimestamp(ftime).strftime('%Y-%m-%d')}-{index}.zip"
if not os.path.exists(zip_path):
break
index += 1
with zipfile.ZipFile(zip_path, "w") as zipf:
logs_files = glob.glob(f"{log_dir}/*.log")
for file in logs_files:
if os.path.exists(file):
zipf.write(file, os.path.basename(file))
os.remove(file)
except PermissionError:
messagebox.showerror(f"ezSyncer {Core.ver_str}",
"Some files are locked...\nProgram cannot works in multiplies instances.")
sys.exit(1)
logger.remove()
fmt = "<green>{elapsed} -- {time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level:<8}</level> | {extra[module]:^12} | {extra[prefix]:<12} | {message}"
logger.add(sys.stdout, level="DEBUG", format=fmt, backtrace=True, diagnose=True)
logger.add(log_file, level="INFO", format=fmt, backtrace=False, diagnose=False, rotation="25 MB")
logger.add(log_dir / "debug.log", level="DEBUG", format=fmt, rotation="10 MB")
logger.add(log_dir / "low_debug.log", level="DEBUG", rotation="10 MB")
sys.excepthook = handle_exception
threading.excepthook = handle_thread_exception
logger.bind(module="LoggerSetup", prefix="init").success("Logger initialized.")
def zip_logs():
try:
if os.path.exists(log_file):
ftime = os.path.getmtime(log_file)
index = 1
while True:
zip_path = log_dir / f"{datetime.fromtimestamp(ftime).strftime('%Y-%m-%d')}-{index}.zip"
if not os.path.exists(zip_path):
break
index += 1
with zipfile.ZipFile(zip_path, "w") as zipf:
logs_files = glob.glob(f"{log_dir}/*.log")
for file in logs_files:
if os.path.exists(file):
zipf.write(file, os.path.basename(file))
os.remove(file)
except PermissionError:
messagebox.showerror(f"ezSyncer {Core.ver_str}",
"Some files are locked...\nProgram cannot works in multiplies instances.")
sys.exit(1)
def setup():
logger.remove()
fmt = "<green>{elapsed} -- {time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level:<8}</level> | {extra[module]:^12} | {extra[prefix]:<12} | {message}"
logger.add(sys.stdout, level="DEBUG", format=fmt, backtrace=True, diagnose=True)
logger.add(log_file, level="INFO", format=fmt, backtrace=False, diagnose=False, rotation="25 MB")
logger.add(log_dir / "debug.log", level="DEBUG", format=fmt, rotation="10 MB")
logger.add(log_dir / "low_debug.log", level="DEBUG", rotation="10 MB")
sys.excepthook = handle_exception
threading.excepthook = handle_thread_exception
logger.bind(module="LoggerSetup", prefix="init").success("Logger initialized.")