Skip to content

Commit 3461b9a

Browse files
liyanhui1228Jon Wayne Parrott
authored andcommitted
Move the code path of get_gae_labels() to emit() (#4824)
1 parent cec6056 commit 3461b9a

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

google/cloud/logging/handlers/app_engine.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
and labels for App Engine logs.
1919
"""
2020

21+
import logging
2122
import os
2223

2324
from google.cloud.logging.handlers._helpers import get_trace_id
24-
from google.cloud.logging.handlers.handlers import CloudLoggingHandler
2525
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
2626
from google.cloud.logging.resource import Resource
2727

@@ -34,7 +34,7 @@
3434
_TRACE_ID_LABEL = 'appengine.googleapis.com/trace_id'
3535

3636

37-
class AppEngineHandler(CloudLoggingHandler):
37+
class AppEngineHandler(logging.StreamHandler):
3838
"""A logging handler that sends App Engine-formatted logs to Stackdriver.
3939
4040
:type client: :class:`~google.cloud.logging.client.Client`
@@ -48,13 +48,13 @@ class AppEngineHandler(CloudLoggingHandler):
4848
"""
4949

5050
def __init__(self, client,
51+
name=_DEFAULT_GAE_LOGGER_NAME,
5152
transport=BackgroundThreadTransport):
52-
super(AppEngineHandler, self).__init__(
53-
client,
54-
name=_DEFAULT_GAE_LOGGER_NAME,
55-
transport=transport,
56-
resource=self.get_gae_resource(),
57-
labels=self.get_gae_labels())
53+
super(AppEngineHandler, self).__init__()
54+
self.name = name
55+
self.client = client
56+
self.transport = transport(client, name)
57+
self.resource = self.get_gae_resource()
5858

5959
def get_gae_resource(self):
6060
"""Return the GAE resource using the environment variables.
@@ -88,3 +88,20 @@ def get_gae_labels(self):
8888
gae_labels[_TRACE_ID_LABEL] = trace_id
8989

9090
return gae_labels
91+
92+
def emit(self, record):
93+
"""Actually log the specified logging record.
94+
95+
Overrides the default emit behavior of ``StreamHandler``.
96+
97+
See https://docs.python.org/2/library/logging.html#handler-objects
98+
99+
:type record: :class:`logging.LogRecord`
100+
:param record: The record to be logged.
101+
"""
102+
message = super(AppEngineHandler, self).format(record)
103+
self.transport.send(
104+
record,
105+
message,
106+
resource=self.resource,
107+
labels=self.get_gae_labels())

google/cloud/logging/handlers/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class CloudLoggingHandler(logging.StreamHandler):
3535
route Python standard logging messages directly to the Stackdriver
3636
Logging API.
3737
38+
This handler is used when not in GAE or GKE environment.
39+
3840
This handler supports both an asynchronous and synchronous transport.
3941
4042
:type client: :class:`google.cloud.logging.client`

tests/unit/handlers/test_app_engine.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ def test_constructor(self):
4646
self.assertEqual(handler.resource.labels['project_id'], 'test_project')
4747
self.assertEqual(handler.resource.labels['module_id'], 'test_service')
4848
self.assertEqual(handler.resource.labels['version_id'], 'test_version')
49-
self.assertEqual(handler.labels, {})
5049

5150
def test_emit(self):
5251
client = mock.Mock(project=self.PROJECT, spec=['project'])
@@ -74,11 +73,10 @@ def _get_gae_labels_helper(self, trace_id):
7473
# The handler actually calls ``get_gae_labels()``.
7574
with get_trace_patch as mock_get_trace:
7675
handler = self._make_one(client, transport=_Transport)
77-
mock_get_trace.assert_called_once_with()
7876

7977
gae_labels = handler.get_gae_labels()
8078
self.assertEqual(mock_get_trace.mock_calls,
81-
[mock.call(), mock.call()])
79+
[mock.call()])
8280

8381
return gae_labels
8482

0 commit comments

Comments
 (0)