-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.py
63 lines (56 loc) · 2.05 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
import datetime
import inspect
import os
import sys
import time
class Logger:
CRITICAL, WARNING, INFO = list(range(3))
levels = ['CRITICAL', 'WARNING', 'INFO']
def write(self, level, line, nick=None, location=None):
"""
Write out to the logfile of either default location or otherwise specified.
Includes calling class in its logged line.
Args:
level: enumerated thing from the logger class.
line: string. thing to write out to the logger.
nick: string. determines filename.
location: where to write the logfile out to.
Returns:
nothing.
"""
if location is not None:
l = location
else:
l = "~/"
if nick is not None:
n = nick
else:
n = 'pylog'
try:
if not os.path.exists(os.path.expanduser(l + '/logs/')):
os.makedirs(os.path.expanduser(l + '/logs'))
f = open(os.path.expanduser(l + '/logs/' + n + '-' +
str(time.strftime("%m-%d-%Y"))) + '.log', "a")
if sys.version_info > (3, 0, 0):
f.write(str(datetime.datetime.now())
+ " (" + inspect.getframeinfo(inspect.stack()
[1][0]).filename
+ ":" +
str(inspect.getframeinfo(inspect.stack()[1][0]).lineno)
+ " <" + inspect.stack()[1][3] +
">) " + str(Logger.levels[level])
+ ": " + line + '\n')
else:
f.write(str(datetime.datetime.now()) +
" (" +
inspect.stack()[1][3] +
") " +
str(Logger.levels[level]) +
": " +
line +
'\n')
except IOError as e:
print("IO Error!")
print(e)
except OSError:
print("probably permission denied.")