Skip to content

Commit

Permalink
Configurable log level
Browse files Browse the repository at this point in the history
Backwards-compatible. Closes #498.
  • Loading branch information
Roy-Orbison committed Mar 15, 2024
1 parent 7b5c6f1 commit d79266e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
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,6 +1,7 @@
import collections
import os
import warnings
import logging
from dataclasses import dataclass, field, fields
from typing import Optional, Sequence, Union, get_args, get_origin # type: ignore

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,6 +1,7 @@
from unittest import mock

import pytest
import logging

from mmpy_bot import Bot, ExamplePlugin, Settings
from mmpy_bot.plugins import PluginManager
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

0 comments on commit d79266e

Please sign in to comment.