Skip to content

Commit a015f99

Browse files
committed
Revert "Stop instantiating logger objects and directly use the logging module"
This reverts commit 792068d
1 parent 792068d commit a015f99

File tree

23 files changed

+148
-94
lines changed

23 files changed

+148
-94
lines changed

pyrogram/client/client.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
from .storage import Storage, FileStorage, MemoryStorage
4949
from .types import User, SentCode, TermsOfService
5050

51+
log = logging.getLogger(__name__)
52+
5153

5254
class Client(Methods, BaseClient):
5355
"""Pyrogram Client, the main means for interacting with Telegram.
@@ -340,7 +342,7 @@ def terminate(self):
340342

341343
if self.takeout_id:
342344
self.send(functions.account.FinishTakeoutSession())
343-
logging.warning("Takeout session {} finished".format(self.takeout_id))
345+
log.warning("Takeout session {} finished".format(self.takeout_id))
344346

345347
Syncer.remove(self)
346348
self.dispatcher.stop()
@@ -728,7 +730,7 @@ def authorize(self) -> User:
728730
print(e.MESSAGE.format(x=e.x))
729731
time.sleep(e.x)
730732
except Exception as e:
731-
logging.error(e, exc_info=True)
733+
log.error(e, exc_info=True)
732734
raise
733735
else:
734736
self.password = None
@@ -828,7 +830,7 @@ def start(self):
828830

829831
if not self.storage.is_bot and self.takeout:
830832
self.takeout_id = self.send(functions.account.InitTakeoutSession()).id
831-
logging.warning("Takeout session {} initiated".format(self.takeout_id))
833+
log.warning("Takeout session {} initiated".format(self.takeout_id))
832834

833835
self.send(functions.updates.GetState())
834836
except (Exception, KeyboardInterrupt):
@@ -1227,7 +1229,7 @@ def fetch_peers(
12271229

12281230
def download_worker(self):
12291231
name = threading.current_thread().name
1230-
logging.debug("{} started".format(name))
1232+
log.debug("{} started".format(name))
12311233

12321234
while True:
12331235
packet = self.download_queue.get()
@@ -1262,7 +1264,7 @@ def download_worker(self):
12621264
os.makedirs(directory, exist_ok=True)
12631265
shutil.move(temp_file_path, final_file_path)
12641266
except Exception as e:
1265-
logging.error(e, exc_info=True)
1267+
log.error(e, exc_info=True)
12661268

12671269
try:
12681270
os.remove(temp_file_path)
@@ -1276,11 +1278,11 @@ def download_worker(self):
12761278
finally:
12771279
done.set()
12781280

1279-
logging.debug("{} stopped".format(name))
1281+
log.debug("{} stopped".format(name))
12801282

12811283
def updates_worker(self):
12821284
name = threading.current_thread().name
1283-
logging.debug("{} started".format(name))
1285+
log.debug("{} started".format(name))
12841286

12851287
while True:
12861288
updates = self.updates_queue.get()
@@ -1308,7 +1310,7 @@ def updates_worker(self):
13081310
pts_count = getattr(update, "pts_count", None)
13091311

13101312
if isinstance(update, types.UpdateChannelTooLong):
1311-
logging.warning(update)
1313+
log.warning(update)
13121314

13131315
if isinstance(update, types.UpdateNewChannelMessage) and is_min:
13141316
message = update.message
@@ -1360,11 +1362,11 @@ def updates_worker(self):
13601362
elif isinstance(updates, types.UpdateShort):
13611363
self.dispatcher.updates_queue.put((updates.update, {}, {}))
13621364
elif isinstance(updates, types.UpdatesTooLong):
1363-
logging.info(updates)
1365+
log.info(updates)
13641366
except Exception as e:
1365-
logging.error(e, exc_info=True)
1367+
log.error(e, exc_info=True)
13661368

1367-
logging.debug("{} stopped".format(name))
1369+
log.debug("{} stopped".format(name))
13681370

13691371
def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
13701372
"""Send raw Telegram queries.
@@ -1539,7 +1541,7 @@ def load_plugins(self):
15391541
if isinstance(handler, Handler) and isinstance(group, int):
15401542
self.add_handler(handler, group)
15411543

1542-
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1544+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
15431545
self.session_name, type(handler).__name__, name, group, module_path))
15441546

15451547
count += 1
@@ -1553,12 +1555,12 @@ def load_plugins(self):
15531555
try:
15541556
module = import_module(module_path)
15551557
except ImportError:
1556-
logging.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
1558+
log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
15571559
self.session_name, module_path))
15581560
continue
15591561

15601562
if "__path__" in dir(module):
1561-
logging.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
1563+
log.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
15621564
self.session_name, module_path))
15631565
continue
15641566

