From b06899e7d4658ab732d1579283771f709a7a4bce Mon Sep 17 00:00:00 2001 From: Rapptz Date: Fri, 9 Jun 2017 18:53:24 -0400 Subject: [PATCH] Defer logging formatting until the logger is actually called. This would cause unnecessary format calls even if you didn't have logging enabled. --- discord/client.py | 4 ++-- discord/gateway.py | 10 +++++----- discord/http.py | 13 ++++++------- discord/opus.py | 2 +- discord/state.py | 2 +- discord/voice_client.py | 5 ++--- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/discord/client.py b/discord/client.py index 56c1a7e4d..f61f48e8f 100644 --- a/discord/client.py +++ b/discord/client.py @@ -405,7 +405,7 @@ class Client: raise retry = backoff.delay() - log.exception("Attempting a reconnect in {:.2f}s".format(retry)) + log.exception("Attempting a reconnect in %.2fs", retry) yield from asyncio.sleep(retry, loop=self.loop) @asyncio.coroutine @@ -725,7 +725,7 @@ class Client: raise ClientException('event registered must be a coroutine function') setattr(self, coro.__name__, coro) - log.info('{0.__name__} has successfully been registered as an event'.format(coro)) + log.info('%s has successfully been registered as an event', coro.__name__) return coro def async_event(self, coro): diff --git a/discord/gateway.py b/discord/gateway.py index 8cc13f044..c8dc08a31 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -205,7 +205,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): client._connection._update_references(ws) - log.info('Created websocket connected to {}'.format(gateway)) + log.info('Created websocket connected to %s', gateway) # poll event for OP Hello yield from ws.poll_event() @@ -425,10 +425,10 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): yield from self.received_message(msg) except websockets.exceptions.ConnectionClosed as e: if self._can_handle_close(e.code): - log.info('Websocket closed with {0.code} ({0.reason}), attempting a reconnect.'.format(e)) + log.info('Websocket closed with %s (%s), attempting a reconnect.', e.code, e.reason) raise ResumeWebSocket(self.shard_id) from e else: - log.info('Websocket closed with {0.code} ({0.reason}), cannot reconnect.'.format(e)) + log.info('Websocket closed with %s (%s), cannot reconnect.', e.code, e.reason) raise ConnectionClosed(e, shard_id=self.shard_id) from e @asyncio.coroutine @@ -646,7 +646,7 @@ class DiscordVoiceWebSocket(websockets.client.WebSocketClientProtocol): struct.pack_into('>I', packet, 0, state.ssrc) state.socket.sendto(packet, (state.endpoint_ip, state.voice_port)) recv = yield from self.loop.sock_recv(state.socket, 70) - log.debug('received packet in initial_connection: {}'.format(recv)) + log.debug('received packet in initial_connection: %s', recv) # the ip is ascii starting at the 4th byte and ending at the first null ip_start = 4 @@ -657,7 +657,7 @@ class DiscordVoiceWebSocket(websockets.client.WebSocketClientProtocol): # yes, this is different endianness from everything else state.port = struct.unpack_from(' r.status >= 200: - log.debug(self.SUCCESS_LOG.format(method=method, url=url, text=data)) + log.debug('%s %s has received %s', method, url, data) return data # we are being rate limited if r.status == 429: - fmt = 'We are being rate limited. Retrying in {:.2} seconds. Handled under the bucket "{}"' + fmt = 'We are being rate limited. Retrying in %.2f seconds. Handled under the bucket "%s"' # sleep a bit retry_after = data['retry_after'] / 1000.0 - log.info(fmt.format(retry_after, bucket)) + log.info(fmt, retry_after, bucket) # check if it's a global rate limit is_global = data.get('global', False) if is_global: - log.info('Global rate limit has been hit. Retrying in {:.2} seconds.'.format(retry_after)) + log.info('Global rate limit has been hit. Retrying in %.2f seconds.', retry_after) self._global_over.clear() yield from asyncio.sleep(retry_after, loop=self.loop) diff --git a/discord/opus.py b/discord/opus.py index fcf27a721..0054da4ef 100644 --- a/discord/opus.py +++ b/discord/opus.py @@ -149,7 +149,7 @@ class OpusError(DiscordException): def __init__(self, code): self.code = code msg = _lib.opus_strerror(self.code).decode('utf-8') - log.info('"{}" has happened'.format(msg)) + log.info('"%s" has happened', msg) super().__init__(msg) class OpusNotLoaded(DiscordException): diff --git a/discord/state.py b/discord/state.py index 67f8f7639..7ff05b3bf 100644 --- a/discord/state.py +++ b/discord/state.py @@ -265,7 +265,7 @@ class ConnectionState: # call GUILD_SYNC after we're done chunking if not self.is_bot: - log.info('Requesting GUILD_SYNC for %s guilds' % len(self.guilds)) + log.info('Requesting GUILD_SYNC for %s guilds', len(self.guilds)) yield from self.syncer([s.id for s in self.guilds]) except asyncio.CancelledError: pass diff --git a/discord/voice_client.py b/discord/voice_client.py index 40122c985..796eabc9e 100644 --- a/discord/voice_client.py +++ b/discord/voice_client.py @@ -227,7 +227,6 @@ class VoiceClient: @asyncio.coroutine def poll_voice_ws(self, reconnect): backoff = ExponentialBackoff() - fmt = 'Disconnected from voice... Reconnecting in {:.2f}s.' while True: try: yield from self.ws.poll_event() @@ -242,7 +241,7 @@ class VoiceClient: raise e retry = backoff.delay() - log.exception(fmt.format(retry)) + log.exception('Disconnected from voice... Reconnecting in %.2fs.', retry) self._connected.clear() yield from asyncio.sleep(retry, loop=self.loop) yield from self.terminate_handshake() @@ -417,6 +416,6 @@ class VoiceClient: try: self.socket.sendto(packet, (self.endpoint_ip, self.voice_port)) except BlockingIOError: - log.warning('A packet has been dropped (seq: {0.sequence}, timestamp: {0.timestamp})'.format(self)) + log.warning('A packet has been dropped (seq: %s, timestamp: %s)', self.sequence, self.timestamp) self.checked_add('timestamp', self.encoder.SAMPLES_PER_FRAME, 4294967295)