-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
24 lines (21 loc) · 913 Bytes
/
Copy pathmain.py
File metadata and controls
24 lines (21 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import time
import datetime
FILE = 'log.txt'
class logger:
def __init__(self, name):
self.name = name
def __enter__(self):
self.start = time.time()
self.date = datetime.strftime(datetime.now(), '%Y-%m-%d')
self.time = datetime.strftime(datetime.now(), '%H:%M:%S')
with open(FILE, 'a', encoding='utf-8') as log:
log.write(f'[{self.date}] НАЧАЛО в {self.time}: {self.name}')
return self
def __exit__(self, exc_type, exc, tb):
self.end = time.time()
duration = self.end - self.start
status = 'УСПЕШНО' if exc_type is None else f'ОШИБКА: {exc}'
with open(FILE, 'a', encoding='utf-8') as log:
log.write(f"[{self.date}] КОНЕЦ: {self.name} | {status} | {duration:.4f} сек\n")
print(f'⏱ Время выполнения: {duration:.4f} сек')
return False