parse_saby/app/error_to_log.py

28 lines
1012 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import datetime
LOG_FILE = "errors.log"
def current_datatime():
return datetime.datetime.now().strftime("[%d-%m-%Y %H:%M:%S]")
def write_to_log(error_message:str, log_file=LOG_FILE):
"""
Записывает ошибку в лог-файл с временной меткой
"""
print(f"Ошибка записана в {log_file} {error_message}")
with open(log_file, 'w', encoding='utf8') as f:
f.write(f"[{current_datatime()}] {error_message}\n")
def read_to_log(log_file=LOG_FILE):
try:
with open(log_file, 'r', encoding='utf8') as f:
for line in f.read():
print(line, end='')
except FileNotFoundError: print(f"{LOG_FILE} не существует")
def clear_to_log(log_file=LOG_FILE):
"""
Очищает лог-файл и записывает новую шапку
"""
with open(log_file, 'w+', encoding='utf-8') as f:
f.write(f'=== Лог ошибок создан в {current_datatime()} ===\n\n')