diff --git a/README.md b/README.md index bab83e7..879f656 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ from webex_bot.formatting import quote_info from webex_bot.models.command import Command from webex_bot.models.response import response_from_adaptive_card -log = logging.getLogger(__name__) +log = logging.getLogger("webex_bot") class EchoCommand(Command): diff --git a/webex_bot/commands/echo.py b/webex_bot/commands/echo.py index 4b3bf39..eb66b35 100644 --- a/webex_bot/commands/echo.py +++ b/webex_bot/commands/echo.py @@ -8,7 +8,7 @@ from webex_bot.models.command import Command from webex_bot.models.response import response_from_adaptive_card -log = logging.getLogger(__name__) +log = logging.getLogger("webex_bot") class EchoCommand(Command): diff --git a/webex_bot/commands/help.py b/webex_bot/commands/help.py index 8ed6f3f..04a4b33 100644 --- a/webex_bot/commands/help.py +++ b/webex_bot/commands/help.py @@ -7,7 +7,7 @@ from webex_bot.models.command import Command, COMMAND_KEYWORD_KEY from webex_bot.models.response import response_from_adaptive_card -log = logging.getLogger(__name__) +log = logging.getLogger("webex_bot") HELP_COMMAND_KEYWORD = "help" diff --git a/webex_bot/models/command.py b/webex_bot/models/command.py index fc20552..94fa258 100644 --- a/webex_bot/models/command.py +++ b/webex_bot/models/command.py @@ -1,7 +1,7 @@ import logging from abc import ABC, abstractmethod -log = logging.getLogger(__name__) +log = logging.getLogger("webex_bot") CALLBACK_KEYWORD_KEY = 'callback_keyword' COMMAND_KEYWORD_KEY = "command_keyword" diff --git a/webex_bot/webex_bot.py b/webex_bot/webex_bot.py index ecf346c..fef1eb9 100644 --- a/webex_bot/webex_bot.py +++ b/webex_bot/webex_bot.py @@ -15,7 +15,7 @@ from webex_bot.models.response import Response from webex_bot.websockets.webex_websocket_client import WebexWebsocketClient, DEFAULT_DEVICE_URL -log = logging.getLogger(__name__) +log = logging.getLogger("webex_bot") class WebexBot(WebexWebsocketClient): @@ -32,6 +32,9 @@ def __init__(self, threads=True, help_command=None, log_level="INFO", + generate_log_file=False, + log_file_name=None, + log_file_log_level="DEBUG", proxies=None): """ Initialise WebexBot. @@ -46,14 +49,39 @@ def __init__(self, @param bot_help_subtitle: Text to show in the help card. @param threads: If True, respond to msg by creating a thread. @param help_command: If None, use internal HelpCommand, otherwise override. - @param log_level: Set loggin level. + @param log_level: Set logging level. + @param generate_log_file: If True, create a log file. + @param log_file_name: Set name for the log file. + @param log_file_log_level: Set logging level for the log file. @param proxies: Dictionary of proxies for connections. """ coloredlogs.install(level=os.getenv("LOG_LEVEL", log_level), - fmt='%(asctime)s [%(levelname)s] ' - '[%(module)s.%(name)s.%(funcName)' - 's]:%(lineno)s %(message)s') + fmt="%(asctime)s [%(levelname)s] " + "[%(module)s.%(name)s.%(funcName)" + "s()]:%(lineno)s [PID:%(process)d, " + "TID:%(thread)d] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S.%f") + + if generate_log_file: + # Default to DEBUG if invalid + log_file_log_level = getattr(logging, + log_file_log_level, + logging.DEBUG) + # Specify log file location + handler = logging.FileHandler(log_file_name) + # Define Formatter + formatter = logging.Formatter(fmt="%(asctime)s.%(msecs)03d " + "[%(levelname)s] [%(module)s." + "%(name)s.%(funcName)s()]:" + "%(lineno)s [PID:%(process)d, " + "TID:%(thread)d] %(message)s", + datefmt="%Y-%m-%d %H:%M:%S") + handler.setFormatter(formatter) + log.addHandler(handler) + log.setLevel(log_file_log_level) + log.info("Initiated logging mechanism...") + log.info("Registering bot with Webex cloud") WebexWebsocketClient.__init__(self, teams_bot_token, diff --git a/webex_bot/websockets/webex_websocket_client.py b/webex_bot/websockets/webex_websocket_client.py index f6ee65c..b3e3586 100644 --- a/webex_bot/websockets/webex_websocket_client.py +++ b/webex_bot/websockets/webex_websocket_client.py @@ -17,7 +17,7 @@ Proxy = None proxy_connect = None -logger = logging.getLogger(__name__) +logger = logging.getLogger("webex_bot") DEFAULT_DEVICE_URL = "https://wdm-a.wbx2.com/wdm/api/v1" @@ -183,6 +183,9 @@ def _get_device_info(self, check_existing=True): def stop(self): def terminate(): + logger.info("Stopping the bot...") + logger.info("Logging mechanism will be shutdown!") + logging.shutdown() raise SystemExit() asyncio.get_event_loop().create_task(terminate())