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

Cameron Simpson cs at cskk.id.au
Thu Jan 21 19:51:06 EST 2021


On 21Jan2021 15:37, Dan Stromberg <drsalists at gmail.com> wrote:
>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.

Is logger-from_flask and logger_in_vmware_exporters_support the same 
using "is"? Have you printed them? Usually loggers have names indicating 
their position in a logger tree - print should show this.

Loggers have handlers to deal with log requests, and it is the handlers 
which have the formats attached. Can you see the handlers for the root 
logger (which looks to be what vmware_exporters_support uses) and for 
the one from flask?

It doesn't seem documents, but dir() on a Logger shows a .handlers 
attribute which is a list of Handlers.

>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.

Can you pass the logger you get from create_logger(app) to the 
vmware_exporters_support setup function? That way you could tell it to 
use your preferred logger.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list