def entry_from_resource(resource, client, loggers): """Detect correct entry type from resource and instantiate. Args: resource (dict): One entry resource from API response. client (~logging_v2.client.Client): Client that owns the log entry. loggers (dict): A mapping of logger fullnames -> loggers. If the logger that owns the entry is not in ``loggers``, the entry will have a newly-created logger. Returns: google.cloud.logging_v2.entries._BaseEntry: The entry instance, constructed via the resource """ if "textPayload" in resource: return TextEntry.from_api_repr(resource, client, loggers=loggers) if "jsonPayload" in resource: return StructEntry.from_api_repr(resource, client, loggers=loggers) if "protoPayload" in resource: return ProtobufEntry.from_api_repr(resource, client, loggers=loggers) return LogEntry.from_api_repr(resource, client, loggers=loggers)
def log_empty(self, **kw): """Add a entry without payload to be logged during :meth:`commit`. Args: kw (Optional[dict]): Additional keyword arguments for the entry. See :class:`~logging_v2.entries.LogEntry`. """ self.entries.append(LogEntry(**kw))
def test_log_empty_defaults(self): from google.cloud.logging_v2.entries import LogEntry ENTRY = LogEntry() client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_empty() self.assertEqual(batch.entries, [ENTRY])
def test_log_empty_explicit(self): import datetime from google.cloud.logging_v2.resource import Resource from google.cloud.logging_v2.entries import LogEntry LABELS = {"foo": "bar", "baz": "qux"} IID = "IID" SEVERITY = "CRITICAL" METHOD = "POST" URI = "https://api.example.com/endpoint" STATUS = "500" REQUEST = { "requestMethod": METHOD, "requestUrl": URI, "status": STATUS } TIMESTAMP = datetime.datetime(2016, 12, 31, 0, 1, 2, 999999) RESOURCE = Resource(type="gae_app", labels={ "module_id": "default", "version_id": "test" }) TRACE = "12345678-1234-5678-1234-567812345678" SPANID = "000000000000004a" ENTRY = LogEntry( labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE, span_id=SPANID, trace_sampled=True, ) client = _Client(project=self.PROJECT, connection=_make_credentials()) logger = _Logger() batch = self._make_one(logger, client=client) batch.log_empty( labels=LABELS, insert_id=IID, severity=SEVERITY, http_request=REQUEST, timestamp=TIMESTAMP, resource=RESOURCE, trace=TRACE, span_id=SPANID, trace_sampled=True, ) self.assertEqual(batch.entries, [ENTRY])
def test_commit_w_unknown_entry_type(self): from google.cloud.logging_v2.entries import _GLOBAL_RESOURCE from google.cloud.logging_v2.entries import LogEntry logger = _Logger() client = _Client(project=self.PROJECT, connection=_make_credentials()) api = client.logging_api = _DummyLoggingAPI() batch = self._make_one(logger, client) batch.entries.append(LogEntry(severity="blah")) ENTRY = {"severity": "blah", "resource": _GLOBAL_RESOURCE._to_dict()} batch.commit() self.assertEqual(list(batch.entries), []) self.assertEqual(api._write_entries_called_with, ([ENTRY], logger.full_name, None, None))