-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
81 lines (65 loc) · 2.95 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import botconfig
import json
PINK = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
WHITE = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# Determine which type of message to log, then call the appropriate function to log it
def log(msg):
if msg['chat']['type'] == 'private':
_log_private(msg)
elif msg['chat']['type'] == 'supergroup':
_log_supergroup(msg)
# Log a message from a supergroup to print the message in a neat format
def _log_supergroup(msg):
if 'text' in msg:
if 'last_name' in msg['from']:
print('%s%s%s - %s%s %s%s : %s%s%s' % (RED, msg['chat']['title'], PINK,
BLUE, msg['from']['first_name'],
msg['from']['last_name'], PINK,
WHITE, msg['text'], WHITE))
else:
print('%s%s%s - %s%s%s : %s%s%s' % (RED, msg['chat']['title'], PINK,
BLUE, msg['from']['first_name'], PINK,
WHITE, msg['text'], WHITE))
# Log a message from a private message to print it in a neat format
def _log_private(msg):
if 'text' in msg:
if 'last_name' in msg['from']:
print('%s%s%s - %s%s %s%s : %s%s%s' % (RED, 'PRIVATE', PINK,
BLUE, msg['from']['first_name'],
msg['from']['last_name'], PINK,
WHITE, msg['text'], WHITE))
else:
print('%s%s%s - %s%s%s : %s%s%s' % (RED, 'PRIVATE', PINK,
BLUE, msg['from']['first_name'], PINK,
WHITE, msg['text'], WHITE))
# Log an error message
def error(msg):
print(RED + msg + WHITE)
# Log an initialization message
def init_log(msg):
print(YELLOW + msg + WHITE)
# Log a temporary message, usually for testing purposes
def temp_log(msg):
print(PINK + msg + WHITE)
# Prints json message to log file. Usually used for testing purposes
def log_to_file(msg):
f = open(botconfig.logfile, 'a')
f.write(json.dumps(msg, indent=4, sort_keys=True) + ',\n')
f.close()
def log_callback(msg):
if 'text' in msg:
if 'last_name' in msg['from']:
print('%s%s%s - %s%s %s%s : %s%s%s' % (RED, 'CALLBACK', PINK,
BLUE, msg['from']['first_name'],
msg['from']['last_name'], PINK,
WHITE, msg['text'], WHITE))
else:
print('%s%s%s - %s%s%s : %s%s%s' % (RED, 'CALLBACK', PINK,
BLUE, msg['from']['first_name'], PINK,
WHITE, msg['text'], WHITE))