-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog_utils.py
57 lines (45 loc) · 1.44 KB
/
log_utils.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
import logging
from logging.handlers import RotatingFileHandler
log_format = '%(asctime)s - %(name)s - %(levelname)s - ' \
'%(funcName)s(%(lineno)d)- %(message)s'
"""
author: Adam Dziedzic [email protected]
"""
def set_up_logging(log_file, is_debug=False):
handlers = [get_console_handler(), get_log_file_handler(log_file)]
level = logging.INFO
if is_debug:
level = logging.DEBUG
logging.basicConfig(level=level, format=log_format,
handlers=handlers)
logging.info("started logging to: " + log_file)
def get_log_file_handler(log_file):
"""
Log into a file.
:param log_file: the name of the file for logging
:return: the handler to log into file
"""
# https://goo.gl/FxA4Mh
fh = RotatingFileHandler(log_file, mode='a', maxBytes=5 * 1024 * 1024,
backupCount=3, encoding=None, delay=0)
# create formatter
formatter = logging.Formatter(log_format)
# add formatter
fh.setFormatter(formatter)
return fh
def get_console_handler():
"""
Log into the console.
:return: console log handler
"""
# create console handler and set level to debug
ch = logging.StreamHandler()
# create formatter
formatter = logging.Formatter(log_format)
# add formatter
ch.setFormatter(formatter)
return ch
def get_logger(name=__name__):
# create logger
logger = logging.getLogger(name)
return logger