-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging_func.py
32 lines (30 loc) · 1.35 KB
/
logging_func.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
import logging
from logging.handlers import RotatingFileHandler
import os
def set_up_logging(loglevel=logging.DEBUG, saveLogFiles=True, saveLogFolder='logs', maxBytes=100000, backupCount=10):
"""
Set up logging for dequa bot.
:param loglevel: loglevel for the nky_util loggers
:param saveLogFiles: if True adds the rotating file handler
:param saveLogFolder: folder where to save the log files
:param maxBytes: maximum size of each log file
:param backupCount: maximum number of log files
:return: the logger object
"""
logger = logging.getLogger('dequa_bot')
logger.handlers.clear()
logger.setLevel(loglevel)
ch = logging.StreamHandler()
formatter = logging.Formatter('[%(asctime)s] [%(name)s:%(filename)s:%(lineno)d] [%(levelname)s] %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
if saveLogFiles:
if not os.path.exists(saveLogFolder):
os.mkdir(saveLogFolder)
logfile = os.path.join(saveLogFolder, 'dequa_bot.log')
file_handler = RotatingFileHandler(logfile, maxBytes=maxBytes, backupCount=backupCount)
formatter = logging.Formatter('[%(asctime)s] [%(name)s:%(filename)s:%(lineno)d] [%(levelname)s] %(message)s')
file_handler.setFormatter(formatter)
file_handler.setLevel(loglevel)
logger.addHandler(file_handler)
return logger