diff --git a/.travis.yml b/.travis.yml index 9fb3383..f8a5233 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 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 .. diff --git a/README.rst b/README.rst index ca033ee..12acbc2 100644 --- a/README.rst +++ b/README.rst @@ -31,13 +31,13 @@ Usage Example .. code-block:: python - from adafruit_logger import Logger, ERROR, INFO + import adafruit_logging as 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') Contributing diff --git a/adafruit_logger.py b/adafruit_logging.py similarity index 82% rename from adafruit_logger.py rename to adafruit_logging.py index a0c0f05..d1a2df8 100644 --- a/adafruit_logger.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. """ -`adafruit_logger` +`adafruit_logging` ================================================================================ Logging module for CircuitPython @@ -41,6 +41,7 @@ """ #pylint:disable=redefined-outer-name,consider-using-enumerate,no-self-use +#pylint:disable=invalid-name import time @@ -65,7 +66,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] @@ -79,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. @@ -108,30 +107,48 @@ def emit(self, level, msg): # The level module-global variables get created when loaded #pylint:disable=undefined-variable +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] + 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 - - @property - def level(self): - """The level.""" - return self._level - - @level.setter - def level(self, value): + 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. @@ -140,8 +157,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/docs/api.rst b/docs/api.rst index d437289..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:: adafruit_logger +.. automodule:: adafruit_logging :members: diff --git a/docs/conf.py b/docs/conf.py index a9c1c30..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"] +#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 82d6a36..432b48d 100644 --- a/examples/logger_simpletest.py +++ b/examples/logger_simpletest.py @@ -1,8 +1,10 @@ #pylint:disable=undefined-variable,wildcard-import,no-name-in-module -from adafruit_logger import Logger, ERROR, INFO +#pylint:disable=no-member -logger = Logger() +import adafruit_logging as logging -logger.level = ERROR -logger.log(INFO, 'Info message') -logger.log(ERROR, 'Error message') +logger = logging.getLogger('test') + +logger.setLevel(logging.ERROR) +logger.info('Info message') +logger.error('Error message') diff --git a/setup.py b/setup.py index aeab7af..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=['adafruit_logger'], + py_modules=['adafruit_logging'], )