Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
Revert "fix💥: make naff always use the correct logger (#643)"
Browse files Browse the repository at this point in the history
This reverts commit 2448b20.
  • Loading branch information
silasary authored Sep 15, 2022
1 parent ad17e3e commit 397e061
Show file tree
Hide file tree
Showing 27 changed files with 142 additions and 152 deletions.
2 changes: 1 addition & 1 deletion naff/api/events/processors/message_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def _on_raw_message_delete(self, event: "RawGatewayEvent") -> None:
if not message:
message = BaseMessage.from_dict(event.data, self)
self.cache.delete_message(event.data["channel_id"], event.data["id"])
logger().debug(f"Dispatching Event: {event.resolved_name}")
logger.debug(f"Dispatching Event: {event.resolved_name}")
self.dispatch(events.MessageDelete(message))

@Processor.define()
Expand Down
28 changes: 14 additions & 14 deletions naff/api/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,31 +176,31 @@ async def dispatch_opcode(self, data, op: OPCODE) -> None:
match op:

case OPCODE.HEARTBEAT:
logger().debug("Received heartbeat request from gateway")
logger.debug("Received heartbeat request from gateway")
return await self.send_heartbeat()

case OPCODE.HEARTBEAT_ACK:
self.latency.append(time.perf_counter() - self._last_heartbeat)