@@ -1574,13 +1576,13 @@ def load_plugins(self):
15741576
if isinstance(handler, Handler) and isinstance(group, int):
15751577
self.add_handler(handler, group)
15761578

1577-
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
1579+
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
15781580
self.session_name, type(handler).__name__, name, group, module_path))
15791581

15801582
count += 1
15811583
except Exception:
15821584
if warn_non_existent_functions:
1583-
logging.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
1585+
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
15841586
self.session_name, name, module_path))
15851587

15861588
if exclude:
@@ -1591,12 +1593,12 @@ def load_plugins(self):
15911593
try:
15921594
module = import_module(module_path)
15931595
except ImportError:
1594-
logging.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
1596+
log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
15951597
self.session_name, module_path))
15961598
continue
15971599

15981600
if "__path__" in dir(module):
1599-
logging.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
1601+
log.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
16001602
self.session_name, module_path))
16011603
continue
16021604

@@ -1612,20 +1614,20 @@ def load_plugins(self):
16121614
if isinstance(handler, Handler) and isinstance(group, int):
16131615
self.remove_handler(handler, group)
16141616

1615-
logging.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
1617+
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
16161618
self.session_name, type(handler).__name__, name, group, module_path))
16171619

16181620
count -= 1
16191621
except Exception:
16201622
if warn_non_existent_functions:
1621-
logging.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
1623+
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
16221624
self.session_name, name, module_path))
16231625

