Google Cloud Functions Logging (Python)


Use standard Python logging

import logginglogger = logging.getLogger(__name__)def test_logging(request):"I am logger")"I am logging")    return 'OK'

Testing logging


  • logging
  • logging.getLogger
  • integrate with logging.getLogger

Edit requirements.txt


Testing Code

import logginglogger = logging.getLogger(__name__)# import logging as glogging# client = glogging.Client.from_service_account_json('keys/PROJECT_ID-appengine.json')client = glogging.Client()cloud_log = client.logger(__name__)# = client.get_default_handler()cloud_logger = logging.getLogger("cloudLogger")# cloud_logger.setLevel(logging.INFO)cloud_logger.addHandler(handler)def test_logging(request):"--- START ---")    logger.debug("logger.debug")"")    logger.warn("logger.warn")    logger.error("logger.error")"---")    logging.debug("logging.debug")"")    logging.warn("logging.warn")    logging.error("logging.error")"---")    #    #    cloud_log.log_text("cloud_logging.debug", severity='DEBUG')    cloud_log.log_text("", severity='INFO')    cloud_log.log_text("cloud_logging.warn", severity='WARNING')    cloud_log.log_text("cloud_logging.error", severity='ERROR')"---")    cloud_logger.debug("cloud_logger.debug")"")    cloud_logger.warn("cloud_logger.warn")    cloud_logger.error("cloud_logger.error")"--- END ---")    return 'OK'


Google Cloud Functions Logging


logging.getLogger and logging works

  • Strangely, .warn and .error are both mark as Error severity
  • .debug is filtered by default, I assume it could be changed by .setLevel(logging.DEBUG) (I didn't test this though)

Strangely, - log_text didn't work, and I didn't explore further. You might want to try this.

Integrating with logging.getLogger works the same as logging.getLogger and logging.


❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.