17
votes

So I chose to implement logging in my discord.py bot which is fine and dandy and works alright. but once I add logging to files, be it through using a filehandler

handler = logging.FileHandler(
    filename="../logs/bot.log",
    mode="a")
formatter = logging.Formatter("%(asctime)s %(name)-30s %(levelname)-8s %(message)s")
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)
logging.getLogger().addHandler(handler)

Or through the basicConfig

logging.basicConfig(filename="../logs/bot.log", filemode="a", format="%(asctime)s %(name)-30s %(levelname)-8s %(message)s", level=logging.DEBUG)

I always get this Traceback on exit

Exception ignored in: <function ClientSession.__del__ at 0x7fe1330b9790>
Traceback (most recent call last):
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client.py", line 314, in __del__
  File "/usr/lib/python3.8/asyncio/base_events.py", line 1740, in call_exception_handler
  File "/usr/lib/python3.8/logging/__init__.py", line 1463, in error
  File "/usr/lib/python3.8/logging/__init__.py", line 1577, in _log
  File "/usr/lib/python3.8/logging/__init__.py", line 1587, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
  File "/usr/lib/python3.8/logging/__init__.py", line 950, in handle
  File "/usr/lib/python3.8/logging/__init__.py", line 1182, in emit
  File "/usr/lib/python3.8/logging/__init__.py", line 1172, in _open
NameError: name 'open' is not defined
Exception ignored in: <function ClientResponse.__del__ at 0x7fe13300b430>
Traceback (most recent call last):
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client_reqrep.py", line 757, in __del__
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/connector.py", line 177, in release
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/connector.py", line 629, in _release
  File "/home/lukas/PycharmProjects/coconutbot/venv/lib/python3.8/site-packages/aiohttp/client_proto.py", line 62, in close
  File "/usr/lib/python3.8/asyncio/selector_events.py", line 690, in close
  File "/usr/lib/python3.8/asyncio/base_events.py", line 719, in call_soon
  File "/usr/lib/python3.8/asyncio/base_events.py", line 508, in _check_closed
RuntimeError: Event loop is closed

I'm utterly stumped as to what did wrong as the Traceback doesnt include any of my own files. I'm suspecting I might have to do some manual cleanup but even if I do logging.shutdown() I get the same Traceback

1

1 Answers

13
votes

That is because something (asyncio in this case) tries to log during the shutting down phase of the interpreter.

This is a known problem. A workaround for the logging module has been implemented in python 3.10

It happens because the name open has already been deleted by the garbage collector before the file handler got the time to use it.

You can avoid this problem by trying to explicitly close/release your resources before your program exit.