Skip to content

Commit

Permalink
Fixed logging.*(…, deep="…")
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Jul 29, 2019
1 parent c5c32f2 commit 5800db4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
7 changes: 6 additions & 1 deletion scanpy/_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from enum import IntEnum
from pathlib import Path
from time import time
from logging import getLevelName
from typing import Tuple, Union, Any, List, Iterable, TextIO, Optional

from . import logging
Expand All @@ -15,7 +16,6 @@
'hint': 'HINT',
'debug': 'DEBUG',
}
verbosity_names = list(_VERBOSITY_TO_LOGLEVEL.keys())
# Python 3.7 ensures iteration order
for v, level in enumerate(list(_VERBOSITY_TO_LOGLEVEL.values())):
_VERBOSITY_TO_LOGLEVEL[v] = level
Expand All @@ -28,6 +28,11 @@ class Verbosity(IntEnum):
hint = 3
debug = 4

@property
def level(self) -> int:
# getLevelName(str) returns the int level…
return getLevelName(_VERBOSITY_TO_LOGLEVEL[self])


def _type_check(var: Any, varname: str, types: Union[type, Tuple[type, ...]]):
if isinstance(var, types):
Expand Down
5 changes: 4 additions & 1 deletion scanpy/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ def log(
time: datetime = None,
deep: Optional[str] = None,
) -> datetime:
from . import settings
now = datetime.now(timezone.utc)
time_passed: timedelta = None if time is None else now - time
extra = {
**(extra or {}),
'deep': deep,
'deep': deep if settings.verbosity.level < level else None,
'time_passed': time_passed
}
super().log(level, msg, extra=extra)
Expand Down Expand Up @@ -98,6 +99,8 @@ def format(self, record: logging.LogRecord):
record.msg = record.msg.replace('{time_passed}', str(record.time_passed))
else:
self._style._fmt += ' ({time_passed})'
if record.deep:
record.msg = f'{record.msg}: {record.deep}'
result = logging.Formatter.format(self, record)
self._style._fmt = format_orig
return result
Expand Down
14 changes: 14 additions & 0 deletions scanpy/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ def test_formats(capsys, logging_state):
assert capsys.readouterr().err == ' 4\n'


def test_deep(capsys, logging_state):
s.logfile = sys.stderr
s.verbosity = Verbosity.hint
l.hint('0')
assert capsys.readouterr().err == '--> 0\n'
l.hint('1', deep='1!')
assert capsys.readouterr().err == '--> 1\n'
s.verbosity = Verbosity.debug
l.hint('2')
assert capsys.readouterr().err == '--> 2\n'
l.hint('3', deep='3!')
assert capsys.readouterr().err == '--> 3: 3!\n'


def test_logfile(tmp_path, logging_state):
s.verbosity = Verbosity.hint

Expand Down

0 comments on commit 5800db4

Please sign in to comment.