Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
ska-logging
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
LOFAR2.0
ska-logging
Commits
102b269f
Commit
102b269f
authored
5 years ago
by
Anton Joubert
Browse files
Options
Downloads
Plain Diff
Merge branch '
SAR-55
-expose-formatter' into 'master'
SAR-55
Provide access to default formatter See merge request ska-telescope/ska-logging!2
parents
2cc53ae6
8339bf17
Branches
Branches containing commit
Tags
0.2.0
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.md
+19
-0
19 additions, 0 deletions
README.md
ska_logging/__init__.py
+12
-2
12 additions, 2 deletions
ska_logging/__init__.py
ska_logging/configuration.py
+49
-0
49 additions, 0 deletions
ska_logging/configuration.py
tests/test_configuration.py
+43
-11
43 additions, 11 deletions
tests/test_configuration.py
with
123 additions
and
13 deletions
README.md
+
19
−
0
View file @
102b269f
...
...
@@ -89,6 +89,25 @@ def main():
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
multiple calls to the function, although that will generally not be required. This behaviour can be
overridden using the
`"disable_existing_loggers"`
key.
...
...
This diff is collapsed.
Click to expand it.
ska_logging/__init__.py
+
12
−
2
View file @
102b269f
...
...
@@ -2,12 +2,22 @@
"""
Module init code.
"""
__all__
=
(
"
configure_logging
"
,)
__all__
=
(
"
configure_logging
"
,
"
get_default_formatter
"
,
"
SkaLoggingError
"
,
"
SkaLoggingTagsFormatError
"
,
)
__author__
=
"
Anton Joubert
"
__email__
=
"
ajoubert+ska@ska.ac.za
"
from
.configuration
import
configure_logging
from
.configuration
import
(
configure_logging
,
get_default_formatter
,
SkaLoggingError
,
SkaLoggingTagsFormatError
,
)
# BEGIN VERSION CHECK
...
...
This diff is collapsed.
Click to expand it.
ska_logging/configuration.py
+
49
−
0
View file @
102b269f
...
...
@@ -7,6 +7,14 @@ import logging.config
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
):
converter
=
time
.
gmtime
...
...
@@ -38,6 +46,8 @@ _FORMAT_STR_WITH_TAGS = (
"
%(message)s
"
)
_INVALID_TAG_CHARS
=
(
"
|
"
,
"
%
"
)
_LOGGING_CONFIG
=
{
"
version
"
:
1
,
"
disable_existing_loggers
"
:
False
,
...
...
@@ -107,6 +117,45 @@ def configure_logging(level=None, tags_filter=None, overrides=None):
logging
.
config
.
dictConfig
(
config
)
def
get_default_formatter
(
tags
=
False
):
"""
Return a formatter configured with the standard logging format.
Parameters
----------
tags : bool or str, optional
If boolean, then treated as a toggle:
- True: include the
"
tags
"
field in the format string. This requires
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
-------
logging.Formatter
A new default formatter.
Raises
------
SkaLoggingTagsFormatError:
If the static tags string has an invalid format.
"""
if
isinstance
(
tags
,
str
):
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
else
:
format_str
=
_FORMAT_STR_NO_TAGS
return
_UTCFormatter
(
fmt
=
format_str
)
def
_override
(
config
,
overrides
):
"""
Update a config dictionary with overrides, merging dictionaries.
...
...
This diff is collapsed.
Click to expand it.
tests/test_configuration.py
+
43
−
11
View file @
102b269f
...
...
@@ -9,7 +9,7 @@ import pytest
import
ska_logging.configuration
from
ska_logging
import
configure_logging
from
ska_logging
import
configure_logging
,
get_default_formatter
,
SkaLoggingTagsFormatError
@pytest.fixture
...
...
@@ -103,31 +103,31 @@ class TestConfigureLogging:
configure_logging
(
tags_filter
=
MyFilter
,
overrides
=
RECORDER_OVERRIDES
)
yield
logging
.
getLogger
(
"
ska.logger
"
)
def
test_
configure_logging_
includes_console_handler
(
self
,
default_logger
):
def
test_includes_console_handler
(
self
,
default_logger
):
assert
get_named_handler
(
default_logger
,
"
console
"
)
def
test_
configure_logging_
multiple_calls_non_root_logger_still_enabled
(
self
,
default_logger
):
def
test_multiple_calls_non_root_logger_still_enabled
(
self
,
default_logger
):
logger
=
logging
.
getLogger
(
"
ska.logger.test
"
)
assert
not
logger
.
disabled
configure_logging
()
assert
not
logger
.
disabled
def
test_
configure_logging_
default_log_level_info
(
self
,
default_logger
):
def
test_default_log_level_info
(
self
,
default_logger
):
logger
=
logging
.
getLogger
(
"
ska.logger.test
"
)
assert
logger
.
getEffectiveLevel
()
==
logging
.
INFO
assert
default_logger
.
getEffectiveLevel
()
==
logging
.
INFO
def
test_
configure_logging_
set_log_level_int
(
self
):
def
test_set_log_level_int
(
self
):
configure_logging
(
level
=
logging
.
DEBUG
)
logger
=
logging
.
getLogger
(
"
ska.logger.test
"
)
assert
logger
.
getEffectiveLevel
()
==
logging
.
DEBUG
def
test_
configure_logging_
set_log_level_string
(
self
):
def
test_set_log_level_string
(
self
):
configure_logging
(
level
=
"
WARNING
"
)
logger
=
logging
.
getLogger
(
"
ska.logger.test
"
)
assert
logger
.
getEffectiveLevel
()
==
logging
.
WARNING
def
test_
configure_logging_
default_uses_utc_time
(
self
,
recording_logger
):
def
test_default_uses_utc_time
(
self
,
recording_logger
):
recording_logger
.
info
(
"
UTC message
"
)
recorder
=
get_named_handler
(
recording_logger
,
"
recorder
"
)
record
=
recorder
.
records
[
0
]
...
...
@@ -136,17 +136,17 @@ class TestConfigureLogging:
assert
"
UTC message
"
in
log_message
assert
expected_time
in
log_message
# just testing UTC, so ignore the milliseconds part
def
test_
configure_logging_
default_no_tags
(
self
,
default_logger
):
def
test_default_no_tags
(
self
,
default_logger
):
handler
=
get_named_handler
(
default_logger
,
"
console
"
)
formatter
=
handler
.
formatter
assert
"
%(tag)s
"
not
in
formatter
.
_fmt
def
test_
configure_logging_
tags_filter_adds_tags_field
(
self
,
recording_tags_logger
):
def
test_tags_filter_adds_tags_field
(
self
,
recording_tags_logger
):
handler
=
get_named_handler
(
recording_tags_logger
,
"
console
"
)
formatter
=
handler
.
formatter
assert
"
%(tags)s
"
in
formatter
.
_fmt
def
test_
configure_logging_
tags_filter_emits_tags_value
(
self
,
recording_tags_logger
):
def
test_tags_filter_emits_tags_value
(
self
,
recording_tags_logger
):
recording_tags_logger
.
info
(
"
Tags message
"
)
recorder
=
get_named_handler
(
recording_tags_logger
,
"
recorder
"
)
record
=
recorder
.
records
[
0
]
...
...
@@ -154,7 +154,7 @@ class TestConfigureLogging:
assert
record
.
tags
==
"
key1:value1,key2:value2
"
assert
log_message
.
endswith
(
"
|key1:value1,key2:value2|Tags message
"
)
def
test_
configure_logging_
override
(
self
):
def
test_override
(
self
):
overrides
=
{
"
handlers
"
:
{
"
test
"
:
{
"
class
"
:
"
logging.StreamHandler
"
,
"
formatter
"
:
"
default
"
}},
"
root
"
:
{
"
handlers
"
:
[
"
console
"
,
"
test
"
]},
...
...
@@ -165,6 +165,38 @@ class TestConfigureLogging:
assert
get_named_handler
(
logger
,
"
test
"
)
class
TestGetDefaultFormatter
:
"""
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_tags_disabled
(
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_tags_enabled
(
self
):
formatter
=
get_default_formatter
(
tags
=
True
)
assert
isinstance
(
formatter
,
ska_logging
.
configuration
.
_UTCFormatter
)
assert
formatter
.
_fmt
==
ska_logging
.
configuration
.
_FORMAT_STR_WITH_TAGS
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
:
"""
Tests for :func:`~ska_logging.configuration._override`.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment