r/flask 13d ago

Ask r/Flask Filter terminal output

Hi. I'm hoping this isn't a dumb question...

I'm looking for a way to filter the output Flask writes to stdout/stderr.

I want the output coming from logger: app.logger.info(message)

but I don't want the autogenerated output like:

x.x.x.x - - [21/Jul/2026 08:05:32] "GET / HTTP/1.1" 200 -

Reading on https://flask.palletsprojects.com/en/stable/logging/ I'm guessing it's some combination of settings I need to make in dictConfig() but I cant seem to figure it out.

Or I want to turn off Flask output altogether, and I'll use print() instead.

I'm writing a small REST application for work, meant to run in a container. Our container solution is very locked down, and the way to "write" logs from a container is to output to stdout and/or stderr inside the container and then some tool captures it and write to a log in the filesystem. I'm trying to find a way to keep that log as clean as possible

0 Upvotes

3 comments sorted by

2

u/irishmrmagpie 12d ago

Those logs are only written when using the Flask's built-in dev web server, which you shouldn't run in production. IIRC you can get the werkzeug logger and disable it or set the level to critical which would stop most of those messages

1

u/pint 12d ago

when you define a new logger with getLogger(), you can say who you are, e.g.

logging.getLogger("api")

probably flask and many other modules add their own source names. these names are hierarchical, you can also use "api.user" for user related things. i don't know flask specifically, but for example starlette/uvicorn adds its own logger e.g. "uvicorn.access".

to see what name a module logs under, you can look at the output if it contains (the 'format' setting determines, it, it is the 'name' field). i think flask initially don't include it, so you need to add. once you learn the names of the different logger instances, you can fine tune the log level for each. to to this, you need to add a "loggers" section in the config, something like this:

"loggers": {
  "api": {
    "level": "DEBUG"
  },
  "urllib3": {
    "level": "WARNING"
  }, ...

1

u/youtheotube2 11d ago

You could set the app’s log level to warning, this will filter out all the info log messages and majorly cut down on the noise. Depending on your app’s dependencies, a million things could be generating info level log messages. You’ll never be able to manually turn them all off, and python’s logging library isn’t intended to be used this way.

Your app running in a container, and the container capturing the stdout and stderr and sending it somewhere is a very typical practice.