|
| 1 | +Using the API |
| 2 | +============= |
| 3 | + |
| 4 | + |
| 5 | +Authentication and Configuration |
| 6 | +-------------------------------- |
| 7 | + |
| 8 | +- For an overview of authentication in ``gcloud-python``, |
| 9 | + see :doc:`gcloud-auth`. |
| 10 | + |
| 11 | +- In addition to any authentication configuration, you should also set the |
| 12 | + :envvar:`GCLOUD_PROJECT` environment variable for the project you'd like |
| 13 | + to interact with. If you are Google App Engine or Google Compute Engine |
| 14 | + this will be detected automatically. |
| 15 | + |
| 16 | +- After configuring your environment, create a |
| 17 | + :class:`Client <gcloud.logging.client.Client>` |
| 18 | + |
| 19 | + .. doctest:: |
| 20 | + |
| 21 | + >>> from gcloud import logging |
| 22 | + >>> client = logging.Client() |
| 23 | + |
| 24 | + or pass in ``credentials`` and ``project`` explicitly |
| 25 | + |
| 26 | + .. doctest:: |
| 27 | + |
| 28 | + >>> from gcloud import logging |
| 29 | + >>> client = logging.Client(project='my-project', credentials=creds) |
| 30 | + |
| 31 | + |
| 32 | +Writing log entries |
| 33 | +------------------- |
| 34 | + |
| 35 | +Write a simple text entry to a logger. |
| 36 | + |
| 37 | +.. doctest:: |
| 38 | + |
| 39 | + >>> from gcloud import logging |
| 40 | + >>> client = logging.Client() |
| 41 | + >>> logger = client.logger('log_name') |
| 42 | + >>> logger.log_text("A simple entry") # API call |
| 43 | + |
| 44 | +Write a dictionary entry to a logger. |
| 45 | + |
| 46 | +.. doctest:: |
| 47 | + |
| 48 | + >>> from gcloud import logging |
| 49 | + >>> client = logging.Client() |
| 50 | + >>> logger = client.logger('log_name') |
| 51 | + >>> logger.log_struct( |
| 52 | + ... message="My second entry", |
| 53 | + ... weather="partly cloudy") # API call |
| 54 | + |
| 55 | + |
| 56 | +Retrieving log entries |
| 57 | +---------------------- |
| 58 | + |
| 59 | +Fetch entries for the default project. |
| 60 | + |
| 61 | +.. doctest:: |
| 62 | + |
| 63 | + >>> from gcloud import logging |
| 64 | + >>> client = logging.Client() |
| 65 | + >>> entries, token = client.list_entries() # API call |
| 66 | + >>> for entry in entries: |
| 67 | + ... timestamp = entry.timestamp.isoformat() |
| 68 | + ... print('%sZ: %s | %s' % |
| 69 | + ... (timestamp, entry.text_payload, entry.struct_payload)) |
| 70 | + 2016-02-17T20:35:49.031864072Z: A simple entry | None |
| 71 | + 2016-02-17T20:38:15.944418531Z: None | {'message': 'My second entry', 'weather': 'partly cloudy'} |
| 72 | + |
| 73 | +Fetch entries across multiple projects. |
| 74 | + |
| 75 | +.. doctest:: |
| 76 | + |
| 77 | + >>> from gcloud import logging |
| 78 | + >>> client = logging.Client() |
| 79 | + >>> entries, token = client.list_entries( |
| 80 | + ... project_ids=['one-project', 'another-project']) # API call |
| 81 | + |
| 82 | +Filter entries retrieved using the `Advanced Logs Filters`_ syntax |
| 83 | + |
| 84 | +.. _Advanced Logs Filters: https://cloud.google.com/logging/docs/view/advanced_filters |
| 85 | + |
| 86 | +.. doctest:: |
| 87 | + |
| 88 | + >>> from gcloud import logging |
| 89 | + >>> client = logging.Client() |
| 90 | + >>> FILTER = "log:log_name AND textPayload:simple" |
| 91 | + >>> entries, token = client.list_entries(filter=FILTER) # API call |
| 92 | + |
| 93 | +Sort entries in descending timestamp order. |
| 94 | + |
| 95 | +.. doctest:: |
| 96 | + |
| 97 | + >>> from gcloud import logging |
| 98 | + >>> client = logging.Client() |
| 99 | + >>> entries, token = client.list_entries(order_by=logging.DESCENDING) # API call |
| 100 | + |
| 101 | +Retrieve entities in batches of 10, iterating until done. |
| 102 | + |
| 103 | +.. doctest:: |
| 104 | + |
| 105 | + >>> from gcloud import logging |
| 106 | + >>> client = logging.Client() |
| 107 | + >>> retrieved = [] |
| 108 | + >>> token = None |
| 109 | + >>> while True: |
| 110 | + ... entries, token = client.list_entries(page_size=10, page_token=token) # API call |
| 111 | + ... retrieved.extend(entries) |
| 112 | + ... if token is None: |
| 113 | + ... break |
| 114 | + |
| 115 | + |
| 116 | +Delete all entries for a logger |
| 117 | +------------------------------- |
| 118 | + |
| 119 | +.. doctest:: |
| 120 | + |
| 121 | + >>> from gcloud import logging |
| 122 | + >>> client = logging.Client() |
| 123 | + >>> logger = client.logger('log_name') |
| 124 | + >>> logger.delete_entries() # API call |
| 125 | + |
| 126 | + |
| 127 | +Manage log metrics |
| 128 | +------------------ |
| 129 | + |
| 130 | +Metrics are counters of entries which match a given filter. They can be |
| 131 | +used within Cloud Monitoring to create charts and alerts. |
| 132 | + |
| 133 | +Create a metric: |
| 134 | + |
| 135 | +.. doctest:: |
| 136 | + |
| 137 | + >>> from gcloud import logging |
| 138 | + >>> client = logging.Client() |
| 139 | + >>> metric = client.metric( |
| 140 | + ... "robots", "Robots all up in your server", |
| 141 | + ... filter='log:apache-access AND textPayload:robot') |
| 142 | + >>> metric.exists() # API call |
| 143 | + False |
| 144 | + >>> metric.create() # API call |
| 145 | + >>> metric.exists() # API call |
| 146 | + True |
| 147 | + |
| 148 | +List all metrics for a project: |
| 149 | + |
| 150 | +.. doctest:: |
| 151 | + |
| 152 | + >>> from gcloud import logging |
| 153 | + >>> client = logging.Client() |
| 154 | + >>> metrics, token = client.list_metrics() |
| 155 | + >>> len(metrics) |
| 156 | + 1 |
| 157 | + >>> metric = metrics[0] |
| 158 | + >>> metric.name |
| 159 | + "robots" |
| 160 | + |
| 161 | +Refresh local information about a metric: |
| 162 | + |
| 163 | +.. doctest:: |
| 164 | + |
| 165 | + >>> from gcloud import logging |
| 166 | + >>> client = logging.Client() |
| 167 | + >>> metric = client.metric("robots") |
| 168 | + >>> metric.reload() # API call |
| 169 | + >>> metric.description |
| 170 | + "Robots all up in your server" |
| 171 | + >>> metric.filter |
| 172 | + "log:apache-access AND textPayload:robot" |
| 173 | + |
| 174 | +Update a metric: |
| 175 | + |
| 176 | +.. doctest:: |
| 177 | + |
| 178 | + >>> from gcloud import logging |
| 179 | + >>> client = logging.Client() |
| 180 | + >>> metric = client.metric("robots") |
| 181 | + >>> metric.exists() # API call |
| 182 | + True |
| 183 | + >>> metric.reload() # API call |
| 184 | + >>> metric.description = "Danger, Will Robinson!" |
| 185 | + >>> metric.update() # API call |
| 186 | + |
| 187 | +Delete a metric: |
| 188 | + |
| 189 | +.. doctest:: |
| 190 | + |
| 191 | + >>> from gcloud import logging |
| 192 | + >>> client = logging.Client() |
| 193 | + >>> metric = client.metric("robots") |
| 194 | + >>> metric.exists() # API call |
| 195 | + True |
| 196 | + >>> metric.delete() # API call |
| 197 | + >>> metric.exists() # API call |
| 198 | + False |
| 199 | + |
| 200 | + |
| 201 | +Export log entries using sinks |
| 202 | +------------------------------ |
| 203 | + |
| 204 | +Sinks allow exporting entries which match a given filter to Cloud Storage |
| 205 | +buckets, BigQuery datasets, or Cloud Pub/Sub topics. |
| 206 | + |
| 207 | +Create a Cloud Storage sink: |
| 208 | + |
| 209 | +.. doctest:: |
| 210 | + |
| 211 | + >>> from gcloud import logging |
| 212 | + >>> client = logging.Client() |
| 213 | + >>> sink = client.sink( |
| 214 | + ... "robots-storage", |
| 215 | + ... filter='log:apache-access AND textPayload:robot') |
| 216 | + >>> sink.storage_bucket = "my-bucket-name" |
| 217 | + >>> sink.exists() # API call |
| 218 | + False |
| 219 | + >>> sink.create() # API call |
| 220 | + >>> sink.exists() # API call |
| 221 | + True |
| 222 | + |
| 223 | +Create a BigQuery sink: |
| 224 | + |
| 225 | +.. doctest:: |
| 226 | + |
| 227 | + >>> from gcloud import logging |
| 228 | + >>> client = logging.Client() |
| 229 | + >>> sink = client.sink( |
| 230 | + ... "robots-bq", |
| 231 | + ... filter='log:apache-access AND textPayload:robot') |
| 232 | + >>> sink.bigquery_dataset = "projects/my-project/datasets/my-dataset" |
| 233 | + >>> sink.exists() # API call |
| 234 | + False |
| 235 | + >>> sink.create() # API call |
| 236 | + >>> sink.exists() # API call |
| 237 | + True |
| 238 | + |
| 239 | +Create a Cloud Pub/Sub sink: |
| 240 | + |
| 241 | +.. doctest:: |
| 242 | + |
| 243 | + >>> from gcloud import logging |
| 244 | + >>> client = logging.Client() |
| 245 | + >>> sink = client.sink( |
| 246 | + ... "robots-pubsub", |
| 247 | + ... filter='log:apache-access AND textPayload:robot') |
| 248 | + >>> sink.pubsub_topic = 'projects/my-project/topics/my-topic' |
| 249 | + >>> sink.exists() # API call |
| 250 | + False |
| 251 | + >>> sink.create() # API call |
| 252 | + >>> sink.exists() # API call |
| 253 | + True |
| 254 | + |
| 255 | +List all sinks for a project: |
| 256 | + |
| 257 | +.. doctest:: |
| 258 | + |
| 259 | + >>> from gcloud import logging |
| 260 | + >>> client = logging.Client() |
| 261 | + >>> sinks, token = client.list_sinks() |
| 262 | + >>> for sink in sinks: |
| 263 | + ... print('%s: %s' % (sink.name, sink.destination)) |
| 264 | + robots-storage: storage.googleapis.com/my-bucket-name |
| 265 | + robots-bq: bigquery.googleapis.com/projects/my-project/datasets/my-dataset |
| 266 | + robots-pubsub: pubsub.googleapis.com/projects/my-project/topics/my-topic |
| 267 | + |
| 268 | +Refresh local information about a sink: |
| 269 | + |
| 270 | +.. doctest:: |
| 271 | + |
| 272 | + >>> from gcloud import logging |
| 273 | + >>> client = logging.Client() |
| 274 | + >>> sink = client.sink('robots-storage') |
| 275 | + >>> sink.filter is None |
| 276 | + True |
| 277 | + >>> sink.reload() # API call |
| 278 | + >>> sink.filter |
| 279 | + 'log:apache-access AND textPayload:robot' |
| 280 | + >>> sink.destination |
| 281 | + 'storage.googleapis.com/my-bucket-name' |
| 282 | + |
| 283 | +Update a sink: |
| 284 | + |
| 285 | +.. doctest:: |
| 286 | + |
| 287 | + >>> from gcloud import logging |
| 288 | + >>> client = logging.Client() |
| 289 | + >>> sink = client.sink("robots") |
| 290 | + >>> sink.reload() # API call |
| 291 | + >>> sink.filter = "log:apache-access" |
| 292 | + >>> sink.update() # API call |
| 293 | + |
| 294 | +Delete a sink: |
| 295 | + |
| 296 | +.. doctest:: |
| 297 | + |
| 298 | + >>> from gcloud import logging |
| 299 | + >>> client = logging.Client() |
| 300 | + >>> sink = client.sink( |
| 301 | + ... "robots", |
| 302 | + ... filter='log:apache-access AND textPayload:robot') |
| 303 | + >>> sink.exists() # API call |
| 304 | + True |
| 305 | + >>> sink.delete() # API call |
| 306 | + >>> sink.exists() # API call |
| 307 | + False |
0 commit comments