Skip to content

Allow use of root logger #40

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

Merged
merged 3 commits into from
Oct 10, 2022
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
23 changes: 11 additions & 12 deletions adafruit_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,8 @@
from collections import namedtuple

try:
from typing import Optional

try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol
from typing import Optional, Hashable
from typing_extensions import Protocol

class WriteableStream(Protocol):
"""Any stream that can ``write`` strings"""
Expand Down Expand Up @@ -245,20 +241,23 @@ def emit(self, record: LogRecord) -> None:
logger_cache = {}


def _addLogger(logger_name: str) -> None:
def _addLogger(logger_name: Hashable) -> None:
"""Adds the logger if it doesn't already exist"""
if logger_name not in logger_cache:
new_logger = Logger(logger_name)
new_logger.addHandler(StreamHandler())
logger_cache[logger_name] = new_logger


def getLogger(logger_name: str) -> "Logger":
def getLogger(logger_name: Hashable = "") -> "Logger":
"""Create or retrieve a logger by name; only retrieves loggers
made using this function; if a Logger with this name does not
exist it is created

:param str logger_name: The name of the `Logger` to create/retrieve.
:param Hashable logger_name: The name of the `Logger` to create/retrieve, this
is typically a ``str``. If none is provided, the single root logger will
be created/retrieved. Note that unlike CPython, a blank string will also
access the root logger.
"""
_addLogger(logger_name)
return logger_cache[logger_name]
Expand All @@ -267,12 +266,12 @@ def getLogger(logger_name: str) -> "Logger":
class Logger:
"""The actual logger that will provide the logging API.

:param str name: The name of the logger, typically assigned by the
value from `getLogger`
:param Hashable name: The name of the logger, typically assigned by the
value from `getLogger`; this is typically a ``str``
:param int level: (optional) The log level, default is ``NOTSET``
"""

def __init__(self, name: str, level: int = NOTSET) -> None:
def __init__(self, name: Hashable, level: int = NOTSET) -> None:
"""Create an instance."""
self._level = level
self.name = name
Expand Down