相关疑难解决方法(0)

使用@property与getter和setter

这是一个纯Python特定的设计问题:

class MyClass(object):
    ...
    def get_my_attr(self):
        ...

    def set_my_attr(self, value):
        ...
Run Code Online (Sandbox Code Playgroud)

class MyClass(object):
    ...        
    @property
    def my_attr(self):
        ...

    @my_attr.setter
    def my_attr(self, value):
        ...
Run Code Online (Sandbox Code Playgroud)

Python让我们可以这样做.如果你要设计一个Python程序,你会使用哪种方法?为什么?

python properties getter-setter

711
推荐指数
13
解决办法
41万
查看次数

在gunicorn中运行时,Flask app logger无法正常工作

我正在尝试从日志文件中的一个非常简单的烧瓶应用程序中保存应用程序日志消息.当我使用嵌入式Flask服务器运行应用程序时,这完美无缺,但在gUnicorn中运行时根本无法正常工作,基本上,没有应用程序输出重定向日志文件(在我的Flask应用程序中指定的那个)或者运行gunicorn时的STDOUT.

那就是说,这是我的Flask应用程序:

@app.route('/')
def index():
    app.logger.debug('Into /!!!!')
    print 'Will this print?'
    return 'Flask is running!'


if __name__ == '__main__':
    #Setup the logger
    file_handler = FileHandler('test.log')
    handler = logging.StreamHandler()
    file_handler.setLevel(logging.DEBUG)
    handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(Formatter(
    '%(asctime)s %(levelname)s: %(message)s '
    '[in %(pathname)s:%(lineno)d]'))
    handler.setFormatter(Formatter(
    '%(asctime)s %(levelname)s: %(message)s '
    '[in %(pathname)s:%(lineno)d]'))
    app.logger.addHandler(handler)
    app.logger.addHandler(file_handler)
    app.run(debug=True)
Run Code Online (Sandbox Code Playgroud)

现在如果我启动应用程序:

python app.py
Run Code Online (Sandbox Code Playgroud)

我得到了预期的输出:

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat

--------------------------------------------------------------------------------
DEBUG in app [app.py:23]:
Into /!!!!
--------------------------------------------------------------------------------
2015-03-11 09:36:18,375 DEBUG: Into /!!!! [in app.py:23]
Will this …
Run Code Online (Sandbox Code Playgroud)

python logging flask gunicorn

20
推荐指数
3
解决办法
1万
查看次数

如何从烧瓶的记录中删除" - - "?

当我运行烧瓶0.9时,我得到了日志记录:

127.0.0.1 - - [30/Mar/2016 10:08:38] "GET / HTTP/1.1" 200 -
Run Code Online (Sandbox Code Playgroud)
  1. 我应该怎么做,除去- -之间127.0.0.1[30/Mar/2006 10:08:38]

  2. 如果我想200从日志消息中删除响应代码,我该怎么办?

任何建议将被认真考虑!

正如@alecxe建议的那样,我列出了我的代码段相对日志记录:

logging.basicConfig(filename='werkzeug.log', level=logging.INFO)
logger = logging.getLogger('werkzeug')
logger.setLevel(logging.INFO)
Run Code Online (Sandbox Code Playgroud)

python werkzeug flask flask-sqlalchemy

8
推荐指数
1
解决办法
1612
查看次数