Spaces:
Build error
Build error
File size: 1,073 Bytes
b1add7a |
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 |
import json
from datetime import datetime
import os
MAX_LOG_ENTRIES = 100
log_file = os.path.join(os.path.dirname(__file__), "logs.json")
def log(text, from_lang, to_lang, translated_text):
log_entry = {
"text": text,
"from_lang": from_lang,
"to_lang": to_lang,
"translated_text": translated_text,
"timestamp": datetime.now().strftime("%d-%m-%Y %H:%M:%S")
}
try:
with open(log_file, 'r+') as file:
logs = json.load(file)
if "chats" not in logs:
logs["chats"] = []
logs["chats"].append(log_entry)
if len(logs["chats"]) > MAX_LOG_ENTRIES:
logs["chats"] = logs["chats"][-MAX_LOG_ENTRIES:]
file.seek(0)
json.dump(logs, file, indent=4)
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error occurred: {e}")
with open(log_file, 'w') as file:
json.dump({"chats": [log_entry]}, file, indent=4)
|