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

Bug: litestar run --debug does not propagate to loggers correctly #1804

Open
1 of 4 tasks
JacobCoffee opened this issue Jun 9, 2023 · 10 comments
Open
1 of 4 tasks

Bug: litestar run --debug does not propagate to loggers correctly #1804

JacobCoffee opened this issue Jun 9, 2023 · 10 comments
Labels
Bug 🐛 This is something that is not working as expected CLI This is related to our CLI Help Wanted 🆘 This is good for people to work on Logging This is related to our logging

Comments

@JacobCoffee
Copy link
Member

JacobCoffee commented Jun 9, 2023

Description

The error issue from #1672 was resolved in #1742; however, the log levels should properly be set when --debug is set with the CLI.

URL to code causing the issue

MCVE

# mcve.py
from litestar import Litestar, get, Request
from litestar.logging import StructLoggingConfig


@get("/")
def hello_world(request: Request) -> dict[str, str]:
    """Keeping the tradition alive with hello world."""
    request.logger.info("info inside a request")
    request.logger.debug("debug inside a request")
    return {"hello": "world"}

app = Litestar(
    route_handlers=[hello_world],
    logging_config=StructLoggingConfig(),
)

Steps to reproduce

1. Create MCVE file, e.g. mcve.py
2. Run the file `litestar run --debug --app "mcve.py"`
3. Browse to `http://localhost:8000`
4. Observe that `request.logger.debug("debug inside a request")` is within the expected log filter level but is not logged.

Screenshots

No response

Logs

No response

Litestar Version

2.0a7

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
Fund with Polar
@JacobCoffee JacobCoffee added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage labels Jun 9, 2023
@JacobCoffee
Copy link
Member Author

JacobCoffee commented Jun 10, 2023

In testing

litestar/litestar/app.py

Lines 449 to 455 in d94717e

@debug.setter
def debug(self, value: bool) -> None:
if self.logger:
self.logger.setLevel(logging.DEBUG if value else logging.INFO)
if isinstance(self.logging_config, LoggingConfig):
self.logging_config.loggers["litestar"]["level"] = "DEBUG" if value else "INFO"
self._debug = value

@debug.setter
    def debug(self, value: bool) -> None:
        import logging
        print("Inside the debug setter")
        if self.logger:
            print("Inside the debug setter and logger is not None")
            self.logger.setLevel(logging.DEBUG if value else logging.INFO)
        if isinstance(self.logging_config, LoggingConfig):
            print("Inside the debug setter and logging_config is not None")
            # Here, we print out the root logger's level before trying to set it
            print(f"Root logger level before setting: {logging.getLogger().level}")
            logging.getLogger().setLevel(logging.DEBUG if value else logging.INFO)
            print(f"Root logger level after setting: {logging.getLogger().level}")
        self._debug = value
        print("Outside the debug setter")
        print(f"Root logger level at end of setter: {logging.getLogger().level}")
        print(self._debug)

mcve.py, testing with and without logging.getLogger, with and without StructLoggingCOnfig, etc.

from litestar import Litestar, get, Request
from litestar.logging import StructLoggingConfig

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

@get("/")
def hello_world(request: Request) -> dict[str, str]:
    """Keeping the tradition alive with hello world."""
    print("inside hello world")

    if request.logger is not None:
        print(
            f"Logger name: {request.logger.name}, Parent logger name: {request.logger.parent.name if request.logger.parent else 'None'}")
    else:
        print("request.logger is None")
    handler_levels = [handler.level for handler in request.logger.handlers]
    print(f"Logger level: {request.logger.level}, Handler levels: {handler_levels}")

    logger.debug("debug inside a request")
    logger.info("info inside a request")

    request.logger.info("info inside a request")
    request.logger.debug("debug inside a request")

    print("outside hello world")

    return {"hello": "world"}

app = Litestar(
    route_handlers=[hello_world],
    # logging_config=StructLoggingConfig(),
)
litestar run --debug 

  @get("/")
Inside the debug setter
Inside the debug setter and logging_config is not None
Root logger level before setting: 30
Root logger level after setting: 10
Outside the debug setter
Root logger level at end of setter: 10
True

Using Litestar app from app:app
Starting server process 

INFO:     Started server process [1157565]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:36263 (Press CTRL+C to quit)

inside hello world
Logger name: root, Parent logger name: None
Logger level: 20, Handler levels: [10]
outside hello world

