Skip to content

Commit 792068d

Browse files
committed
Stop instantiating logger objects and directly use the logging module
1 parent 481e4ee commit 792068d

File tree

23 files changed

+94
-148
lines changed

23 files changed

+94
-148
lines changed

pyrogram/client/client.py

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

51-
log = logging.getLogger(__name__)
52-
5351

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

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

347345
Syncer.remove(self)
348346
self.dispatcher.stop()
@@ -730,7 +728,7 @@ def authorize(self) -> User:
730728
print(e.MESSAGE.format(x=e.x))
731729
time.sleep(e.x)
732730
except Exception as e:
733-
log.error(e, exc_info=True)
731+
logging.error(e, exc_info=True)
734732
raise
735733
else:
736734
self.password = None
@@ -830,7 +828,7 @@ def start(self):
830828

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

835833
self.send(functions.updates.GetState())
836834
except (Exception, KeyboardInterrupt):
@@ -1229,7 +1227,7 @@ def fetch_peers(
12291227

12301228
def download_worker(self):
12311229
name = threading.current_thread().name
1232-
log.debug("{} started".format(name))
1230+
logging.debug("{} started".format(name))
12331231

12341232
while True:
12351233
packet = self.download_queue.get()
@@ -1264,7 +1262,7 @@ def download_worker(self):
12641262
os.makedirs(directory, exist_ok=True)
12651263
shutil.move(temp_file_path, final_file_path)
12661264
except Exception as e:
1267-
log.error(e, exc_info=True)
1265+
logging.error(e, exc_info=True)
12681266

12691267
try:
12701268
os.remove(temp_file_path)
@@ -1278,11 +1276,11 @@ def download_worker(self):
12781276
finally:
12791277
done.set()
12801278

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

12831281
def updates_worker(self):
12841282
name = threading.current_thread().name
1285-
log.debug("{} started".format(name))
1283+
logging.debug("{} started".format(name))
12861284

12871285
while True:
12881286
updates = self.updates_queue.get()
@@ -1310,7 +1308,7 @@ def updates_worker(self):
13101308
pts_count = getattr(update, "pts_count", None)
13111309

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

13151313
if isinstance(update, types.UpdateNewChannelMessage) and is_min:
13161314
message = update.message
@@ -1362,11 +1360,11 @@ def updates_worker(self):
13621360
elif isinstance(updates, types.UpdateShort):
13631361
self.dispatcher.updates_queue.put((updates.update, {}, {}))
13641362
elif isinstance(updates, types.UpdatesTooLong):
1365-
log.info(updates)
1363+
logging.info(updates)
13661364
except Exception as e:
1367-
log.error(e, exc_info=True)
1365+
logging.error(e, exc_info=True)
13681366

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

13711369
def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
13721370
"""Send raw Telegram queries.
@@ -1541,7 +1539,7 @@ def load_plugins(self):
15411539
if isinstance(handler, Handler) and isinstance(group, int):
15421540
self.add_handler(handler, group)
15431541

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

15471545
count += 1
@@ -1555,12 +1553,12 @@ def load_plugins(self):
15551553
try:
15561554
module = import_module(module_path)
15571555
except ImportError:
1558-
log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
1556+
logging.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
15591557
self.session_name, module_path))
15601558
continue
15611559

15621560
if "__path__" in dir(module):
1563-
log.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
1561+
logging.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
15641562
self.session_name, module_path))
15651563
continue
15661564

@@ -1576,13 +1574,13 @@ def load_plugins(self):
15761574
if isinstance(handler, Handler) and isinstance(group, int):
15771575
self.add_handler(handler, group)
15781576

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

15821580
count += 1
15831581
except Exception:
15841582
if warn_non_existent_functions:
1585-
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
1583+
logging.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
15861584
self.session_name, name, module_path))
15871585

15881586
if exclude:
@@ -1593,12 +1591,12 @@ def load_plugins(self):
15931591
try:
15941592
module = import_module(module_path)
15951593
except ImportError:
1596-
log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
1594+
logging.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
15971595
self.session_name, module_path))
15981596
continue
15991597

16001598
if "__path__" in dir(module):
1601-
log.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
1599+
logging.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
16021600
self.session_name, module_path))
16031601
continue
16041602

@@ -1614,20 +1612,20 @@ def load_plugins(self):
16141612
if isinstance(handler, Handler) and isinstance(group, int):
16151613
self.remove_handler(handler, group)
16161614

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

16201618
count -= 1
16211619
except Exception:
16221620
if warn_non_existent_functions:
1623-
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
1621+
logging.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
16241622
self.session_name, name, module_path))
16251623

16261624
if count > 0:
1627-
log.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
1625+
logging.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
16281626
self.session_name, count, "s" if count > 1 else "", root))
16291627
else:
1630-
log.warning('[{}] No plugin loaded from "{}"'.format(
1628+
logging.warning('[{}] No plugin loaded from "{}"'.format(
16311629
self.session_name, root))
16321630

16331631
# def get_initial_dialogs_chunk(self, offset_date: int = 0):
@@ -1644,10 +1642,10 @@ def load_plugins(self):
16441642
# )
16451643
# )
16461644
# except FloodWait as e:
1647-
# log.warning("get_dialogs flood: waiting {} seconds".format(e.x))
1645+
# logging.warning("get_dialogs flood: waiting {} seconds".format(e.x))
16481646
# time.sleep(e.x)
16491647
# else:
1650-
# log.info("Total peers: {}".format(self.storage.peers_count))
1648+
# logging.info("Total peers: {}".format(self.storage.peers_count))
16511649
# return r
16521650
#
16531651
# def get_initial_dialogs(self):
@@ -1870,7 +1868,7 @@ def save_file(
18701868
except Client.StopTransmission:
18711869
raise
18721870
except Exception as e:
1873-
log.error(e, exc_info=True)
1871+
logging.error(e, exc_info=True)
18741872
else:
18751873
if is_big:
18761874
return types.InputFileBig(
@@ -2096,7 +2094,7 @@ def get_file(
20962094
raise e
20972095
except Exception as e:
20982096
if not isinstance(e, Client.StopTransmission):
2099-
log.error(e, exc_info=True)
2097+
logging.error(e, exc_info=True)
21002098

21012099
try:
21022100
os.remove(file_name)

pyrogram/client/ext/dispatcher.py

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

39-
log = logging.getLogger(__name__)
40-
4139

4240
class Dispatcher:
4341
NEW_MESSAGE_UPDATES = (
@@ -158,7 +156,7 @@ def remove_handler(self, handler, group: int):
158156

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

163161
while True:
164162
packet = self.updates_queue.get()
@@ -186,7 +184,7 @@ def update_worker(self, lock):
186184
if handler.check(parsed_update):
187185
args = (parsed_update,)
188186
except Exception as e:
189-
log.error(e, exc_info=True)
187+
logging.error(e, exc_info=True)
190188
continue
191189

192190
elif isinstance(handler, RawUpdateHandler):
@@ -202,12 +200,12 @@ def update_worker(self, lock):
202200
except pyrogram.ContinuePropagation:
203201
continue
204202
except Exception as e:
205-
log.error(e, exc_info=True)
203+
logging.error(e, exc_info=True)
206204

207205
break
208206
except pyrogram.StopPropagation:
209207
pass
210208
except Exception as e:
211-
log.error(e, exc_info=True)
209+
logging.error(e, exc_info=True)
212210

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

pyrogram/client/ext/syncer.py

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

23-
log = logging.getLogger(__name__)
24-
2523

2624
class Syncer:
2725
INTERVAL = 20
@@ -79,9 +77,9 @@ def sync(cls, client):
7977
start = time.time()
8078
client.storage.save()
8179
except Exception as e:
82-
log.critical(e, exc_info=True)
80+
logging.critical(e, exc_info=True)
8381
else:
84-
log.info('Synced "{}" in {:.6} ms'.format(
82+
logging.info('Synced "{}" in {:.6} ms'.format(
8583
client.storage.name,
8684
(time.time() - start) * 1000
8785
))

pyrogram/client/methods/chats/get_chat_members.py

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

28-
log = logging.getLogger(__name__)
29-
3028

3129
class Filters:
3230
ALL = "all"
@@ -153,7 +151,7 @@ def get_chat_members(
153151

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

pyrogram/client/methods/chats/get_dialogs.py

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

28-
log = logging.getLogger(__name__)
29-
3028

3129
class GetDialogs(BaseClient):
3230
def get_dialogs(
@@ -82,7 +80,7 @@ def get_dialogs(
8280
)
8381
)
8482
except FloodWait as e:
85-
log.warning("Sleeping {}s".format(e.x))
83+
logging.warning("Sleeping {}s".format(e.x))
8684
time.sleep(e.x)
8785
else:
8886
break
@@ -111,6 +109,6 @@ def get_dialogs(
111109
if not isinstance(dialog, types.Dialog):
112110
continue
113111

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

116114
return pyrogram.List(parsed_dialogs)

pyrogram/client/methods/contacts/get_contacts.py

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

28-
log = logging.getLogger(__name__)
29-
3028

3129
class GetContacts(BaseClient):
3230
def get_contacts(self) -> List["pyrogram.User"]:
@@ -45,7 +43,7 @@ def get_contacts(self) -> List["pyrogram.User"]:
4543
try:
4644
contacts = self.send(functions.contacts.GetContacts(hash=0))
4745
except FloodWait as e:
48-
log.warning("get_contacts flood: waiting {} seconds".format(e.x))
46+
logging.warning("get_contacts flood: waiting {} seconds".format(e.x))
4947
time.sleep(e.x)
5048
else:
5149
return pyrogram.List(pyrogram.User._parse(self, user) for user in contacts.users)

pyrogram/client/methods/messages/get_history.py

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

29-
log = logging.getLogger(__name__)
30-
3129

3230
class GetHistory(BaseClient):
3331
def get_history(
@@ -103,7 +101,7 @@ def get_history(
103101
)
104102
)
105103
except FloodWait as e:
106-
log.warning("Sleeping for {}s".format(e.x))
104+
logging.warning("Sleeping for {}s".format(e.x))
107105
time.sleep(e.x)
108106
else:
109107
break

pyrogram/client/methods/messages/get_history_count.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
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
2019
from typing import Union
2120

2221
from pyrogram.api import types, functions
2322
from pyrogram.client.ext import BaseClient
2423

25-
log = logging.getLogger(__name__)
26-
2724

2825
class GetHistoryCount(BaseClient):
2926
def get_history_count(

pyrogram/client/methods/messages/get_messages.py

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

28-
log = logging.getLogger(__name__)
29-
3028

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

@@ -116,7 +114,7 @@ def get_messages(
116114
try:
117115
r = self.send(rpc)
118116
except FloodWait as e:
119-
log.warning("Sleeping for {}s".format(e.x))
117+
logging.warning("Sleeping for {}s".format(e.x))
120118
time.sleep(e.x)
121119
else:
122120
break

0 commit comments

Comments
 (0)