-
Notifications
You must be signed in to change notification settings - Fork 0
/
clsErrors.py
44 lines (33 loc) · 1.41 KB
/
clsErrors.py
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
# clsErrors.py - class to log errors
# This is a WIP
import sys
import traceback as trace_Back
import logging
class logger:
def __init__(self, logger_file_name: str = 'mylog.log', display_err_stout: bool = False):
self.display_err_stout = display_err_stout
self.traceBack = trace_Back
# Create or get the logger
self.logger = logging.getLogger(__name__)
handler = logging.FileHandler(filename = logger_file_name)
# create a logging format
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(fmt = formatter)
self.logger.addHandler(hdlr = handler)
# set log level
self.logger.setLevel(level = logging.ERROR)
def log(self, errException, sys_exc_info):
self.logger.exception(msg = errException)
exc_type, exc_value, exc_traceback = sys_exc_info
# eventually log to file and/or email
self.traceBack.print_stack
if self.display_err_stout:
self.traceBack.print_exception(exc_value, file = sys.stdout)
if __name__ == "__main__":
errorLog = logger(logger_file_name = 'mylog.log', display_err_stout = True)
# ==== Test above class ====
try:
y = 2
x = y / 0
except Exception as e:
errorLog.log(errException = e,sys_exc_info = sys.exc_info())