Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce log file feature #67

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion webex_bot/commands/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion webex_bot/commands/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion webex_bot/models/command.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
38 changes: 33 additions & 5 deletions webex_bot/webex_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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.
Expand All @@ -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-%dT%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,
Expand Down
5 changes: 4 additions & 1 deletion webex_bot/websockets/webex_websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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())
Expand Down
Loading