简单的应用程序日志记录/调试与nginx,uwsgi,烧瓶? [英] Easy application logging/debugging with nginx, uwsgi, flask?

查看:204
本文介绍了简单的应用程序日志记录/调试与nginx,uwsgi,烧瓶?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不打算打开危险的调试控制台,但我的应用程序正在得到一个500错误,似乎没有写任何输出,我深入调查。

我看到这个交流在邮件列表,这导致我在这个页面上记录错误。几个问题:

(1)下面的文件应该放在哪个文件中?

  ADMINS = ['yourname@example.com'] 
如果不是app.debug:
导入从logging.handlers记录
导入SMTPHandler
mail_handler = SMTPHandler('127.0.0.1',
'server-error@example.com',
ADMINS,'YourApplication失败')
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)



假设越来越大的文件模式为更大的应用程序? __初始化__。PY config.py run.py



(2)我被这里的选项所淹没,不知道应该选哪个使用。我应该打开哪些记录器,使用什么设置来复制本地python服务器当我运行run.py时得到stdout?我发现默认的本地输出流非常有用,比页面中的交互式调试器更有用。有没有人有一个模式,他们可以分享设置复制这与Nginx的部署,输出到日志的东西吗?



(3)有什么我需要改变,不是在烧瓶级别,而是在nginx中,在我的 / etc / nginx / sites-available / appname 文件中说,启用日志记录?



UPDATE



具体来说,我正在查找python在本地运行时得到的信息,例如为什么某个程序包不工作,或者我的语法错误可能在哪里,或者哪个变量不存在:

  $ python run.py 
Traceback(最近的最后一次调用):
在< module>文件中的run.py,第1行
from myappname import app
在< module>文件中的/home/me/myappname/myappname/__init__.py,第27行。
file_handler.setLevel(logging.debug)
文件/usr/lib/python2.7/logging/__init__.py,第710行,在setLevel
self.level = _checkLevel(level )
文件/usr/lib/python2.7/logging/__init__.py,第190行,在_checkLevel
中引发TypeError(级别不是整数或有效字符串:%r%级别)

当我在服务器上运行烧瓶时,我从来没有看到这个。我只是在浏览器中得到一个uWSGI错误,并不知道哪个代码是有问题的。我只是想像上面这样写入到一个文件。

我还注意到,设置下面的日志记录没有真正写入太多的文件,即使当我把日志的方式改成DEBUG级别:

 从日志导入FileHandler 
file_handler = FileHandler('mylog.log ')
file_handler.setLevel(logging.DEBUG)
app.logger.addHandler(file_handler)

mylog.log 是空的,即使我的应用程序出错了。



我也会在 __ init __。py。



<$ p $中添加我试图设置debug = True的方法p> app = Flask(__ name__)
app.debug = True
app.config ['DEBUG'] = True
from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app,True)
app.config.from_object('config')
app.config.update(DEBUG = True)
app.config [ DEBUG'] = True
if __name__ =='__m ain__':
app.run(debug = True)

在我的config.py文件,我有...

  debug = True 
Debug = True
DEBUG = True

然而,没有调试发生,没有记录或调试,这是很难追查。错误只是终止应用程序与无用的浏览器消息:

  uWSGI错误
找不到Python应用程序


解决方案

Set config ['PROPAGATE_EXCEPTIONS'] True 在生产环境中运行应用程序时,要将回溯记录到日志文件中。 (我还没有尝试使用SMTP处理程序,但是..)

I'm not looking to turn on the dangerous debugging console, but my application is getting a 500 error and doesn't seem to be writing any output for me to investigate more deeply.

I saw this exchange on the mailing list, which led me to this page on logging errors.

However, I still find this very confusing and have a couple of questions:

(1) In which file should the stuff below go?

ADMINS = ['yourname@example.com']
if not app.debug:
    import logging
    from logging.handlers import SMTPHandler
    mail_handler = SMTPHandler('127.0.0.1',
                               'server-error@example.com',
                               ADMINS, 'YourApplication Failed')
    mail_handler.setLevel(logging.ERROR)
    app.logger.addHandler(mail_handler)

...assuming the "getting bigger" file pattern for larger applications? __init__.py? config.py? run.py?

(2) I am overwhelmed by options there, and can't tell which I should use. Which loggers should I turn on, with what settings, to replicate the local python server debug I get to stdout when I run run.py? I find that default, local output stream very useful, more so than the interactive debugger in the page. Does anyone have a pattern they could share on setting up something replicating this with an nginx deployment, outputting to a log?

(3) Is there anything I need to change, not at the flask level, but in nginx, say in my /etc/nginx/sites-available/appname file, to enable logging?

UPDATE

Specifically, I'm looking for information like I get when python runs locally as to why, say, a package isn't working, or where my syntax error might be, or what variable doesn't exist:

$ python run.py 
Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from myappname import app
  File "/home/me/myappname/myappname/__init__.py", line 27, in <module>
    file_handler.setLevel(logging.debug)
  File "/usr/lib/python2.7/logging/__init__.py", line 710, in setLevel
    self.level = _checkLevel(level)
  File "/usr/lib/python2.7/logging/__init__.py", line 190, in _checkLevel
    raise TypeError("Level not an integer or a valid string: %r" % level)

When I run flask on a server, I never see this. I just get a uWSGI error in the browser, and have no idea which code was problematic. I would just like something like the above to be written to a file.

I notice also that setting the following logging didn't really write much to file, even when I turn the log way up to the DEBUG level:

from logging import FileHandler
file_handler = FileHandler('mylog.log')
file_handler.setLevel(logging.DEBUG)
app.logger.addHandler(file_handler)

mylog.log is blank, even when my application errors out.

I'll also add that I've tried to set debug = True in the following ways, in __init__.py:

app = Flask(__name__)
app.debug = True
app.config['DEBUG'] = True
from werkzeug.debug import DebuggedApplication
app.wsgi_app = DebuggedApplication(app.wsgi_app, True)
app.config.from_object('config')
app.config.update(DEBUG=True)
app.config['DEBUG'] = True
if __name__ == '__main__':
    app.run(debug=True)

While in my config.py file, I have...

debug = True
Debug = True
DEBUG = True

Yet, no debugging happens, and without logging or debugging, this is rather hard to track down. Errors simply terminate the application with the un-useful browser message:

uWSGI Error
Python application not found

解决方案

Set config['PROPAGATE_EXCEPTIONS'] to True when running app in production and you want tracebacks to be logged into log files. (I haven't tried with SMTP handler, though..)

这篇关于简单的应用程序日志记录/调试与nginx,uwsgi,烧瓶?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