-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
63 lines (50 loc) · 1.98 KB
/
logger.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
import logging
import os
import sys
def logger(name, file_output=True, stdout_level=logging.INFO,stdout=True):
"""
Creates logger object using default file and stdout settings. File outputs split into
debug and info thresholds.
Args:
name (str): Logger name
file_output (bool): Output logs to file for records
stdout_level (logging.object): Threshold for stdout
stdout (bool): Output logs to stdout
Returns:
"""
handlers = []
# Stdout settings
stdout_handler = logging.StreamHandler(sys.stdout)
stdout_formatter = logging.Formatter('[%(asctime)s] %(message)s', '%Y-%m-%d %H:%M:%S')
stdout_handler.setFormatter(stdout_formatter)
# Stdout threshold
stdout_handler.setLevel(stdout_level)
# Collate settings, initialise
if stdout:
handlers.append(stdout_handler)
# Output logs to files if applicable
if file_output:
# File settings
file_formatter = logging.Formatter('[%(asctime)s] - %(name)s - %(levelname)s - %(message)s',
'%Y-%m-%d %H:%M:%S')
# Generate log folder
log_root_folder = os.getcwd()
# log_root_folder = os.path.dirname(os.path.dirname(__file__))
log_folder = os.path.join(log_root_folder, './logs')
os.makedirs(log_folder, exist_ok=True)
# debug.log file settings
debug_file_handler = logging.FileHandler(filename='./logs/debug.log')
debug_file_handler.setFormatter(file_formatter)
debug_file_handler.setLevel(logging.DEBUG)
# info.log file settings
info_file_handler = logging.FileHandler(filename='./logs/info.log')
info_file_handler.setFormatter(file_formatter)
info_file_handler.setLevel(logging.INFO)
handlers += [
info_file_handler,
debug_file_handler
]
logging.basicConfig(handlers=handlers)
logger_ = logging.getLogger(name)
logger_.setLevel(logging.DEBUG)
return logger_