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: StructlogPlugin loggers do not adhere to set log level #3424

Open
1 of 4 tasks
JacobCoffee opened this issue Apr 23, 2024 · 2 comments
Open
1 of 4 tasks

Bug: StructlogPlugin loggers do not adhere to set log level #3424

JacobCoffee opened this issue Apr 23, 2024 · 2 comments
Labels
Bug 🐛 This is something that is not working as expected Logging This is related to our logging

Comments

@JacobCoffee
Copy link
Member

JacobCoffee commented Apr 23, 2024

Description

The expectation is that any logging done is derivative of the root logger if not otherwise specified.

Perhaps I've done this wrong - but I don't want to see debug level logs when I have set the level to INFO/20 or above.

URL to code causing the issue

No response

MCVE

import logging

import structlog
from litestar import Litestar, get
from litestar.plugins.structlog import StructlogPlugin
from litestar.logging import LoggingConfig, StructLoggingConfig
from litestar.middleware.logging import LoggingMiddlewareConfig
from litestar.plugins.structlog import StructlogConfig

logging_config = StructlogConfig(
    structlog_logging_config=StructLoggingConfig(
        log_exceptions="always",
        traceback_line_limit=4,
        standard_lib_logging_config=LoggingConfig(
            root={"level": logging.getLevelName(20), "handlers": ["queue_listener"]},
            loggers={
                "uvicorn.access": {
                    "propagate": False,
                    "level": 30,
                    "handlers": ["queue_listener"],
                },
                "uvicorn.error": {
                    "propagate": False,
                    "level": 20,
                    "handlers": ["queue_listener"],
                },
            },
        ),
    ),
    middleware_logging_config=LoggingMiddlewareConfig(
        request_log_fields=["method", "path", "path_params", "query"],
        response_log_fields=["status_code"],
    ),
)

logger = structlog.get_logger()

@get("/")
async def foo() -> None:
    logger.debug("you shouldnt see this")
    pass


structlog = StructlogPlugin(config=logging_config)
app = Litestar(route_handlers=[foo], plugins=[structlog])

Steps to reproduce

1. Create and run app: `litestar --app asgi:app run`
2. `curl 0.0.0.0:8000`
3. See error

2024-04-23T22:43:18.589449Z [debug    ] you shouldnt see this         
INFO:     127.0.0.1:58488 - "GET / HTTP/1.1" 200 OK

Screenshots

No response

Logs

✗ litestar --app asgi:app run
Using Litestar app from env: 'asgi:app'
Starting server process ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
┌──────────────────────────────┬──────────────────────┐
│ Litestar version             │ 2.8.2                │
│ Debug mode                   │ Disabled             │
│ Python Debugger on exception │ Disabled             │
│ CORS                         │ Disabled             │
│ CSRF                         │ Disabled             │
│ OpenAPI                      │ Enabled path=/schema │
│ Compression                  │ Disabled             │
│ Middlewares                  │ LoggingMiddleware    │
└──────────────────────────────┴──────────────────────┘
2024-04-23T22:43:18.589367Z [info     ] HTTP Request                   method=GET path=/ path_params={} query={}
2024-04-23T22:43:18.589449Z [debug    ] you shouldnt see this         
INFO:     127.0.0.1:58488 - "GET / HTTP/1.1" 200 OK
2024-04-23T22:43:18.589767Z [info     ] HTTP Response                  status_code=200

Litestar Version

main

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@JacobCoffee JacobCoffee added Bug 🐛 This is something that is not working as expected Logging This is related to our logging labels Apr 23, 2024
@JacobCoffee
Copy link
Member Author

JacobCoffee commented Apr 24, 2024

If i blanketly set it works, so maybe something with set_level?

import logging
import structlog

structlog.configure(
    wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
)

@peterschutt
Copy link
Contributor

Issue is that the logs emitted via structlog's loggers don't pass through the standard library logging. This is why they aren't filtered by the level set on the root logger object, they are getting emitted directly to stdout via structlog.

As @JacobCoffee noted above, this can be addressed by:

StructLoggingConfig(wrapper_class=structlog.make_filtering_bound_logger(logging.INFO), ...)

This gives structlog the information it needs to be able to filter the logs it emits. A positive for this approach is that it can be quite a bit more efficient. Some downsides are that the logs are going through a different processor chain than the standard library ones (our default chains are different for either case), and logs aren't using the queue listener/handler configuration (is that an issue?).

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 Logging This is related to our logging
Projects
Status: Triage
Development

No branches or pull requests

2 participants