Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'Logger.log_text' method. #1567

Merged
merged 1 commit into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions gcloud/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,49 @@ def client(self):
def project(self):
"""Project bound to the logger."""
return self._client.project

@property
def full_name(self):
"""Fully-qualified name used in logging APIs"""
return 'projects/%s/logs/%s' % (self.project, self.name)

def _require_client(self, client):

This comment was marked as spam.

This comment was marked as spam.

"""Check client or verify over-ride.

:type client: :class:`gcloud.logging.client.Client` or ``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current topic.

:rtype: :class:`gcloud.logging.client.Client`
:returns: The client passed in or the currently bound client.
"""
if client is None:
client = self._client
return client

def log_text(self, text, client=None):
"""API call: log a text message via a POST request

See:
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/write

:type text: text
:param text: the log message.

:type client: :class:`gcloud.logging.client.Client` or ``NoneType``
:param client: the client to use. If not passed, falls back to the
``client`` stored on the current logger.
"""
client = self._require_client(client)

data = {
'entries': [{
'logName': self.full_name,
'textPayload': text,
'resource': {
'type': 'global',
},
}],
}
client.connection.api_request(
method='POST', path='/entries:write', data=data)
52 changes: 52 additions & 0 deletions gcloud/logging/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,53 @@ def test_ctor(self):
self.assertEqual(logger.name, self.LOGGER_NAME)
self.assertTrue(logger.client is client)
self.assertEqual(logger.project, self.PROJECT)
self.assertEqual(logger.full_name, 'projects/%s/logs/%s'
% (self.PROJECT, self.LOGGER_NAME))

def test_log_text_w_str_implicit_client(self):
TEXT = 'TEXT'
conn = _Connection({})
client = _Client(self.PROJECT, conn)
logger = self._makeOne(self.LOGGER_NAME, client=client)
logger.log_text(TEXT)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
SENT = {
'entries': [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'textPayload': TEXT,
'resource': {
'type': 'global',
},
}],
}
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/entries:write')
self.assertEqual(req['data'], SENT)

def test_log_text_w_unicode_explicit_client(self):
TEXT = u'TEXT'
conn = _Connection({})
client1 = _Client(self.PROJECT, object())
client2 = _Client(self.PROJECT, conn)
logger = self._makeOne(self.LOGGER_NAME, client=client1)
logger.log_text(TEXT, client=client2)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
SENT = {
'entries': [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'textPayload': TEXT,
'resource': {
'type': 'global',
},
}],
}
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/entries:write')
self.assertEqual(req['data'], SENT)


class _Connection(object):
Expand All @@ -42,6 +89,11 @@ def __init__(self, *responses):
self._responses = responses
self._requested = []

def api_request(self, **kw):
self._requested.append(kw)
response, self._responses = self._responses[0], self._responses[1:]
return response


class _Client(object):

Expand Down