-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
32 lines (23 loc) · 1.03 KB
/
log.py
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
import datetime
import os
from PyQt6.QtWidgets import QPlainTextEdit
class Log(QPlainTextEdit):
def __init__(self, use_log_file=True):
super().__init__()
self.ulf = use_log_file
self.level_colors = {'DEBUG': 'Green',
'ERROR': 'Red',
'INFO': 'Gray'}
if self.ulf:
if not os.path.exists('logs/'):
os.mkdir('logs/')
now = datetime.datetime.now().strftime("./logs/bSIGN-%y_%m_%d-%H-%M-%S")
self.log_file = os.path.join(os.getcwd(), '{}.log'.format(now))
self.setReadOnly(True)
def log(self, msg, level='DEBUG'):
if self.ulf:
with open(self.log_file, 'a+') as f:
f.write('{} :: {} :: {}\n'.format(datetime.datetime.now(), level, msg))
font_color = self.level_colors.get(level, '')
self.appendHtml('<font color={}>{} ></font> {}\n'.format(font_color, level, msg))
self.verticalScrollBar().setValue(self.verticalScrollBar().maximum())