File logger fails when used with a Gunicorn/Pyramid .ini file.

142 views
Skip to first unread message

jens.t...@gmail.com

unread,
Sep 23, 2018, 9:53:55 PM9/23/18
to sqlalchemy-alembic
Hello,

In my project.ini file I have configured logging to use a file logger as follows:

[loggers]
keys = root, …, alembic

[handlers]
keys = console, file

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console
qualname = 

[logger_alembic]
level = INFO
handlers = 
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[handler_file]
class = FileHandler
args = ("%(here)s/project.log", "a")
level = INFO
formatter = generic

Using this .ini file within my project works fine: gunicorn reads the config and expands its values. In particular, it will add the "here" variable when expanding the "args" value in gunicorn/glogging.py (source)

defaults = CONFIG_DEFAULTS.copy()
defaults['__file__'] = cfg.logconfig
defaults['here'] = os.path.dirname(cfg.logconfig)
fileConfig(cfg.logconfig, defaults=defaults, disable_existing_loggers=False)

Alembic, however, instantiates a plain fileConfig without passing extra defaults. And that causes the following failure:

configparser.InterpolationMissingOptionError: Bad value substitution: option 'args' in section 'handler_file' contains an interpolation key 'here' which is not a valid option name. Raw value: '("%(here)s/project.log", "a)'

So now I could just hardcode a path in the .ini file and that would work. But the "here" is actually quite handy and so I was wondering if a PR would be acceptable? Or is there another suggested solution for this?

Thanks!
Jens

Mike Bayer

unread,
Sep 24, 2018, 9:21:42 AM9/24/18
to sqlalchem...@googlegroups.com
Looking at the source code, we already have "here":

if self.config_file_name:
here = os.path.abspath(os.path.dirname(self.config_file_name))
else:
here = ""
self.config_args['here'] = here
file_config = SafeConfigParser(self.config_args)
if self.config_file_name:
file_config.read([self.config_file_name])
else:
file_config.add_section(self.config_ini_section)
return file_config

so...how are you getting this .ini file over to alembic?





>
> Thanks!
> Jens
>
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

jens.t...@gmail.com

unread,
Sep 25, 2018, 7:49:23 PM9/25/18
to sqlalchemy-alembic
Huh…  I use the -c option:

./bin/alembic -c proj-localhost.ini upgrade head

Jens

Mike Bayer

unread,
Sep 25, 2018, 8:06:31 PM9/25/18
to sqlalchem...@googlegroups.com
oh, you know that's in the logging. logging.fileConfig() is used for
that and it's actually in your env.py. Just add "defaults" to that
line in your env.py.

https://docs.python.org/2/library/logging.config.html#logging.config.fileConfig

jens.t...@gmail.com

unread,
Sep 26, 2018, 4:43:32 AM9/26/18
to sqlalchemy-alembic
I haven’t used env.py before, you’re talking about this: https://pypi.org/project/env.py/ , correct?

Mike Bayer

unread,
Sep 26, 2018, 9:30:42 AM9/26/18
to sqlalchem...@googlegroups.com
On Wed, Sep 26, 2018 at 4:43 AM <jens.t...@gmail.com> wrote:
>
> I haven’t used env.py before, you’re talking about this: https://pypi.org/project/env.py/ , correct?

env.py is an integral part of your Alembic project space and you are
using it. Please see the tutorial at
https://alembic.zzzcomputing.com/en/latest/tutorial.html#the-migration-environment
. This should likely also be a feature so
https://bitbucket.org/zzzeek/alembic/issues/509/add-support-for-the-here-s-token-when
has been added.

>
> On Wednesday, September 26, 2018 at 10:06:31 AM UTC+10, Mike Bayer wrote:
>>
>> oh, you know that's in the logging. logging.fileConfig() is used for
>> that and it's actually in your env.py. Just add "defaults" to that
>> line in your env.py.
>>
>> https://docs.python.org/2/library/logging.config.html#logging.config.fileConfig
>>
>>

jens.t...@gmail.com

unread,
Oct 7, 2018, 4:46:17 PM10/7/18
to sqlalchemy-alembic
Great, thank you Mike!
Reply all
Reply to author
Forward
0 new messages