使用python flask记录问题

时间:2017-09-28 06:58:27

标签: python logging flask

我正在尝试使用dictConfig配置我的烧瓶应用程序并配置sqlalchemy日志记录等。

我有一个yaml文件,如下所示

version: 1

root:
  handlers: [console]
  level: INFO


loggers:
  sqlalchemy.engine:
    level: WARN
    propagate: false
    handlers: []
  clportal:
    level: INFO
    handlers: [console]
    propagate: false

handlers:
  console:
    class: logging.StreamHandler
    formatter: standard
    level: INFO
    stream: ext://sys.stdout

formatters:
  standard:
    format: '%(asctime)s %(levelname)-8s %(name)-10s: %(message)s'

我通过执行以下操作进行记录:

def create_app():
    app = Flask('clportal')
    configure_logging()
    app.logger.info('from app.logger')
    app.config.from_pyfile('config.py')
    app.secret_key = app.config["SECRET_KEY"]
    finjector = FlaskInjector(app=app, modules=[ConfigurationModule(app)])
    app.injector = finjector
    #app.logger.setLevel(app.config['LOG_LEVEL'])
    # add stdout handler (used for docker etc)
    #stdout_handler = logging.StreamHandler(sys.stdout)
    # stdout_handler.setFormatter(logging.Formatter('%(message)s'))
    #app.logger.addHandler(stdout_handler)
    #app.logger.info('Set up correctly now?')
    return app


def configure_logging():
    logging.config.dictConfig(yaml.load(open(os.path.join(APP_PATH, 'logconfig.yml'), 'r')))

通过app.logger.info进行日志记录仅在我在create_app函数中注释掉这些内容时才有效。

我希望能够通过yaml文件配置我的所有日​​志记录。任何想法为什么这不起作用?

enter image description here

2 个答案:

答案 0 :(得分:0)

这是因为app.logger的名称并非"app.logger"。如果您尝试打印app.logger.name,您会找到它的真实姓名。您应该在logconfig.yml中使用此名称,例如:

loggers:
  sqlalchemy.engine:
    level: WARN
    propagate: false
    handlers: []
  real_name:
    level: INFO
    handlers: [console]
    propagate: false

或者您可以使用自定义记录器来替换默认记录器:

# python scirpt
logger = logging.getLogger("what_ever_you_want")
app.logger = logger

# logconfig.yml
loggers:
  sqlalchemy.engine:
    level: WARN
    propagate: false
    handlers: []
  what_ever_you_want:
    level: INFO
    handlers: [console]
    propagate: false

答案 1 :(得分:0)

我已尝试通过http://flask.pocoo.org/docs/dev/logging/作为第一步,但这对我不起作用。

相反,我发现要做以下事情。通过使用语句访问app.logger来启动默认记录器,然后使用yaml文件运行config logging,一切都会很好。如果我在创建应用程序之前通过yaml执行了配置,如帮助中所指定的那样,它将不会覆盖默认处理程序。

def create_app():
    app = Flask('clportal')
    app.logger.info('initing app.logger')
    logging.config.dictConfig(yaml.load(open(os.path.join(APP_PATH, 'logconfig.yml'), 'r')))
    app.logger.info('app logger has now been reset')
    app.config.from_pyfile(os.path.join(APP_PATH, 'config.py'))
    app.secret_key = app.config["SECRET_KEY"]
    finjector = FlaskInjector(app=app, modules=[ConfigurationModule(app)])
    app.injector = finjector
    return app

if __name__ == '__main__':
    create_app().run()

和我工作的yaml

version: 1
disable_existing_loggers: true
root:
  handlers: [console]
  level: INFO


loggers:
  sqlalchemy.engine:
    level: WARN
    propagate: false
    handlers: []
  clportal:
    level: INFO
    handlers: [console]
    propagate: false

handlers:
  console:
    class: logging.StreamHandler
    formatter: standard
    level: INFO
    stream: ext://sys.stdout

formatters:
  standard:
    format: '%(asctime)s %(levelname)-8s %(name)-10s: %(message)s'