Skip to content

Commit

Permalink
feat: move to logoo logging (#80)
Browse files Browse the repository at this point in the history
* feat: start logoo integration

* chore: remove un-needed metadata

* feat: migrate to Logoo

* chore: shush httpx logs
  • Loading branch information
Skelmis authored Mar 7, 2024
1 parent acff1c1 commit e083d20
Show file tree
Hide file tree
Showing 22 changed files with 432 additions and 146 deletions.
16 changes: 16 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import alaric
from alaric import Cursor
from dotenv import load_dotenv
from logoo import PrimaryLogger

import suggestions

Expand All @@ -29,6 +30,9 @@
shard_logger = logging.getLogger("disnake.shard")
shard_logger.setLevel(logging.WARNING)

httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.WARNING)

suggestions_logger = logging.getLogger("suggestions")
suggestions_logger.setLevel(logging.DEBUG)
member_stats_logger = logging.getLogger("suggestions.objects.stats.member_stats")
Expand All @@ -40,6 +44,18 @@ async def run_bot():
log = logging.getLogger(__name__)
bot = await suggestions.create_bot()

logger: PrimaryLogger = PrimaryLogger(
__name__,
base_url="https://logs.suggestions.gg",
org="default",
stream="prod_bot" if bot.is_prod else "test_bot",
username=os.environ["LOGOO_USER"],
password=os.environ["LOGOO_PASSWORD"],
poll_time=15,
global_metadata={"cluster": bot.cluster_id},
)
await logger.start_consumer()

