-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.py
68 lines (61 loc) · 1.77 KB
/
Logger.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
Custom (simple) Logger class to let me save logs to file and print with
one command.
"""
import sys
class Logger(object):
def __init__(self, filepath, secondary=sys.stdout):
"""
Constructor
:param filepath: location of log file (must not exist)
:param secondary: user may change secondary location from stdout
"""
self.fp = filepath
sys.stdout = secondary # change print() location; default is print()
#File must not exist
try:
fTest = open(self.fp, 'r')
except FileNotFoundError:
with open(self.fp, 'w') as f:
f.write("###LOG START###\n")
f.close()
else:
fTest.close()
raise FileExistsError
def debug(self, *line):
"""
prints line to stdout(secondary) and specified file.
:param *line: the text to put in file and print()
:return: void
"""
text = ""
for item in line:
text += str(item)
text+= '\n'
with open(self.fp, 'a') as file:
file.write(text)
file.close()
print(text, end="")
def log_to_file(self, *line):
"""
Write Line only into specifed file.
:param *line: the text to put in file
:return: void
"""
text = ""
for item in line:
text += str(item)
text+= '\n'
with open(self.fp, 'a') as file:
file.write(text)
file.close()
def log_to_secondary(self, *line):
"""
Write line only to secondary (default stdout)
:param *line: text to print()
:return: void
"""
text = ""
for item in line:
text += item
print(text)