From b4668932f028dea4b9bb27d8dcdeae6a3962f701 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 3 Mar 2016 14:38:47 -0500 Subject: [PATCH] Add 'Logger.log_text' method. --- gcloud/logging/logger.py | 46 +++++++++++++++++++++++++++++++ gcloud/logging/test_logger.py | 52 +++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/gcloud/logging/logger.py b/gcloud/logging/logger.py index 9d76fa6ac5e7..7d5959347d7c 100644 --- a/gcloud/logging/logger.py +++ b/gcloud/logging/logger.py @@ -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) diff --git a/gcloud/logging/test_logger.py b/gcloud/logging/test_logger.py index e3d8107d882c..fcac20719c87 100644 --- a/gcloud/logging/test_logger.py +++ b/gcloud/logging/test_logger.py @@ -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): @@ -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):