Skip to content

Commit 6355873

Browse files
authored
Merge pull request #4 from dastels/master
Make more like CPython's logger
2 parents 3d4a850 + 07a4070 commit 6355873

File tree

7 files changed

+55
-36
lines changed

7 files changed

+55
-36
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ install:
4242
- pip install --force-reinstall pylint==1.9.2
4343

4444
script:
45-
- pylint adafruit_logger.py
45+
- pylint adafruit_logging.py
4646
- ([[ ! -d "examples" ]] || pylint --disable=missing-docstring,invalid-name,bad-whitespace examples/*.py)
4747
- circuitpython-build-bundles --filename_prefix adafruit-circuitpython-logger --library_location .
4848
- cd docs && sphinx-build -E -W -b html . _build/html && cd ..

README.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ Usage Example
3131

3232
.. code-block:: python
3333
34-
from adafruit_logger import Logger, ERROR, INFO
34+
import adafruit_logging as logging
3535
36-
logger = Logger()
36+
logger = logging.getLogger('test')
3737
38-
logger.level = ERROR
39-
logger.log(INFO, 'Info message')
40-
logger.log(ERROR, 'Error message')
38+
logger.setLevel(logging.ERROR)
39+
logger.info('Info message')
40+
logger.error('Error message')
4141
4242
4343
Contributing

adafruit_logger.py renamed to adafruit_logging.py

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222
"""
23-
`adafruit_logger`
23+
`adafruit_logging`
2424
================================================================================
2525
2626
Logging module for CircuitPython
@@ -41,6 +41,7 @@
4141
4242
"""
4343
#pylint:disable=redefined-outer-name,consider-using-enumerate,no-self-use
44+
#pylint:disable=invalid-name
4445

4546
import time
4647

@@ -65,7 +66,9 @@ def level_for(value):
6566
6667
"""
6768
for i in range(len(LEVELS)):
68-
if value < LEVELS[i][0]:
69+
if value == LEVELS[i][0]:
70+
return LEVELS[i][1]
71+
elif value < LEVELS[i][0]:
6972
return LEVELS[i-1][1]
7073
return LEVELS[0][1]
7174

@@ -79,11 +82,7 @@ def format(self, level, msg):
7982
:param msg: the message to log
8083
8184
"""
82-
now = time.localtime()
83-
time_vals = (now.tm_year, now.tm_mon, now.tm_mday,
84-
now.tm_hour, now.tm_min, now.tm_sec)
85-
timestamp = '%4d/%02d/%02d %02d:%02d:%02d' % time_vals
86-
return '{0}: {1} - {2}'.format(timestamp, level_for(level), msg)
85+
return '{0}: {1} - {2}'.format(time.monotonic(), level_for(level), msg)
8786

8887
def emit(self, level, msg):
8988
"""Send a message where it should go.
@@ -108,30 +107,48 @@ def emit(self, level, msg):
108107
# The level module-global variables get created when loaded
109108
#pylint:disable=undefined-variable
110109

110+
logger_cache = dict()
111+
112+
def getLogger(name):
113+
"""Create or retrieve a logger by name.
114+
115+
:param name: the name of the logger to create/retrieve
116+
117+
"""
118+
if name not in logger_cache:
119+
logger_cache[name] = Logger()
120+
return logger_cache[name]
121+
111122
class Logger(object):
112123
"""Provide a logging api."""
113124

114-
def __init__(self, handler=None):
125+
def __init__(self):
115126
"""Create an instance.
116127
117128
:param handler: what to use to output messages. Defaults to a PrintHandler.
118129
119130
"""
120131
self._level = NOTSET
121-
if handler is None:
122-
self._handler = PrintHandler()
123-
else:
124-
self._handler = handler
125-
126-
@property
127-
def level(self):
128-
"""The level."""
129-
return self._level
130-
131-
@level.setter
132-
def level(self, value):
132+
self._handler = PrintHandler()
133+
134+
def setLevel(self, value):
135+
"""Set the logging cuttoff level.
136+
137+
:param value: the lowest level to output
138+
139+
"""
133140
self._level = value
134141

142+
def addHandler(self, hldr):
143+
"""Sets the handler of this logger to the specified handler.
144+
*NOTE* this is slightly different from the CPython equivalent which adds
145+
the handler rather than replaceing it.
146+
147+
:param hldr: the handler
148+
149+
"""
150+
self._handler = hldr
151+
135152
def log(self, level, format_string, *args):
136153
"""Log a message.
137154
@@ -140,8 +157,8 @@ def log(self, level, format_string, *args):
140157
:param args: arguments to ``format_string.format()``, can be empty
141158
142159
"""
143-
if self._level != NOTSET and level >= self._level:
144-
self._handler.emit(level, format_string.format(*args))
160+
if level >= self._level:
161+
self._handler.emit(level, format_string % args)
145162

146163
def debug(self, format_string, *args):
147164
"""Log a debug message.

docs/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
.. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py)
55
.. use this format as the module name: "adafruit_foo.foo"
66
7-
.. automodule:: adafruit_logger
7+
.. automodule:: adafruit_logging
88
:members:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# Uncomment the below if you use native CircuitPython modules such as
2121
# digitalio, micropython and busio. List the modules you use. Without it, the
2222
# autodoc module docs will fail to generate with a warning.
23-
# autodoc_mock_imports = ["digitalio", "busio"]
23+
#autodoc_mock_imports = ["digitalio", "busio"]
2424

2525

2626
intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}

examples/logger_simpletest.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#pylint:disable=undefined-variable,wildcard-import,no-name-in-module
2-
from adafruit_logger import Logger, ERROR, INFO
2+
#pylint:disable=no-member
33

4-
logger = Logger()
4+
import adafruit_logging as logging
55

6-
logger.level = ERROR
7-
logger.log(INFO, 'Info message')
8-
logger.log(ERROR, 'Error message')
6+
logger = logging.getLogger('test')
7+
8+
logger.setLevel(logging.ERROR)
9+
logger.info('Info message')
10+
logger.error('Error message')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@
5959
# simple. Or you can use find_packages().
6060
# TODO: IF LIBRARY FILES ARE A PACKAGE FOLDER,
6161
# CHANGE `py_modules=['...']` TO `packages=['...']`
62-
py_modules=['adafruit_logger'],
62+
py_modules=['adafruit_logging'],
6363
)

0 commit comments

Comments
 (0)