Skip to content
Snippets Groups Projects
Commit 85c3972d authored by Anton Joubert's avatar Anton Joubert
Browse files

Merge branch 'SAR-55-fix-default-formatter-time' into 'master'

SAR-55:  Fix default formatter time

See merge request ska-telescope/ska-logging!3
parents 102b269f a582c138
No related branches found
No related tags found
No related merge requests found
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
__all__ = ( __all__ = (
"configure_logging", "configure_logging",
"get_default_formatter", "get_default_formatter",
"SkaLoggingError",
"SkaLoggingTagsFormatError",
) )
__author__ = "Anton Joubert" __author__ = "Anton Joubert"
__email__ = "ajoubert+ska@ska.ac.za" __email__ = "ajoubert+ska@ska.ac.za"
...@@ -15,8 +13,6 @@ __email__ = "ajoubert+ska@ska.ac.za" ...@@ -15,8 +13,6 @@ __email__ = "ajoubert+ska@ska.ac.za"
from .configuration import ( from .configuration import (
configure_logging, configure_logging,
get_default_formatter, get_default_formatter,
SkaLoggingError,
SkaLoggingTagsFormatError,
) )
......
...@@ -7,14 +7,6 @@ import logging.config ...@@ -7,14 +7,6 @@ import logging.config
import time import time
class SkaLoggingError(Exception):
"""Base class for all SKA Logger exceptions."""
class SkaLoggingTagsFormatError(SkaLoggingError):
"""Invalid format for the 'tags' field string."""
class _UTCFormatter(logging.Formatter): class _UTCFormatter(logging.Formatter):
converter = time.gmtime converter = time.gmtime
...@@ -46,7 +38,7 @@ _FORMAT_STR_WITH_TAGS = ( ...@@ -46,7 +38,7 @@ _FORMAT_STR_WITH_TAGS = (
"%(message)s" "%(message)s"
) )
_INVALID_TAG_CHARS = ("|", "%") _FORMAT_STR_DATE = "%Y-%m-%dT%H:%M:%S"
_LOGGING_CONFIG = { _LOGGING_CONFIG = {
"version": 1, "version": 1,
...@@ -55,7 +47,7 @@ _LOGGING_CONFIG = { ...@@ -55,7 +47,7 @@ _LOGGING_CONFIG = {
"default": { "default": {
"()": _UTCFormatter, "()": _UTCFormatter,
"format": _FORMAT_STR_NO_TAGS, "format": _FORMAT_STR_NO_TAGS,
"datefmt": "%Y-%m-%dT%H:%M:%S", "datefmt": _FORMAT_STR_DATE,
} }
}, },
"handlers": { "handlers": {
...@@ -103,7 +95,7 @@ def configure_logging(level=None, tags_filter=None, overrides=None): ...@@ -103,7 +95,7 @@ def configure_logging(level=None, tags_filter=None, overrides=None):
""" """
config = copy.deepcopy(_LOGGING_CONFIG) config = copy.deepcopy(_LOGGING_CONFIG)
if overrides: if overrides:
config = _override(config, overrides) config = _override(config, copy.deepcopy(overrides))
if level: if level:
config["root"]["level"] = level config["root"]["level"] = level
if tags_filter: if tags_filter:
...@@ -123,37 +115,21 @@ def get_default_formatter(tags=False): ...@@ -123,37 +115,21 @@ def get_default_formatter(tags=False):
Parameters Parameters
---------- ----------
tags : bool or str, optional tags : bool, optional
If boolean, then treated as a toggle: If true, then include the "tags" field in the format string. This requires
- True: include the "tags" field in the format string. This requires
a tags filter to be linked to the corresponding handler. a tags filter to be linked to the corresponding handler.
- False: exclude the "tags" field from the format string.
If string, then it is a static tag. Instead of using a logging filter, the
formatter will just use this static string for the "tags" field directly.
Returns Returns
------- -------
logging.Formatter logging.Formatter
A new default formatter. A new default formatter.
Raises
------
SkaLoggingTagsFormatError:
If the static tags string has an invalid format.
""" """
if isinstance(tags, str): if tags:
invalid_chars = [c for c in _INVALID_TAG_CHARS if c in tags]
if invalid_chars:
raise SkaLoggingTagsFormatError(
"Invalid char(s) {} in tags: {!r}".format(invalid_chars, tags)
)
format_str = _FORMAT_STR_WITH_TAGS.replace("%(tags)s", tags)
elif tags:
format_str = _FORMAT_STR_WITH_TAGS format_str = _FORMAT_STR_WITH_TAGS
else: else:
format_str = _FORMAT_STR_NO_TAGS format_str = _FORMAT_STR_NO_TAGS
return _UTCFormatter(fmt=format_str) return _UTCFormatter(fmt=format_str, datefmt=_FORMAT_STR_DATE)
def _override(config, overrides): def _override(config, overrides):
......
...@@ -9,7 +9,7 @@ import pytest ...@@ -9,7 +9,7 @@ import pytest
import ska_logging.configuration import ska_logging.configuration
from ska_logging import configure_logging, get_default_formatter, SkaLoggingTagsFormatError from ska_logging import configure_logging, get_default_formatter
@pytest.fixture @pytest.fixture
...@@ -67,18 +67,15 @@ RECORDER_OVERRIDES = { ...@@ -67,18 +67,15 @@ RECORDER_OVERRIDES = {
} }
@pytest.mark.usefixtures("reset_logging")
class TestConfigureLogging:
"""Tests for :func:`~ska_logging.configuration.configure_logging`."""
@pytest.fixture @pytest.fixture
def default_logger(self): def default_logger():
"""Return user logger instance with default configuration.""" """Return user logger instance with default configuration."""
configure_logging() configure_logging()
yield logging.getLogger("ska.test.app") yield logging.getLogger("ska.test.app")
@pytest.fixture @pytest.fixture
def recording_logger(self): def recording_logger():
"""Return user logger, including a recording handler. """Return user logger, including a recording handler.
The additional handler has the name "recorder". It uses the default formatter, The additional handler has the name "recorder". It uses the default formatter,
...@@ -91,8 +88,9 @@ class TestConfigureLogging: ...@@ -91,8 +88,9 @@ class TestConfigureLogging:
configure_logging(overrides=RECORDER_OVERRIDES) configure_logging(overrides=RECORDER_OVERRIDES)
yield logging.getLogger("ska.logger") yield logging.getLogger("ska.logger")
@pytest.fixture @pytest.fixture
def recording_tags_logger(self): def recording_tags_logger():
"""Return user logger like :func:`recording_logger`, but including tags filter.""" """Return user logger like :func:`recording_logger`, but including tags filter."""
class MyFilter(logging.Filter): class MyFilter(logging.Filter):
...@@ -103,6 +101,11 @@ class TestConfigureLogging: ...@@ -103,6 +101,11 @@ class TestConfigureLogging:
configure_logging(tags_filter=MyFilter, overrides=RECORDER_OVERRIDES) configure_logging(tags_filter=MyFilter, overrides=RECORDER_OVERRIDES)
yield logging.getLogger("ska.logger") yield logging.getLogger("ska.logger")
@pytest.mark.usefixtures("reset_logging")
class TestConfigureLogging:
"""Tests for :func:`~ska_logging.configuration.configure_logging`."""
def test_includes_console_handler(self, default_logger): def test_includes_console_handler(self, default_logger):
assert get_named_handler(default_logger, "console") assert get_named_handler(default_logger, "console")
...@@ -165,36 +168,34 @@ class TestConfigureLogging: ...@@ -165,36 +168,34 @@ class TestConfigureLogging:
assert get_named_handler(logger, "test") assert get_named_handler(logger, "test")
@pytest.mark.usefixtures("reset_logging")
class TestGetDefaultFormatter: class TestGetDefaultFormatter:
"""Tests for :func:`~ska_logging.configuration.get_default_formatter`.""" """Tests for :func:`~ska_logging.configuration.get_default_formatter`."""
def test_default_no_tags(self): def get_recorded_message(self, logger):
logger.info("test message")
recorder = get_named_handler(logger, "recorder")
record = recorder.records[0]
message = recorder.logs[0]
return record, message
def test_default_no_tags(self, recording_logger):
formatter = get_default_formatter() formatter = get_default_formatter()
assert isinstance(formatter, ska_logging.configuration._UTCFormatter) record, message = self.get_recorded_message(recording_logger)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_NO_TAGS actual_log_message = formatter.format(record)
assert actual_log_message == message
def test_get_tags_disabled(self): def test_get_tags_disabled(self, recording_logger):
formatter = get_default_formatter(tags=False) formatter = get_default_formatter(tags=False)
assert isinstance(formatter, ska_logging.configuration._UTCFormatter) record, message = self.get_recorded_message(recording_logger)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_NO_TAGS actual_log_message = formatter.format(record)
assert actual_log_message == message
def test_get_tags_enabled(self): def test_get_tags_enabled(self, recording_tags_logger):
formatter = get_default_formatter(tags=True) formatter = get_default_formatter(tags=True)
assert isinstance(formatter, ska_logging.configuration._UTCFormatter) record, message = self.get_recorded_message(recording_tags_logger)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_WITH_TAGS actual_log_message = formatter.format(record)
assert actual_log_message == message
def test_get_tags_static_string(self):
formatter = get_default_formatter(tags="test-key:test-value")
assert isinstance(formatter, ska_logging.configuration._UTCFormatter)
tags_format = ska_logging.configuration._FORMAT_STR_WITH_TAGS
expected_format = tags_format.replace("%(tags)s", "test-key:test-value")
assert formatter._fmt == expected_format
def test_get_tags_invalid_static_string(self):
with pytest.raises(SkaLoggingTagsFormatError):
get_default_formatter(tags="no|pipes|allowed")
with pytest.raises(SkaLoggingTagsFormatError):
get_default_formatter(tags="no%percentage%symbols%allowed")
class TestOverride: class TestOverride:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment