【问题标题】:How to escape percent char in Python Paste deploy configuration file?如何在 Python Paste 部署配置文件中转义百分比字符?
【发布时间】:2015-10-19 14:26:49
【问题描述】:

我已经使用 Python Paste Deploy 脚本来部署 Flask+Gunicorn 项目。但是,我不能在 (h)s %(l)s %(u)s %(t)s "%(r)s" %(s 的部署脚本中编写 access_log_format )s %(b)s "%(f)"。因为我们的 %(h)s 格式在 Paste Deploy 脚本中有特殊的含义。如文档中所述:

You can use variable substitution, which will pull variables from 
the section [DEFAULT] (case sensitive!) with markers like %
(var_name)s. The special variable %(here)s is the directory
containing the configuration file;

但是我可以解决这个问题吗?

【问题讨论】:

  • 你试过用引号包裹字符串吗? (例如access_log_format = '%(h)s'
  • 您能否将access_log_format 字段移至[DEFAULT] 之外的部分?该措辞暗示变量替换仅在该配置部分中起作用。在我自己的 Pyramid 应用程序中,日志配置看起来很像 Pyramid logging configuration 中的示例
  • @IanMarcinkowski 不,我把它放在 [server:main] 部分,而不是 [DEFAULT] 部分。

标签: python python-paste


【解决方案1】:

在这种情况下,您尝试配置应用程序的两个组件 - Gunicorn 和 Flask 应用程序(使用 Paste Deploy)。 Gunicorn 负责访问日志,而 Flask / Paste Deploy 使用您现有的配置进行配置。 Paste Deploy负责自己的配置文件中神奇的变量替换,与Gunicorn访问日志配置冲突。

您可以为 Gunicorn 提供单独的配置文件,其中将包含您的访问日志格式。例如:

gunicorn -c gunicorn.conf --paste demo.conf

gunicorn.conf

accesslog = access.log
access_log_format = %(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"

【讨论】:

  • 不,它不能在粘贴部署中工作。像这样的东西将是错误消息。错误:文件 demo.conf 中的错误:错误值替换:部分:[server:main] 选项:access_log_format 键:h rawval:“%(h)s”
  • 有没有想过在命令行传递访问日志格式?我没有看到任何兼容的方式通过 Paster 格式的配置文件向 Gunicorn 提供访问日志格式。您是否使用gunicornpserve 从命令行运行应用程序?
  • 你可以创建2个配置文件;一个用于 Gunicorn,一个用于您的 Flask 应用程序。您尝试配置的访问日志是 Gunicorn 的一部分,而不是 Flask 应用程序。试试这样的:gunicorn -c gunicorn.conf --paste application.conf [...]
  • @IanMarchinkowski,这对我来说似乎很好。我会试一试。我终于用命令行中指定的访问日志格式参数进行了尝试。
  • 太棒了!我会更新我的答案。如果您还有任何问题,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2012-05-27
  • 2020-11-02
  • 2019-10-04
  • 2017-04-30
  • 2017-03-05
  • 1970-01-01
相关资源
最近更新 更多