简体   繁体   中英

How can I get Google Cloud Logging to show my flask + gunicorn app's logs with the correct alert level?

I have a GCP Cloud Run service written in python that serves up an API using Flask. The API runs in a docker container using gunicorn, like so:

CMD exec gunicorn --bind :$PORT --workers 8 --threads 8 --timeout 0 --log-level=info main:app

and then that container is deployed to Cloud Run. In the API, I log messages out using Flask's built-in logger, like so:

app.logger.critical("Example message")

and I have set up the google cloud client library as suggested by GCP docs, like so:

logging_client = google.cloud.logging.Client()
logging_handler = logging_client.setup_logging()

I do this before creating the flask app in main.py . Specifically:

logging_client = google.cloud.logging.Client()
logging_handler = logging_client.setup_logging()
dictConfig(
    {
        "version": 1,
        "formatters": {
            "default": {
                "format": "[%(asctime)s] %(levelname)s in %(module)s: %(message)s",
            }
        },
        "handlers": {
            "wsgi": {
                "class": "logging.StreamHandler",
                "stream": "ext://flask.logging.wsgi_errors_stream",
                "formatter": "default",
            },
            "gcp_logging": {
                "class": "google.cloud.logging.handlers.CloudLoggingHandler",
                "client": logging_client,
            },
        },
        "root": {"level": "INFO", "handlers": ["wsgi", "gcp_logging"]},
    }
)

app = Flask(__name__)

When I run the service in Cloud Run, the log messages show up but the alert level is empty.

How can I configure flask + gunicorn + GCP cloud logging so that my log messages show the correct log level when they're displayed in the logging console?

You may be running into issues with the handlers you are using. You call logging_client.setup_logging() , which should configure the handlers automatically based on the environment the code is running in, but then you also seem to manually set new handlers in the dictConfig .

The log with incorrect severity is likely coming from the StreamHandler . Cloud Run will automatically capture stdout logs from the environment, but the built-in StreamHandler doesn't properly format the log in such a way that metadata like severity will be captured automatically. The CloudLoggingHandler you attached should show the proper severity, but it can have issues on serverless environments, since it tries to send logs in batches over the network. When the environment spins down, unsent log batches may be lost.

Instead, we recommend using the StructuredLogHandler , which outputs logs as structured JSON to stdout, which is then captured by the GCP agents and sent to Cloud Logging. If you run logging_client.setup_logging() without overwriting the handlers, it should be set up for you automatically.

More information can be found on this GitHUb thread , and in the library docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM