-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from databio/dev
reduce message width
- Loading branch information
Showing
6 changed files
with
71 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
# Changelog | ||
|
||
## *v0.1* (2019-04-30): | ||
## [0.2.0] -- (2019-06-02) | ||
|
||
### Changed | ||
- Deprecated `setup_logger` in favor of `init_logger` | ||
- Changed argparser help interface to fit each arg on one line | ||
|
||
## [0.1.0] -- (2019-04-30) | ||
|
||
### Added | ||
- Parameter to `setup_logger` to pass argument to `style` parameter of `logging.Formatter` | ||
|
||
## *v0.0.2* (2019-04-14): | ||
## [0.0.2] -- (2019-04-14) | ||
### Changed | ||
- Lessen level of some messages | ||
### Fixed | ||
- Avoid erroneous missing-option exception when adding standard logging options | ||
|
||
## *v0.0.1* (2019-04-09): | ||
## [0.0.1] -- (2019-04-09) | ||
### Fixed | ||
- Fixed a bug preventing installation | ||
|
||
## *v0.0* (2019-04-08): | ||
## [0.0.0] -- (2019-04-08): | ||
- Initial release | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
__version__ = "0.1" | ||
|
||
__version__ = "0.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""Project configuration, particularly for logging. | ||
"""Project logging configuration. | ||
Project-scope constants may reside here, but more importantly, some setup here | ||
will provide a logging infrastructure for all of the project's modules. | ||
|
@@ -10,12 +10,14 @@ | |
import logging | ||
import os | ||
import sys | ||
import warnings | ||
from ._version import __version__ | ||
|
||
__author__ = "Vince Reuter" | ||
__email__ = "[email protected]" | ||
|
||
__all__ = ["add_logging_options", "logger_via_cli", "setup_logger", "AbsentOptionException"] | ||
__all__ = ["add_logging_options", "logger_via_cli", "init_logger", | ||
"setup_logger", "AbsentOptionException"] | ||
|
||
|
||
BASIC_LOGGING_FORMAT = "%(message)s" | ||
|
@@ -38,17 +40,23 @@ | |
# in logging level, making verbosity a more intuitive specification mechanism. | ||
_WARN_REPR = "WARN" | ||
LEVEL_BY_VERBOSITY = ["CRITICAL", "ERROR", _WARN_REPR, "INFO", "DEBUG"] | ||
_MIN_VERBOSITY = 1 | ||
_MAX_VERBOSITY = len(LEVEL_BY_VERBOSITY) | ||
_VERBOSITY_CHOICES = [str(x) for x in | ||
range(_MIN_VERBOSITY, len(LEVEL_BY_VERBOSITY) + 1)] + \ | ||
LEVEL_BY_VERBOSITY + ["WARNING"] | ||
|
||
LOGGING_CLI_OPTDATA = { | ||
SILENCE_LOGS_OPTNAME: { | ||
"action": "store_true", "help": "Silence logging"}, | ||
"action": "store_true", | ||
"help": "Silence logging. Overrides {}.".format(VERBOSITY_OPTNAME)}, | ||
VERBOSITY_OPTNAME: { | ||
"help": "Relative measure of interest in logs; this can be an " | ||
"integer in [0, 5], or a Python builtin logging name)"}, | ||
"metavar": "V", "choices": _VERBOSITY_CHOICES, | ||
"help": "Set logging level ({}-{} or logging module level name)". | ||
format(_MIN_VERBOSITY, len(LEVEL_BY_VERBOSITY))}, | ||
DEVMODE_OPTNAME: { | ||
"action": "store_true", | ||
"help": "Handle logging in development mode; perhaps among other " | ||
"facets, make the format more information-rich."} | ||
"help": "Expand content of logging message format."} | ||
} | ||
|
||
|
||
|
@@ -98,10 +106,10 @@ def logger_via_cli(opts, **kwargs): | |
# between the CLI version and the logger setup signature). | ||
logs_cli_args[PARAM_BY_OPTNAME.get(optname, name)] = optval | ||
logs_cli_args.update(kwargs) | ||
return setup_logger(**logs_cli_args) | ||
return init_logger(**logs_cli_args) | ||
|
||
|
||
def setup_logger( | ||
def init_logger( | ||
name="", level=None, stream=None, logfile=None, | ||
make_root=None, propagate=False, silent=False, devmode=False, | ||
verbosity=None, fmt=None, datefmt=None, plain_format=False, style=None): | ||
|
@@ -252,6 +260,18 @@ def setup_logger( | |
return logger | ||
|
||
|
||
def setup_logger( | ||
name="", level=None, stream=None, logfile=None, | ||
make_root=None, propagate=False, silent=False, devmode=False, | ||
verbosity=None, fmt=None, datefmt=None, plain_format=False, style=None): | ||
""" Old alias for init_logger for backwards compatibility """ | ||
warnings.warn("Please use init_logger in place of setup_logger", | ||
DeprecationWarning) | ||
return init_logger( | ||
name, level, stream, logfile, make_root, propagate, | ||
silent, devmode, verbosity, fmt, datefmt, plain_format, style) | ||
|
||
|
||
def _level_from_verbosity(verbosity): | ||
""" | ||
Translation of verbosity into logging level. | ||
|
@@ -279,10 +299,7 @@ def _level_from_verbosity(verbosity): | |
"{}".format(verbosity, ", ".join(LEVEL_BY_VERBOSITY))) | ||
return getattr(logging, v) | ||
elif isinstance(verbosity, int): | ||
# Allow negative value to mute even ERROR level but not CRITICAL. | ||
# Also handle excessively high verbosity request. | ||
v = min(max(verbosity, 0), len(LEVEL_BY_VERBOSITY) - 1) | ||
return LEVEL_BY_VERBOSITY[v] | ||
return LEVEL_BY_VERBOSITY[verbosity - 1] # 1-based user, 0-based internal | ||
else: | ||
raise TypeError("Verbosity must be string or int; got {} ({})" | ||
.format(verbosity, type(verbosity))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ | |
import pytest | ||
from logmuse import add_logging_options, logger_via_cli | ||
from logmuse.est import AbsentOptionException, LEVEL_BY_VERBOSITY, \ | ||
LOGGING_CLI_OPTDATA, SILENCE_LOGS_OPTNAME, VERBOSITY_OPTNAME | ||
LOGGING_CLI_OPTDATA, SILENCE_LOGS_OPTNAME, VERBOSITY_OPTNAME, \ | ||
_MIN_VERBOSITY, _MAX_VERBOSITY | ||
|
||
__author__ = "Vince Reuter" | ||
__email__ = "[email protected]" | ||
|
@@ -58,37 +59,34 @@ def test_silence(parser, cmdl, flag, hdlr_type): | |
assert isinstance(hs[0], hdlr_type) | ||
|
||
|
||
@pytest.mark.parametrize("verbosity", list(range(5))) | ||
@pytest.mark.parametrize("verbosity", range(_MIN_VERBOSITY, _MAX_VERBOSITY + 1)) | ||
def test_typical_verbosity(parser, verbosity): | ||
""" Typical verbosity specifications yield logger with expected level. """ | ||
opts = parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
logger = logger_via_cli(opts) | ||
exp = getattr(logging, LEVEL_BY_VERBOSITY[verbosity]) | ||
exp = getattr(logging, LEVEL_BY_VERBOSITY[verbosity - 1]) | ||
_assert_level(logger, exp) | ||
|
||
|
||
@given(verbosity=st.integers(-sys.maxsize, -1)) | ||
def test_negative_verbosity(parser, verbosity): | ||
""" Verbosity is pulled up to min logging level. """ | ||
opts = parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
logger = logger_via_cli(opts) | ||
_assert_level(logger, logging.CRITICAL) | ||
with pytest.raises(SystemExit): | ||
parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
|
||
|
||
@given(verbosity=st.integers(len(LEVEL_BY_VERBOSITY), sys.maxsize)) | ||
@given(verbosity=st.integers(len(LEVEL_BY_VERBOSITY) + 1, sys.maxsize)) | ||
def test_excess_verbosity(parser, verbosity): | ||
""" Verbosity saturates / maxes out. """ | ||
opts = parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
logger = logger_via_cli(opts) | ||
_assert_level(logger, logging.DEBUG) | ||
with pytest.raises(SystemExit): | ||
parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
|
||
|
||
@pytest.mark.parametrize("verbosity", ["a", "NOTALEVEL", 2.5]) | ||
def test_invalid_verbosity_is_exceptional(parser, verbosity): | ||
""" Verbosity must be a valid level name or an integer. """ | ||
opts = parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
with pytest.raises(Exception): | ||
logger_via_cli(opts) | ||
with pytest.raises(SystemExit): | ||
parser.parse_args([VERBOSITY_OPTNAME, str(verbosity)]) | ||
|
||
|
||
def _assert_level(log, lev): | ||
|