-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.py
More file actions
32 lines (23 loc) · 660 Bytes
/
Copy pathlog.py
File metadata and controls
32 lines (23 loc) · 660 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
25
26
27
28
29
30
31
32
from os.path import exists
from datetime import datetime
def check_file(func):
def wrapper(*args):
if not exists(args[0].path):
with open(args[0].path, 'w') as f:
f.write('')
return func(*args)
return wrapper
class Logger:
path = 'logs\\log.txt'
def __init__(self, path: str = 'logs\\log.txt'):
self.path = path
self.log('new session\n\n')
@check_file
def log(self, text):
print(text)
with open(self.path, 'a', encoding="utf-8") as f:
f.write(datetime.now().strftime('%d.%m [%H:%M:%S] ') + text + '\n')
def get_logger() -> Logger:
if 'logger' not in globals():
globals()['logger'] = Logger()
return globals()['logger']