-
Notifications
You must be signed in to change notification settings - Fork 89
/
log_generator.py
74 lines (56 loc) · 1.83 KB
/
log_generator.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
from faker import Faker
from datetime import datetime
import random
import time
LINE = """\
{remote_addr} - - [{time_local} +0000] "{request_type} {request_path} HTTP/1.1" {status} {body_bytes_sent} "{http_referer}" "{http_user_agent}"\
"""
LOG_FILE_A = "log_a.txt"
LOG_FILE_B = "log_b.txt"
LOG_MAX = 100
def generate_log_line():
fake = Faker()
now = datetime.now()
remote_addr = fake.ipv4()
time_local = now.strftime('%d/%b/%Y:%H:%M:%S')
request_type = random.choice(["GET", "POST", "PUT"])
request_path = "/" + fake.uri_path()
status = random.choice([200, 401, 404])
body_bytes_sent = random.choice(range(5, 1000, 1))
http_referer = fake.uri()
http_user_agent = fake.user_agent()
log_line = LINE.format(
remote_addr=remote_addr,
time_local=time_local,
request_type=request_type,
request_path=request_path,
status=status,
body_bytes_sent=body_bytes_sent,
http_referer=http_referer,
http_user_agent=http_user_agent
)
return log_line
def write_log_line(log_file, line):
with open(log_file, "a") as f:
f.write(line)
f.write("\n")
def clear_log_file(log_file):
with open(log_file, "w+") as f:
f.write("")
if __name__ == "__main__":
current_log_file = LOG_FILE_A
lines_written = 0
clear_log_file(LOG_FILE_A)
clear_log_file(LOG_FILE_B)
while True:
line = generate_log_line()
write_log_line(current_log_file, line)
lines_written += 1
if lines_written % LOG_MAX == 0:
new_log_file = LOG_FILE_B
if current_log_file == LOG_FILE_B:
new_log_file = LOG_FILE_A
clear_log_file(new_log_file)
current_log_file = new_log_file
sleep_time = random.choice(range(1, 5, 1))
time.sleep(sleep_time)