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

SAR-55 Expose default formatter

The logging configuration in the LMC base class SKABaseDevice adds
and removes logging handlers at runtime.  These handlers must use
the standard logging format, so it is now available via a public
function.
parent 2a301041
No related branches found
No related tags found
No related merge requests found
...@@ -89,6 +89,25 @@ def main(): ...@@ -89,6 +89,25 @@ def main():
configure_logging(overrides=ADDITIONAL_LOGGING_CONFIG) configure_logging(overrides=ADDITIONAL_LOGGING_CONFIG)
``` ```
Custom handlers that use the standard logging format may be useful. In this case, the function
`get_default_formatter` is available. The example below is contrived, but shows the approach.
A more practical use case is adding and removing handlers at runtime.
```python
import logging
import logging.handlers
from ska_logging import configure_logging, get_default_formatter
def main():
configure_logging()
logger = logging.getLogger("ska.example")
handler = logging.handlers.MemoryHandler(capacity=10)
handler.setFormatter(get_default_formatter())
logger.addHandler(handler)
logger.info("Logging started for Example application")
```
By default, calls to `configure_logging` do not disable existing non-root loggers. This allows By default, calls to `configure_logging` do not disable existing non-root loggers. This allows
multiple calls to the function, although that will generally not be required. This behaviour can be multiple calls to the function, although that will generally not be required. This behaviour can be
overridden using the `"disable_existing_loggers"` key. overridden using the `"disable_existing_loggers"` key.
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
"""Module init code.""" """Module init code."""
__all__ = ("configure_logging",) __all__ = ("configure_logging", "get_default_formatter")
__author__ = "Anton Joubert" __author__ = "Anton Joubert"
__email__ = "ajoubert+ska@ska.ac.za" __email__ = "ajoubert+ska@ska.ac.za"
from .configuration import configure_logging from .configuration import configure_logging, get_default_formatter
# BEGIN VERSION CHECK # BEGIN VERSION CHECK
......
...@@ -107,6 +107,29 @@ def configure_logging(level=None, tags_filter=None, overrides=None): ...@@ -107,6 +107,29 @@ def configure_logging(level=None, tags_filter=None, overrides=None):
logging.config.dictConfig(config) logging.config.dictConfig(config)
def get_default_formatter(tags=False):
"""Return a formatter configured with the standard logging format.
Parameters
----------
tags : bool, optional
If true, then include the "tags" field in the format string. This requires
a tags filter to be linked to the corresponding handler.
Returns
-------
logging.Formatter
A new default formatter.
"""
if tags:
format_str = _FORMAT_STR_WITH_TAGS
else:
format_str = _FORMAT_STR_NO_TAGS
return _UTCFormatter(fmt=format_str)
def _override(config, overrides): def _override(config, overrides):
"""Update a config dictionary with overrides, merging dictionaries. """Update a config dictionary with overrides, merging dictionaries.
......
...@@ -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 from ska_logging import configure_logging, get_default_formatter
@pytest.fixture @pytest.fixture
...@@ -165,6 +165,25 @@ class TestConfigureLogging: ...@@ -165,6 +165,25 @@ class TestConfigureLogging:
assert get_named_handler(logger, "test") assert get_named_handler(logger, "test")
class TestGetFormatter:
"""Tests for :func:`~ska_logging.configuration.get_default_formatter`."""
def test_default_no_tags(self):
formatter = get_default_formatter()
assert isinstance(formatter, ska_logging.configuration._UTCFormatter)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_NO_TAGS
def test_get_without_tags(self):
formatter = get_default_formatter(tags=False)
assert isinstance(formatter, ska_logging.configuration._UTCFormatter)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_NO_TAGS
def test_get_with_tags(self):
formatter = get_default_formatter(tags=True)
assert isinstance(formatter, ska_logging.configuration._UTCFormatter)
assert formatter._fmt == ska_logging.configuration._FORMAT_STR_WITH_TAGS
class TestOverride: class TestOverride:
"""Tests for :func:`~ska_logging.configuration._override`. """Tests for :func:`~ska_logging.configuration._override`.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment