-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.py
More file actions
26 lines (20 loc) · 971 Bytes
/
Copy pathlogger.py
File metadata and controls
26 lines (20 loc) · 971 Bytes
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
from datetime import datetime
recent_messages = {}
def log(message):
"""Функция для логирования сообщений."""
msg_id = getattr(message, 'message_id', None)
if msg_id and msg_id in recent_messages:
return
if msg_id:
recent_messages[msg_id] = True
with open('log.txt', 'a', encoding="utf-8") as log_file:
if isinstance(message, str):
msg = f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - {message}\n"
else:
user_info = (f"{message.from_user.first_name or ''} "
f"{message.from_user.last_name or ''} "
f"(@{message.from_user.username or 'нет_никнейма'}, "
f"id: {message.from_user.id})").strip()
msg = (f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - "
f"Сообщение от {user_info}: {message.text}\n")
log_file.write(msg)