Gunicorn: dictConfig in logconfig

Created on 21 Jul 2015  路  13Comments  路  Source: benoitc/gunicorn

From logging docs:

Note: The fileConfig() API is older than the dictConfig() API and does not provide functionality to cover certain aspects of logging. For example, you cannot configure Filter objects, which provide for filtering of messages beyond simple integer levels, using fileConfig(). If you need to have instances of Filter in your logging configuration, you will need to use dictConfig(). Note that future enhancements to configuration functionality will be added to dictConfig(), so it鈥檚 worth considering transitioning to this newer API when it鈥檚 convenient to do so.

Could you suppport dictConfig in logconfig option?

Improvement FeaturLogging - Mailing List -

Most helpful comment

Please excuse my ignorance, but I am slightly confused how #1602 enables me to load a dict config via the gunicorn command line:

$ gunicorn --log-config-dict={} 'flask.main:app'
Error: Value is not a dictionary: {}

$ gunicorn --log-config-dict="{}" 'flask.main:app'
Error: Value is not a dictionary: {}

What am I missing?

All 13 comments

Can you confirm what you mean. Having a dictionary somehow in the command line with --log-config, is not what you want, correct? But a dictionary when using the logconfig setting in your gunicorn conf python file you want to set a dictionary?

My application reads config from file and part of this config is for logging. I want to pass that part to dictConfig. I'm not using command line arguments for logging config at all.

Looks like a reasonable request to me. I think we can also deprecate logconfig (or encourage to use the new setting) and add a new setting for this.

+1 . Any PR is welcome :)

Hello - I'm also interested in this feature. Is the PR above similar to what you had in mind?

I fail to see how the command line can be used. Can someone provide a proper example? And test?

@benoitc is a CLI option needed for every setting? It's not something I've addressed in the pull request above - that just works with the python configuration file.

If it was a CLI option, how would you use it? The only way I could see it working is if the value is dumped in a json or yaml file and the filename is used as the setting, but that just makes things more complicated if you are already using the python config.

Maybe the way Circus handles logging configuration could be used as a model? See circus/util.py:configure_logger().

@akaihola how is it different from the way we are doing it now? Also we should not forget that the logging backend is pluggable in Gunicorn.

I found a workaround using --config parameter: one can use dictConfig there to setup the logging.

At least seems to work with the default worker type.

@ssalonen
I found a workaround using --config parameter: one can use dictConfig there to setup the logging.

At least seems to work with the default worker type.

I figured out how to do this for dictConfig using a json config file.

In your gunicorn config.py file you put the following code.

import json
import logging.config

with open('/path/to/logging.conf') as conf_file:
    logging.config.dictConfig(json.load(conf_file))

bind = "0.0.0.0:5000"
(the rest of your gunicorn config settings)

Then start with gunicorn -c config.py app:app

This should let you configure your logging using a json file instead of the default INI format.
Or you can put the logging config as a dict directly in the config.py file.

Closable with #1602 landed, or is this staying open until a release is cut including #1602?

Please excuse my ignorance, but I am slightly confused how #1602 enables me to load a dict config via the gunicorn command line:

$ gunicorn --log-config-dict={} 'flask.main:app'
Error: Value is not a dictionary: {}

$ gunicorn --log-config-dict="{}" 'flask.main:app'
Error: Value is not a dictionary: {}

What am I missing?

Was this page helpful?
0 / 5 - 0 ratings