[英]App Engine stackdriver logging to Global log instead of service log
I'm trying to set up logging for a django app hosted as an App Engine service on GAE. 我正在尝试为在GAE上作为App Engine服务托管的Django应用设置日志记录。
I have set up the logging succesfully, except that the logging is showing up in the global log for that entire project instead of the log for that service. 我已经成功设置了日志记录,除了日志记录显示在整个项目的全局日志中,而不是该服务的日志中。 I would like for the logs to show up only in the specific service logs
我希望日志仅显示在特定的服务日志中
this is my django logging config: 这是我的Django日志记录配置:
from google.cloud import logging as google_cloud_logging
log_client = google_cloud_logging.Client()
log_client.setup_logging()
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'stackdriver_logging': {
'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
'client': log_client
},
},
'loggers': {
'': {
'handlers': ['stackdriver_logging'],
'level': 'INFO',
}
},
}
And I am able to succesfully log to the Global project log by calling like this: 通过这样调用,我能够成功登录到全局项目日志:
def fetch_orders(request):
logger.error('test error')
logger.critical('test critical')
logger.warning('test warning')
logger.info('test info')
return redirect('dashboard')
I'm wanting to figure out if I can configure the logger to always use the log for the service that it's running in. 我想弄清楚是否可以将记录器配置为始终将日志用于运行它的服务。
EDIT: 编辑:
I tried the suggestion below, however now it is returning the following error: 我尝试了下面的建议,但是现在返回以下错误:
Traceback (most recent call last):
File "/env/lib/python3.7/site-packages/google/cloud/logging/handlers/transports/background_thread.py", line 122, in _safely_commit_batch
batch.commit()
File "/env/lib/python3.7/site-packages/google/cloud/logging/logger.py", line 381, in commit
entries = [entry.to_api_repr() for entry in self.entries]
File "/env/lib/python3.7/site-packages/google/cloud/logging/logger.py", line 381, in <listcomp>
entries = [entry.to_api_repr() for entry in self.entries]
File "/env/lib/python3.7/site-packages/google/cloud/logging/entries.py", line 318, in to_api_repr
info = super(StructEntry, self).to_api_repr()
File "/env/lib/python3.7/site-packages/google/cloud/logging/entries.py", line 241, in to_api_repr
info["resource"] = self.resource._to_dict()
AttributeError: 'ConvertingDict' object has no attribute '_to_dict'
I can over-ride this in the package source code to make it work, however the GAE environment requires that I use the package as supplied by google for the cloud logging. 我可以在程序包源代码中改写它以使其工作,但是GAE环境要求我使用google提供的程序包进行云日志记录。 Is there any way to go from here?
有什么办法可以从这里走吗?
To my understanding, it should be possible to accomplish what you want using the resource
option of CloudLoggingHandler . 据我了解,应该可以使用CloudLoggingHandler的
resource
选项完成您想要的事情 。 In the Stackdriver Logging (and Stackdriver Monitoring) API, each object (log line, time-series point) is associated with a "resource" (some thing that exists in a project, that can be provisioned, and can be the source of logs or time-series or the thing that the logs or time-series are being written about). 在Stackdriver Logging(和Stackdriver Monitoring)API中,每个对象(日志行,时间序列点)都与“资源”(项目中存在的某些事物相关联,可以配置,可以作为日志的来源)关联。或时间序列或记录日志或时间序列的内容)。 When the
resource
option is omitted, the CloudLoggingHandler
defaults to global
as you have observed. 如您
CloudLoggingHandler
,当省略resource
选项时, CloudLoggingHandler
缺省为global
。
There are a number of monitored resource types , including gae_app
, which can be used to represent a specific version of a particular service that is deployed on GAE. 有许多受监视的资源类型 ,包括
gae_app
,可以用来表示在GAE上部署的特定服务的特定版本。 Based on your code, this would look something like: 根据您的代码,这看起来像:
from google.cloud.logging import resource
def get_monitored_resource():
project_id = get_project_id()
gae_service = get_gae_service()
gae_service_version = get_gae_service_version()
resource_type = 'gae_app'
resource_labels = {
'project_id': project_id,
'module_id': gae_service,
'version_id': gae_service_version
}
return resource.Resource(resource_type, resource_labels)
GAE_APP_RESOURCE = get_monitored_resource()
LOGGING = {
# ...
'handlers': {
'stackdriver_logging': {
'class': 'google.cloud.logging.handlers.CloudLoggingHandler',
'client': log_client,
'resource': GAE_APP_RESOURCE,
},
},
# ...
}
In the code above, the functions get_project_id
, get_gae_service
, and get_gae_service_version
can be implemented in terms of the environment variables GOOGLE_CLOUD_PROJECT
, GAE_SERVICE
, and GAE_VERSION
in the Python flexible environment as documented by The Flexible Python Runtime as in: 在上面的代码,所述功能
get_project_id
, get_gae_service
和get_gae_service_version
可以在该环境变量的角度来实施GOOGLE_CLOUD_PROJECT
, GAE_SERVICE
和GAE_VERSION
如记录由在Python灵活的环境的弹性Python运行时 ,如下所示:
def get_project_id():
return os.getenv('GOOGLE_CLOUD_PROJECT')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.