Skip to content

Commit 0a52479

Browse files
authored
Add configurable logging functionality (#20)
Add configurable logging functionality
1 parent f47be2a commit 0a52479

File tree

4 files changed

+545
-27
lines changed

4 files changed

+545
-27
lines changed

README.rst

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,51 @@ retrieve the corresponding service client instance:
7070
7171
google_ads_service = client.get_service('GoogleAdsService')
7272
73+
Enabling and Configuring logging
74+
################################
75+
The library uses Python's built in logging framework. You can specify your
76+
configuration via the configuration file; see `google-ads.yaml`_
77+
for an example. The library logs to ``stderr`` by default. You can easily pipe
78+
log messages to a file; when running an example:
79+
80+
.. code-block:: bash
81+
82+
python example.py args 2> example.log
83+
84+
It's also possible to configure logging programmatically using `Python's
85+
built-in logging library`_ by setting a logging configuration *before*
86+
initializing the client. You can retrieve the client logger instance and
87+
configure it with the following example:
88+
89+
.. code-block:: python
90+
91+
logging.basicConfig(level=logging.INFO, format='[%(asctime)s - %(levelname)s] %(message).5000s')
92+
logging.getLogger('google.ads.google_ads.client').setLevel(logging.INFO)
93+
94+
**NOTE:** The client logger is configured when the client is initialized, so if
95+
you have logger configurations in your google-ads.yaml file and you want to
96+
override them programmatically, you will need to call the above lines _before_
97+
initializing the client, otherwise the configuration from yaml will take
98+
precedent as it's provided first.
99+
100+
The client generates logs at a few different levels and you can set your
101+
configuration to see some or all of the below:
102+
103+
+-------------+--------------------------------------------------------------------+---------------------------------------------------------------------------------------+
104+
| Level | Successful Request | Failed Request |
105+
+=============+====================================================================+=======================================================================================+
106+
| ``DEBUG`` | A detailed log with complete request and response objects as JSON. | None |
107+
+-------------+--------------------------------------------------------------------+---------------------------------------------------------------------------------------+
108+
| ``INFO`` | A concise summary with specific request and response fields. | A detailed log with complete request and exception objects as JSON. |
109+
+-------------+--------------------------------------------------------------------+---------------------------------------------------------------------------------------+
110+
| ``WARNING`` | None | A concise summary with specific request information, the exception state and message. |
111+
+-------------+--------------------------------------------------------------------+---------------------------------------------------------------------------------------+
112+
113+
Since the Python logging framework ignores log messages that are less severe
114+
than the configured level, setting to ``WARNING`` means you will only see
115+
concise messages related to failed requests, but setting to ``DEBUG`` means
116+
you will see all possible types of logs in the above table.
117+
73118
Miscellaneous
74119
-------------
75120

@@ -83,16 +128,19 @@ Authors
83128

84129
* `Mark Saniscalchi`_
85130
* `David Wihl`_
131+
* `Ben Karl`_
86132

87133
.. _pip: https://pip.pypa.io/en/stable/installing
88134
.. _template: https://github.com/googleads/google-ads-python/blob/master/google-ads.yaml
89135
.. _Authorization guide: https://developers.google.com/google-ads/api/docs/oauth/overview
90136
.. _authentication samples: https://github.com/googleads/google-ads-python/blob/master/examples/authentication
91137
.. _Obtain your developer token: https://developers.google.com/google-ads/api/docs/first-call/dev-token
138+
.. _google-ads.yaml: https://github.com/googleads/google-ads-python/blob/master/google-ads.yaml
139+
.. _Python's built-in logging library: https://docs.python.org/2/library/logging.html
92140
.. _Wiki: https://github.com/googleads/google-ads-python/wiki
93141
.. _Issue tracker: https://github.com/googleads/google-ads-python/issues
94142
.. _API documentation: https://developers.google.com/google-ads/api/
95143
.. _API Support: https://developers.google.com/adwords/api/community/
96144
.. _Mark Saniscalchi: https://github.com/msaniscalchi
97145
.. _David Wihl: https://github.com/wihl
98-
146+
.. _Ben Karl: https://github.com/BenRKarl

google-ads.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,28 @@ refresh_token: INSERT_REFRESH_TOKEN_HERE
88
# instead of 123-456-7890. You can also specify this later in code if your
99
# application uses multiple manager account + OAuth pairs.
1010
login_customer_id: INSERT_LOGIN_CUSTOMER_ID_HERE
11+
12+
# Logging configuration
13+
###############################################################################
14+
# Below you may specify the logging configuration. This will be provided as #
15+
# an input to logging.config.dictConfig. Use the "level" block under the root #
16+
# logger configuration to adjust the logging level. Note in the "format" #
17+
# field that log messages are truncated to 5000 characters by default. You #
18+
# can change this to any length by removing the ".5000" portion or changing #
19+
# it to a different number. #
20+
# #############################################################################
21+
# logging:
22+
# version: 1
23+
# disable_existing_loggers: False
24+
# formatters:
25+
# default_fmt:
26+
# format: '[%(asctime)s - %(levelname)s] %(message).5000s'
27+
# datefmt: '%Y-%m-%d %H:%M:%S'
28+
# handlers:
29+
# default_handler:
30+
# class: logging.StreamHandler
31+
# formatter: default_fmt
32+
# loggers:
33+
# "":
34+
# handlers: [default_handler]
35+
# level: INFO

0 commit comments

Comments
 (0)