Enable Logging

The library provides versatile logging for Google Ads API interactions. You can capture:

  • Detailed information: Full requests sent to the API and responses received.
  • Concise summaries: A high-level overview of the interactions.

And you can control these logging settings in two ways:

  • Client Library Configuration: Use the library's specific configuration options.
  • Programmatically with Python: Use Python's built-in logging framework for more direct control.

Logging is configured automatically when a GoogleAdsClient instance is initialized. This initialization step occurs, for example, when you use the load_from_storage method. At this point, the library will:

When this library is used as an installed package, you need to integrate its logging with your application's logging setup. Specifically, you must add a logging handler to the library's own logger instance with the addHandler method. This handler will dictate where the library's log messages are directed (e.g., to the console, a file, etc.). To do this, first retrieve the library's logger instance as shown here:

import logging

logger = logging.getLogger('google.ads.googleads.client')

After retrieving the library's logger, you can tell it where to output log messages.

  • Sending Logs to the Console: To display log messages on your console, you add a basic handler. Here's how to direct logs to standard output (stdout):

    And here's how to set a basic handler that will tell the logger to print to stdout:

    import sys
    
    # Assuming 'logger' was retrieved as per previous instructions
    logger.addHandler(logging.StreamHandler(sys.stdout))
    

    If you prefer to send logs to standard error (stderr), which is often used for error messages and warnings:

    import sys
    
    # Assuming 'logger' was retrieved as per previous instructions
    logger.addHandler(logging.StreamHandler(sys.stderr))
    
  • Configuring Other Logging Settings Programmatically: Once you have the logger object, you can also programmatically change other logging settings using Python's built-in logging module. For example, to change the logging level to DEBUG (which will show more detailed messages):

    logger.setLevel(logging.DEBUG)
    

Log levels

The client generates logs at a few different levels and you can set your configuration to see some or all of the below:

Level Successful Request Failed Request
DEBUG A detailed log with complete request and response objects as JSON. A detailed log with complete request and exception objects as JSON.
INFO A concise summary with specific request and response fields. A detailed log with complete request and exception objects as JSON.
WARNING None A concise summary with specific request information, the exception state and message.

Since the Python logging framework ignores log messages that are less severe than the configured level, setting to WARNING means you will only see concise messages related to failed requests, but setting to DEBUG means you will see all possible types of logs in the above table.

Logging to file

When you run example scripts like get_campaigns.py from your command line, any log messages typically printed to the console can be redirected (or "piped") to a file. This is a feature of your operating system's shell, not the Python library itself. Here's how:

To save standard output (most logs) to a file (overwriting it):

python get_campaigns.py -c $CLIENT_ID > campaign_logs.txt

To append standard output to a file:

python get_campaigns.py -c $CLIENT_ID >> campaign_logs.txt

To save both standard output and standard error (for errors/warnings) to the same file:

python get_campaigns.py -c $CLIENT_ID > all_logs.txt 2>&1

(Or, on some modern shells like Bash 4+):

python get_campaigns.py -c $CLIENT_ID &> all_logs.txt

Logging interceptors

The Python client library uses gRPC interceptors to access and log request and response details. You can set up your own custom logging by creating a gRPC interceptor with custom logic. See the Logging guide for more details and an example of a custom logging interceptor.