Description
My objective is to format correctly the logs into my Python app for having a good ingestion and formatting on Stackdriver logging console with level well interpreted.
The standard logging doesn't work for App Engine python 3.7 (work in Python 2) and on Cloud Run, because the logs aren't formatted in FluentD.
However, with Cloud Function, it's the case.
I found how to perform a workaround, (see example), but I think there is a bug in your library. Indeed, the function client.setup_logging()
should set up my logger with the correct format according with the environment.
It's not the case, and that's why I have to implement the workaround. Sadly, this workaround don't take into account the environment and when I run locally the app, I have this log trace:
* Serving Flask app "server" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
{"message": " * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)", "timestamp": {"seconds": 1571789029, "nanos": 987048387}, "thread": 140201867048192, "severity": "INFO"}
{"message": "call info1", "timestamp": {"seconds": 1571789035, "nanos": 285192966}, "thread": 140201732208384, "severity": "INFO"}
{"message": "call info2", "timestamp": {"seconds": 1571789035, "nanos": 285630702}, "thread": 140201732208384, "severity": "INFO"}
{"message": "this is an error2", "timestamp": {"seconds": 1571789035, "nanos": 285893201}, "thread": 140201732208384, "severity": "ERROR"}
{"message": "127.0.0.1 - - [23/Oct/2019 02:03:55] \"GET / HTTP/1.1\" 200 -", "timestamp": {"seconds": 1571789035, "nanos": 286895513}, "thread": 140201732208384, "severity": "INFO"}
Which is in FluentD format, but barely lisible.
The automatic configuration promised at this page doesn't happen!
Environment details
- Specify the API at the beginning of the title (for example, "BigQuery: ...")
General, Core, and Other are also allowed as types
Logging API for App Engine and Cloud Run
-
OS type and version
Python 3.7, linux( appengine, container, local environment) -
Python version and virtual environment information:
python --version
3.7. Version not detailed because occur on App Engine -
google-cloud- version:
pip show google-<service>
orpip freeze
google-cloud-logging==1.14.0
Steps to reproduce
Deploy an app on Cloud Run or App Engine standard Python 3.7. Bellow an example with the workaround. Try to change the log handler with client.setup_logging()
no special formater are set, and the log on Stackdriver aren't well ingested.
Code example
This is a working example. I don't use client.setup_logging()
import os
from flask import Flask, request
import logging
from google.cloud.logging.handlers.container_engine import ContainerEngineHandler
# This change nothing to the standard logger on App Engine and Cloud Run env
# client = google.cloud.logging.Client()
# client.setup_logging()
# This is a working workaround on GCP but with not lisible log in local env
handler = ContainerEngineHandler()
cloud_logger = logging.getLogger()
cloud_logger.setLevel(logging.DEBUG)
cloud_logger.addHandler(handler)
app = Flask(__name__)
@app.route('/', methods=['GET'])
def call_oracle_connection():
logger = logging.getLogger("cloudLogger")
logger.info('call info1')
logger = logging.getLogger()
logger.info('call info2')
logging.error('this is an error2')
return oracle_connection(request)
if __name__ == "__main__":
app.run(host='0.0.0.0',port=int(os.environ.get('PORT',8080)))
Thanks!