diff --git a/CHANGELOG.md b/CHANGELOG.md index 351c461b..3f1c1b00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ ## Enhancements * tabs - tab color can now be a css var and better support for css colors in general https://github.com/anvilistas/anvil-extras/pull/570 +* messaging - support custom loggers in messaging module + https://github.com/anvilistas/anvil-extras/issues/569 + +## Deprecations +- messaging - with_logging argument is deprecated and maybe removed in a future version + logging messages is now off by default - to turn it on provide a custom `logger` argument + https://github.com/anvilistas/anvil-extras/pull/572/files # v3.0.0 diff --git a/client_code/messaging.py b/client_code/messaging.py index fc19a00f..b7541bc9 100644 --- a/client_code/messaging.py +++ b/client_code/messaging.py @@ -5,14 +5,17 @@ # # This software is published at https://github.com/anvilistas/anvil-extras -from logging import INFO -from logging import Logger as _Logger - +from .logging import INFO +from .logging import Logger as _Logger from .utils._warnings import warn as _warn __version__ = "3.0.0" +_null_logger = _Logger() +_null_logger.disabled = True + + class Message: def __init__(self, title, content=None): self.title = title @@ -26,11 +29,10 @@ def __init__(self, subscriber, handler): class Publisher: - default_logger = None default_log_level = INFO - def __init__(self, logger: _Logger = None, **kwargs): - self.logger = logger or self.default_logger + def __init__(self, *, logger: _Logger = None, **kwargs): + self.logger = logger or _null_logger self.subscribers = {} self._deprecation_warnings(**kwargs) @@ -48,22 +50,20 @@ def publish(self, channel, title, content=None, **kwargs): subscribers = self.subscribers.get(channel, []) for subscriber in subscribers: subscriber.handler(message) - if self.logger is not None: - self.logger.log( - self.default_log_level, - f"Published '{message.title}' message on '{channel}' channel to " - f"{len(subscribers)} subscriber(s)", - ) + self.logger.log( + self.default_log_level, + f"Published '{message.title}' message on '{channel}' channel to " + f"{len(subscribers)} subscriber(s)", + ) def subscribe(self, channel, subscriber, handler, **kwargs): self._deprecation_warnings(**kwargs) if channel not in self.subscribers: self.subscribers[channel] = [] self.subscribers[channel].append(Subscriber(subscriber, handler)) - if self.logger is not None: - self.logger.log( - self.default_log_level, f"Added subscriber to {channel} channel" - ) + self.logger.log( + self.default_log_level, f"Added subscriber to {channel} channel" + ) def unsubscribe(self, channel, subscriber, **kwargs): self._deprecation_warnings(**kwargs) @@ -71,17 +71,15 @@ def unsubscribe(self, channel, subscriber, **kwargs): self.subscribers[channel] = [ s for s in self.subscribers[channel] if s.subscriber != subscriber ] - if self.logger is not None: - self.logger.log( - self.default_log_level, f"Removed subscriber from {channel} channel" - ) + self.logger.log( + self.default_log_level, f"Removed subscriber from {channel} channel" + ) def close_channel(self, channel, **kwargs): self._deprecation_warnings(**kwargs) subscribers_count = len(self.subscribers[channel]) del self.subscribers[channel] - if self.logger is not None: - self.logger.log( - self.default_log_level, - f"{channel} closed ({subscribers_count} subscribers)", - ) + self.logger.log( + self.default_log_level, + f"{channel} closed ({subscribers_count} subscribers)", + )