# Make sure we don't shutdown due to a previous shutdown request
cursor: Cursor = (
Cursor.from_document(bot.db.cluster_shutdown_requests)
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ yarl==1.7.2
zonis==1.2.5
types-aiobotocore==2.11.2
aiobotocore==2.11.2
logoo==1.1.0
34 changes: 31 additions & 3 deletions suggestions/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
from alaric import Cursor
from bot_base.wraps import WrappedChannel
from cooldowns import CallableOnCooldown
from disnake import Locale, LocalizationKeyError, GatewayParams
from disnake import Locale, LocalizationKeyError
from disnake.ext import commands
from bot_base import BotBase, BotContext, PrefixNotFound
from logoo import Logger

from suggestions import State, Colors, Emojis, ErrorCode, Garven
from suggestions.exceptions import (
Expand Down Expand Up @@ -48,6 +49,7 @@
from suggestions.zonis_routes import ZonisRoutes

log = logging.getLogger(__name__)
logger = Logger(__name__)


class SuggestionsBot(commands.AutoShardedInteractionBot, BotBase):
Expand Down Expand Up @@ -189,7 +191,7 @@ def error_embed(
text=f"Error code {error_code.value} | Cluster ID {self.cluster_id}"
)

log.debug("Encountered %s", error_code.name)
logger.debug("Encountered %s", error_code.name)
elif error:
embed.set_footer(text=f"Error ID {error.id}")

Expand Down Expand Up @@ -529,6 +531,14 @@ async def on_slash_command_error(

elif isinstance(exception, disnake.NotFound):
log.debug("disnake.NotFound: %s", exception.text)
logger.debug(
"disnake.NotFound: %s",
exception.text,
extra_metadata={
"guild_id": interaction.guild_id,
"author_id": interaction.author.id,
},
)
gid = interaction.guild_id if interaction.guild_id else None
await interaction.send(
embed=self.error_embed(
Expand All @@ -544,6 +554,14 @@ async def on_slash_command_error(

elif isinstance(exception, disnake.Forbidden):
log.debug("disnake.Forbidden: %s", exception.text)
logger.debug(
"disnake.Forbidden: %s",
exception.text,
extra_metadata={
"guild_id": interaction.guild_id,
"author_id": interaction.author.id,
},
)
await interaction.send(
embed=self.error_embed(
exception.text,
Expand Down Expand Up @@ -573,6 +591,9 @@ async def on_slash_command_error(
log.debug(
"disnake.HTTPException: Interaction has already been acknowledged"
)
logger.debug(
"disnake.HTTPException: Interaction has already been acknowledged"
)
return

ih: InteractionHandler = await InteractionHandler.fetch_handler(
Expand Down Expand Up @@ -793,7 +814,7 @@ async def process_update_bot_listings():
json=body,
) as r:
if r.status != 200:
log.warning("%s", r.text)
logger.warning("%s", r.text)

log.debug("Updated bot listings")
await self.sleep_with_condition(
Expand Down Expand Up @@ -939,6 +960,9 @@ async def inner():
await self.sleep_with_condition(60, lambda: self.state.is_closing)
except (aiohttp.ClientConnectorError, ConnectionRefusedError):
log.warning("push_status failed to connect, retrying in 10 seconds")
logger.warning(
"push_status failed to connect, retrying in 10 seconds"
)
await self.sleep_with_condition(10, lambda: self.state.is_closing)
except Exception as e:
if not self.is_prod:
Expand All @@ -950,6 +974,10 @@ async def inner():
"Status update failed: %s",
tb,
)
logger.error(
"Status update failed: %s",
tb,
)
await self.garven.notify_devs(
title="Status page ping error",
description=tb,
Expand Down
28 changes: 23 additions & 5 deletions suggestions/clunk2/edits.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import annotations

import asyncio
import logging
from typing import TYPE_CHECKING

import disnake
from logoo import Logger

from suggestions.low_level import MessageEditing
from suggestions.objects import Suggestion

if TYPE_CHECKING:
from suggestions import SuggestionsBot

log = logging.getLogger(__name__)
logger = Logger(__name__)

pending_edits: set[str] = set()

Expand All @@ -24,17 +24,28 @@ async def update_suggestion_message(
time_after: float = 10,
):
if suggestion.suggestion_id in pending_edits:
log.debug("Ignoring already existing item %s", suggestion.suggestion_id)
logger.debug(
"Ignoring already existing item %s",
suggestion.suggestion_id,
extra_metadata={
"guild_id": suggestion.guild_id,
"suggestion_id": suggestion.suggestion_id,
},
)
return

pending_edits.add(suggestion.suggestion_id)
await asyncio.sleep(time_after)
if suggestion.channel_id is None or suggestion.message_id is None:
log.debug(
logger.debug(
"Suggestion %s had a NoneType by the time it was to be edited channel_id=%s, message_id=%s",
suggestion.suggestion_id,
suggestion.channel_id,
suggestion.message_id,
extra_metadata={
"guild_id": suggestion.guild_id,
"suggestion_id": suggestion.suggestion_id,
},
)
pending_edits.discard(suggestion.suggestion_id)
return
Expand All @@ -50,6 +61,13 @@ async def update_suggestion_message(
message_id=up_to_date_suggestion.message_id,
).edit(embed=await up_to_date_suggestion.as_embed(bot))
except (disnake.HTTPException, disnake.NotFound):
log.error("Failed to update suggestion %s", suggestion.suggestion_id)
logger.error(
"Failed to update suggestion %s",
suggestion.suggestion_id,
extra_metadata={
"guild_id": suggestion.guild_id,
"suggestion_id": suggestion.suggestion_id,
},
)

pending_edits.discard(suggestion.suggestion_id)
27 changes: 24 additions & 3 deletions suggestions/cogs/blacklist_cog.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING

import disnake
from commons.caching import NonExistentEntry
from disnake.ext import commands
from logoo import Logger

from suggestions.objects import GuildConfig, Suggestion

if TYPE_CHECKING:
from suggestions import State, SuggestionsBot

log = logging.getLogger(__name__)
logger = Logger(__name__)


class BlacklistCog(commands.Cog):
Expand Down Expand Up @@ -61,6 +61,16 @@ async def add(
"They will be unable to create suggestions in the future.",
ephemeral=True,
)
logger.debug(
"User %s added %s to the blocklist for guild %s",
interaction.author.id,
suggestion.suggestion_author_id,
interaction.guild_id,
extra_metadata={
"author_id": interaction.author.id,
"guild_id": interaction.guild_id,
},
)

@blocklist.sub_command()
async def remove(
Expand Down Expand Up @@ -102,6 +112,16 @@ async def remove(
guild_config.blocked_users.discard(user_id)
await self.bot.db.guild_configs.upsert(guild_config, guild_config)
await interaction.send("I have un-blocklisted that user for you.")
logger.debug(
"User %s removed %s from the blocklist for guild %s",
interaction.author.id,
user_id,
interaction.guild_id,
extra_metadata={
"author_id": interaction.author.id,
"guild_id": interaction.guild_id,
},
)

@add.autocomplete("suggestion_id")
@remove.autocomplete("suggestion_id")
Expand All @@ -120,9 +140,10 @@ async def get_sid_for(
)
else:
if not values:
log.debug(
logger.debug(
"Values was found, but empty in guild %s thus populating",
interaction.guild_id,
extra_metadata={"guild_id": interaction.guild_id},
)
values: list[str] = await self.state.populate_sid_cache(
interaction.guild_id
Expand Down
Loading

0 comments on commit e083d20

Please sign in to comment.