-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
32 lines (23 loc) · 1.07 KB
/
Copy pathlog.py
File metadata and controls
32 lines (23 loc) · 1.07 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
import logging
import sys
from types import TracebackType
def handle_exception(*args):
args: tuple[type[BaseException], BaseException, TracebackType|None]
if isinstance(args[1], KeyboardInterrupt):
sys.__excepthook__(*args)
logging.debug("Keyboard interrupt")
return
logging.critical("Unhandled exception", exc_info=args)
def setup_logging():
logging.getLogger().setLevel(logging.DEBUG)
simple_formatter = logging.Formatter(fmt="{levelname}: {message}", style='{')
named_formatter = logging.Formatter(fmt="{levelname} ({name}): {message}", style='{')
console_handler = logging.StreamHandler(stream=sys.stdout)
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(simple_formatter)
file_handler = logging.FileHandler(filename="userdata/last.log", mode='w', encoding='utf-8')
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(named_formatter)
logging.getLogger().addHandler(console_handler)
logging.getLogger().addHandler(file_handler)
sys.excepthook = handle_exception