|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Client for interacting with the Stackdriver Logging API""" |
| 17 | + |
| 18 | +import traceback |
| 19 | + |
| 20 | +import gcloud.logging.client |
| 21 | + |
| 22 | +DEFAULT_SERVICE = 'python' |
| 23 | + |
| 24 | + |
| 25 | +class Client(gcloud.logging.client.Client): |
| 26 | + """Error Reporting client. Currently Error Reporting is done via the |
| 27 | + Logging Client so it just subclasses that.""" |
| 28 | + |
| 29 | + def report_error(self, message="", service=DEFAULT_SERVICE): |
| 30 | + """ Reports the details of the latest exceptions to Stackdriver Error |
| 31 | + Reporting. |
| 32 | +
|
| 33 | + https://cloud.google.com/error-reporting/docs/formatting-error-messages |
| 34 | +
|
| 35 | + :type message: str |
| 36 | + :param message: An optional message to include with the exception |
| 37 | + detail |
| 38 | +
|
| 39 | + :type service: str |
| 40 | + :param service: An identifier of the service, such as the name of |
| 41 | + the executable, job, or Google App Engine module |
| 42 | + name. This field is expected to have a low number |
| 43 | + of values that are relatively stable over time, |
| 44 | + as opposed to version, which can be changed |
| 45 | + whenever new code is deployed. |
| 46 | +
|
| 47 | + Example:: |
| 48 | +
|
| 49 | + >>> try: |
| 50 | + >>> raise NameError |
| 51 | + >>> except Exception: |
| 52 | + >>> client.report_error("Something went wrong!") |
| 53 | + """ |
| 54 | + payload = { |
| 55 | + 'serviceContext': {'service': service}, |
| 56 | + 'message': '{0} : {1}'.format(message, traceback.format_exc()) |
| 57 | + } |
| 58 | + logger = self.logger('errors') |
| 59 | + logger.log_struct(payload) |
0 commit comments