-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlogger.py
executable file
·58 lines (47 loc) · 2.11 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
"""Code for logging
Author: Ian Shih
Email: yjshih23@gmail.com
Date: 2021/11/03
"""
import time
import os
def getRed(skk): return("\033[91m{}\033[00m" .format(skk))
def getGreen(skk): return("\033[92m{}\033[00m" .format(skk))
def getYellow(skk): return("\033[93m{}\033[00m" .format(skk))
def getLightPurple(skk): return("\033[94m{}\033[00m" .format(skk))
def getPurple(skk): return("\033[95m{}\033[00m" .format(skk))
def getCyan(skk): return("\033[96m{}\033[00m" .format(skk))
def getLightGray(skk): return("\033[97m{}\033[00m" .format(skk))
def getBlack(skk): return("\033[98m{}\033[00m" .format(skk))
class logger():
def __init__(self, filepath,overrite=False):
self.filepath = filepath
self.colorfilepath = "{}_colored".format(filepath)
print("Log file at : {}".format(self.filepath))
if overrite or not os.path.exists(self.filepath):
with open(self.filepath, "w") as f:
f.write("Log created at {}\n".format(
time.strftime("%Y/%m/%d %H : %M : %S")))
print("Log file created : {}".format(self.filepath))
if overrite or not os.path.exists(self.colorfilepath):
with open(self.colorfilepath, "w") as f:
f.write("Log created at {}\n".format(
time.strftime("%Y/%m/%d %H:%M:%S")))
print("Color Log file created : {}".format(self.colorfilepath))
def log(self, content, include_header=False, show=True, end='\n'):
with open(self.filepath, "a") as f:
if include_header:
f.write("[{}] ".format(time.strftime("%Y/%m/%d %H:%M:%S")))
f.write("{}".format(content))
f.write(end)
with open(self.colorfilepath, "a") as f:
if include_header:
f.write("[{}] ".format(
getPurple(time.strftime("%Y/%m/%d %H:%M:%S"))))
f.write("{}".format(content))
f.write(end)
if show:
print(content, end=end)
if __name__ == "__main__":
mylogger = logger("./logs/test.log")
mylogger.log("aasdasd")