28 lines
1012 B
Python
28 lines
1012 B
Python
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') |