-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
106 lines (87 loc) · 3.28 KB
/
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import logging
import sys
import coloredlogs
from logging.handlers import TimedRotatingFileHandler
FMT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
DATE_FMT = "%m-%d-%Y %H:%M"
FILE_FORMATTER = logging.Formatter(
"%(asctime)s - %(funcName)s:%(lineno)d - %(name)s - %(levelname)s - %(message)s"
)
CONSOLE_FORMATTER = logging.Formatter(FMT)
# humanfriendly --demo
CUSTOM_FIELD_STYLES = {
"asctime": {"color": "green"},
"hostname": {"color": "magenta"},
"levelname": {"bold": True, "color": "black"},
"name": {"color": 200},
"programname": {"color": "cyan"},
"username": {"color": "yellow"},
}
def get_console_handler(debug):
"""
Since we don't want to overwhelm or freak out the user, we're just going to send the output
of debugging over to the file, and only send INFO out to the user.
"""
# First, let's set the console StreamHandler
console_handler = logging.StreamHandler(sys.stdout)
# If debug is true, print it out to the screen.
if debug == True:
console_handler.setLevel(logging.DEBUG)
else:
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(CONSOLE_FORMATTER)
return console_handler
def get_file_handler(debug=False, log_file_name="log.txt"):
"""
We're going to print out the debug output to the log file.
"""
file_handler = TimedRotatingFileHandler(log_file_name, when="midnight")
# We want to print out debug information to this file.
if debug:
file_handler.setLevel(logging.DEBUG)
else:
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(FILE_FORMATTER)
return file_handler
def get_logger(
logger_name="Template Repository Logger",
log_file_name="log.txt",
debug=False,
create_log_file=True,
):
"""Get the logger, for the current namespace.
Args:
logger_name (str, optional): Logger Name. Defaults to "Template Repository Logger".
debug (bool, optional): Debugger boolean. Defaults to False.
Returns:
logger: return the logger for the current namespace, if it exists. If it does not, create it.
"""
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
# If the logger already has the two handlers we've set, no need to add more.
if len(logger.handlers) < 2:
logger.addHandler(get_console_handler(debug))
if create_log_file != False:
logger.addHandler(get_file_handler(debug, log_file_name=log_file_name))
# If debugging is not true, we don't want to output DEBUG information to the console.
if debug != False:
coloredlogs.install(
level="DEBUG",
logger=logger,
datefmt=DATE_FMT,
fmt=FMT,
field_styles=CUSTOM_FIELD_STYLES,
)
logger.debug("Added the debug logger to the console output...")
else:
coloredlogs.install(
level="INFO",
logger=logger,
datefmt=DATE_FMT,
fmt=FMT,
field_styles=CUSTOM_FIELD_STYLES,
)
# With this pattern, it's rarely necessary to propagate the error up to parent.
logger.propagate = False
logger.debug("Returning logger to process...")
return logger