pyramid子进程调用STDERR和STDOUT捕获日志记录

时间:2018-02-26 00:29:58

标签: logging subprocess pyramid wsgi cookiecutter

我正在使用一个views.py子进程os调用,该调用因非零返回代码而失败,我需要捕获出错的地方。我还想记录所有内容并努力遵循说明(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

在运行时     pserve production.ini

views.py call

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也需要改变吗?

Biolerplate

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

1 个答案:

答案 0 :(得分:0)

您应该使用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直接传递给命令行。