当前位置:  开发笔记 > 编程语言 > 正文

在本地运行时抛出python-logstash错误

如何解决《在本地运行时抛出python-logstash错误》经验,为你挑选了1个好方法。

如果我导入logstash,在本地运行,则会出现以下错误

Connected to pydev debugger (build 162.1812.1)
/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/flask/exthook.py:71: ExtDeprecationWarning: Importing flask.ext.cache is deprecated, use flask_cache instead.
  .format(x=modname), ExtDeprecationWarning
Traceback (most recent call last):
  File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 1580, in 
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 964, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/vagrant/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/emailService/app.py", line 59, in 
    import logstash
  File "/home/vagrant/.envs/emailservice/lib/python3.4/site-packages/logstash/__init__.py", line 2, in 
    from event import Event
ImportError: No module named 'event'

Process finished with exit code 1

我的app.py文件看起来像这样。我通过一个无聊的会话在本地运行。如果我从if的本地分支中删除导入logstash,则应用程序可以正常启动,并且获得本地控制台日志输出。

import logging
import os
import sys

from flask import Flask
from flask_restful import Api
from flask_cache import Cache
from flask_sqlalchemy import SQLAlchemy
from opbeat.contrib.flask import Opbeat

from tasks import make_celery


app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'SUCHSECRETSWOW')
app.config.from_object(os.environ.get('APP_SETTINGS', 'config.DevelopmentConfig'))
cache = Cache(app)
db = SQLAlchemy(app)
api = Api(app)
celery = make_celery(app)

if len(app.config['OPBEAT_ORGANIZATION_ID']):
    opbeat = Opbeat(
        app,
        organization_id=app.config['OPBEAT_ORGANIZATION_ID'],
        app_id=app.config['OPBEAT_APP_ID'],
        secret_token=app.config['OPBEAT_SECRET_TOKEN'],
    )

@app.after_request
def after_request(response):
    response.headers.add('Access-Control-Allow-Origin', '*')
    response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
    response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE')
    return response


def clear_cache():
    cache.clear()


def start_resources():
    from emailService.api import HealthApi

    api.add_resource(HealthApi, '/health')


def start_tasks():
    from emailService.tasks import KickoffFetchEmailPeriodTask


if __name__ == '__main__':

    if app.config.get('DEVELOPMENT', False):
        #The reason this exists is purely because of my error.
        import logstash
        app.logger.setLevel(logging.DEBUG)
        app.logger.addHandler(logging.StreamHandler())

    else:
        import logstash
        app.logger = logging.getLogger('python-logstash-logger')
        app.logger.setLevel(logging.INFO)
        app.logger.addHandler(logstash.LogstashHandler('myhost.veryhost.suchhost', 5959, version=1))
        app.logger.addHandler(logging.StreamHandler())

    clear_cache()
    start_tasks()
    start_resources()

    app.logger.debug('Starting app')

    app.run(host='0.0.0.0', port=16600, debug=True, use_reloader=False)

所有的google搜索都会产生很大的一笔总和,无济于事。



1> Val..:

您可能正在遇到此问题,您已经安装logstash了pip 而不是python-logstash

运行此命令,此后应该可以运行:

> pip uninstall logstash
> pip install python-logstash

推荐阅读
夏晶阳--艺术
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有