forked from Delgan/loguru
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_filesink_rotation.py
179 lines (144 loc) · 7.15 KB
/
test_filesink_rotation.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pytest
import pendulum
import datetime
import os
from loguru import logger
def test_renaming(monkeypatch_now, tmpdir):
i = logger.start(tmpdir.join("file.log"), rotation=0, format="{message}")
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00.000000"))
logger.debug("a")
assert tmpdir.join("file.log").read() == "a\n"
assert tmpdir.join("file.2018-01-01_00-00-00_000000.log").read() == ""
monkeypatch_now(lambda *a, **k: pendulum.parse("2019-01-01 00:00:00.000000"))
logger.debug("b")
assert tmpdir.join("file.log").read() == "b\n"
assert tmpdir.join("file.2019-01-01_00-00-00_000000.log").read() == "a\n"
assert tmpdir.join("file.2018-01-01_00-00-00_000000.log").read() == ""
def test_no_renaming(monkeypatch_now, tmpdir):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00.000000"))
i = logger.start(tmpdir.join("file_{time}.log"), rotation=0, format="{message}")
monkeypatch_now(lambda *a, **k: pendulum.parse("2019-01-01 00:00:00.000000"))
logger.debug("a")
assert tmpdir.join("file_2018-01-01_00-00-00_000000.log").read() == ""
assert tmpdir.join("file_2019-01-01_00-00-00_000000.log").read() == "a\n"
monkeypatch_now(lambda *a, **k: pendulum.parse("2020-01-01 00:00:00.000000"))
logger.debug("b")
assert tmpdir.join("file_2018-01-01_00-00-00_000000.log").read() == ""
assert tmpdir.join("file_2019-01-01_00-00-00_000000.log").read() == "a\n"
assert tmpdir.join("file_2020-01-01_00-00-00_000000.log").read() == "b\n"
def test_delayed(monkeypatch_now, tmpdir):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00.000000"))
i = logger.start(tmpdir.join('file.log'), rotation=0, delay=True, format="{message}")
logger.debug('a')
logger.stop(i)
assert len(tmpdir.listdir()) == 2
assert tmpdir.join("file.log").read() == "a\n"
assert tmpdir.join("file.2018-01-01_00-00-00_000000.log").read() == ""
def test_delayed_early_stop(monkeypatch_now, tmpdir):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00.000000"))
i = logger.start(tmpdir.join('file.log'), rotation=0, delay=True, format="{message}")
logger.stop(i)
assert len(tmpdir.listdir()) == 0
@pytest.mark.parametrize('size', [
8, 8.0, 7.99, "8 B", "8e-6MB", "0.008 kiB", "64b"
])
def test_size_rotation(monkeypatch_now, tmpdir, size):
date = pendulum.parse("2018-01-01 00:00:00.000000")
def patch(*a, **k):
nonlocal date
date = date.add(seconds=1)
return date
monkeypatch_now(patch)
file = tmpdir.join("test_{time}.log")
i = logger.start(file.realpath(), format='{message}', rotation=size, mode='w')
logger.debug("abcde")
logger.debug("fghij")
logger.debug("klmno")
logger.stop(i)
assert [f.read() for f in sorted(tmpdir.listdir())] == ['abcde\n', 'fghij\n', 'klmno\n']
# TODO: Decomment test cases once pendulum/#211 is fixed
@pytest.mark.parametrize('when, hours', [
# hours = [Should not trigger, should trigger, should not trigger, should trigger, should trigger]
#('13', [0, 1, 20, 4, 24]),
#('13:00', [0.2, 0.9, 23, 1, 48]),
('13:00:00', [0.5, 1.5, 10, 15, 72]),
('13:00:00.123456', [0.9, 2, 10, 15, 256]),
#('11:00', [22.9, 0.2, 23, 1, 24]),
('w0', [11, 1, 24 * 7 - 1, 1, 24 * 7]),
#('W0 at 00:00', [10, 24 * 7 - 5, 0.1, 24 * 30, 24 * 14]),
('W6', [24, 24 * 28, 24 * 5, 24, 364 * 24]),
('saturday', [25, 25 * 12, 0, 25 * 12, 24 * 8]),
#('w6 at 00', [8, 24 * 7, 24 * 6, 24, 24 * 8]),
#(' W6 at 13 ', [0.5, 1, 24 * 6, 24 * 6, 365 * 24]),
('w2 at 11:00:00', [48 + 22, 3, 24 * 6, 24, 366 * 24]),
('MoNdAy at 11:00:30.123', [22, 24, 24, 24 * 7, 24 * 7]),
('sunday', [0.1, 24 * 7 - 10, 24, 24 * 6, 24 * 7]),
#('SUNDAY at 11:00', [1, 24 * 7, 2, 24*7, 30*12]),
('sunDAY at 13:00:00', [0.9, 0.2, 24 * 7 - 2, 3, 24 * 8]),
(datetime.time(15), [2, 3, 19, 5, 24]),
(pendulum.Time(18, 30, 11, 123), [1, 5.51, 20, 24, 40]),
("2 h", [1, 2, 0.9, 0.5, 10]),
("1 hour", [0.5, 1, 0.1, 100, 1000]),
("7 days", [24 * 7 - 1, 1, 48, 24 * 10, 24 * 365]),
("1h 30 minutes", [1.4, 0.2, 1, 2, 10]),
("1 w, 2D", [24 * 8, 24 * 2, 24, 24 * 9, 24 * 9]),
("1.5d", [30, 10, 0.9, 48, 35]),
("1.222 hours, 3.44s", [1.222, 0.1, 1, 1.2, 2]),
(datetime.timedelta(hours=1), [0.9, 0.2, 0.7, 0.5, 3]),
(pendulum.Duration(minutes=30), [0.48, 0.04, 0.07, 0.44, 0.5]),
('hourly', [0.9, 0.2, 0.8, 3, 1]),
('daily', [11, 1, 23, 1, 24]),
('WEEKLY', [11, 2, 24 * 6, 24, 24 * 7]),
(' mOnthLY', [0, 24 * 13, 29 * 24, 60 * 24, 24 * 35]),
('yearly ', [100, 24 * 7 * 30, 24 * 300, 24 * 100, 24 * 400]),
])
def test_time_rotation(monkeypatch_now, tmpdir, when, hours):
now = pendulum.parse("2017-06-18 12:00:00") # Sunday
monkeypatch_now(lambda *a, **k: now)
i = logger.start(tmpdir.join('test_{time}.log').realpath(), format='{message}', rotation=when, mode='w')
for h, m in zip(hours, ['a', 'b', 'c', 'd', 'e']):
now = now.add(hours=h)
logger.debug(m)
logger.stop(i)
assert len(tmpdir.listdir()) == 4
assert [f.read() for f in sorted(tmpdir.listdir())] == ['a\n', 'b\nc\n', 'd\n', 'e\n']
def test_function_rotation(monkeypatch_now, tmpdir):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00.000000"))
x = iter([False, True, False])
i = logger.start(tmpdir.join("test_{time}.log"), rotation=lambda *_: next(x), format="{message}")
logger.debug("a")
assert tmpdir.join("test_2018-01-01_00-00-00_000000.log").read() == "a\n"
monkeypatch_now(lambda *a, **k: pendulum.parse("2019-01-01 00:00:00.000000"))
logger.debug("b")
assert tmpdir.join("test_2019-01-01_00-00-00_000000.log").read()== "b\n"
monkeypatch_now(lambda *a, **k: pendulum.parse("2020-01-01 00:00:00.000000"))
logger.debug("c")
assert len(tmpdir.listdir()) == 2
assert tmpdir.join("test_2018-01-01_00-00-00_000000.log").read() == "a\n"
assert tmpdir.join("test_2019-01-01_00-00-00_000000.log").read()== "b\nc\n"
@pytest.mark.parametrize('mode', ['w', 'x'])
def test_rotation_at_stop(monkeypatch_now, tmpdir, mode):
monkeypatch_now(lambda *a, **k: pendulum.parse("2018-01-01 00:00:00"))
i = logger.start(tmpdir.join("test_{time:YYYY}.log"), rotation="10 MB", mode=mode, format="{message}")
logger.debug("test")
logger.stop(i)
assert len(tmpdir.listdir()) == 1
assert tmpdir.join("test_2018.log").read() == "test\n"
@pytest.mark.parametrize('mode', ['a', 'a+'])
def test_no_rotation_at_stop(tmpdir, mode):
i = logger.start(tmpdir.join("test.log"), rotation="10 MB", mode=mode, format="{message}")
logger.debug("test")
logger.stop(i)
assert len(tmpdir.listdir()) == 1
assert tmpdir.join("test.log").read() == "test\n"
@pytest.mark.parametrize('rotation', [
"w7", "w10", "w-1", "h", "M",
"w1at13", "www", "13 at w2", "w",
"K", "tufy MB", "111.111.111 kb", "3 Ki",
"2017.11.12", "11:99", "monday at 2017",
"e days", "2 days 8 pouooi", "foobar",
object(), os, pendulum.Date(2017, 11, 11), pendulum.now(), 1j,
])
def test_invalid_rotation(rotation):
with pytest.raises(ValueError):
logger.start('test.log', rotation=rotation)