forked from Daenara/rhasspy_weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_logger.py
31 lines (24 loc) · 1.01 KB
/
custom_logger.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
import logging
import datetime
import sys
import pytz
def custom_logger(path):
logging_format = '%(asctime)s - %(levelname)-5s - %(name)s.%(funcName)s[%(lineno)d]: %(message)s'
date_format = "%Y-%m-%d %H:%M:%S"
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
file_handler = logging.FileHandler(path, encoding='utf-8')
file_handler.terminator = '\n'
file_handler.setFormatter(logging.Formatter(logging_format, date_format))
logging.Formatter.converter = custom_time
logger.addHandler(file_handler)
sys.excepthook = exception_to_log
return logger
def exception_to_log(error_type, error_value, error_traceback):
if issubclass(error_type, KeyboardInterrupt):
sys.__excepthook__(error_type, error_value, error_traceback)
return
logging.exception("Uncaught exception: ", exc_info=(error_type, error_value, error_traceback))
def custom_time(*args):
converted = datetime.datetime.now(pytz.timezone('Europe/Berlin'))
return converted.timetuple()