Skip to content

Commit

Permalink
Merge pull request #1567 from tseaver/logging-logger_log_text
Browse files Browse the repository at this point in the history
Add 'Logger.log_text' method.
  • Loading branch information
tseaver committed Mar 9, 2016
2 parents 95238be + b466893 commit b04eaa6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
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):
"""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

0 comments on commit b04eaa6

Please sign in to comment.