mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
[Core] Add Sentry logger for core cogs data gathering only (#813)
This commit is contained in:
parent
422bbba7f6
commit
94cfd23c00
@ -4,8 +4,10 @@ import datetime
|
||||
import logging
|
||||
from discord.ext import commands
|
||||
from core.utils.chat_formatting import inline
|
||||
from core.sentry_setup import should_log
|
||||
|
||||
log = logging.getLogger("red")
|
||||
sentry_log = logging.getLogger("red.sentry")
|
||||
|
||||
INTRO = ("{0}===================\n"
|
||||
"{0} Red - Discord Bot \n"
|
||||
@ -100,6 +102,12 @@ def init_events(bot, cli_flags):
|
||||
error, error.__traceback__))
|
||||
bot._last_exception = exception_log
|
||||
await ctx.send(inline(message))
|
||||
|
||||
module = ctx.command.module
|
||||
if should_log(module):
|
||||
sentry_log.exception("Exception in command '{}'"
|
||||
"".format(ctx.command.qualified_name),
|
||||
exc_info=error.original)
|
||||
elif isinstance(error, commands.CommandNotFound):
|
||||
pass
|
||||
elif isinstance(error, commands.CheckFailure):
|
||||
|
||||
@ -5,15 +5,9 @@ from raven.handlers.logging import SentryHandler
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
client = None
|
||||
__all__ = ("init_sentry_logging", "should_log")
|
||||
|
||||
|
||||
def init_sentry_logging():
|
||||
global client
|
||||
client = Client(
|
||||
dsn=("https://27f3915ba0144725a53ea5a99c9ae6f3:87913fb5d0894251821dcf06e5e9cfe6@"
|
||||
"sentry.telemetry.red/19?verify_ssl=0"),
|
||||
include_paths = (
|
||||
'core',
|
||||
'cogs.alias',
|
||||
@ -28,15 +22,22 @@ def init_sentry_logging():
|
||||
'tests.core.test_sentry',
|
||||
'main',
|
||||
'launcher'
|
||||
),
|
||||
)
|
||||
|
||||
client = None
|
||||
|
||||
|
||||
def init_sentry_logging(logger):
|
||||
global client
|
||||
client = Client(
|
||||
dsn=("https://27f3915ba0144725a53ea5a99c9ae6f3:87913fb5d0894251821dcf06e5e9cfe6@"
|
||||
"sentry.telemetry.red/19?verify_ssl=0"),
|
||||
release=fetch_git_sha(str(Path.cwd()))
|
||||
)
|
||||
|
||||
handler = SentryHandler(client)
|
||||
setup_logging(
|
||||
handler,
|
||||
exclude=(
|
||||
"asyncio",
|
||||
"discord"
|
||||
)
|
||||
)
|
||||
logger.addHandler(handler)
|
||||
|
||||
|
||||
def should_log(module_name: str) -> bool:
|
||||
return any(module_name.startswith(path) for path in include_paths)
|
||||
|
||||
14
main.py
14
main.py
@ -26,12 +26,15 @@ if discord.version_info.major < 1:
|
||||
|
||||
|
||||
def init_loggers(cli_flags):
|
||||
# d.py stuff
|
||||
dpy_logger = logging.getLogger("discord")
|
||||
dpy_logger.setLevel(logging.WARNING)
|
||||
console = logging.StreamHandler()
|
||||
console.setLevel(logging.WARNING)
|
||||
dpy_logger.addHandler(console)
|
||||
|
||||
# Red stuff
|
||||
|
||||
logger = logging.getLogger("red")
|
||||
|
||||
red_format = logging.Formatter(
|
||||
@ -56,12 +59,16 @@ def init_loggers(cli_flags):
|
||||
logger.addHandler(fhandler)
|
||||
logger.addHandler(stdout_handler)
|
||||
|
||||
return logger
|
||||
# Sentry stuff
|
||||
sentry_logger = logging.getLogger("red.sentry")
|
||||
sentry_logger.setLevel(logging.WARNING)
|
||||
|
||||
return logger, sentry_logger
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli_flags = parse_cli_flags()
|
||||
log = init_loggers(cli_flags)
|
||||
log, sentry_log = init_loggers(cli_flags)
|
||||
description = "Red v3 - Alpha"
|
||||
red = Red(cli_flags, description=description, pm_help=None)
|
||||
init_global_checks(red)
|
||||
@ -89,7 +96,7 @@ if __name__ == '__main__':
|
||||
ask_sentry(red)
|
||||
|
||||
if red.db.enable_sentry():
|
||||
init_sentry_logging()
|
||||
init_sentry_logging(sentry_log)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
cleanup_tasks = True
|
||||
@ -114,6 +121,7 @@ if __name__ == '__main__':
|
||||
red._shutdown_mode = ExitCodes.SHUTDOWN
|
||||
except Exception as e:
|
||||
log.critical("Fatal exception", exc_info=e)
|
||||
sentry_log.critical("Fatal Exception", exc_info=e)
|
||||
loop.run_until_complete(red.logout())
|
||||
finally:
|
||||
if cleanup_tasks:
|
||||
|
||||
@ -1,8 +1,10 @@
|
||||
from core import sentry_setup
|
||||
import logging
|
||||
|
||||
|
||||
def test_sentry_capture():
|
||||
sentry_setup.init_sentry_logging()
|
||||
log = logging.getLogger(__name__)
|
||||
sentry_setup.init_sentry_logging(log)
|
||||
|
||||
assert sentry_setup.client is not None
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user