|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +def use_logging_handler(): |
| 19 | + # [START logging_handler_setup] |
| 20 | + # Imports the Google Cloud client library |
| 21 | + import google.cloud.logging |
| 22 | + |
| 23 | + # Instantiates a client |
| 24 | + client = google.cloud.logging.Client() |
| 25 | + |
| 26 | + # Connects the logger to the root logging handler; by default this captures |
| 27 | + # all logs at INFO level and higher |
| 28 | + client.setup_logging() |
| 29 | + # [END logging_handler_setup] |
| 30 | + |
| 31 | + # [START logging_handler_usage] |
| 32 | + # Imports Python standard library logging |
| 33 | + import logging |
| 34 | + |
| 35 | + # The data to log |
| 36 | + text = 'Hello, world!' |
| 37 | + |
| 38 | + # Emits the data using the standard logging module |
| 39 | + logging.warn(text) |
| 40 | + # [END logging_handler_usage] |
| 41 | + |
| 42 | + print('Logged: {}'.format(text)) |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == '__main__': |
| 46 | + use_logging_handler() |
0 commit comments