INFO:     127.0.0.1:42922 - "GET / HTTP/1.1" 200 OK
INFO - 2023-06-10 11:24:32,-1507875053 - root - http - info inside a request

It seems like once set, something reverts the logging level back to 20

@JacobCoffee
Copy link
Member Author

JacobCoffee commented Jun 10, 2023

@get("/")
def hello_world(request: Request) -> dict[str, str]:
    """Keeping the tradition alive with hello world."""
    print(f"Root logger level at start of request: {logging.getLogger().level}")
    print("inside hello world")

    if request.logger is not None:
        print(
            f"Logger name: {request.logger.name}, Parent logger name: {request.logger.parent.name if request.logger.parent else 'None'}")
    else:
        print("request.logger is None")
    handler_levels = [handler.level for handler in request.logger.handlers]
    print(f"Logger level: {request.logger.level}, Handler levels: {handler_levels}")

    logger.debug("debug inside a request")
    logger.info("info inside a request")

    request.logger.info("info inside a request")
    request.logger.debug("debug inside a request")

    print("outside hello world")

    return {"hello": "world"}

app = Litestar(
    route_handlers=[hello_world],
    # logging_config=StructLoggingConfig(),
)


Root logger level at start of request: 10
inside hello world
Logger name: root, Parent logger name: None
Logger level: 20, Handler levels: [10]
outside hello world
INFO - 2023-06-10 11:26:24,-1507762523 - root - http - info inside a request

Different Loggers:

@get("/")
def hello_world(request: Request) -> dict[str, str]:
    """Keeping the tradition alive with hello world."""
    root_logger_id = id(logging.getLogger())
    request_logger_id = id(request.logger)
    print(f"Root logger ID: {root_logger_id}, request logger ID: {request_logger_id}")

Root logger ID: 139982122524304, request logger ID: 139982057730832
Root logger level at start of request: 10
inside hello world
Logger name: root, Parent logger name: None
Logger level: 20, Handler levels: [10]
outside hello world

@JacobCoffee JacobCoffee removed the Triage Required 🏥 This requires triage label Jun 10, 2023
@JacobCoffee JacobCoffee self-assigned this Jun 10, 2023
@cofin
Copy link
Member

cofin commented Jul 2, 2023

@JacobCoffee is this still an issue?

@JacobCoffee
Copy link
Member Author

Yes

@JacobCoffee
Copy link
Member Author

The original issue was fixed but the expected behavior of this I haven't been able to solve

@Mattwmaster58
Copy link
Contributor

Mattwmaster58 commented Aug 9, 2023

Would this be the issue that would be causing my app to send 500s opaquely? On version 2.0.0, I can repro the following issue:

@get("/")
async def perch() -> str:
    return "helo"
    
app = Litestar(
    route_handlers=[perch],
    compression_config=CompressionConfig(backend="brotli")
)

Ensure --debug is used to start the app.py, ensure that brotli is NOT installed, and you will see opaque 500s in the console because brotli is not installed. Setting debug in the Litestar obj does work, though

@JacobCoffee
Copy link
Member Author

@Mattwmaster58
Copy link
Contributor

Mattwmaster58 commented Aug 10, 2023

I manually patched from site packages (hope that's a valid way, I verified this was the code that was running with a print statements) - no dice. My issue still persists, I see │ Debug mode │ Enabled │, but getting opaque 500s. I applied the whole patch, not just the file diff linked.

@provinzkraut
Copy link
Member

I don't think this is the same issue, but would have to investigate.

@Mattwmaster58 Could you open a separate issue with an MCVE for your case?

@Mattwmaster58
Copy link
Contributor

Mattwmaster58 commented Aug 10, 2023 via email

@provinzkraut provinzkraut removed this from the 2.0 milestone Aug 25, 2023
@JacobCoffee JacobCoffee self-assigned this Sep 18, 2023
@provinzkraut provinzkraut added the Help Wanted 🆘 This is good for people to work on label Oct 13, 2023
@JacobCoffee JacobCoffee added CLI This is related to our CLI Logging This is related to our logging labels Dec 8, 2023
@github-project-automation github-project-automation bot moved this to Triage in Overview Dec 8, 2023
@JacobCoffee JacobCoffee moved this from Triage to Backlog in Overview Dec 8, 2023
@JacobCoffee JacobCoffee removed their assignment Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected CLI This is related to our CLI Help Wanted 🆘 This is good for people to work on Logging This is related to our logging
Projects
Status: Backlog
4 participants