-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_system.py
More file actions
49 lines (45 loc) · 1.68 KB
/
log_system.py
File metadata and controls
49 lines (45 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os
import sys
import time
import platform
import traceback
import logging
# local imports
from main import VERSION
def log_sys_info() -> None:
"""Logs system info."""
logging.info(f"Build: {VERSION}")
logging.info(time.ctime())
logging.info(platform.platform())
logging.info(f"Python Version: {platform.python_version()}")
logging.info(f"Directory: {os.getcwd()}")
def exception_handler_hook(ex_type, ex_val, ex_tb):
"""Extend the exception handler to log unhandled exceptions"""
logging.critical("Unhandled exception: ", exc_info = (ex_type, ex_val, ex_tb))
print(''.join(traceback.format_exception(ex_type, ex_val, ex_tb)))
def init_logging(log_level: int = logging.INFO):
lfmt = "%(levelname)s [%(funcName)s]: %(message)s"
log_file_name = 'spo2.log'
log_file_path = ''
if hasattr(sys, "_MEIPASS"):
plat = sys.platform
log_file_dir = ''
if plat == "darwin":
log_file_dir = os.path.expanduser('~/Library/Logs/')
elif plat == "win32":
log_file_dir = os.path.expanduser('~/APPDATA/LOCAL/')
elif plat == "linux":
log_file_dir = os.path.expanduser('~/.config/')
else:
raise OSError(f"Unsupported platform: {sys.platform}")
log_file_path = os.path.join(log_file_dir, log_file_name)
else:
log_file_path = os.path.join(".", log_file_name)
try:
logging.basicConfig(filename=log_file_path, level=log_level, filemode='w', format=lfmt)
except OSError as e:
logging.basicConfig(level=logging.INFO, format=lfmt)
logging.error(e)
sys.excepthook = exception_handler_hook
logging.info("LOG START")
log_sys_info()