Use lazy % formatting in logging functions
492 try:
493 system.shutdown()
494 except Exception as h_e: # pylint: disable=broad-except
495 LOG.error(f"heroku app error : {h_e}")496 offline_start_time += 20
497 await asyncio.sleep(10)
498 continue
Use lazy % formatting in logging functions
507 f"#WARNING\n\ndyno kill worker `{prog}%` completed !"
508 f"\n`{rem}`s remaining !")
509 warned = True
510 LOG.info(f"< dyno kill worker ... ({prog}%)({mins}) >")511 await asyncio.sleep(1)
512 count += 1
513 LOG.info("< state changed to online ! >")
Use lazy % formatting in logging functions
187 done.append(tmp)
188
189 if done:
190 _LOG.info(f"{work_type.rstrip('e')}ed {plg}")191
192 return done
Use lazy % formatting in logging functions
259 try:
260 await task
261 except Exception as i_e:
262 _LOG.error(f"({meth}) [{plg.cat}/{plg.name}] - {i_e}")263
264 tasks.clear()
265
Use lazy % formatting in logging functions
263
264 tasks.clear()
265
266 _LOG.info(f"on_{meth} tasks completed !")267
268 async def start(self) -> None:
269 self._event.clear()
Use lazy % formatting in logging functions
21 @staticmethod
22 async def restart(hard: bool = False, **_) -> None:
23 """ Restart the Userge """
24 _LOG.info(f"Restarting Userge [{'HARD' if hard else 'SOFT'}]")25 restart(hard)
Use lazy % formatting in logging functions
44
45
46atexit.register(_stop)
47_LOG.info(f"Started Pool : {_MAX} Workers")
Use lazy % formatting in logging functions
40
41def _stop():
42 _EXECUTOR.shutdown()
43 _LOG.info(f"Stopped Pool : {_MAX} Workers")44
45
46atexit.register(_stop)
Use lazy % formatting in logging functions
360 _LOG.info(f"Running Coroutine - {mode}")
361 self.loop.run_until_complete(coro)
362 else:
363 _LOG.info(f"Idling Userge - {mode}")364 self.loop.run_until_complete(idle_event.wait())
365
366 if not log_errored:
Use lazy % formatting in logging functions
345 log_errored = True
346
347 def _handle(num, _) -> None:
348 _LOG.info(349 f"Received Stop Signal [{signal.Signals(num).name}], Exiting Userge ...")
350
351 idle_event.set()
Use lazy % formatting in logging functions
226 _LOG.info(f"started bot: {self._me.username}")
227 else:
228 RawClient.USER_ID = self._me.id
229 _LOG.info(f"started user: {self._me.first_name}")230
231 def __eq__(self, o: object) -> bool:
232 return isinstance(o, _AbstractUserge) and self.id == o.id
Use lazy % formatting in logging functions
223
224 if self.is_bot:
225 RawClient.BOT_ID = self._me.id
226 _LOG.info(f"started bot: {self._me.username}")227 else:
228 RawClient.USER_ID = self._me.id
229 _LOG.info(f"started user: {self._me.first_name}")
Use lazy % formatting in logging functions
122
123 counter += timeout
124 if counter >= max_:
125 _LOG.info(f"Max timeout reached ! [{max_} sec]")126 break
127
128
Use lazy % formatting in logging functions
116 max_ = 1800 # 30 min
117
118 while await _is_running():
119 _LOG.info("Waiting for the Termination of "120 f"previous Userge instance ... [{timeout} sec]")
121 time.sleep(timeout)
122
Use lazy % formatting in logging functions
41 try:
42 imported = importlib.import_module(path)
43 except Exception as i_e:
44 _LOG.error(f"[{path}] - {i_e}") 45
46 return imported
47
Use lazy % formatting in logging functions
357
358 with suppress(asyncio.exceptions.CancelledError, RuntimeError):
359 if coro:
360 _LOG.info(f"Running Coroutine - {mode}")361 self.loop.run_until_complete(coro)
362 else:
363 _LOG.info(f"Idling Userge - {mode}")
Description
Formatting the message manually before passing it to a logging
call does unnecessary work if logging is disabled. Consider using the logging
module's built-in formatting features to avoid that.
If your logging level is set to "WARNING"
, but the logging call is:
logging.debug("Data: %s", data)
In the above example, there will be no logging as the level debug
is below warning
. The conversion of the data
object to a string will be skipped by the logging module resulting in improved performance.
logging.debug(f"Data: {data}")
In the code shown above, the conversion of the data
object to a string will happen regardless of whether the message is logged or not (which is based on the log level).
Bad practice
import logging
import inspect
logging.basicConfig(level=logging.INFO)
logging.debug(f"Entered function {inspect.stack[0][3]}"))
Recommended
import logging
import inspect
logging.basicConfig(level=logging.INFO)
logging.debug("Entered function %s", inspect.stack[0][3])