Reputation: 201
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
Upvotes: 20
Views: 21896
Reputation: 2154
I ran into this same issue, using both Python 3.9 and 3.10 (despite the discussion noted in @dragon2fly's answer). I wasn't using asyncio directly, but I was using a Rich logging handler which I suspect is using asynio in the background. Calls to logging.shutdown()
did not fix the issue as suggested in https://bugs.python.org/issue42203.
The only thing that worked is right before my script terminates is to call
logger.handlers.clear()
which effectively unregisters the Rich handler for my logger object logger
.
Upvotes: 0
Reputation: 2419
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.
Upvotes: 23