-
Notifications
You must be signed in to change notification settings - Fork 3
/
log_helper.py
26 lines (23 loc) · 912 Bytes
/
log_helper.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
import logging
def setup_logger(name, level, log_to_file):
"""
Create and configure logger
Create console handler and logfile handlers
:param name: logger name and logfile name if also log to file
:param level: change to logging.DEBUG for verbose output, change to logging.INFO for standard output
:param log_to_file: boolean
:return: created and configured logger object
"""
logger = logging.getLogger(name)
logger.setLevel(level)
console = logging.StreamHandler()
console.setLevel(level)
formatter = logging.Formatter('[%(asctime)s - %(levelname)s] %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
if log_to_file:
logfile = logging.FileHandler('{0}.log'.format(name), mode='w')
logfile.setLevel(logging.DEBUG)
logfile.setFormatter(formatter)
logger.addHandler(logfile)
return logger