def test_list_log_entries(self): # Setup Expected Response next_page_token = "" entries_element = {} entries = [entries_element] expected_response = { "next_page_token": next_page_token, "entries": entries } expected_response = logging_pb2.ListLogEntriesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = logging_v2.LoggingServiceV2Client() # Setup Request resource_names = [] paged_list_response = client.list_log_entries(resource_names) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.entries[0] == resources[0] assert len(channel.requests) == 1 expected_request = logging_pb2.ListLogEntriesRequest( resource_names=resource_names) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_list_log_entries(self): # Setup Expected Response next_page_token = '' entries_element = {} entries = [entries_element] expected_response = { 'next_page_token': next_page_token, 'entries': entries } expected_response = logging_pb2.ListLogEntriesResponse( **expected_response) # Mock the API response channel = ChannelStub(responses=[expected_response]) client = logging_v2.LoggingServiceV2Client(channel=channel) # Setup Request resource_names = [] paged_list_response = client.list_log_entries(resource_names) resources = list(paged_list_response) assert len(resources) == 1 assert expected_response.entries[0] == resources[0] assert len(channel.requests) == 1 expected_request = logging_pb2.ListLogEntriesRequest( resource_names=resource_names) actual_request = channel.requests[0][1] assert expected_request == actual_request
def list_log_entries( self, resource_names, project_ids=None, filter_=None, order_by=None, page_size=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None, ): """ Lists log entries. Use this method to retrieve log entries that originated from a project/folder/organization/billing account. For ways to export log entries, see `Exporting Logs <https://cloud.google.com/logging/docs/export>`__. Example: >>> from google.cloud import logging_v2 >>> >>> client = logging_v2.LoggingServiceV2Client() >>> >>> # TODO: Initialize `resource_names`: >>> resource_names = [] >>> >>> # Iterate over all results >>> for element in client.list_log_entries(resource_names): ... # process element ... pass >>> >>> >>> # Alternatively: >>> >>> # Iterate over results one page at a time >>> for page in client.list_log_entries(resource_names).pages: ... for element in page: ... # process element ... pass Args: resource_names (list[str]): Required. Names of one or more parent resources from which to retrieve log entries: :: "projects/[PROJECT_ID]" "organizations/[ORGANIZATION_ID]" "billingAccounts/[BILLING_ACCOUNT_ID]" "folders/[FOLDER_ID]" Projects listed in the ``project_ids`` field are added to this list. project_ids (list[str]): Deprecated. Use ``resource_names`` instead. One or more project identifiers or project numbers from which to retrieve log entries. Example: ``"my-project-1A"``. filter_ (str): Optional. A filter that chooses which log entries to return. See `Advanced Logs Filters <https://cloud.google.com/logging/docs/view/advanced_filters>`__. Only log entries that match the filter are returned. An empty filter matches all log entries in the resources listed in ``resource_names``. Referencing a parent resource that is not listed in ``resource_names`` will cause the filter to return no results. The maximum length of the filter is 20000 characters. order_by (str): Optional. How the results should be sorted. Presently, the only permitted values are ``"timestamp asc"`` (default) and ``"timestamp desc"``. The first option returns entries in order of increasing values of ``LogEntry.timestamp`` (oldest first), and the second option returns entries in order of decreasing timestamps (newest first). Entries with equal timestamps are returned in order of their ``insert_id`` values. page_size (int): The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will be retried using a default configuration. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.api_core.page_iterator.PageIterator` instance. An iterable of :class:`~google.cloud.logging_v2.types.LogEntry` instances. You can also iterate over the pages of the response using its `pages` property. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ # Wrap the transport method to add retry and timeout logic. if "list_log_entries" not in self._inner_api_calls: self._inner_api_calls[ "list_log_entries"] = google.api_core.gapic_v1.method.wrap_method( self.transport.list_log_entries, default_retry=self._method_configs["ListLogEntries"].retry, default_timeout=self._method_configs["ListLogEntries"]. timeout, client_info=self._client_info, ) request = logging_pb2.ListLogEntriesRequest( resource_names=resource_names, project_ids=project_ids, filter=filter_, order_by=order_by, page_size=page_size, ) iterator = google.api_core.page_iterator.GRPCIterator( client=None, method=functools.partial( self._inner_api_calls["list_log_entries"], retry=retry, timeout=timeout, metadata=metadata, ), request=request, items_field="entries", request_token_field="page_token", response_token_field="next_page_token", ) return iterator
def list_log_entries(self, resource_names, project_ids=None, filter_=None, order_by=None, page_size=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Lists log entries. Use this method to retrieve log entries from Stackdriver Logging. For ways to export log entries, see `Exporting Logs <https://cloud.google.com/logging/docs/export>`_. Example: >>> from google.cloud import logging_v2 >>> >>> client = logging_v2.LoggingServiceV2Client() >>> >>> resource_names = [] >>> >>> >>> # Iterate over all results >>> for element in client.list_log_entries(resource_names): ... # process element ... pass >>> >>> # Or iterate over results one page at a time >>> for page in client.list_log_entries(resource_names, options=CallOptions(page_token=INITIAL_PAGE)): ... for element in page: ... # process element ... pass Args: resource_names (list[str]): Required. Names of one or more parent resources from which to retrieve log entries: :: \"projects/[PROJECT_ID]\" \"organizations/[ORGANIZATION_ID]\" \"billingAccounts/[BILLING_ACCOUNT_ID]\" \"folders/[FOLDER_ID]\" Projects listed in the ``project_ids`` field are added to this list. project_ids (list[str]): Deprecated. Use ``resource_names`` instead. One or more project identifiers or project numbers from which to retrieve log entries. Example: ``\"my-project-1A\"``. If present, these project identifiers are converted to resource name format and added to the list of resources in ``resource_names``. filter_ (str): Optional. A filter that chooses which log entries to return. See [Advanced Logs Filters](/logging/docs/view/advanced_filters). Only log entries that match the filter are returned. An empty filter matches all log entries in the resources listed in ``resource_names``. Referencing a parent resource that is not listed in ``resource_names`` will cause the filter to return no results. The maximum length of the filter is 20000 characters. order_by (str): Optional. How the results should be sorted. Presently, the only permitted values are ``\"timestamp asc\"`` (default) and ``\"timestamp desc\"``. The first option returns entries in order of increasing values of ``LogEntry.timestamp`` (oldest first), and the second option returns entries in order of decreasing timestamps (newest first). Entries with equal timestamps are returned in order of their ``insert_id`` values. page_size (int): The maximum number of resources contained in the underlying API response. If page streaming is performed per- resource, this parameter does not affect the return value. If page streaming is performed per-page, this determines the maximum number of resources in a page. retry (Optional[google.api_core.retry.Retry]): A retry object used to retry requests. If ``None`` is specified, requests will not be retried. timeout (Optional[float]): The amount of time, in seconds, to wait for the request to complete. Note that if ``retry`` is specified, the timeout applies to each individual attempt. metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata that is provided to the method. Returns: A :class:`~google.gax.PageIterator` instance. By default, this is an iterable of :class:`~google.cloud.logging_v2.types.LogEntry` instances. This object can also be configured to iterate over the pages of the response through the `options` parameter. Raises: google.api_core.exceptions.GoogleAPICallError: If the request failed for any reason. google.api_core.exceptions.RetryError: If the request failed due to a retryable error and retry attempts failed. ValueError: If the parameters are invalid. """ if metadata is None: metadata = [] metadata = list(metadata) request = logging_pb2.ListLogEntriesRequest( resource_names=resource_names, project_ids=project_ids, filter=filter_, order_by=order_by, page_size=page_size, ) iterator = google.api_core.page_iterator.GRPCIterator( client=None, method=functools.partial(self._list_log_entries, retry=retry, timeout=timeout, metadata=metadata), request=request, items_field='entries', request_token_field='page_token', response_token_field='next_page_token', ) return iterator