Skip to content

Commit

Permalink
Address jon and bill's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
liyanhui1228 committed May 13, 2017
1 parent 61490fc commit 5b79d48
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 34 deletions.
28 changes: 11 additions & 17 deletions logging/google/cloud/logging/handlers/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# /appengine-vmruntime/vmruntime/cloud_logging.py

import logging.handlers
import os

from google.cloud.logging.handlers.handlers import CloudLoggingHandler
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
Expand All @@ -36,7 +37,14 @@

EXCLUDED_LOGGER_DEFAULTS = ('google.cloud', 'oauth2client')

_GLOBAL_RESOURCE = Resource(type='global', labels={})
GAE_RESOURCE = Resource(
type='gae_app',
labels={
'project_id': os.getenv('GCLOUD_PROJECT'),
'module_id': os.getenv('GAE_SERVICE'),
'version_id': os.getenv('GAE_VERSION'),
},
)


class AppEngineHandler(CloudLoggingHandler):
Expand Down Expand Up @@ -71,19 +79,5 @@ class AppEngineHandler(CloudLoggingHandler):
def __init__(self, client,
name=DEFAULT_LOGGER_NAME,
transport=BackgroundThreadTransport,
resource=_GLOBAL_RESOURCE):
super(AppEngineHandler, self).__init__(client, name, transport)
self.resource=resource

def emit(self, record):
"""Actually log the specified logging record.
Overrides the default emit behavior of ``StreamHandler``.
See: https://docs.python.org/2/library/logging.html#handler-objects
:type record: :class:`logging.LogRecord`
:param record: The record to be logged.
"""
message = super(CloudLoggingHandler, self).format(record)
self.transport.send(record, message, self.resource)
resource=GAE_RESOURCE):
super(AppEngineHandler, self).__init__(client, name, transport, resource)
10 changes: 8 additions & 2 deletions logging/google/cloud/logging/handlers/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging

from google.cloud.logging.handlers.transports import BackgroundThreadTransport
from google.cloud.logging.logger import _GLOBAL_RESOURCE

DEFAULT_LOGGER_NAME = 'python'

Expand Down Expand Up @@ -52,6 +53,9 @@ class CloudLoggingHandler(logging.StreamHandler):
:class:`.BackgroundThreadTransport`. The other
option is :class:`.SyncTransport`.
:type resource: :class:`~google.cloud.logging.resource.Resource`
:param resource: (Optional) Monitored resource of the entry
Example:
.. code-block:: python
Expand All @@ -73,11 +77,13 @@ class CloudLoggingHandler(logging.StreamHandler):

def __init__(self, client,
name=DEFAULT_LOGGER_NAME,
transport=BackgroundThreadTransport):
transport=BackgroundThreadTransport,
resource=_GLOBAL_RESOURCE):
super(CloudLoggingHandler, self).__init__()
self.name = name
self.client = client
self.transport = transport(client, name)
self.resource=resource

def emit(self, record):
"""Actually log the specified logging record.
Expand All @@ -90,7 +96,7 @@ def emit(self, record):
:param record: The record to be logged.
"""
message = super(CloudLoggingHandler, self).format(record)
self.transport.send(record, message)
self.transport.send(record, message, self.resource)


def setup_logging(handler, excluded_loggers=EXCLUDED_LOGGER_DEFAULTS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,13 @@
from six.moves import queue

from google.cloud.logging.handlers.transports.base import Transport
from google.cloud.logging.resource import Resource

from google.cloud.logging.logger import _GLOBAL_RESOURCE

_DEFAULT_GRACE_PERIOD = 5.0 # Seconds
_DEFAULT_MAX_BATCH_SIZE = 10
_WORKER_THREAD_NAME = 'google.cloud.logging.Worker'
_WORKER_TERMINATOR = object()
_LOGGER = logging.getLogger(__name__)
_GLOBAL_RESOURCE = Resource(type='global', labels={})


def _get_many(queue_, max_items=None):
Expand Down
13 changes: 8 additions & 5 deletions logging/tests/unit/handlers/test_app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import logging
import os
import unittest
from google.cloud.logging.resource import Resource

Expand All @@ -37,12 +38,14 @@ def test_emit(self):
RESOURCE = Resource(
type='gae_app',
labels={
'module_id': 'default',
'version_id': 'test',
})
'project_id': os.getenv('GCLOUD_PROJECT'),
'module_id': os.getenv('GAE_SERVICE'),
'version_id': os.getenv('GAE_VERSION'),
},
)

client = _Client(self.PROJECT)
handler = self._make_one(client, transport=_Transport, resource=RESOURCE)
handler = self._make_one(client, transport=_Transport)
logname = 'loggername'
message = 'hello world'
record = logging.LogRecord(logname, logging, None, None, message,
Expand All @@ -64,4 +67,4 @@ def __init__(self, client, name):
pass

def send(self, record, message, resource):
self.send_called_with = (record, message, resource)
self.send_called_with = (record, message, resource)
10 changes: 6 additions & 4 deletions logging/tests/unit/handlers/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ def test_ctor(self):
self.assertEqual(handler.client, client)

def test_emit(self):
from google.cloud.logging.logger import _GLOBAL_RESOURCE

client = _Client(self.PROJECT)
handler = self._make_one(client, transport=_Transport)
handler = self._make_one(client, transport=_Transport, resource=_GLOBAL_RESOURCE)
logname = 'loggername'
message = 'hello world'
record = logging.LogRecord(logname, logging, None, None, message,
None, None)
handler.emit(record)

self.assertEqual(handler.transport.send_called_with, (record, message))
self.assertEqual(handler.transport.send_called_with, (record, message, _GLOBAL_RESOURCE))


class TestSetupLogging(unittest.TestCase):
Expand Down Expand Up @@ -108,5 +110,5 @@ class _Transport(object):
def __init__(self, client, name):
pass

def send(self, record, message):
self.send_called_with = (record, message)
def send(self, record, message, resource):
self.send_called_with = (record, message, resource)
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ def __init__(self):
self.commit_called = False
self.commit_count = None

def log_struct(self, record, severity=logging.INFO, resource=_GLOBAL_RESOURCE):
self.log_struct_called_with = (record, severity, resource)
self.entries.append(record)
def log_struct(self, info, severity=logging.INFO, resource=_GLOBAL_RESOURCE):
self.log_struct_called_with = (info, severity, resource)
self.entries.append(info)

def commit(self):
self.commit_called = True
Expand Down

0 comments on commit 5b79d48

Please sign in to comment.