NameError: name 'open' is not defined When trying to log to files
Asked Answered
M

2

20

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

Mosesmosey answered 4/11, 2020 at 11:24 Comment(0)
M
23

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.

Metathesize answered 5/11, 2020 at 1:46 Comment(2)
Well I tried doing that by calling logging.shutdown() in atexit but it didnt work. Other than that nothing I can do?Mosesmosey
@Mosesmosey logging.shutdown() has already been registered for atexit ever since. You don’t need to call it by yourself. Instead, try to close the event loop, the connection, etc that your program has.Metathesize
H
0

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.

Huldahuldah answered 27/7, 2023 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.