if self._last_heartbeat != 0 and self.latency[-1] >= 15:
logger().warning(
logger.warning(
f"High Latency! shard ID {self.shard[0]} heartbeat took {self.latency[-1]:.1f}s to be acknowledged!"
)
else:
logger().debug(f"❤ Heartbeat acknowledged after {self.latency[-1]:.5f} seconds")
logger.debug(f"❤ Heartbeat acknowledged after {self.latency[-1]:.5f} seconds")

return self._acknowledged.set()

case OPCODE.RECONNECT:
logger().debug("Gateway requested reconnect. Reconnecting...")
logger.debug("Gateway requested reconnect. Reconnecting...")
return await self.reconnect(resume=True, url=self.ws_resume_url)

case OPCODE.INVALIDATE_SESSION:
logger().warning("Gateway has invalidated session! Reconnecting...")
logger.warning("Gateway has invalidated session! Reconnecting...")
return await self.reconnect()

case _:
return logger().debug(f"Unhandled OPCODE: {op} = {OPCODE(op).name}")
return logger.debug(f"Unhandled OPCODE: {op} = {OPCODE(op).name}")

async def dispatch_event(self, data, seq, event) -> None:
match event:
Expand All @@ -212,13 +212,13 @@ async def dispatch_event(self, data, seq, event) -> None:
self.ws_resume_url = (
f"{data['resume_gateway_url']}?encoding=json&v={__api_version__}&compress=zlib-stream"
)
logger().info(f"Shard {self.shard[0]} has connected to gateway!")
logger().debug(f"Session ID: {self.session_id} Trace: {self._trace}")
logger.info(f"Shard {self.shard[0]} has connected to gateway!")
logger.debug(f"Session ID: {self.session_id} Trace: {self._trace}")
# todo: future polls, improve guild caching here. run the debugger. you'll see why
return self.state.client.dispatch(events.WebsocketReady(data))

case "RESUMED":
logger().info(f"Successfully resumed connection! Session_ID: {self.session_id}")
logger.info(f"Successfully resumed connection! Session_ID: {self.session_id}")
self.state.client.dispatch(events.Resume())
return

Expand All @@ -233,9 +233,9 @@ async def dispatch_event(self, data, seq, event) -> None:
try:
asyncio.create_task(processor(events.RawGatewayEvent(data.copy(), override_name=event_name)))
except Exception as ex:
logger().error(f"Failed to run event processor for {event_name}: {ex}")
logger.error(f"Failed to run event processor for {event_name}: {ex}")
else:
logger().debug(f"No processor for `{event_name}`")
logger.debug(f"No processor for `{event_name}`")

self.state.client.dispatch(events.RawGatewayEvent(data.copy(), override_name="raw_gateway_event"))
self.state.client.dispatch(events.RawGatewayEvent(data.copy(), override_name=f"raw_{event.lower()}"))
Expand Down Expand Up @@ -264,7 +264,7 @@ async def _identify(self) -> None:
serialized = OverriddenJson.dumps(payload)
await self.ws.send_str(serialized)

logger().debug(
logger.debug(
f"Shard ID {self.shard[0]} has identified itself to Gateway, requesting intents: {self.state.intents}!"
)

Expand All @@ -286,11 +286,11 @@ async def _resume_connection(self) -> None:
serialized = OverriddenJson.dumps(payload)
await self.ws.send_str(serialized)

logger().debug(f"{self.shard[0]} is attempting to resume a connection")
logger.debug(f"{self.shard[0]} is attempting to resume a connection")

async def send_heartbeat(self) -> None:
await self.send_json({"op": OPCODE.HEARTBEAT, "d": self.sequence}, bypass=True)
logger().debug(f"❤ Shard {self.shard[0]} is sending a Heartbeat")
logger.debug(f"❤ Shard {self.shard[0]} is sending a Heartbeat")

async def change_presence(self, activity=None, status: Status = Status.ONLINE, since=None) -> None:
"""Update the bot's presence status."""
Expand Down
18 changes: 8 additions & 10 deletions naff/api/gateway/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import naff
from naff.api import events
from naff.client.const import Absent, MISSING
from naff.client.const import logger, MISSING, Absent
from naff.client.errors import NaffException, WebSocketClosed
from naff.client.utils.attr_utils import define, field
from naff.models.discord.activity import Activity
Expand Down Expand Up @@ -68,7 +68,7 @@ async def start(self) -> None:
"""Connect to the Discord Gateway."""
self.gateway_url = await self.client.http.get_gateway()

self.client.logger.debug(f"Starting Shard ID {self.shard_id}")
logger.debug(f"Starting Shard ID {self.shard_id}")
self.start_time = datetime.now()
self._shard_task = asyncio.create_task(self._ws_connect())

Expand All @@ -80,7 +80,7 @@ async def start(self) -> None:

async def stop(self) -> None:
"""Disconnect from the Discord Gateway."""
self.client.logger.debug(f"Shutting down shard ID {self.shard_id}")
logger.debug(f"Shutting down shard ID {self.shard_id}")
if self.gateway is not None:
self.gateway.close()
self.gateway = None
Expand All @@ -98,7 +98,7 @@ def clear_ready(self) -> None:

async def _ws_connect(self) -> None:
"""Connect to the Discord Gateway."""
self.client.logger.info(f"Shard {self.shard_id} is attempting to connect to gateway...")
logger.info(f"Shard {self.shard_id} is attempting to connect to gateway...")
try:
async with GatewayClient(self, (self.shard_id, self.client.total_shards)) as self.gateway:
try:
Expand All @@ -123,7 +123,7 @@ async def _ws_connect(self) -> None:

except Exception as e:
self.client.dispatch(events.Disconnect())
self.client.logger.error("".join(traceback.format_exception(type(e), e, e.__traceback__)))
logger.error("".join(traceback.format_exception(type(e), e, e.__traceback__)))

async def change_presence(
self, status: Optional[Union[str, Status]] = Status.ONLINE, activity: Absent[Union[Activity, str]] = MISSING
Expand All @@ -149,17 +149,15 @@ async def change_presence(

if activity.type == ActivityType.STREAMING:
if not activity.url:
self.client.logger.warning("Streaming activity cannot be set without a valid URL attribute")
logger.warning("Streaming activity cannot be set without a valid URL attribute")
elif activity.type not in [
ActivityType.GAME,
ActivityType.STREAMING,
ActivityType.LISTENING,
ActivityType.WATCHING,
ActivityType.COMPETING,
]:
self.client.logger.warning(
f"Activity type `{ActivityType(activity.type).name}` may not be enabled for bots"
)
logger.warning(f"Activity type `{ActivityType(activity.type).name}` may not be enabled for bots")
else:
activity = self.client.activity

Expand All @@ -174,7 +172,7 @@ async def change_presence(
if self.client.status:
status = self.client.status
else:
self.client.logger.warning("Status must be set to a valid status type, defaulting to online")
logger.warning("Status must be set to a valid status type, defaulting to online")
status = Status.ONLINE

self.client._status = status
Expand Down
14 changes: 7 additions & 7 deletions naff/api/gateway/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ async def send(self, data: str, bypass=False) -> None:
bypass: Should the rate limit be ignored for this send (used for heartbeats)
"""
logger().debug(f"Sending data to websocket: {data}")
logger.debug(f"Sending data to websocket: {data}")

async with self._race_lock:
if self.ws is None:
return logger().warning("Attempted to send data while websocket is not connected!")
return logger.warning("Attempted to send data while websocket is not connected!")
if not bypass:
await self.rl_manager.rate_limit()

Expand Down Expand Up @@ -177,7 +177,7 @@ async def receive(self, force: bool = False) -> str:
resp = await self.ws.receive()

if resp.type == WSMsgType.CLOSE:
logger().debug(f"Disconnecting from gateway! Reason: {resp.data}::{resp.extra}")
logger.debug(f"Disconnecting from gateway! Reason: {resp.data}::{resp.extra}")
if resp.data >= 4000:
# This should propagate to __aexit__() which will forcefully shut down everything
# and cleanup correctly.
Expand Down Expand Up @@ -232,7 +232,7 @@ async def receive(self, force: bool = False) -> str:
try:
msg = OverriddenJson.loads(msg)
except Exception as e:
logger().error(e)
logger.error(e)
continue

return msg
Expand Down Expand Up @@ -270,7 +270,7 @@ async def run_bee_gees(self) -> None:
await self._start_bee_gees()
except Exception:
self.close()
logger().error("The heartbeater raised an exception!", exc_info=True)
logger.error("The heartbeater raised an exception!", exc_info=True)

async def _start_bee_gees(self) -> None:
if self.heartbeat_interval is None:
Expand All @@ -283,10 +283,10 @@ async def _start_bee_gees(self) -> None:
else:
return

logger().debug(f"Sending heartbeat every {self.heartbeat_interval} seconds")
logger.debug(f"Sending heartbeat every {self.heartbeat_interval} seconds")
while not self._kill_bee_gees.is_set():
if not self._acknowledged.is_set():
logger().warning(
logger.warning(
f"Heartbeat has not been acknowledged for {self.heartbeat_interval} seconds,"
" likely zombied connection. Reconnect!"
)
Expand Down
18 changes: 9 additions & 9 deletions naff/api/http/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def ingest_ratelimit(self, route: Route, header: CIMultiDictProxy, bucket_lock:

if bucket_lock.bucket_hash:
# We only ever try and cache the bucket if the bucket hash has been set (ignores unlimited endpoints)
logger().debug(f"Caching ingested rate limit data for: {bucket_lock.bucket_hash}")
logger.debug(f"Caching ingested rate limit data for: {bucket_lock.bucket_hash}")
self._endpoints[route.rl_bucket] = bucket_lock.bucket_hash
self.ratelimit_locks[bucket_lock.bucket_hash] = bucket_lock

Expand Down Expand Up @@ -296,14 +296,14 @@ async def request(
if result.get("global", False):
# global ratelimit is reached
# if we get a global, that's pretty bad, this would usually happen if the user is hitting the api from 2 clients sharing a token
logger().error(
logger.error(
f"Bot has exceeded global ratelimit, locking REST API for {result['retry_after']} seconds"
)
await self.global_lock.lock(float(result["retry_after"]))
continue
elif result.get("message") == "The resource is being rate limited.":
# resource ratelimit is reached
logger().warning(
logger.warning(
f"{route.endpoint} The resource is being rate limited! "
f"Reset in {result.get('retry_after')} seconds"
)
Expand All @@ -314,21 +314,21 @@ async def request(
# endpoint ratelimit is reached
# 429's are unfortunately unavoidable, but we can attempt to avoid them
# so long as these are infrequent we're doing well
logger().warning(
logger.warning(
f"{route.endpoint} Has exceeded it's ratelimit ({lock.limit})! Reset in {lock.delta} seconds"
)
await lock.defer_unlock() # lock this route and wait for unlock
continue
elif lock.remaining == 0:
# Last call available in the bucket, lock until reset
logger().debug(
logger.debug(
f"{route.endpoint} Has exhausted its ratelimit ({lock.limit})! Locking route for {lock.delta} seconds"
)
await lock.blind_defer_unlock() # lock this route, but continue processing the current response

elif response.status in {500, 502, 504}:
# Server issues, retry
logger().warning(
logger.warning(
f"{route.endpoint} Received {response.status}... retrying in {1 + attempt * 2} seconds"
)
await asyncio.sleep(1 + attempt * 2)
Expand All @@ -337,7 +337,7 @@ async def request(
if not 300 > response.status >= 200:
await self._raise_exception(response, route, result)

logger().debug(
logger.debug(
f"{route.endpoint} Received {response.status} :: [{lock.remaining}/{lock.limit} calls remaining]"
)
return result
Expand All @@ -348,7 +348,7 @@ async def request(
raise

async def _raise_exception(self, response, route, result) -> None:
logger().error(f"{route.method}::{route.url}: {response.status}")
logger.error(f"{route.method}::{route.url}: {response.status}")

if response.status == 403:
raise Forbidden(response, response_data=result, route=route)
Expand All @@ -360,7 +360,7 @@ async def _raise_exception(self, response, route, result) -> None:
raise HTTPException(response, response_data=result, route=route)

async def request_cdn(self, url, asset) -> bytes: # pyright: ignore [reportGeneralTypeIssues]
logger().debug(f"{asset} requests {url} from CDN")
logger.debug(f"{asset} requests {url} from CDN")
async with cast(ClientSession, self.__session).get(url) as response:
if response.status == 200:
return await response.read()
Expand Down
6 changes: 3 additions & 3 deletions naff/api/voice/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ def run(self) -> None:
self._stopped.clear()

asyncio.run_coroutine_threadsafe(self.state.ws.speaking(True), self.loop)
logger().debug(f"Now playing {self.current_audio!r}")
logger.debug(f"Now playing {self.current_audio!r}")
start = None

try:
while not self._stop_event.is_set():
if not self.state.ws.ready.is_set() or not self._resume.is_set():
run_coroutine_threadsafe(self.state.ws.speaking(False), self.loop)
logger().debug("Voice playback has been suspended!")
logger.debug("Voice playback has been suspended!")

wait_for = []

Expand All @@ -122,7 +122,7 @@ def run(self) -> None:
continue

run_coroutine_threadsafe(self.state.ws.speaking(), self.loop)
logger().debug("Voice playback has been resumed!")
logger.debug("Voice playback has been resumed!")
start = None
loops = 0

Expand Down
Loading

0 comments on commit 397e061

Please sign in to comment.