-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_logger.py
More file actions
29 lines (24 loc) · 945 Bytes
/
app_logger.py
File metadata and controls
29 lines (24 loc) · 945 Bytes
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
import logging
import build
def init(log_file=None, log_level=logging.INFO):
"""
get a logger that prints to a log_file, and to stdout/terminal. Is root logger so returns nothing
:param log_file:
:param log_level for logger
"""
if not log_file:
log_file = build.__name__ + '.log'
# we don't require finely grained contextual info, so using straight string modifier instead of filters
formatter = logging.Formatter(
'%(asctime)s [{}] %(levelname)s %(module)s - %(funcName)s: %(message)s'.format(build.__version__))
log = logging.getLogger()
log.setLevel(log_level)
if len(log.handlers) > 0:
log.handlers = []
file_handler = logging.FileHandler(log_file)
file_handler.setFormatter(formatter)
log.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setFormatter(formatter)
log.addHandler(console_handler)
return log