Logging does not work before the server starts
Galax028 opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
My logs are not appearing before the server is started. Access logs and error logs work fine. Logging in route handlers also works fine. It is only in this state that logging does not work.
Code snippet
from sanic import Sanic
from sanic.log import logger
class MyServer(Sanic):
def __init__(self) -> None:
super().__init__(
name="MyServer",
dumps=orjson.dumps,
loads=orjson.loads,
)
logger.root.debug("Configuration loaded")
# Other code goes here...
logger.root.debug("Configured other things")
self.config.OAS = False
logger.root.debug("Configured Sanic extensions")
server = MyServer()
if __name__ == "__main__":
server.run(...)
Output when running:
$ python3 main.py
[2023-06-17 19:43:45 +0700] [46230] [DEBUG] Creating multiprocessing context using 'spawn'
[2023-06-17 19:43:45 +0700] [46230] [DEBUG] Starting a process: Sanic-Server-0-0
[2023-06-17 19:43:45 +0700] [46230] [DEBUG] Starting a process: Sanic-Reloader-0
[2023-06-17 19:43:45 +0700] [46238] [INFO] Sanic Extensions:
[2023-06-17 19:43:45 +0700] [46238] [INFO] > injection [0 dependencies; 0 constants]
[2023-06-17 19:43:45 +0700] [46238] [INFO] > http
[2023-06-17 19:43:45 +0700] [46238] [DEBUG] Process ack: Sanic-Server-0-0 [46238]
[2023-06-17 19:43:45 +0700] [46238] [INFO] Starting worker [46238]
Expected Behavior
I expected the logs to show up on the console.
How do you run Sanic?
As a script (app.run
or Sanic.serve
)
Operating System
Arch Linux x86_64 6.3.4-arch1-1
Sanic Version
23.3.0
Additional context
No response
- Why are you using the root logger? Sanic exposes its loggers for use if you want, and changes logging defaults for PROD or DEV mode. But, it never touches the root logger. Speaking of which...
- Unless you are not changing the root logger config somewhere outside your code, it should probably be defaulted to
WARNING
. Which, I believe is Python's default for the root logger. Therefore, ... - Your code should log those lines if you change from
debug
towarning
. Or, alternatively, you can use Sanic's logger and not the root logger. Assumingdebug=True
(ordev=True
), your lines should also be logged.
I do not see how there is a bug here.