[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)

Introduction

Describes how to output Cloud Functions logs written in Python to Cloud Logging (Stackdriver Logging).

procedure

Write as follows. Cloud Logging records anything above the level specified by cloud_logger.setLevel.

import logging
import google.cloud.logging
from google.cloud.logging.handlers import CloudLoggingHandler

client = google.cloud.logging.Client()
handler = CloudLoggingHandler(client)
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.DEBUG) # defaults to WARN
# cloud_logger.setLevel(logging.INFO) # defaults to WARN
cloud_logger.addHandler(handler)

cloud_logger.debug('debug')
cloud_logger.info('info')
cloud_logger.warn('warn')

Please describe the following two in requirements.txt. (Version number is omitted)

google-cloud-logging
google-cloud-core

When I didn't write google-cloud-core, I got the following error.

ERROR: (gcloud.beta.functions.deploy) OperationError: code=3, message=Function failed on loading user code. Error message: __init__() takes 2 positional arguments but 3 were given

reference

Integration with Python logging module https://googleapis.dev/python/logging/latest/stdlib-usage.html

Python Logging Module Handler https://google-cloud-python.readthedocs.io/en/0.32.0/logging/handlers.html

Recommended Posts

[GCP] How to output Cloud Functions log to Cloud Logging (Stackdriver Logging) (Python)
How to call Cloud API from GCP Cloud Functions
[CentOS8] How to output Python standard output to systemd log
[GCP] How to publish Cloud Storage signed URLs (temporary URLs) in Python
How to connect to Cloud Firestore from Google Cloud Functions with python code
How to update the python version of Cloud Shell on GCP
Output Python log to console with GAE
How to switch python versions in cloud9
Google Cloud Functions log output (and addicted)
GCP: Repeat from Pub / Sub to Cloud Functions, Cloud Functions to Pub / Sub
Output python log to both console and file
How to erase the characters output by Python
How to enable python3 to run when sending jobs from GCP Cloud Composer to Dataflow
How to install Python
How to install python
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Output log in JSON format with Python standard logging
[Python] How to output the list values in order
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
Python logging standard library for file output by log level
[Python] How to use list 1
How to update Python Tkinter to 8.6
[Python] Summary of how to use split and join functions
How to upload files to Cloud Storage with Firebase's python SDK
[GCP] A memorandum when running a Python program on Cloud Functions
Python: How to use pydub
Comparison of how to use higher-order functions in Python 2 and 3
[Python] How to use checkio
How to run Notepad ++ Python
Python learning basics ~ How to output (display) a character string? ~
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
Let's use Watson from Python! --How to use Developer Cloud Python SDK
python3: How to use bottle
How to output additional information when logging with python's logging module
Emulate GCP Cloud functions locally
How to log in to AtCoder with Python and submit automatically
How to use Python bytes
How to install OpenCV on Cloud9 and run it in Python
How to change the log level of Azure SDK for Python
Post a message from IBM Cloud Functions to Slack in Python
How to use functions in separate files Perl and Python versions
Basic grammar of Python3 system (how to use functions, closures, lambda functions)
[python3] Implement debug log output function easily with logging and Click
How to write a Python class
[Python] How to FFT mp3 data
[Python] How to do PCA in Python
Python: How to use async with
[Python] How to derive nCk (ABC156-D)
[Python] How to use Pandas Series
How to collect images in Python
Output to csv file with Python
[Introduction to Python] How to parse JSON
How to use Python's logging module
How to get the Python version