2023-03-10 08:43:18 -05:00
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
def getlogger():
|
2023-04-17 09:20:13 -04:00
|
|
|
# create a custom logger if no log handler
|
2023-03-10 08:43:18 -05:00
|
|
|
logger = logging.getLogger(__name__)
|
2023-04-17 09:20:13 -04:00
|
|
|
if not logger.hasHandlers():
|
|
|
|
logger.setLevel(logging.INFO)
|
|
|
|
# create handlers
|
|
|
|
warn_handler = logging.StreamHandler()
|
|
|
|
info_handler = logging.StreamHandler()
|
|
|
|
error_handler = logging.FileHandler('bot.log', mode='a')
|
|
|
|
warn_handler.setLevel(logging.WARNING)
|
|
|
|
error_handler.setLevel(logging.ERROR)
|
|
|
|
info_handler.setLevel(logging.INFO)
|
2023-03-10 08:43:18 -05:00
|
|
|
|
2023-04-17 09:20:13 -04:00
|
|
|
# create formatters
|
|
|
|
warn_format = logging.Formatter(
|
2023-04-20 03:39:14 -04:00
|
|
|
'%(asctime)s - %(funcName)s - %(levelname)s - %(message)s')
|
2023-04-17 09:20:13 -04:00
|
|
|
error_format = logging.Formatter(
|
|
|
|
'%(asctime)s - %(name)s - %(funcName)s - %(levelname)s - %(message)s')
|
2023-04-20 03:39:14 -04:00
|
|
|
info_format = logging.Formatter(
|
|
|
|
'%(asctime)s - %(levelname)s - %(message)s')
|
2023-03-10 08:43:18 -05:00
|
|
|
|
2023-04-17 09:20:13 -04:00
|
|
|
# set formatter
|
|
|
|
warn_handler.setFormatter(warn_format)
|
|
|
|
error_handler.setFormatter(error_format)
|
|
|
|
info_handler.setFormatter(info_format)
|
2023-03-10 08:43:18 -05:00
|
|
|
|
2023-04-17 09:20:13 -04:00
|
|
|
# add handlers to logger
|
|
|
|
logger.addHandler(warn_handler)
|
|
|
|
logger.addHandler(error_handler)
|
|
|
|
logger.addHandler(info_handler)
|
2023-03-10 08:43:18 -05:00
|
|
|
|
|
|
|
return logger
|