Skip to content

Commit

Permalink
Merge pull request #574 from anvilistas/messaging-logger
Browse files Browse the repository at this point in the history
Add default null logger to messaging
  • Loading branch information
meatballs authored Nov 9, 2024
2 parents 78bb408 + f6434c6 commit 265c331
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 23 additions & 25 deletions client_code/messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -48,40 +50,36 @@ 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)
if channel in self.subscribers:
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)",
)

0 comments on commit 265c331

Please sign in to comment.