16241626
if count > 0:
1625-
logging.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
1627+
log.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
16261628
self.session_name, count, "s" if count > 1 else "", root))
16271629
else:
1628-
logging.warning('[{}] No plugin loaded from "{}"'.format(
1630+
log.warning('[{}] No plugin loaded from "{}"'.format(
16291631
self.session_name, root))
16301632

16311633
# def get_initial_dialogs_chunk(self, offset_date: int = 0):
@@ -1642,10 +1644,10 @@ def load_plugins(self):
16421644
# )
16431645
# )
16441646
# except FloodWait as e:
1645-
# logging.warning("get_dialogs flood: waiting {} seconds".format(e.x))
1647+
# log.warning("get_dialogs flood: waiting {} seconds".format(e.x))
16461648
# time.sleep(e.x)
16471649
# else:
1648-
# logging.info("Total peers: {}".format(self.storage.peers_count))
1650+
# log.info("Total peers: {}".format(self.storage.peers_count))
16491651
# return r
16501652
#
16511653
# def get_initial_dialogs(self):
@@ -1868,7 +1870,7 @@ def save_file(
18681870
except Client.StopTransmission:
18691871
raise
18701872
except Exception as e:
1871-
logging.error(e, exc_info=True)
1873+
log.error(e, exc_info=True)
18721874
else:
18731875
if is_big:
18741876
return types.InputFileBig(
@@ -2094,7 +2096,7 @@ def get_file(
20942096
raise e
20952097
except Exception as e:
20962098
if not isinstance(e, Client.StopTransmission):
2097-
logging.error(e, exc_info=True)
2099+
log.error(e, exc_info=True)
20982100

20992101
try:
21002102
os.remove(file_name)

pyrogram/client/ext/dispatcher.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler
3737
)
3838

39+
log = logging.getLogger(__name__)
40+
3941

4042
class Dispatcher:
4143
NEW_MESSAGE_UPDATES = (
@@ -156,7 +158,7 @@ def remove_handler(self, handler, group: int):
156158

157159
def update_worker(self, lock):
158160
name = threading.current_thread().name
159-
logging.debug("{} started".format(name))
161+
log.debug("{} started".format(name))
160162

161163
while True:
162164
packet = self.updates_queue.get()
@@ -184,7 +186,7 @@ def update_worker(self, lock):
184186
if handler.check(parsed_update):
185187
args = (parsed_update,)
186188
except Exception as e:
187-
logging.error(e, exc_info=True)
189+
log.error(e, exc_info=True)
188190
continue
189191

190192
elif isinstance(handler, RawUpdateHandler):
@@ -200,12 +202,12 @@ def update_worker(self, lock):
200202
except pyrogram.ContinuePropagation:
201203
continue
202204
except Exception as e:
203-
logging.error(e, exc_info=True)
205+
log.error(e, exc_info=True)
204206

205207
break
206208
except pyrogram.StopPropagation:
207209
pass
208210
except Exception as e:
209-
logging.error(e, exc_info=True)
211+
log.error(e, exc_info=True)
210212

211-
logging.debug("{} stopped".format(name))
213+
log.debug("{} stopped".format(name))

pyrogram/client/ext/syncer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import time
2121
from threading import Thread, Event, Lock
2222

23+
log = logging.getLogger(__name__)
24+
2325

2426
class Syncer:
2527
INTERVAL = 20
@@ -77,9 +79,9 @@ def sync(cls, client):
7779
start = time.time()
7880
client.storage.save()
7981
except Exception as e:
80-
logging.critical(e, exc_info=True)
82+
log.critical(e, exc_info=True)
8183
else:
82-
logging.info('Synced "{}" in {:.6} ms'.format(
84+
log.info('Synced "{}" in {:.6} ms'.format(
8385
client.storage.name,
8486
(time.time() - start) * 1000
8587
))

pyrogram/client/methods/chats/get_chat_members.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pyrogram.errors import FloodWait
2626
from ...ext import BaseClient
2727

28+
log = logging.getLogger(__name__)
29+
2830

2931
class Filters:
3032
ALL = "all"
@@ -151,7 +153,7 @@ def get_chat_members(
151153

152154
return pyrogram.List(pyrogram.ChatMember._parse(self, member, users) for member in members)
153155
except FloodWait as e:
154-
logging.warning("Sleeping for {}s".format(e.x))
156+
log.warning("Sleeping for {}s".format(e.x))
155157
time.sleep(e.x)
156158
else:
157159
raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id))

pyrogram/client/methods/chats/get_dialogs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pyrogram.errors import FloodWait
2626
from ...ext import BaseClient, utils
2727

28+
log = logging.getLogger(__name__)
29+
2830

2931
class GetDialogs(BaseClient):
3032
def get_dialogs(
@@ -80,7 +82,7 @@ def get_dialogs(
8082
)
8183
)
8284
except FloodWait as e:
83-
logging.warning("Sleeping {}s".format(e.x))
85+
log.warning("Sleeping {}s".format(e.x))
8486
time.sleep(e.x)
8587
else:
8688
break
@@ -109,6 +111,6 @@ def get_dialogs(
109111
if not isinstance(dialog, types.Dialog):
110112
continue
111113

112-
parsed_dialogs.append(pyrogram.Dialogging._parse(self, dialog, messages, users, chats))
114+
parsed_dialogs.append(pyrogram.Dialog._parse(self, dialog, messages, users, chats))
113115

114116
return pyrogram.List(parsed_dialogs)

pyrogram/client/methods/contacts/get_contacts.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pyrogram.errors import FloodWait
2626
from ...ext import BaseClient
2727

28+
log = logging.getLogger(__name__)
29+
2830

2931
class GetContacts(BaseClient):
3032
def get_contacts(self) -> List["pyrogram.User"]:
@@ -43,7 +45,7 @@ def get_contacts(self) -> List["pyrogram.User"]:
4345
try:
4446
contacts = self.send(functions.contacts.GetContacts(hash=0))
4547
except FloodWait as e:
46-
logging.warning("get_contacts flood: waiting {} seconds".format(e.x))
48+
log.warning("get_contacts flood: waiting {} seconds".format(e.x))
4749
time.sleep(e.x)
4850
else:
4951
return pyrogram.List(pyrogram.User._parse(self, user) for user in contacts.users)

pyrogram/client/methods/messages/get_history.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
from pyrogram.errors import FloodWait
2727
from ...ext import BaseClient
2828

29+
log = logging.getLogger(__name__)
30+
2931

3032
class GetHistory(BaseClient):
3133
def get_history(
@@ -101,7 +103,7 @@ def get_history(
101103
)
102104
)
103105
except FloodWait as e:
104-
logging.warning("Sleeping for {}s".format(e.x))
106+
log.warning("Sleeping for {}s".format(e.x))
105107
time.sleep(e.x)
106108
else:
107109
break

pyrogram/client/methods/messages/get_history_count.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
import logging
1920
from typing import Union
2021

2122
from pyrogram.api import types, functions
2223
from pyrogram.client.ext import BaseClient
2324

25+
log = logging.getLogger(__name__)
26+
2427

2528
class GetHistoryCount(BaseClient):
2629
def get_history_count(

pyrogram/client/methods/messages/get_messages.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
from pyrogram.errors import FloodWait
2626
from ...ext import BaseClient, utils
2727

28+
log = logging.getLogger(__name__)
29+
2830

2931
# TODO: Rewrite using a flag for replied messages and have message_ids non-optional
3032

@@ -114,7 +116,7 @@ def get_messages(
114116
try:
115117
r = self.send(rpc)
116118
except FloodWait as e:
117-
logging.warning("Sleeping for {}s".format(e.x))
119+
log.warning("Sleeping for {}s".format(e.x))
118120
time.sleep(e.x)
119121
else:
120122
break

0 commit comments

Comments
 (0)