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

Configurable log level #500

Merged
merged 3 commits into from
Mar 15, 2024
Merged
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
4 changes: 3 additions & 1 deletion mmpy_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ def _register_logger(self):
**{
"format": self.settings.LOG_FORMAT,
"datefmt": self.settings.LOG_DATE_FORMAT,
"level": logging.DEBUG if self.settings.DEBUG else logging.INFO,
"level": (
logging.DEBUG if self.settings.DEBUG else self.settings.LOG_LEVEL
),
"filename": self.settings.LOG_FILE,
"filemode": "w",
}
Expand Down
9 changes: 9 additions & 0 deletions mmpy_bot/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections
import logging
import os
import warnings
from dataclasses import dataclass, field, fields
Expand Down Expand Up @@ -59,6 +60,7 @@ class Settings:
DEBUG: bool = False
# Respond to channel message "!help" (without @bot)
RESPOND_CHANNEL_HELP: bool = False
LOG_LEVEL: int = logging.INFO
LOG_FILE: Optional[str] = None
LOG_FORMAT: str = "[%(asctime)s][%(name)s][%(levelname)s] %(message)s"
LOG_DATE_FORMAT: str = "%m/%d/%Y %H:%M:%S"
Expand Down Expand Up @@ -91,6 +93,13 @@ def __post_init__(self):
)
self.MATTERMOST_API_PATH = self.MATTERMOST_API_PATH[: -len(api_url)]

if self.DEBUG:
warnings.warn(
"DEBUG has been deprecated and will be removed in a future release. "
"Set LOG_LEVEL to logging.DEBUG to increase verbosity.",
DeprecationWarning,
)

def _check_environment_variables(self):
for f in fields(self):
if f.name in os.environ:
Expand Down
3 changes: 2 additions & 1 deletion tests/unit_tests/bot_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from unittest import mock

import pytest
Expand All @@ -12,7 +13,7 @@
def bot():
# Patch login to avoid sending requests to the internet
with mock.patch("mmpy_bot.driver.Driver.login") as login:
bot = Bot(plugins=[ExamplePlugin()], settings=Settings(DEBUG=True))
bot = Bot(plugins=[ExamplePlugin()], settings=Settings(LOG_LEVEL=logging.DEBUG))
login.assert_called_once()
yield bot
bot.stop() # if the bot was started, stop it
Expand Down
Loading