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
GitLab 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
b1c063e6
Unverified
Commit
b1c063e6
authored
4 years ago
by
SKAJohanVenter
Browse files
Options
Downloads
Patches
Plain Diff
SAR-149
Added optional logger for transaction context and test
parent
c3aac0e8
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/ska/logging/transactions.py
+40
-3
40 additions, 3 deletions
src/ska/logging/transactions.py
tests/test_transactions.py
+9
-0
9 additions, 0 deletions
tests/test_transactions.py
with
49 additions
and
3 deletions
src/ska/logging/transactions.py
+
40
−
3
View file @
b1c063e6
...
@@ -5,7 +5,7 @@ import json
...
@@ -5,7 +5,7 @@ import json
import
logging
import
logging
import
os
import
os
from
typing
import
Mapping
,
Text
from
typing
import
Mapping
,
Optional
,
Text
from
ska.skuid.client
import
SkuidClient
from
ska.skuid.client
import
SkuidClient
...
@@ -63,11 +63,48 @@ class Transaction:
...
@@ -63,11 +63,48 @@ class Transaction:
params
:
dict
=
{},
params
:
dict
=
{},
transaction_id
:
str
=
""
,
transaction_id
:
str
=
""
,
transaction_id_key
:
str
=
"
transaction_id
"
,
transaction_id_key
:
str
=
"
transaction_id
"
,
logger
:
Optional
[
logging
.
Logger
]
=
None
,
):
):
"""
Create the transaction context handler.
A new transaction ID is generated if none is passed in via `transaction_id` or
in `params`.
If there is a transaction ID in `params` and `transaction_id` is also passed in
then the passed in `transaction_id` will take precedence.
By default the key `transaction_id` will be used to get a transaction ID out of
`params`. If a different key is required then `transaction_id_key` can be
specified.
Parameters
----------
name : str
A description for the context. This is usually the Tango device command.
params : dict, optional
The parameters will be logged and will be used to retrieve the transaction
ID if `transaction_id` is not passed in, by default {}
transaction_id : str, optional
The transaction ID to be used for the context, by default
""
transaction_id_key : str, optional
The key to use to get the transaction ID from params,
by default
"
transaction_id
"
logger : logging.Logger, optional
The logger to use for logging, by default None.
If no logger is specified a new one named `ska.transaction` will be used.
Raises
------
TransactionParamsError
If the `params` passed in is not valid.
"""
if
not
isinstance
(
params
,
Mapping
):
if
not
isinstance
(
params
,
Mapping
):
raise
TransactionParamsError
(
"
params must be dict-like (Mapping)
"
)
raise
TransactionParamsError
(
"
params must be dict-like (Mapping)
"
)
# Get the root logger
self
.
logger
=
logging
.
getLogger
()
if
logger
:
self
.
logger
=
logger
else
:
self
.
logger
=
logging
.
getLogger
(
"
ska.transaction
"
)
self
.
_name
=
name
self
.
_name
=
name
self
.
_params
=
params
self
.
_params
=
params
self
.
_transaction_id_key
=
transaction_id_key
self
.
_transaction_id_key
=
transaction_id_key
...
...
This diff is collapsed.
Click to expand it.
tests/test_transactions.py
+
9
−
0
View file @
b1c063e6
...
@@ -138,6 +138,15 @@ class TestTransactionLogging:
...
@@ -138,6 +138,15 @@ class TestTransactionLogging:
assert
0
,
f
"
RuntimeError and transaction tag not found in exception logs:
{
record_logs
}
"
assert
0
,
f
"
RuntimeError and transaction tag not found in exception logs:
{
record_logs
}
"
def
test_specified_logger
(
self
):
logger
=
MagicMock
()
parameters
=
{}
with
transaction
(
"
name
"
,
parameters
,
logger
=
logger
)
as
transaction_id
:
logger
.
info
(
"
A message
"
)
for
i
,
message
in
enumerate
([
"
Generated
"
,
"
Enter
"
,
"
A message
"
,
"
Exit
"
]):
assert
logger
.
info
.
call_args_list
[
i
].
starts_with
(
message
)
assert
logger
.
info
.
call_count
==
4
,
f
"
Log calls incorrect
{
logger
.
info
.
call_args_list
}
"
class
TestTransactionIdGenerator
:
class
TestTransactionIdGenerator
:
"""
Tests for :class:`~ska.logging.transactions.TransactionIdGenerator`.
"""
"""
Tests for :class:`~ska.logging.transactions.TransactionIdGenerator`.
"""
...
...
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