|
17 | 17 |
|
18 | 18 | from gcloud.client import JSONClient
|
19 | 19 | from gcloud.logging.connection import Connection
|
| 20 | +from gcloud.logging.entries import StructEntry |
| 21 | +from gcloud.logging.entries import TextEntry |
20 | 22 | from gcloud.logging.logger import Logger
|
21 | 23 |
|
22 | 24 |
|
@@ -53,3 +55,82 @@ def logger(self, name):
|
53 | 55 | :returns: Logger created with the current client.
|
54 | 56 | """
|
55 | 57 | return Logger(name, client=self)
|
| 58 | + |
| 59 | + def _entry_from_resource(self, resource, loggers): |
| 60 | + """Detect correct entry type from resource and instantiate. |
| 61 | +
|
| 62 | + :type resource: dict |
| 63 | + :param resource: one entry resource from API response |
| 64 | +
|
| 65 | + :type loggers: dict or None |
| 66 | + :param loggers: A mapping of logger fullnames -> loggers. If not |
| 67 | + passed, the entry will have a newly-created logger. |
| 68 | +
|
| 69 | + :rtype; One of: |
| 70 | + :class:`gcloud.logging.entries.TextEntry`, |
| 71 | + :class:`gcloud.logging.entries.StructEntry`, |
| 72 | + :returns: the entry instance, constructed via the resource |
| 73 | + """ |
| 74 | + if 'textPayload' in resource: |
| 75 | + return TextEntry.from_api_repr(resource, self, loggers) |
| 76 | + elif 'jsonPayload' in resource: |
| 77 | + return StructEntry.from_api_repr(resource, self, loggers) |
| 78 | + raise ValueError('Cannot parse job resource') |
| 79 | + |
| 80 | + def list_entries(self, projects=None, filter_=None, order_by=None, |
| 81 | + page_size=None, page_token=None): |
| 82 | + """Return a page of log entries. |
| 83 | +
|
| 84 | + See: |
| 85 | + https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/entries/list |
| 86 | +
|
| 87 | + :type projects: list of strings |
| 88 | + :param projects: project IDs to include. If not passed, |
| 89 | + defaults to the project bound to the client. |
| 90 | +
|
| 91 | + :type filter_: string |
| 92 | + :param filter_: a filter expression. See: |
| 93 | + https://cloud.google.com/logging/docs/view/advanced_filters |
| 94 | +
|
| 95 | + :type order_by: string |
| 96 | + :param order_by: One of :data:`gcloud.logging.ASCENDING` or |
| 97 | + :data:`gcloud.logging.DESCENDING`. |
| 98 | +
|
| 99 | + :type page_size: int |
| 100 | + :param page_size: maximum number of topics to return, If not passed, |
| 101 | + defaults to a value set by the API. |
| 102 | +
|
| 103 | + :type page_token: string |
| 104 | + :param page_token: opaque marker for the next "page" of topics. If not |
| 105 | + passed, the API will return the first page of |
| 106 | + topics. |
| 107 | +
|
| 108 | + :rtype: tuple, (list, str) |
| 109 | + :returns: list of :class:`gcloud.logging.entry.TextEntry`, plus a |
| 110 | + "next page token" string: if not None, indicates that |
| 111 | + more topics can be retrieved with another call (pass that |
| 112 | + value as ``page_token``). |
| 113 | + """ |
| 114 | + if projects is None: |
| 115 | + projects = [self.project] |
| 116 | + |
| 117 | + params = {'projectIds': projects} |
| 118 | + |
| 119 | + if filter_ is not None: |
| 120 | + params['filter'] = filter_ |
| 121 | + |
| 122 | + if order_by is not None: |
| 123 | + params['orderBy'] = order_by |
| 124 | + |
| 125 | + if page_size is not None: |
| 126 | + params['pageSize'] = page_size |
| 127 | + |
| 128 | + if page_token is not None: |
| 129 | + params['pageToken'] = page_token |
| 130 | + |
| 131 | + resp = self.connection.api_request(method='POST', path='/entries:list', |
| 132 | + data=params) |
| 133 | + loggers = {} |
| 134 | + entries = [self._entry_from_resource(resource, loggers) |
| 135 | + for resource in resp.get('entries', ())] |
| 136 | + return entries, resp.get('nextPageToken') |
0 commit comments