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_struct' #1568

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
27 changes: 27 additions & 0 deletions gcloud/logging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,30 @@ def log_text(self, text, client=None):
}
client.connection.api_request(
method='POST', path='/entries:write', data=data)

def log_struct(self, info, 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 info: dict
:param info: the log entry information

: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,
'jsonPayload': info,

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

'resource': {
'type': 'global',
},
}],
}
client.connection.api_request(
method='POST', path='/entries:write', data=data)
45 changes: 45 additions & 0 deletions gcloud/logging/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,51 @@ def test_log_text_w_unicode_explicit_client(self):
self.assertEqual(req['path'], '/entries:write')
self.assertEqual(req['data'], SENT)

def test_log_struct_w_implicit_client(self):
STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'}
conn = _Connection({})
client = _Client(self.PROJECT, conn)
logger = self._makeOne(self.LOGGER_NAME, client=client)
logger.log_struct(STRUCT)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
SENT = {
'entries': [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'jsonPayload': STRUCT,
'resource': {
'type': 'global',
},
}],
}
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/entries:write')
self.assertEqual(req['data'], SENT)

def test_log_struct_w_explicit_client(self):
STRUCT = {'message': 'MESSAGE', 'weather': 'cloudy'}
conn = _Connection({})
client1 = _Client(self.PROJECT, object())
client2 = _Client(self.PROJECT, conn)
logger = self._makeOne(self.LOGGER_NAME, client=client1)
logger.log_struct(STRUCT, client=client2)
self.assertEqual(len(conn._requested), 1)
req = conn._requested[0]
SENT = {
'entries': [{
'logName': 'projects/%s/logs/%s' % (
self.PROJECT, self.LOGGER_NAME),
'jsonPayload': STRUCT,
'resource': {
'type': 'global',
},
}],
}
self.assertEqual(req['method'], 'POST')
self.assertEqual(req['path'], '/entries:write')
self.assertEqual(req['data'], SENT)


class _Connection(object):

Expand Down