Skip to content

Logging example does not output anything without a StreamHandler #2604

Open
@jnowakowski

Description

@jnowakowski

Title

Logging example does not show output due to missing StreamHandler


Environment details

  • OS type and version: macOS 12.7
  • Python version: Python 3.10.13
  • pip version: pip 23.2.1
  • google-api-python-client version: 2.125.0

Steps to reproduce

  1. Copy the logging example from https://googleapis.github.io/google-api-python-client/docs/logging.html
  2. Run the script in terminal or standard Python environment
  3. No log output is shown, even though logging level is set to INFO

Code example

import logging
from googleapiclient.discovery import build

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def main():
    service = build('translate', 'v2', developerKey='your_api_key')
    print(service.translations().list(
        source='en',
        target='fr',
        q=['flower', 'car']
    ).execute())

if __name__ == '__main__':
    main()

Stack trace

(no output shown unless developer adds a StreamHandler manually)

Description

The logging example in the documentation sets the logging level with logger.setLevel(logging.INFO) but does not include a StreamHandler.

As a result, no output appears in most environments (e.g. terminal, script execution), unless a handler is explicitly added to send logs to stdout.

This can confuse users trying to debug API usage.

Proposed fix:

Update the example to include:

import sys
handler = logging.StreamHandler(sys.stdout)
logger.addHandler(handler)

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: processA process-related concern. May include testing, release, or the like.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions