-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger.py
28 lines (20 loc) · 852 Bytes
/
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
import logging
import sys
from logging.handlers import TimedRotatingFileHandler
FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
def get_console_handler():
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(FORMATTER)
return console_handler
def get_file_handler(log_file):
file_handler = TimedRotatingFileHandler(log_file, when='midnight')
file_handler.setFormatter(FORMATTER)
return file_handler
def get_logger(logger_name, log_file, log_level):
logger = logging.getLogger(logger_name)
logger.setLevel(log_level) # better to have too much log than not enough
logger.addHandler(get_console_handler())
logger.addHandler(get_file_handler(log_file))
# with this pattern, it's rarely necessary to propagate the error up to parent
logger.propagate = False
return logger