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

Allow disabling default logging handlers #251

Merged
merged 2 commits into from
Aug 18, 2021
Merged
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
34 changes: 21 additions & 13 deletions mmpy_bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,17 @@ def __init__(
self,
settings: Optional[Settings] = None,
plugins: Optional[Sequence[Plugin]] = None,
enable_logging: bool = True,
):
if plugins is None:
plugins = [ExamplePlugin(), WebHookExample()]
# Use default settings if none were specified.
self.settings = settings or Settings()
logging.basicConfig(
**{
"format": self.settings.LOG_FORMAT,
"datefmt": "%m/%d/%Y %H:%M:%S",
"level": logging.DEBUG if self.settings.DEBUG else logging.INFO,
"filename": self.settings.LOG_FILE,
"filemode": "w",
}
)
# define and add a Handler which writes log messages to the sys.stdout
self.console = logging.StreamHandler(stream=sys.stdout)
self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT))
logging.getLogger("").addHandler(self.console)

if enable_logging:
self._register_logger()
else:
self.console = None

self.driver = Driver(
{
Expand All @@ -65,6 +58,21 @@ def __init__(

self.running = False

def _register_logger(self):
logging.basicConfig(
**{
"format": self.settings.LOG_FORMAT,
"datefmt": "%m/%d/%Y %H:%M:%S",
"level": logging.DEBUG if self.settings.DEBUG else logging.INFO,
"filename": self.settings.LOG_FILE,
"filemode": "w",
}
)
# define and add a Handler which writes log messages to the sys.stdout
self.console = logging.StreamHandler(stream=sys.stdout)
self.console.setFormatter(logging.Formatter(self.settings.LOG_FORMAT))
logging.getLogger("").addHandler(self.console)

def _initialize_plugins(self, plugins: Sequence[Plugin]):
for plugin in plugins:
plugin.initialize(self.driver, self.settings)
Expand Down