-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
126 lines (103 loc) · 3.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import logging
import sys
import os
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)
CREATE_LOGFILE = True
# If the operating system is Windows, have a different path for the log, else it is linux
if os.name == "nt":
LOG_FILE = "scriptoutput.log"
else:
LOG_FILE = "scriptoutput.log"
# This is for coloredlogs, requires a dictionary
# https://coloredlogs.readthedocs.io/en/latest/api.html#coloredlogs.DEFAULT_FIELD_STYLES
# You can also change directories into the site-packages and then run the following command to see what output looks like for your terminal
# 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"},
}
# We don't want to create a logfile
# if CREATE_LOGFILE != False:
# if os.name == 'nt':
# try:
# os.makedirs("")
# except:
# pass
# else:
# try:
# os.makedirs("/var/log/")
# except:
# pass
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, log_file_name=LOG_FILE):
"""
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.
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(FILE_FORMATTER)
return file_handler
def get_logger(
logger_name="Template Repository Logger", log_file_name=LOG_FILE, debug=False
):
"""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_LOGFILE != 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