-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
50 lines (37 loc) · 1.48 KB
/
utils.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
45
46
47
48
49
50
import json
import logging
from logging.handlers import RotatingFileHandler
from exceptions import UnsupportedConfigFileError
LOG_FILENAME = "yt_views.log"
# Create a custom logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# Create handlers
console_handler = logging.StreamHandler()
file_handler = RotatingFileHandler(
LOG_FILENAME, maxBytes=20971520, encoding="utf-8", backupCount=50
)
console_handler.setLevel(logging.INFO)
file_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
console_log_format = "%(asctime)s [%(levelname)5s] %(lineno)3d: %(message)s"
file_log_format = "%(asctime)s [%(levelname)5s] %(filename)s:%(lineno)3d: %(message)s"
console_formatter = logging.Formatter(console_log_format, datefmt="%d-%m-%Y %H:%M:%S")
console_handler.setFormatter(console_formatter)
file_formatter = logging.Formatter(file_log_format, datefmt="%d-%m-%Y %H:%M:%S")
file_handler.setFormatter(file_formatter)
# Add handlers to the logger
logger.addHandler(console_handler)
logger.addHandler(file_handler)
def get_configuration(filename="config.json") -> dict:
"""Read configuration file
:type filename: str
:param filename: Name of the configuration file
:rtype: dict
:returns: Configuration as dict
"""
if not filename.endswith(".json"):
raise UnsupportedConfigFileError("Config file must be a json file!")
with open(filename, encoding="utf-8") as configfile:
config = json.load(configfile)
return config