【问题标题】:pyramid subprocess call STDERR and STDOUT trapping logging金字塔子进程调用 STDERR 和 STDOUT 捕获日志
【发布时间】:2018-08-05 09:42:02
【问题描述】:

我正在使用 views.py 子进程操作系统调用,该调用失败并返回非零代码,我需要捕获问题所在。我还想记录所有内容并努力按照说明(https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html?awesome > 高级配置 > 见下文)来完成这项工作。

TIA

金字塔结构是最近建成的

virtualenv --no-site-packages myproj
cd /home/user/myproj/
source bin/activate
pip3.6 install pyramid pyramid-debugtoolbar pyramid-jinja2 waitress cookiecutter
pcreate -s starter mywsgi
cd mywsgi
python3.6 setup.py develop

在运行时 保存 production.ini

views.py 调用

import os
import subprocess
import json
from pyramid.view import view_config

@view_config(route_name='overlay_event', renderer='json')
def event_view(request):
    return {'new_overlay': subprocess.check_output(['/usr/bin/foo', '/path/thing.script',json.dumps(request.json_body)])

production.ini 是样板文件,如下所示。我的版本低于此。 对于 subprocess.check_output 的非零返回代码失败,我确实获得了控制台日志记录,但没有从失败的脚本调用发送到 STDOUT 和 STDERR 的内容。我的脚本运行良好,所以它一定是某个环境、路径、JSON、ARGV 或其他问题。但我也只想记录。

当我根据上述日志记录指令编辑以下内容时,我收到有关错误处理程序的错误。当我逐渐将我的编辑回滚到 production.ini 时,就会发生这种情况。也许这缺少一些额外的包含或? view.py 也需要改变吗?

生物板

###
# app configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/environment.html
###

[app:main]
use = egg:myproj

pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en

###
# wsgi server configuration
###

[server:main]
use = egg:waitress#main
listen = *:6543

###
# logging configuration
# https://docs.pylonsproject.org/projects/pyramid/en/1.9-branch/narr/logging.html
###

[loggers]
keys = root, myproj

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console

[logger_myproj]
level = WARN
handlers =
qualname = myproj

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

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s

我对 production.ini 文件的编辑

[loggers]
keys = root, filelog

[handlers]
keys = console, myproj, filelog

[formatters]
keys = generic

[logger_root]
level = INFO
handlers = console, filelog

[logger_filelog]
class = FileHandler
args = ('%(here)s/myproj.log','a')
level = INFO
formatter = generic

[logger_myproj]
level = WARN
qualname = myproj

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

[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s

【问题讨论】:

    标签: logging subprocess pyramid wsgi cookiecutter


    【解决方案1】:

    您应该使用run 而不是check_output

    try:
        result = subprocess.run(
            ['/usr/bin/foo', '/path/thing.script', json.dumps(request.json_body)],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        )
    except subprocess.CalledProcessError as ex:
        output = ex.stdout.decode('utf-8')
        log.error('Call to /usr/bin/foo failed: %s', output)
    

    另外,我会小心将request.json_body 直接传递到命令行,以防可能的注入攻击。

    【讨论】:

    • 谢谢你——事实上它是 python 2.7,所以它没有 .run。我有一个运行 link 的反向移植的镜头,但 python 不喜欢 def 参数 input=None 和 check=False 说它们是语法错误。
    • 啊,好的。抱歉,对于 Python 2,我无法为您提供帮助,我已经用它做了一段时间了。祝你好运!您可能可以使用 popen 来实现,但我不能确定这是否可行以及它是否是最佳解决方案。
    • 好的,我看到了问题。建议的 2.7 反向移植是 3+ 语言,呵呵。让我试着让它通过更多的黑客攻击来工作。
    • 好吧,我就是无法让它工作。我总是返回 CalledProcessError: command= .... 返回状态,没有 stderr 或 stdout。看来我需要覆盖 CalledProcessError
    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2023-04-06
    • 2013-09-02
    • 2011-11-01
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    相关资源
    最近更新 更多