Skip to content

Commit 01cd8bb

Browse files
committed
Optimize log calls
1 parent d298c62 commit 01cd8bb

8 files changed

Lines changed: 35 additions & 35 deletions

File tree

pyrogram/client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ async def authorize(self) -> User:
396396
except BadRequest as e:
397397
print(e.MESSAGE)
398398
except Exception as e:
399-
log.error(e, exc_info=True)
399+
log.exception(e)
400400
raise
401401
else:
402402
self.password = None
@@ -684,11 +684,11 @@ def load_plugins(self):
684684
try:
685685
module = import_module(module_path)
686686
except ImportError:
687-
log.warning(f'[{self.name}] [LOAD] Ignoring non-existent module "{module_path}"')
687+
log.warning('[%s] [LOAD] Ignoring non-existent module "%s"', self.name, module_path)
688688
continue
689689

690690
if "__path__" in dir(module):
691-
log.warning(f'[{self.name}] [LOAD] Ignoring namespace "{module_path}"')
691+
log.warning('[%s] [LOAD] Ignoring namespace "%s"', self.name, module_path)
692692
continue
693693

694694
if handlers is None:
@@ -719,11 +719,11 @@ def load_plugins(self):
719719
try:
720720
module = import_module(module_path)
721721
except ImportError:
722-
log.warning(f'[{self.name}] [UNLOAD] Ignoring non-existent module "{module_path}"')
722+
log.warning('[%s] [UNLOAD] Ignoring non-existent module "%s"', self.name, module_path)
723723
continue
724724

725725
if "__path__" in dir(module):
726-
log.warning(f'[{self.name}] [UNLOAD] Ignoring namespace "{module_path}"')
726+
log.warning('[%s] [UNLOAD] Ignoring namespace "%s"', self.name, module_path)
727727
continue
728728

729729
if handlers is None:
@@ -750,7 +750,7 @@ def load_plugins(self):
750750
log.info('[{}] Successfully loaded {} plugin{} from "{}"'.format(
751751
self.name, count, "s" if count > 1 else "", root))
752752
else:
753-
log.warning(f'[{self.name}] No plugin loaded from "{root}"')
753+
log.warning('[%s] No plugin loaded from "%s"', self.name, root)
754754

755755
async def handle_download(self, packet):
756756
file_id, directory, file_name, in_memory, file_size, progress, progress_args = packet
@@ -1012,7 +1012,7 @@ async def get_file(
10121012
except pyrogram.StopTransmission:
10131013
raise
10141014
except Exception as e:
1015-
log.error(e, exc_info=True)
1015+
log.exception(e)
10161016

10171017
def guess_mime_type(self, filename: str) -> Optional[str]:
10181018
return self.mimetypes.guess_type(filename)[0]

pyrogram/dispatcher.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ async def start(self):
151151
self.loop.create_task(self.handler_worker(self.locks_list[-1]))
152152
)
153153

154-
log.info(f"Started {self.client.workers} HandlerTasks")
154+
log.info("Started %s HandlerTasks", self.client.workers)
155155

156156
async def stop(self):
157157
if not self.client.no_updates:
@@ -164,7 +164,7 @@ async def stop(self):
164164
self.handler_worker_tasks.clear()
165165
self.groups.clear()
166166

167-
log.info(f"Stopped {self.client.workers} HandlerTasks")
167+
log.info("Stopped %s HandlerTasks", self.client.workers)
168168

169169
def add_handler(self, handler, group: int):
170170
async def fn():
@@ -226,7 +226,7 @@ async def handler_worker(self, lock):
226226
if await handler.check(self.client, parsed_update):
227227
args = (parsed_update,)
228228
except Exception as e:
229-
log.error(e, exc_info=True)
229+
log.exception(e)
230230
continue
231231

232232
elif isinstance(handler, RawUpdateHandler):
@@ -250,10 +250,10 @@ async def handler_worker(self, lock):
250250
except pyrogram.ContinuePropagation:
251251
continue
252252
except Exception as e:
253-
log.error(e, exc_info=True)
253+
log.exception(e)
254254

255255
break
256256
except pyrogram.StopPropagation:
257257
pass
258258
except Exception as e:
259-
log.error(e, exc_info=True)
259+
log.exception(e)

pyrogram/methods/advanced/save_file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ async def worker(session):
107107
try:
108108
await session.invoke(data)
109109
except Exception as e:
110-
log.error(e)
110+
log.exception(e)
111111

112112
part_size = 512 * 1024
113113

