Python flask logging: two different formats, but want one format

Dan Stromberg drsalists at gmail.com
Thu Jan 21 18:37:16 EST 2021


I am working on a REST API using Flask.

The API is currently divided across two Python 3.6 modules: update.py and
vmware_exporters_support.py.

update.py logs the way I want. vmware_exporters_support.py does not log the
way I want. I want vmware_exporters_support.py to use update.py's logging
format without logging things twice.

In update.py, the logging is set up with:
    from flask.logging import create_logger
    app = Flask('collector_api')
    logger = create_logger(app)
    import vmware_exporters_support

And create_logger, which is part of Flask, is at
https://github.com/pallets/flask/blob/1.1.x/src/flask/logging.py

Then in vmware_exporters_support.py I'm setting up logging with:
    logger = logging.getLogger()

It seems like this should just get the root logger from update.py, but I'm
not sure it does really considering how differently it's acting.

An illustrative log snippet looks like:

    [2021-01-21 12:12:29,810] INFO in update: Writing container yaml
/data/vmware-exporter/vmware_exporter_1
    2021-01-21.12:12:29  INFO     Writing container yaml
/data/vmware-exporter/vmware_exporter_1

The [2021-01-21 12:12:29,810] (with the square brackets) is coming from
update.py, and the 2021-01-21.12:12:29 (without the square brackets) is
coming from vmware_exporters_support.py

What do I need to do to get vmware_exporters_support.py to use the same
logging format as update.py?

BTW, update.py is the __main__, not vmware_exporters_support.py.

And I'm using Flask 1.1.2.

Thanks in advance!


More information about the Python-list mailing list