简体   繁体   中英

How to change Flask logging/debug screen output format?

I need to change default Flask/Werkzeug debugging output.

I've tried this:

app = Flask(__name__)
app.config["TESTING"] = True
...
log = logging.getLogger("werkzeug")
log.disabled = True

And that's working. Output is fully supressed, no more lines like:

111.222.81.83 - - [26/Mar/2019 13:31:04] "POST /catchdata HTTP/1.1" 200 -
112.122.81.83 - - [26/Mar/2019 13:31:07] "POST /catchdata HTTP/1.1" 200 -

Which is good!

I, then, included this line of code:

@app.route("/catchdata", methods=["POST"])
def catchdata():
    app.logger.info("In Catch")
    ...

and this is what I've got:

--------------------------------------------------------------------------------
INFO in __init__ [/srv/www/wsgi/Flask/service_py3/app/srv/__init__.py:438]:
In Catch
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
INFO in __init__ [/srv/www/wsgi/Flask/service_py3/app/srv/__init__.py:438]:
In Catch
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
INFO in __init__ [/srv/www/wsgi/Flask/service_py3/app/srv/__init__.py:438]:
In Catch
--------------------------------------------------------------------------------

How do I change default formatting to get only:

In Catch
In Catch
In Catch

I don't need logging to a file. Screen only. Thank you!

Flask uses standard Python logging objects with a default configuration consisting of a StreamHandler with a simple Formatter.

If a custom logging setup is required, the Flask logging documentation references an example using Python's logging.config.dictConfig and recommends to do the custom setup as soon as possible in the program:

When you want to configure logging for your project, you should do it as soon as possible when the program starts. If app.logger is accessed before logging is configured, it will add a default handler. If possible, configure logging before creating the application object .

If you just want to change the output format of the default handler, you could simply set a new Formatter, initialized with the desired format to it:

import logging
from flask import Flask, logging as flog

app = Flask(__name__)

@app.route("/")
def random_name():
    app.logger.info("default flask logging format")
    flog.default_handler.setFormatter(logging.Formatter("%(message)s"))
    app.logger.info("message-only custom format")
    return "random value"

which yields the following output

[2019-03-26 14:42:06,511] INFO in main: default flask logging format
message-only custom format

Python 3 logging docs

Edit
Turns out, that Flask's logging setup was significantly changed with this commit that seems to have ended up in the 1.0 release, introducing the default_handler .
With older versions you can fall back to the following solution:

app.logger.handlers[0].setFormatter(logging.Formatter("%(message)s"))

I'd like to point out, that this approach uses the undocumented handlers attribute on native Logger objects. While it is not defined as what is to be considered private, I'd still be wary to use it in production code.
This solution also relies on the StreamHandler to be the first one added to the logger.

In any case, a clean, dedicated logging setup, as outlined in the Flask docs I linked, should be the preferred method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM