Skip to content

Null Logger #18

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
Mar 17, 2021
Merged
Show file tree
Hide file tree
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
57 changes: 50 additions & 7 deletions adafruit_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,35 @@ def emit(self, level, msg):
# pylint:disable=undefined-variable

logger_cache = dict()
null_logger = None


# pylint:disable=global-statement
def getLogger(name):
"""Create or retrieve a logger by name.

:param name: the name of the logger to create/retrieve
:param name: the name of the logger to create/retrieve None will cause the
NullLogger instance to be returned.

"""
global null_logger
if not name or name == "":
if not null_logger:
null_logger = NullLogger()
return null_logger

if name not in logger_cache:
logger_cache[name] = Logger()
return logger_cache[name]


# pylint:enable=global-statement


class Logger:
"""Provide a logging api."""

def __init__(self):
"""Create an instance.

:param handler: what to use to output messages. Defaults to a PrintHandler.

"""
"""Create an instance."""
self._level = NOTSET
self._handler = PrintHandler()

Expand Down Expand Up @@ -201,3 +208,39 @@ def critical(self, format_string, *args):

"""
self.log(CRITICAL, format_string, *args)


class NullLogger:
"""Provide an empty logger.
This can be used in place of a real logger to more efficiently disable logging."""

def __init__(self):
"""Dummy implementation."""

def setLevel(self, value):
"""Dummy implementation."""

def getEffectiveLevel(self):
"""Dummy implementation."""
return NOTSET

def addHandler(self, hldr):
"""Dummy implementation."""

def log(self, level, format_string, *args):
"""Dummy implementation."""

def debug(self, format_string, *args):
"""Dummy implementation."""

def info(self, format_string, *args):
"""Dummy implementation."""

def warning(self, format_string, *args):
"""Dummy implementation."""

def error(self, format_string, *args):
"""Dummy implementation."""

def critical(self, format_string, *args):
"""Dummy implementation."""
22 changes: 21 additions & 1 deletion examples/logging_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@
# SPDX-License-Identifier: MIT

# pylint:disable=undefined-variable,wildcard-import,no-name-in-module
# pylint:disable=no-member
# pylint:disable=no-member,invalid-name

"""Briefly exercise the logger and null logger."""

import adafruit_logging as logging

# This should produce an error output

logger = logging.getLogger("test")

logger.setLevel(logging.ERROR)
logger.info("Info message")
logger.error("Error message")

# This should produce no output

null_logger = logging.getLogger(None)

null_logger.setLevel(logging.ERROR)
null_logger.info("Info message")
null_logger.error("Error message")

# This should produce no output

null_logger = logging.getLogger("")

null_logger.setLevel(logging.ERROR)
null_logger.info("Info message")
null_logger.error("Error message")