GAE-Python 3.7-如何记录? [英] GAE - Python 3.7 - How to log?

查看:98
本文介绍了GAE-Python 3.7-如何记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python 3.7中有一个Google App Engine项目,我想在其中写一些日志. 我习惯于在App Engine python 2.7中编程,而我使用的是简单代码:

I have a google app engine project in python 3.7 in which I would like to write some logs. I am used to program in app engine python 2.7 and I was using the simple code:

 logging.info('hi there!')

将任何日志写入Google云日志控制台. 上面的命令现在不再起作用,它说:

to write any log onto the google cloud log console. That command above now doesn't work anymore and it says:

logging has no attribute 'info'

我搜索了,发现了这个可能的新代码

I searched and I found this possible new code

from flask import Flask
from google.cloud import logging

app = Flask(__name__)

@app.route('/l')
def hello():
    logging_client = logging.Client()
    log_name = LOG_NAME
    logger = logging_client.logger(LOG_NAME)
    text = 'Hello, world!'
    logger.log_text(text, severity='CRITICAL')
    return text

上面的代码在堆栈驱动器报告页面中没有给出任何错误,但在日志页面中什么都没有显示.

This code above doesn't give any error in the stack-driver report page BUT it displays nothing at all in the log page.

那么如何在python3.7中为我的App Engine项目编写日志?

So how can I write a log for my app engine project in python3.7?

推荐答案

第二代标准环境(包括python 3.7)比第一代标准环境(包括python 2.7)更贴近灵活环境.

The second generation standard environment (which includes python 3.7) is IMHO closer to the flexible environment than to the first generation standard environment (which includes python 2.7).

如果第1代的自定义版本(由GAE团队维护)的许多API如果各自的功能或多或少地被替代的,更通用的涵盖,则它们(或至少尚未)移植到第二代中.已经在灵活环境中使用的方法(大多数方法基于GAE以外的团队开发和维护的服务).

Many of the APIs which had customized versions for the 1st generation (maintained by the GAE team) were not (or at least not yet) ported in the 2nd generation if the respective functionality was more or less covered by alternate, more generic approaches, already in use in the flexible environment (most of them based on services developed and maintained by teams other than the GAE one).

您会在这两个迁移指南中注意到许多服务部分之间的相似之处(这使我得出了以上的总结结论):

You'll notice the similarity between many of the service sections in these 2 migration guides (which led me to the above summary conclusion):

  • Understanding differences between Python 2 and Python 3 on the App Engine standard environment
  • Migrating Services from the Standard Environment to the Flexible Environment (i.e 1st generation standard to flexible)

日志记录是两个指南中列出的服务之一.第一代使用标准python logging库的自定义版本(在Stackdriver成为独立服务之前).对于第二代,日志记录只是被委派为使用现在普遍可用的 Stackdriver日志记录服务(即您显示的摘录来自什么).从日志记录(在第一个指南中):

Logging is one of the services listed in both guides. The 1st generation used a customized version of the standard python logging library (that was before Stackdriver became a standalone service). For the 2nd generation logging was simply delegated to using the now generally available Stackdriver logging service (which is what the snippet you shown comes from). From Logging (in the 1st guide):

请求日志不再自动关联,但仍将 出现在Stackdriver Logging中.使用Stackdriver Logging客户端 库来实现您所需的日志记录行为.

Request logs are no longer automatically correlated but will still appear in Stackdriver Logging. Use the Stackdriver Logging client libraries to implement your desired logging behavior.

您显示的代码片段确实对应于Stackdriver Logging.但是您似乎直接使用客户端库.我不知道这是否有问题(GAE通常有点不同),但是也许您也可以尝试使用标准的Python日志记录代替:

The code snippet you show corresponds, indeed, to Stackdriver Logging. But you seem to be Using the client library directly. I don't know if this is a problem (GAE is often a bit different), but maybe you could also try using the standard Python logging instead:

通过附加Stackdriver将所有日志条目发送到Stackdriver 将处理程序记录到Python根记录器,请使用setup_logging 辅助方法:

To send all log entries to Stackdriver by attaching the Stackdriver Logging handler to the Python root logger, use the setup_logging helper method:

# Imports the Google Cloud client library
import google.cloud.logging

# Instantiates a client
client = google.cloud.logging.Client()

# Connects the logger to the root logging handler; by default this captures
# all logs at INFO level and higher
client.setup_logging()

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