forked from slernercx/localdockerotel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (43 loc) · 1.94 KB
/
main.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
import logging
import json
from datetime import datetime, timezone
import time
import os
from logging.handlers import RotatingFileHandler
# Basic configuration for logging
logger = logging.getLogger('CustomJsonLogger')
logger.setLevel(logging.INFO)
# StreamHandler for stdout
stream_handler = logging.StreamHandler()
logger.addHandler(stream_handler)
# Directory and file for log storage
log_directory = "/hostfs/var/log/myapp"
log_file = f"{log_directory}/app.log"
# Ensure log directory exists
os.makedirs(log_directory, exist_ok=True)
# RotatingFileHandler for logging to a file with rotation
log_file_max_size = 10 * 1024 * 1024 # Max log file size (e.g., 10 MB)
log_file_backup_count = 3 # Number of backup log files to keep
rotating_file_handler = RotatingFileHandler(
log_file, maxBytes=log_file_max_size, backupCount=log_file_backup_count
)
logger.addHandler(rotating_file_handler)
# Preamble and logging functions (unchanged)
preamble = "We the People of the United States, in Order to form a more perfect Union, establish Justice, insure domestic Tranquility, provide for the common defence, promote the general Welfare, and secure the Blessings of Liberty to ourselves and our Posterity, do ordain and establish this Constitution for the United States of America."
def repeat_preamble_to_size(target_size_kb):
single_message_size_bytes = len(preamble.encode('utf-8'))
repetitions_needed = (target_size_kb * 1024) // single_message_size_bytes
return preamble * repetitions_needed
def log_single_message_with_two_sizes():
while True:
log_entry = {
"timestamp": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")[:-3] + "Z",
"severity": "INFO",
"body": {
"message1": repeat_preamble_to_size(22),
"message2": repeat_preamble_to_size(6)
}
}
logger.info(json.dumps(log_entry))
time.sleep(2)
log_single_message_with_two_sizes()