From 63670125a01d70ca878ef4618b8d16dd22300b90 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Mon, 11 Mar 2019 20:34:36 -0400 Subject: [PATCH 1/9] Make more like CPython's logger --- README.rst | 10 +++++----- docs/api.rst | 2 +- examples/logger_simpletest.py | 10 +++++----- adafruit_logger.py => logging.py | 25 ++++++++++++++----------- setup.py | 2 +- 5 files changed, 26 insertions(+), 23 deletions(-) rename adafruit_logger.py => logging.py (93%) diff --git a/README.rst b/README.rst index ca033ee..6cd8612 100644 --- a/README.rst +++ b/README.rst @@ -31,13 +31,13 @@ Usage Example .. code-block:: python - from adafruit_logger import Logger, ERROR, INFO + import logging - logger = Logger() + logger = getLogger('test') - logger.level = ERROR - logger.log(INFO, 'Info message') - logger.log(ERROR, 'Error message') + logger.setLevel(logging.ERROR) + logger.info('Info message') + logger.error('Error message') Contributing diff --git a/docs/api.rst b/docs/api.rst index d437289..0062cc2 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -4,5 +4,5 @@ .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) .. use this format as the module name: "adafruit_foo.foo" -.. automodule:: adafruit_logger +.. automodule:: logging :members: diff --git a/examples/logger_simpletest.py b/examples/logger_simpletest.py index 82d6a36..ca902c4 100644 --- a/examples/logger_simpletest.py +++ b/examples/logger_simpletest.py @@ -1,8 +1,8 @@ #pylint:disable=undefined-variable,wildcard-import,no-name-in-module -from adafruit_logger import Logger, ERROR, INFO +import logging -logger = Logger() +logger = logging.getLogger('test') -logger.level = ERROR -logger.log(INFO, 'Info message') -logger.log(ERROR, 'Error message') +logger.setLevel(logging.ERROR) +logger.info('Info message') +logger.error('Error message') diff --git a/adafruit_logger.py b/logging.py similarity index 93% rename from adafruit_logger.py rename to logging.py index a0c0f05..ffc9fe5 100644 --- a/adafruit_logger.py +++ b/logging.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ -`adafruit_logger` +`logging` ================================================================================ Logging module for CircuitPython @@ -65,7 +65,9 @@ def level_for(value): """ for i in range(len(LEVELS)): - if value < LEVELS[i][0]: + if value == LEVELS[i][0]: + return LEVELS[i][1] + elif value < LEVELS[i][0]: return LEVELS[i-1][1] return LEVELS[0][1] @@ -108,6 +110,13 @@ def emit(self, level, msg): # The level module-global variables get created when loaded #pylint:disable=undefined-variable +logger_cache = dict() + +def getLogger(name): + if name not in logger_cache: + logger_cache[name] = Logger() + return logger_cache[name] + class Logger(object): """Provide a logging api.""" @@ -123,13 +132,7 @@ def __init__(self, handler=None): else: self._handler = handler - @property - def level(self): - """The level.""" - return self._level - - @level.setter - def level(self, value): + def setLevel(self, value): self._level = value def log(self, level, format_string, *args): @@ -140,8 +143,8 @@ def log(self, level, format_string, *args): :param args: arguments to ``format_string.format()``, can be empty """ - if self._level != NOTSET and level >= self._level: - self._handler.emit(level, format_string.format(*args)) + if level >= self._level: + self._handler.emit(level, format_string % args) def debug(self, format_string, *args): """Log a debug message. diff --git a/setup.py b/setup.py index aeab7af..dc94fb6 100644 --- a/setup.py +++ b/setup.py @@ -59,5 +59,5 @@ # simple. Or you can use find_packages(). # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, # CHANGE `py_modules=['...']` TO `packages=['...']` - py_modules=['adafruit_logger'], + py_modules=['logging'], ) From 3748d1a68bc0ad94a615c9bae91b9375c859bc9f Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 12:11:34 -0400 Subject: [PATCH 2/9] Tweak handler handling --- logging.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/logging.py b/logging.py index ffc9fe5..bbfef39 100644 --- a/logging.py +++ b/logging.py @@ -113,6 +113,11 @@ def emit(self, level, msg): logger_cache = dict() def getLogger(name): + """Create or retrieve a logger by name. + + :param name: the name of the logger to create/retrieve + + """ if name not in logger_cache: logger_cache[name] = Logger() return logger_cache[name] @@ -120,21 +125,33 @@ def getLogger(name): class Logger(object): """Provide a logging api.""" - def __init__(self, handler=None): + def __init__(self): """Create an instance. :param handler: what to use to output messages. Defaults to a PrintHandler. """ self._level = NOTSET - if handler is None: - self._handler = PrintHandler() - else: - self._handler = handler + self._handler = PrintHandler() def setLevel(self, value): + """Set the logging cuttoff level. + + :param value: the lowest level to output + + """ self._level = value + def addHandler(self, hldr): + """Sets the handler of this logger to the specified handler. + *NOTE* this is slightly different from the CPython equivalent which adds + the handler rather than replaceing it. + + :param hldr: the handler + + """ + self._handler = hldr + def log(self, level, format_string, *args): """Log a message. From de4615681fa4d417119d20b4fa5104aa2b8276fe Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 12:15:47 -0400 Subject: [PATCH 3/9] Fix name --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 9fb3383..0b5faa2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ install: - pip install --force-reinstall pylint==1.9.2 script: - - pylint adafruit_logger.py + - pylint logging.py - ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py) - circuitpython-build-bundles --filename_prefix adafruit-circuitpython-logger --library_location . - cd docs && sphinx-build -E -W -b html . _build/html && cd .. From 238fa6f732042a068fe183362563ccbd48737f7d Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 12:38:05 -0400 Subject: [PATCH 4/9] Pylint tweaks --- examples/logger_simpletest.py | 2 ++ logging.py | 1 + 2 files changed, 3 insertions(+) diff --git a/examples/logger_simpletest.py b/examples/logger_simpletest.py index ca902c4..78a827e 100644 --- a/examples/logger_simpletest.py +++ b/examples/logger_simpletest.py @@ -1,4 +1,6 @@ #pylint:disable=undefined-variable,wildcard-import,no-name-in-module +#pylint:disable=no-member + import logging logger = logging.getLogger('test') diff --git a/logging.py b/logging.py index bbfef39..9274296 100644 --- a/logging.py +++ b/logging.py @@ -41,6 +41,7 @@ """ #pylint:disable=redefined-outer-name,consider-using-enumerate,no-self-use +#pylint:disable=invalid-name import time From 8bc4c3e218af6468411cf5370d122efedb162aae Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 13:15:54 -0400 Subject: [PATCH 5/9] Localtime is kind of pointless without an RTC, so just use monotonic --- logging.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/logging.py b/logging.py index 9274296..136feb7 100644 --- a/logging.py +++ b/logging.py @@ -82,11 +82,7 @@ def format(self, level, msg): :param msg: the message to log """ - now = time.localtime() - time_vals = (now.tm_year, now.tm_mon, now.tm_mday, - now.tm_hour, now.tm_min, now.tm_sec) - timestamp = '%4d/%02d/%02d %02d:%02d:%02d' % time_vals - return '{0}: {1} - {2}'.format(timestamp, level_for(level), msg) + return '{0}: {1} - {2}'.format(time.monotonic(), level_for(level), msg) def emit(self, level, msg): """Send a message where it should go. From 282d25a60ddf829899400d51c2960cebd35ddbd1 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 14:20:24 -0400 Subject: [PATCH 6/9] Try forcing it to use the local logging --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index a9c1c30..bb0a2f2 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,7 +20,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -# autodoc_mock_imports = ["digitalio", "busio"] +autodoc_mock_imports = ["digitalio", "busio", "logging"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From 38af8740bb5a2f172ac79fe9a637a752d82c00c3 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 14:58:09 -0400 Subject: [PATCH 7/9] Try forcing local logging module --- docs/api.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.rst b/docs/api.rst index 0062cc2..11cb6a7 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -4,5 +4,5 @@ .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) .. use this format as the module name: "adafruit_foo.foo" -.. automodule:: logging +.. automodule:: .logging :members: From ee6abb8ca46958b3ff177ae8d437a89a0c564ce1 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 15:24:28 -0400 Subject: [PATCH 8/9] rename logging -> adafruit_logging --- README.rst | 4 ++-- logging.py => adafruit_logging.py | 2 +- docs/api.rst | 2 +- docs/conf.py | 2 +- examples/logger_simpletest.py | 2 +- setup.py | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) rename logging.py => adafruit_logging.py (99%) diff --git a/README.rst b/README.rst index 6cd8612..12acbc2 100644 --- a/README.rst +++ b/README.rst @@ -31,9 +31,9 @@ Usage Example .. code-block:: python - import logging + import adafruit_logging as logging - logger = getLogger('test') + logger = logging.getLogger('test') logger.setLevel(logging.ERROR) logger.info('Info message') diff --git a/logging.py b/adafruit_logging.py similarity index 99% rename from logging.py rename to adafruit_logging.py index 136feb7..d1a2df8 100644 --- a/logging.py +++ b/adafruit_logging.py @@ -20,7 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ -`logging` +`adafruit_logging` ================================================================================ Logging module for CircuitPython diff --git a/docs/api.rst b/docs/api.rst index 11cb6a7..aced610 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -4,5 +4,5 @@ .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) .. use this format as the module name: "adafruit_foo.foo" -.. automodule:: .logging +.. automodule:: adafruit_logging :members: diff --git a/docs/conf.py b/docs/conf.py index bb0a2f2..a9c9fba 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -20,7 +20,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -autodoc_mock_imports = ["digitalio", "busio", "logging"] +#autodoc_mock_imports = ["digitalio", "busio"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} diff --git a/examples/logger_simpletest.py b/examples/logger_simpletest.py index 78a827e..432b48d 100644 --- a/examples/logger_simpletest.py +++ b/examples/logger_simpletest.py @@ -1,7 +1,7 @@ #pylint:disable=undefined-variable,wildcard-import,no-name-in-module #pylint:disable=no-member -import logging +import adafruit_logging as logging logger = logging.getLogger('test') diff --git a/setup.py b/setup.py index dc94fb6..acaadf2 100644 --- a/setup.py +++ b/setup.py @@ -59,5 +59,5 @@ # simple. Or you can use find_packages(). # TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER, # CHANGE `py_modules=['...']` TO `packages=['...']` - py_modules=['logging'], + py_modules=['adafruit_logging'], ) From 07a4070b51b369b47235e6e9477c4c95c9f81f03 Mon Sep 17 00:00:00 2001 From: Dave Astels Date: Wed, 13 Mar 2019 15:26:56 -0400 Subject: [PATCH 9/9] Missed a spot --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 0b5faa2..f8a5233 100644 --- a/.travis.yml +++ b/.travis.yml @@ -42,7 +42,7 @@ install: - pip install --force-reinstall pylint==1.9.2 script: - - pylint logging.py + - pylint adafruit_logging.py - ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py) - circuitpython-build-bundles --filename_prefix adafruit-circuitpython-logger --library_location . - cd docs && sphinx-build -E -W -b html . _build/html && cd ..