8000 Add sample for standard library logging handler configuration (#1233) · johnmanong/python-docs-samples@53e782a · GitHub
[go: up one dir, main page]

Skip to content

Commit 53e782a

Browse files
authored
Add sample for standard library logging handler configuration (GoogleCloudPlatform#1233)
* Add sample for standard library logging handler configuration * Add handler.py to readme
1 parent 5f42921 commit 53e782a

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

logging/cloud-client/README.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ To run this sample:
6363
$ python quickstart.py
6464
6565
66+
Handler
67+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
68+
69+
70+
71+
To run this sample:
72+
73+
.. code-block:: bash
74+
75+
$ python handler.py
76+
77+
6678
Snippets
6779
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6880

logging/cloud-client/handler.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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()

logging/cloud-client/handler_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import handler
17+
18+
19+
def test_handler(capsys):
20+
handler.use_logging_handler()
21+
out, _ = capsys.readouterr()
22+
assert 'Logged' in out

0 commit comments

Comments
 (0)
0