def logger(self, name): """Creates a logger bound to the current client. :type name: string :param name: the name of the logger to be constructed. :rtype: :class:`gcloud.logging.logger.Logger` :returns: Logger created with the current client. """ return Logger(name, client=self)
def test_commit_w_alternate_client(self): import json from google.protobuf.json_format import MessageToJson from google.protobuf.struct_pb2 import Struct, Value from gcloud.logging.logger import Logger TEXT = 'This is the entry text' STRUCT = {'message': TEXT, 'weather': 'partly cloudy'} message = Struct(fields={'foo': Value(bool_value=True)}) DEFAULT_LABELS = {'foo': 'spam'} LABELS = { 'foo': 'bar', 'baz': 'qux', } SEVERITY = 'CRITICAL' METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, 'status': STATUS, } client1 = _Client(project=self.PROJECT) client2 = _Client(project=self.PROJECT) api = client2.logging_api = _DummyLoggingAPI() logger = Logger('logger_name', client1, labels=DEFAULT_LABELS) RESOURCE = {'type': 'global'} ENTRIES = [ { 'textPayload': TEXT, 'labels': LABELS }, { 'jsonPayload': STRUCT, 'severity': SEVERITY }, { 'protoPayload': json.loads(MessageToJson(message)), 'httpRequest': REQUEST }, ] batch = self._makeOne(logger, client=client1) batch.log_text(TEXT, labels=LABELS) batch.log_struct(STRUCT, severity=SEVERITY) batch.log_proto(message, http_request=REQUEST) batch.commit(client=client2) self.assertEqual(list(batch.entries), []) self.assertEqual(api._write_entries_called_with, (ENTRIES, logger.path, RESOURCE, DEFAULT_LABELS))
def test_context_mgr_success(self): import json from google.protobuf.json_format import MessageToJson from google.protobuf.struct_pb2 import Struct, Value from gcloud.logging.logger import Logger TEXT = 'This is the entry text' STRUCT = {'message': TEXT, 'weather': 'partly cloudy'} message = Struct(fields={'foo': Value(bool_value=True)}) DEFAULT_LABELS = {'foo': 'spam'} LABELS = {'foo': 'bar', 'baz': 'qux'} SEVERITY = 'CRITICAL' METHOD = 'POST' URI = 'https://api.example.com/endpoint' STATUS = '500' REQUEST = { 'requestMethod': METHOD, 'requestUrl': URI, 'status': STATUS, } conn = _Connection({}) CLIENT = _Client(project=self.PROJECT, connection=conn) logger = Logger('logger_name', CLIENT, labels=DEFAULT_LABELS) SENT = { 'logName': logger.path, 'resource': { 'type': 'global', }, 'labels': DEFAULT_LABELS, 'entries': [ {'textPayload': TEXT, 'httpRequest': REQUEST}, {'jsonPayload': STRUCT, 'labels': LABELS}, {'protoPayload': json.loads(MessageToJson(message)), 'severity': SEVERITY}, ], } batch = self._makeOne(logger, client=CLIENT) with batch as other: other.log_text(TEXT, http_request=REQUEST) other.log_struct(STRUCT, labels=LABELS) other.log_proto(message, severity=SEVERITY) self.assertEqual(list(batch.entries), []) self.assertEqual(len(conn._requested), 1) req = conn._requested[0] self.assertEqual(req['method'], 'POST') self.assertEqual(req['path'], '/entries:write') self.assertEqual(req['data'], SENT)