-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_log.py
50 lines (36 loc) · 1.39 KB
/
custom_log.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 time
loggers = {}
is_verbose = False
log_path = None
class CustomLog:
def __init__(self, verbose, log):
global log_path, is_verbose
is_verbose = verbose
log_path = log
@staticmethod
def get_logger(logger_name):
global loggers
if loggers.get(logger_name):
return loggers.get(logger_name)
else:
if is_verbose:
root_logger = logging.getLogger()
else:
root_logger = logging.getLogger(logger_name)
save_messages_to_file = False if log_path is None else True
log_formatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
root_logger.setLevel(logging.INFO)
if save_messages_to_file:
file_handler = logging.FileHandler("{0}/{1}.log".format(log_path, time.strftime("%Y%m%d")))
file_handler.setFormatter(log_formatter)
root_logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(log_formatter)
root_logger.addHandler(console_handler)
loggers[logger_name] = root_logger
return root_logger
@staticmethod
def log_info(message):
root_logger = CustomLog.get_logger("azure-log")
root_logger.info(message)