-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
49 lines (39 loc) · 1.41 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
import logging
import logging.handlers
import os
import sys
def create_logger(file: str, name: str, level: str, rotating: bool = False):
path = os.path.split(file)[0]
isExist = os.path.exists(path)
if not isExist:
# Create a new directory because it does not exist
os.makedirs(path)
print("New directory is created: {}".format(path))
logger = logging.getLogger(name)
logger.setLevel(level)
# If logger already has handler, assume it was already created
if len(logger.handlers) == 1:
return logger
if rotating:
handler = logging.handlers.RotatingFileHandler(
file, maxBytes=10000000, backupCount=3)
else:
handler = logging.FileHandler(file)
formatter = logging.Formatter(
fmt='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%d/%m/%Y %I:%M:%S %p')
handler.setFormatter(formatter)
logger.addHandler(handler)
#
handler_stdout = logging.StreamHandler(sys.stdout)
handler_stdout.setLevel(logging.DEBUG)
handler_stdout.setFormatter(formatter)
logger.addHandler(handler_stdout)
return logger
def pretty_print(dictionary, indent=''):
# ANSI color terminal escape sequences
bold='\033[01m'
OKGREEN = '\033[92m'
ENDC = '\033[0m'
for key in dictionary.keys():
print ('{} {}{}"{}"{}: {}'.format(indent, bold, OKGREEN, key, ENDC, dictionary[key]))