@@ -201,7 +201,7 @@ async def worker(session):
201201
except StopTransmission:
202202
raise
203203
except Exception as e:
204-
log.error(e, exc_info=True)
204+
log.exception(e)
205205
else:
206206
if is_big:
207207
return raw.types.InputFileBig(

pyrogram/methods/auth/terminate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def terminate(
4141

4242
if self.takeout_id:
4343
await self.invoke(raw.functions.account.FinishTakeoutSession())
44-
log.warning(f"Takeout session {self.takeout_id} finished")
44+
log.warning("Takeout session %s finished", self.takeout_id)
4545

4646
await self.storage.save()
4747
await self.dispatcher.stop()

pyrogram/methods/utilities/start.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def main():
6363

6464
if not await self.storage.is_bot() and self.takeout:
6565
self.takeout_id = (await self.invoke(raw.functions.account.InitTakeoutSession())).id
66-
log.warning(f"Takeout session {self.takeout_id} initiated")
66+
log.warning("Takeout session %s initiated", self.takeout_id)
6767

6868
await self.invoke(raw.functions.updates.GetState())
6969
except (Exception, KeyboardInterrupt):

pyrogram/parser/html.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def handle_endtag(self, tag):
103103
line, offset = self.getpos()
104104
offset += 1
105105

106-
log.debug(f"Unmatched closing tag </{tag}> at line {line}:{offset}")
106+
log.debug("Unmatched closing tag </%s> at line %s:%s", tag, line, offset)
107107
else:
108108
if not self.tag_entities[tag]:
109109
self.tag_entities.pop(tag)
@@ -131,7 +131,7 @@ async def parse(self, text: str):
131131
for tag, entities in parser.tag_entities.items():
132132
unclosed_tags.append(f"<{tag}> (x{len(entities)})")
133133

134-
log.warning(f"Unclosed tags: {', '.join(unclosed_tags)}")
134+
log.warning("Unclosed tags: %s", ", ".join(unclosed_tags))
135135

136136
entities = []
137137

pyrogram/session/auth.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,34 @@ async def create(self):
7979
self.connection = Connection(self.dc_id, self.test_mode, self.ipv6, self.proxy)
8080

8181
try:
82-
log.info(f"Start creating a new auth key on DC{self.dc_id}")
82+
log.info("Start creating a new auth key on DC%s", self.dc_id)
8383

8484
await self.connection.connect()
8585

8686
# Step 1; Step 2
8787
nonce = int.from_bytes(urandom(16), "little", signed=True)
88-
log.debug(f"Send req_pq: {nonce}")
88+
log.debug("Send req_pq: %s", nonce)
8989
res_pq = await self.invoke(raw.functions.ReqPqMulti(nonce=nonce))
90-
log.debug(f"Got ResPq: {res_pq.server_nonce}")
91-
log.debug(f"Server public key fingerprints: {res_pq.server_public_key_fingerprints}")
90+
log.debug("Got ResPq: %s", res_pq.server_nonce)
91+
log.debug("Server public key fingerprints: %s", res_pq.server_public_key_fingerprints)
9292

9393
for i in res_pq.server_public_key_fingerprints:
9494
if i in rsa.server_public_keys:
95-
log.debug(f"Using fingerprint: {i}")
95+
log.debug("Using fingerprint: %s", i)
9696
public_key_fingerprint = i
9797
break
9898
else:
99-
log.debug(f"Fingerprint unknown: {i}")
99+
log.debug("Fingerprint unknown: %s", i)
100100
else:
101101
raise Exception("Public key not found")
102102

103103
# Step 3
104104
pq = int.from_bytes(res_pq.pq, "big")
105-
log.debug(f"Start PQ factorization: {pq}")
105+
log.debug("Start PQ factorization: %s", pq)
106106
start = time.time()
107107
g = prime.decompose(pq)
108108
p, q = sorted((g, pq // g)) # p < q
109-
log.debug(f"Done PQ factorization ({round(time.time() - start, 3)}s): {p} {q}")
109+
log.debug("Done PQ factorization (%ss): %s %s", round(time.time() - start, 3), p, q)
110110

111111
# Step 4
112112
server_nonce = res_pq.server_nonce
@@ -168,7 +168,7 @@ async def create(self):
168168
dh_prime = int.from_bytes(server_dh_inner_data.dh_prime, "big")
169169
delta_time = server_dh_inner_data.server_time - time.time()
170170

171-
log.debug(f"Delta time: {round(delta_time, 3)}")
171+
log.debug("Delta time: %s", round(delta_time, 3))
172172

173173
# Step 6
174174
g = server_dh_inner_data.g
@@ -262,11 +262,11 @@ async def create(self):
262262
# Step 9
263263
server_salt = aes.xor(new_nonce[:8], server_nonce[:8])
264264

265-
log.debug(f"Server salt: {int.from_bytes(server_salt, 'little')}")
265+
log.debug("Server salt: %s", int.from_bytes(server_salt, "little"))
266266

267-
log.info(f"Done auth key exchange: {set_client_dh_params_answer.__class__.__name__}")
267+
log.info("Done auth key exchange: %s", set_client_dh_params_answer.__class__.__name__)
268268
except Exception as e:
269-
log.info(f"Retrying due to {type(e).__name__}: {e}")
269+
log.info("Retrying due to %s: %s", type(e).__name__, e)
270270

271271
if retries_left:
272272
retries_left -= 1

pyrogram/types/messages_and_media/message.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3045,13 +3045,13 @@ async def copy(
30453045
RPCError: In case of a Telegram RPC error.
30463046
"""
30473047
if self.service:
3048-
log.warning(f"Service messages cannot be copied. "
3049-
f"chat_id: {self.chat.id}, message_id: {self.id}")
3048+
log.warning("Service messages cannot be copied. chat_id: %s, message_id: %s",
3049+
self.chat.id, self.id)
30503050
elif self.game and not await self._client.storage.is_bot():
3051-
log.warning(f"Users cannot send messages with Game media type. "
3052-
f"chat_id: {self.chat.id}, message_id: {self.id}")
3051+
log.warning("Users cannot send messages with Game media type. chat_id: %s, message_id: %s",
3052+
self.chat.id, self.id)
30533053
elif self.empty:
3054-
log.warning(f"Empty messages cannot be copied. ")
3054+
log.warning("Empty messages cannot be copied.")
30553055
elif self.text:
30563056
return await self._client.send_message(
30573057
chat_id,

0 commit comments

Comments
 (0)