Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
beat
beat.cmdline
Commits
c2177195
Commit
c2177195
authored
May 15, 2018
by
Samuel GAIST
Committed by
Samuel Gaist
May 15, 2018
Browse files
[test][utils] Add mock logging handler
This will allow to analyse the content of logs generated during a test.
parent
66f2507b
Changes
1
Hide whitespace changes
Inline
Side-by-side
beat/cmdline/test/utils.py
View file @
c2177195
...
...
@@ -29,6 +29,7 @@ Helper functions to run tests
"""
import
os
import
logging
from
beat.core.experiment
import
Experiment
from
beat.core.hash
import
toPath
...
...
@@ -53,3 +54,35 @@ def index_experiment_dbs(experiment_name):
infos
[
'set'
]),
suffix
=
'.db'
)
view
.
index
(
os
.
path
.
join
(
tmp_prefix
,
filename
))
# Based on https://stackoverflow.com/a/20553331/5843716
class
MockLoggingHandler
(
logging
.
Handler
):
"""Mock logging handler to check for expected logs.
Messages are available from an instance's ``messages`` dict, in order,
indexed by a lowercase log level string (e.g., 'debug', 'info', etc.).
"""
def
__init__
(
self
,
*
args
,
**
kwargs
):
self
.
messages
=
{
'debug'
:
[],
'info'
:
[],
'warning'
:
[],
'error'
:
[],
'critical'
:
[],
'extra'
:
[]
}
super
(
MockLoggingHandler
,
self
).
__init__
(
*
args
,
**
kwargs
)
def
emit
(
self
,
record
):
"Store a message from ``record`` in the instance's ``messages`` dict."
try
:
self
.
messages
[
record
.
levelname
.
lower
()].
append
(
record
.
getMessage
())
except
Exception
:
self
.
handleError
(
record
)
def
reset
(
self
):
self
.
acquire
()
try
:
for
message_list
in
self
.
messages
.
values
():
message_list
.
clear
()
finally
:
self
.
release
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment