-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_logging.py
93 lines (81 loc) · 2.17 KB
/
test_logging.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""Test."""
import logging
import logging.config
import time
from logging_rate_limiter.utils import RateLimitingFilter
global log_list
log_list = list()
# here list handler is used to perform testing easily, you can use any log handler in your code
class ListHandler(logging.Handler):
"""ListHandler."""
def __init__(self, level=logging.NOTSET, **kwargs):
"""Init."""
super().__init__(level, **kwargs)
self.log_list = log_list
def emit(self, record):
"""emit."""
self.log_list.append(record.msg)
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"list": {
"level": "DEBUG",
"()": ListHandler,
"filters": ["rate_limiting_filter"]
}
},
"filters": {
"rate_limiting_filter": {
"()": RateLimitingFilter,
"default": {
"tokens_per_sec": 1,
"starting_tokens": 1,
"max_tokens_balance": 1
},
"DEBUG": {
"tokens_per_sec": 1,
"starting_tokens": 1,
"max_tokens_balance": 1
},
"result_callback": None
}
},
"loggers": {
"": {
"level": "DEBUG",
"handlers": ["list"],
"propagate": True
}
}
})
print("starting logging")
logger = logging.getLogger(__name__)
def test_logger():
"""test_logger."""
logger.debug("test")
assert len(log_list) == 1
logger.debug("test")
assert len(log_list) == 1
logger.debug("test")
assert len(log_list) == 1
logger.debug("test")
assert len(log_list) == 1
logger.debug("test")
assert len(log_list) == 1
logger.debug("test")
assert len(log_list) == 1
time.sleep(1)
logger.debug("test")
assert len(log_list) == 2
logger.info("test info")
assert len(log_list) == 3
logger.info("test info")
assert len(log_list) == 3
logger.info("test info")
assert len(log_list) == 3
logger.info("test info")
assert len(log_list) == 3
time.sleep(1)
logger.info("test info")
assert len(log_list) == 4