|
14 | 14 |
|
15 | 15 | """Logging handler for App Engine Flexible |
16 | 16 |
|
17 | | -Logs to the well-known file that the fluentd sidecar container on App Engine |
18 | | -Flexible is configured to read from and send to Stackdriver Logging. |
19 | | -
|
20 | | -See the fluentd configuration here: |
21 | | -
|
22 | | -https://github.com/GoogleCloudPlatform/appengine-sidecars-docker/tree/master/fluentd_logger |
| 17 | +Sends logs to the Stackdriver Logging API with the appropriate resource and labels for App Engine logs. |
23 | 18 | """ |
24 | 19 |
|
25 | | -# This file is largely copied from: |
26 | | -# https://github.com/GoogleCloudPlatform/python-compat-runtime/blob/master |
27 | | -# /appengine-vmruntime/vmruntime/cloud_logging.py |
28 | | - |
29 | | -import logging.handlers |
30 | 20 | import os |
31 | 21 |
|
32 | 22 | from google.cloud.logging.handlers.handlers import CloudLoggingHandler |
33 | 23 | from google.cloud.logging.handlers.transports import BackgroundThreadTransport |
34 | 24 | from google.cloud.logging.resource import Resource |
35 | 25 |
|
36 | | -DEFAULT_LOGGER_NAME = 'python' |
37 | | - |
38 | | -EXCLUDED_LOGGER_DEFAULTS = ('google.cloud', 'oauth2client') |
39 | | - |
40 | | -GAE_RESOURCE = Resource( |
41 | | - type='gae_app', |
42 | | - labels={ |
43 | | - 'project_id': os.getenv('GCLOUD_PROJECT'), |
44 | | - 'module_id': os.getenv('GAE_SERVICE'), |
45 | | - 'version_id': os.getenv('GAE_VERSION'), |
46 | | - }, |
47 | | -) |
| 26 | +_GAE_PROJECT_ENV = 'GCLOUD_PROJECT' |
| 27 | +_GAE_SERVICE_ENV = 'GAE_SERVICE' |
| 28 | +_GAE_VERSION_ENV = 'GAE_VERSION' |
48 | 29 |
|
49 | 30 |
|
50 | 31 | class AppEngineHandler(CloudLoggingHandler): |
@@ -76,8 +57,20 @@ class AppEngineHandler(CloudLoggingHandler): |
76 | 57 | to the global resource type. |
77 | 58 | """ |
78 | 59 |
|
| 60 | + DEFAULT_LOGGER_NAME = 'projects/{}/logs/app'.format(os.environ.get(_GAE_PROJECT_ENV)) |
| 61 | + |
79 | 62 | def __init__(self, client, |
80 | | - name=DEFAULT_LOGGER_NAME, |
81 | | - transport=BackgroundThreadTransport, |
82 | | - resource=GAE_RESOURCE): |
83 | | - super(AppEngineHandler, self).__init__(client, name, transport, resource) |
| 63 | + transport=BackgroundThreadTransport): |
| 64 | + super(AppEngineHandler, self).__init__(client, name=self.DEFAULT_LOGGER_NAME, |
| 65 | + transport=transport, resource=self.get_gae_resource()) |
| 66 | + |
| 67 | + def get_gae_resource(self): |
| 68 | + gae_resource = Resource( |
| 69 | + type='gae_app', |
| 70 | + labels={ |
| 71 | + 'project_id': os.environ.get(_GAE_PROJECT_ENV), |
| 72 | + 'module_id': os.environ.get(_GAE_SERVICE_ENV), |
| 73 | + 'version_id': os.environ.get(_GAE_VERSION_ENV), |
| 74 | + }, |
| 75 | + ) |
| 76 | + return gae_resource |
0 commit comments