-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.py
81 lines (71 loc) · 2.12 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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: UTF-8 -*-
# @Time : 1/25/18 6:26 PM
# @Author : Lester
from datetime import datetime
from post.post import Post
class Log:
path_to_log = 'log'
file = open(path_to_log, 'w', encoding='utf8')
@staticmethod
def create(path):
Log.path_to_log = path or 'log'
Log.file.close()
Log.file = open(Log.path_to_log, 'w', encoding='utf8')
@staticmethod
def open():
Log.file.close()
Log.file = open(Log.path_to_log, 'a', encoding='utf8')
@staticmethod
def log(message):
timestamp = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.now())
text = timestamp + ' '
if message is None:
text += 'POST has no response.'
elif message.__class__ is Post:
text += message.response_json['message']
elif message.__class__ is str:
text += message
print(text)
Log.file.write(text + '\n')
@staticmethod
def flush():
Log.file.flush()
@staticmethod
def close():
Log.file.close()
@staticmethod
def clear():
Log.create()
Log.open()
# deprecated
# def __init__(self, path):
# self.path_to_log = path or 'log'
# self.file = open(self.path_to_log, 'w', encoding='utf8')
#
# def open(self):
# self.file.close()
# self.file = open(self.path_to_log, 'a', encoding='utf8')
#
# def log(self, message):
# timestamp = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.now())
# text = timestamp + ' '
#
# if message is None:
# text += 'POST has no response.'
# elif message.__class__ is Post:
# text += message.response_json['message']
# elif message.__class__ is str:
# text += message
# print(text)
# self.file.write(text + '\n')
#
# def flush(self):
# self.file.flush()
#
# def close(self):
# self.file.close()
#
# def clear(self):
# self.file.close()
# open(self.path_to_log, 'w', encoding='utf8').close()
# self.file = open(self.path_to_log, 'a', encoding='utf8')