2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-10 11:15:27 +00:00

Stop instantiating logger objects and directly use the logging module

This commit is contained in:
Dan
2019-09-08 12:59:28 +02:00
parent 481e4ee339
commit 792068d7c8
23 changed files with 94 additions and 148 deletions

View File

@@ -48,8 +48,6 @@ from .methods import Methods
from .storage import Storage, FileStorage, MemoryStorage
from .types import User, SentCode, TermsOfService
log = logging.getLogger(__name__)
class Client(Methods, BaseClient):
"""Pyrogram Client, the main means for interacting with Telegram.
@@ -342,7 +340,7 @@ class Client(Methods, BaseClient):
if self.takeout_id:
self.send(functions.account.FinishTakeoutSession())
log.warning("Takeout session {} finished".format(self.takeout_id))
logging.warning("Takeout session {} finished".format(self.takeout_id))
Syncer.remove(self)
self.dispatcher.stop()
@@ -730,7 +728,7 @@ class Client(Methods, BaseClient):
print(e.MESSAGE.format(x=e.x))
time.sleep(e.x)
except Exception as e:
log.error(e, exc_info=True)
logging.error(e, exc_info=True)
raise
else:
self.password = None
@@ -830,7 +828,7 @@ class Client(Methods, BaseClient):
if not self.storage.is_bot and self.takeout:
self.takeout_id = self.send(functions.account.InitTakeoutSession()).id
log.warning("Takeout session {} initiated".format(self.takeout_id))
logging.warning("Takeout session {} initiated".format(self.takeout_id))
self.send(functions.updates.GetState())
except (Exception, KeyboardInterrupt):
@@ -1229,7 +1227,7 @@ class Client(Methods, BaseClient):
def download_worker(self):
name = threading.current_thread().name
log.debug("{} started".format(name))
logging.debug("{} started".format(name))
while True:
packet = self.download_queue.get()
@@ -1264,7 +1262,7 @@ class Client(Methods, BaseClient):
os.makedirs(directory, exist_ok=True)
shutil.move(temp_file_path, final_file_path)
except Exception as e:
log.error(e, exc_info=True)
logging.error(e, exc_info=True)
try:
os.remove(temp_file_path)
@@ -1278,11 +1276,11 @@ class Client(Methods, BaseClient):
finally:
done.set()
log.debug("{} stopped".format(name))
logging.debug("{} stopped".format(name))
def updates_worker(self):
name = threading.current_thread().name
log.debug("{} started".format(name))
logging.debug("{} started".format(name))
while True:
updates = self.updates_queue.get()
@@ -1310,7 +1308,7 @@ class Client(Methods, BaseClient):
pts_count = getattr(update, "pts_count", None)
if isinstance(update, types.UpdateChannelTooLong):
log.warning(update)
logging.warning(update)
if isinstance(update, types.UpdateNewChannelMessage) and is_min:
message = update.message
@@ -1362,11 +1360,11 @@ class Client(Methods, BaseClient):
elif isinstance(updates, types.UpdateShort):
self.dispatcher.updates_queue.put((updates.update, {}, {}))
elif isinstance(updates, types.UpdatesTooLong):
log.info(updates)
logging.info(updates)
except Exception as e:
log.error(e, exc_info=True)
logging.error(e, exc_info=True)
log.debug("{} stopped".format(name))
logging.debug("{} stopped".format(name))
def send(self, data: TLObject, retries: int = Session.MAX_RETRIES, timeout: float = Session.WAIT_TIMEOUT):
"""Send raw Telegram queries.
@@ -1541,7 +1539,7 @@ class Client(Methods, BaseClient):
if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group)
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
self.session_name, type(handler).__name__, name, group, module_path))
count += 1
@@ -1555,12 +1553,12 @@ class Client(Methods, BaseClient):
try:
module = import_module(module_path)
except ImportError:
log.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
logging.warning('[{}] [LOAD] Ignoring non-existent module "{}"'.format(
self.session_name, module_path))
continue
if "__path__" in dir(module):
log.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
logging.warning('[{}] [LOAD] Ignoring namespace "{}"'.format(
self.session_name, module_path))
continue
@@ -1576,13 +1574,13 @@ class Client(Methods, BaseClient):
if isinstance(handler, Handler) and isinstance(group, int):
self.add_handler(handler, group)
log.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
logging.info('[{}] [LOAD] {}("{}") in group {} from "{}"'.format(
self.session_name, type(handler).__name__, name, group, module_path))
count += 1
except Exception:
if warn_non_existent_functions:
log.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
logging.warning('[{}] [LOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.session_name, name, module_path))
if exclude:
@@ -1593,12 +1591,12 @@ class Client(Methods, BaseClient):
try:
module = import_module(module_path)
except ImportError:
log.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
logging.warning('[{}] [UNLOAD] Ignoring non-existent module "{}"'.format(
self.session_name, module_path))
continue
if "__path__" in dir(module):
log.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
logging.warning('[{}] [UNLOAD] Ignoring namespace "{}"'.format(
self.session_name, module_path))
continue
@@ -1614,20 +1612,20 @@ class Client(Methods, BaseClient):
if isinstance(handler, Handler) and isinstance(group, int):
self.remove_handler(handler, group)
log.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
logging.info('[{}] [UNLOAD] {}("{}") from group {} in "{}"'.format(
self.session_name, type(handler).__name__, name, group, module_path))
count -= 1
except Exception:
if warn_non_existent_functions:
log.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
logging.warning('[{}] [UNLOAD] Ignoring non-existent function "{}" from "{}"'.format(
self.session_name, name, module_path))
if count > 0:
log.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
logging.warning('[{}] Successfully loaded {} plugin{} from "{}"'.format(
self.session_name, count, "s" if count > 1 else "", root))
else:
log.warning('[{}] No plugin loaded from "{}"'.format(
logging.warning('[{}] No plugin loaded from "{}"'.format(
self.session_name, root))
# def get_initial_dialogs_chunk(self, offset_date: int = 0):
@@ -1644,10 +1642,10 @@ class Client(Methods, BaseClient):
# )
# )
# except FloodWait as e:
# log.warning("get_dialogs flood: waiting {} seconds".format(e.x))
# logging.warning("get_dialogs flood: waiting {} seconds".format(e.x))
# time.sleep(e.x)
# else:
# log.info("Total peers: {}".format(self.storage.peers_count))
# logging.info("Total peers: {}".format(self.storage.peers_count))
# return r
#
# def get_initial_dialogs(self):
@@ -1870,7 +1868,7 @@ class Client(Methods, BaseClient):
except Client.StopTransmission:
raise
except Exception as e:
log.error(e, exc_info=True)
logging.error(e, exc_info=True)
else:
if is_big:
return types.InputFileBig(
@@ -2096,7 +2094,7 @@ class Client(Methods, BaseClient):
raise e
except Exception as e:
if not isinstance(e, Client.StopTransmission):
log.error(e, exc_info=True)
logging.error(e, exc_info=True)
try:
os.remove(file_name)