-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger_errors.py
executable file
·51 lines (46 loc) · 1.81 KB
/
Logger_errors.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
a class for making log files, during stdout print
"""
import os
import colorama
import sys
import time
class Logger:
def __init__(self, log_file, path=None, err_file=None, emblem=None):
if path is None:
path = os.getcwd()+'/'
elif path[-1] != '/':
path += '/'
self.emblem = emblem if emblem is not None else '~'
self.HOME = os.environ['HOME']
self.log_file = open(path+log_file, 'w+')
self.log('given command: %s'
% ' '.join(sys.argv))
self.files_logged = {}
def log(self, string, to_print=True, emphasize=False, time_stamp=True):
ts = "@%s" % time.strftime("%H:%M") if time_stamp else ""
pt = os.path.expanduser(os.getcwd()).replace(self.HOME, '~')
if pt.count('/') > 3:
s = pt.split('/')
pt = "%s/.../%s" % (s[0], s[-1])
if to_print:
if emphasize:
print(colorama.Fore.RED + colorama.Back.GREEN + string + colorama.Style.RESET_ALL)
else:
print('<%s%s> %s' % (pt, ts, string))
self.log_file.write('<%s%s> %s\n' % (pt, ts, string))
self.log_file.flush()
sys.stdout.flush()
def log_text_file(self, file_name: str, to_print: bool = True):
if file_name in self.files_logged.keys():
self.log('file logged at %i' % self.files_logged[file_name])
else:
self.files_logged[file_name] = len(list(self.files_logged.keys()))+1
self.log('logging the file %s at num %i' % (file_name, len(list(self.files_logged.keys()))+1))
for l in open(file_name, 'r'):
if to_print:
print(l.rstrip())
self.log_file.write(l)
self.log_file.write('\n\n')
def close(self):
self.log_file.close()