首页 > 解决方案 > GAE 灵活和 Stackriver 使用 python 记录严重性级别

问题描述

我正在谷歌灵活应用引擎中运行 python 3 应用程序。我正在使用默认的 python 根记录器,并希望能够查看此图像中的日志级别。

使用 gunicorn 在 GAE 中部署具有以下日志配置的应用程序时:

import logging

logging.basicConfig(level=logging.INFO)
logging.warn('Warning level message')

所有日志级别都显示在 GAE 中的任何日志级别下,与严重性级别无关。

我尝试使用Python 的 Stackdriver 日志记录客户端

import logging
from google.cloud import logging as gcloud_logging

client = gcloud_logging.Client()
client.setup_logging(logging.INFO)

logging.basicConfig(level=logging.INFO)
logging.warn('Warning level message')

在本地执行时,此日志会记录到 Stackdriver(在“全局”下),日志级别实际上运行正常。但是,当我在 GAE 中部署此应用程序时,GAE 的日志级别仍显示在“任何日志级别”下。此外,“全局”下的日志记录似乎不起作用。

有没有一种简单的方法可以让日志严重级别与带有 python 3 的谷歌标准应用程序引擎一起工作?

标签: pythonpython-3.xgoogle-app-enginegoogle-cloud-platformstackdriver

解决方案


您是否尝试过将 Stackdriver Logging 与 Python 日志记录模块集成?

来自https://gcloud-python.readthedocs.io/en/latest/logging/usage.html#integration-with-python-logging-module

import logging
handler = client.get_default_handler()
cloud_logger = logging.getLogger('cloudLogger')
cloud_logger.setLevel(logging.INFO)
cloud_logger.addHandler(handler)
cloud_logger.error('bad news')

推荐阅读