/
logger.py
207 lines (181 loc) · 7.8 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import asyncio
import os
import sys
import json
from datetime import timezone
import logging
from logging import handlers
import asyncpg
import discord
from meowth.utils import snowflake
from .cog_base import Cog
get_id = snowflake.create()
LOGGERS = ('meowth_logs', 'discord_logs')
module_logger = logging.getLogger('meowth.logger')
def init_logger(bot, debug_flag=False):
# set root logger level
logging.getLogger().setLevel(logging.DEBUG)
# setup discord logger
discord_log = logging.getLogger("discord")
discord_log.setLevel(logging.INFO)
# setup bot logger
meowth_log = logging.getLogger("meowth")
# setup log directory
log_path = os.path.join(bot.data_dir, 'logs')
if not os.path.exists(log_path):
os.makedirs(log_path)
# file handler factory
def create_fh(file_name):
fh_path = os.path.join(log_path, file_name)
return handlers.RotatingFileHandler(
filename=fh_path, encoding='utf-8', mode='a',
maxBytes=400000, backupCount=20)
# set bot log formatting
log_format = logging.Formatter(
'%(asctime)s %(name)s %(levelname)s %(module)s %(funcName)s %(lineno)d: '
'%(message)s',
datefmt="[%d/%m/%Y %H:%M]")
# create file handlers
meowth_fh = create_fh('meowth.log')
meowth_fh.setLevel(logging.INFO)
meowth_fh.setFormatter(log_format)
meowth_log.addHandler(meowth_fh)
discord_fh = create_fh('discord.log')
discord_fh.setLevel(logging.INFO)
discord_fh.setFormatter(log_format)
discord_log.addHandler(discord_fh)
# create console handler
console_std = sys.stdout if debug_flag else sys.stderr
meowth_console = logging.StreamHandler(console_std)
meowth_console.setLevel(logging.INFO if debug_flag else logging.ERROR)
meowth_console.setFormatter(log_format)
meowth_log.addHandler(meowth_console)
discord_console = logging.StreamHandler(console_std)
discord_console.setLevel(logging.ERROR)
discord_console.setFormatter(log_format)
discord_log.addHandler(discord_console)
# create db handler
meowth_db = DBLogHandler(bot, 'meowth_logs')
bot.log_meowth_db = meowth_db
meowth_log.addHandler(meowth_db)
discord_db = DBLogHandler(bot, 'discord_logs')
discord_log.addHandler(discord_db)
bot.add_cog(ActivityLogging(bot))
return meowth_log
class DBLogHandler(logging.Handler):
def __init__(self, bot, log_name: str, level=logging.INFO):
if log_name not in LOGGERS:
raise RuntimeError(f'Unknown Log Name: {log_name}')
self.bot = bot
self.log_name = log_name
self.logger = module_logger.getChild('DBLogHandler')
super().__init__(level=level)
def emit(self, record):
record_id = next(get_id)
asyncio.run_coroutine_threadsafe(
self.submit_log(record_id, record), self.bot.loop)
async def submit_log(self, log_id, record):
data = dict(log_id=log_id,
created=record.created,
logger_name=str(record.name),
level_name=str(record.levelname),
file_path=str(record.pathname),
module=str(record.module),
func_name=str(record.funcName),
line_no=record.lineno,
message=str(record.message),
traceback=str(record.exc_info))
try:
table = self.bot.dbi.table(self.log_name)
table.insert(**data)
await table.insert.commit()
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)
class ActivityLogging(Cog):
def __init__(self, bot):
self.bot = bot
self.logger = module_logger.getChild('ActivityLogging')
@Cog.listener()
async def on_message(self, msg):
sent = int(msg.created_at.replace(tzinfo=timezone.utc).timestamp())
guild_id = msg.guild.id if msg.guild else None
embeds = [json.dumps(e.to_dict()) for e in msg.embeds]
attachments = [a.url for a in msg.attachments]
data = dict(message_id=msg.id, sent=sent, is_edit=False, deleted=False,
author_id=msg.author.id, channel_id=msg.channel.id,
guild_id=guild_id, content=msg.content,
clean_content=msg.clean_content, embeds=embeds,
webhook_id=msg.webhook_id, attachments=attachments)
try:
table = self.bot.dbi.table('discord_messages')
table.insert(**data)
await table.insert.commit()
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)
@Cog.listener()
async def on_raw_message_delete(self, payload):
try:
table = self.bot.dbi.table('discord_messages')
table.update(deleted=True)
table.update.where(message_id=payload.message_id)
await table.update.commit()
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)
@Cog.listener()
async def on_raw_bulk_message_delete(self, payload):
try:
table = self.bot.dbi.table('discord_messages')
table.update(deleted=True)
m_ids = payload.message_ids
conditions = [table['message_id'] == m_id for m_id in m_ids]
table.update.where(conditions)
await table.update.commit()
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)
@Cog.listener()
async def on_message_edit(self, before, after):
if before.type == discord.MessageType.call:
return
if before.content == after.content:
return
msg = after
sent = int(msg.edited_at.replace(tzinfo=timezone.utc).timestamp())
guild_id = msg.guild.id if msg.guild else None
embeds = [json.dumps(e.to_dict()) for e in msg.embeds]
attachments = [a.url for a in msg.attachments]
data = dict(message_id=msg.id, sent=sent, is_edit=True, deleted=False,
author_id=msg.author.id, channel_id=msg.channel.id,
guild_id=guild_id, content=msg.content,
clean_content=msg.clean_content, embeds=embeds,
webhook_id=msg.webhook_id, attachments=attachments)
try:
table = self.bot.dbi.table('discord_messages')
table.insert(**data)
# update existing data
table.insert.primaries('message_id', 'sent')
await table.insert.commit(do_update=True)
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)
@Cog.listener()
async def on_command(self, ctx):
created = ctx.message.created_at
sent = int(created.replace(tzinfo=timezone.utc).timestamp())
guild = ctx.guild.id if ctx.guild else None
cog = ctx.cog.__class__.__name__ if ctx.cog else None
isc = ctx.invoked_subcommand.name if ctx.invoked_subcommand else None
cmd = ctx.command.name if ctx.command else None
data = dict(message_id=ctx.message.id, sent=sent,
author_id=ctx.author.id, channel_id=ctx.channel.id,
guild_id=guild, prefix=ctx.prefix,
command=cmd, invoked_with=ctx.invoked_with,
invoked_subcommand=isc,
subcommand_passed=ctx.subcommand_passed,
command_failed=ctx.command_failed, cog=cog)
try:
table = self.bot.dbi.table('command_log')
table.insert(**data)
table.insert.primaries('message_id', 'sent')
# ignore conflicts
await table.insert.commit(do_update=False)
except asyncpg.PostgresError as e:
self.logger.exception(type(e).__name__, exc_info=e)