Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在使用sanic框架的项目里导入aiotieba会导致sanic的logger模块失效 #100

Closed
adk23333 opened this issue Feb 8, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@adk23333
Copy link

adk23333 commented Feb 8, 2023

测试代码如下

from sanic import Sanic
from sanic.response import text
import aiotieba

app = Sanic("MyHelloWorldApp")

@app.get("/")
async def hello_world(request):
    return text("Hello, world.")

if __name__ == '__main__':
    app.run(host="127.0.0.1", port=8080, debug=True)

技术力低,找不到冲突的地方,来这问问😥

@n0099
Copy link

n0099 commented Feb 8, 2023

possible related #73

@lumina37 lumina37 added the bug Something isn't working label Feb 8, 2023
@lumina37
Copy link
Owner

lumina37 commented Feb 9, 2023

If someone use logging.logProcesses = False to disable the calling of os.getpid() while logging, the default formatter of Sanic will lead to the exception as shown below.

See logging.logProcesses

  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 445, in _format
    return self._fmt % values
           ~~~~~~~~~~^~~~~~~~
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 449, in format
    return self._format(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 659, in formatMessage
    return self._style.format(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 690, in format
    s = self.formatMessage(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 953, in format
    return fmt.format(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 1110, in emit
    msg = self.format(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 978, in handle
    self.emit(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 1706, in callHandlers
    hdlr.handle(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 1644, in handle
    self.callHandlers(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 1634, in _log
    self.handle(record)
  File "C:\Program Files\Python\Python311\Lib\logging\__init__.py", line 1489, in info
    self._log(INFO, msg, args, **kwargs)
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\application\motd.py", line 113, in display
    out(indent("\n".join(lines), "  "))
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\application\motd.py", line 39, in output
    motd_class(logo, serve_location, data, extra).display()
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\mixins\startup.py", line 579, in motd
    MOTD.output(logo, serve_location, display, extra)
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\mixins\startup.py", line 533, in _helper
    self.motd(server_settings=server_settings)
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\mixins\startup.py", line 327, in prepare
    server_settings = self._helper(
  File "C:\Program Files\Python\Python311\Lib\site-packages\sanic\mixins\startup.py", line 176, in run
    self.prepare(
...
TypeError: %d format: a real number is required, not NoneType

Without os.getpid(), the LogRecord will only be generated with a value dict like values = {'process': None, ...}. Then, exception raises when the formatter tries to translate values = {'process': None, ...} into "[%(process)d]".

You can manually switch logging.logProcesses to True as a temporary solution. And I will remove the logging.logProcesses hook in the next version 3.1.7 since os.getpid() is not that costly.

lumina37 added a commit that referenced this issue Feb 9, 2023
@lumina37
Copy link
Owner

lumina37 commented Feb 9, 2023

临时解决方案,在import aiotieba后面加一条logging.logProcesses=True
e137352 已经修复了这个问题

@lumina37
Copy link
Owner

lumina37 commented Feb 9, 2023

sanic-org/sanic#2677

@adk23333
Copy link
Author

adk23333 commented Feb 9, 2023

临时解决方案,在后面加一条 e137352 已经修复了这个问题import aiotieba``logging.logProcesses=True

thx, this is very useful.

@lumina37 lumina37 closed this as completed Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants